当前位置:网站首页>[learning] audio and video development from scratch (9) -- nuplayer
[learning] audio and video development from scratch (9) -- nuplayer
2022-04-23 08:18:00 【Panic Lin】
NuPlayer be based on StagefrightPlayer Basic class construction of , Using the underlying ALooper/AHandler
Mechanism for asynchronous decoding and playback ,Alooper Round the messages in the queue , Push the message to AHandler Handle , Finally through handleMessage Function callback , Do the corresponding logical processing
NuPlayer The overall structure
MediaPlayerFactory adopt Factory mode establish NuPlayerFactory
, And then through NuPlayerFactory establish NuPlayerDriver
, Then passed NuPlayerDriver Construct a NuPlayer
,NuPlayer As a player , Analysis of design data 、 decode 、 Rendering and other processes
NuPlayer The building process of
Call... At the upper level setDataSource After the function , Begin to arrive at MediaPlayerService Of setDataSource function , adopt getPlayerType
Function to get the player type
Player types are enumerated as follows
Then go back to MediaPlayerFactory Look at it getPlayerType function
Next, enter a macro function , The main function of macros is to replace , Improve code execution efficiency , Because it eliminates the need to allocate and release stack frames 、 The ginseng 、 Pass the return value and a series of work
The label of this macro function traverses map Stored in Player factory class , call scoreFactory
You can get the player's playback ability , If it is NuPlayer, There is one by default 0.8 Value , Back to ret Namely NuPlayerFactory object , If you get a value of 0.0, To get into getDefaultPlayerType
function
The default is NuPlayerFactory
in the light of NuPlayerFactory It will not be created directly NuPlayer, But in NuPlayerDriver Create a NuPlayerDriver
stay NuPlayerDriver Of Constructors in , Created a NuPlayer,NuPlayerDriver It's right NuPlayer Of encapsulation , Inherit MediaPlayerInterface
Interface . adopt NuPlayer Can realize the playback function , It is mainly used to notify the above process
NuPlayer Inherited from AHandler
, And introduced AMessage, adopt ALooper To process the message , Like its prepareAsync
function
It's actually sending a kWhatPrepare
news . stay onMessageReceived
Function , Receive and process messages ( The function is too long , Extract only kWhatPrepare Partial analysis )
NuPlayer Data analysis module
The main modules of parsing are NuPlayerSource
And several classes that inherit it , Such as HTTPLiveSource、RTSPSource、GenericSource etc.
stay NuPlayer Call in setDataSourceAsync
After the function
It will be based on Different protocols Choose different Source object , With this Source After the object , send out kWhatSetDataSource
news ( The following figure is also a partial screenshot of the function )
Through concrete Source After analyzing the data , And then Source Cast to mSource
to Decoder Use , At this time, it contains data related information , One of them HTTPLiveSource
, It is mainly used to analyze HLS agreement
When calling HTTPLiveSource Of prepareAsync Function time
It mainly constructs a LiveSession
object , adopt LiveSession Inside connectAsync
function , Create a session
see onConnect function
Network request , Start getting playlists , Call back after getting the playlist ( The following figure shows the callback function )
The above code is mainly based on URL Back to M3U file , Get the corresponding BandwidthItem
, If you are familiar with M3U file , You can know M3U Files have primary and secondary indexes , The following figure shows some examples
Its main function is to Dynamic rate adaptation ,BANDWIDTH
The bigger it is , The higher the resolution .BINDWIDTH It's actually bandwidth
If you want to get a Track Information about , Get the video 、 Audio 、 Caption related information , Can pass LiveSession Of getTrackInfo
function
This completes the data analysis module , The main function of video source is to get audio and video data information , Finally through mSource
Variables are passed to the decoder
NuPlayer Decoding module of
It introduces NuPlayerDecoderBase
, This is a base class , The real decoder logic is NuPlayerDecoder.cpp in , It inherits the base class , Let's look at the instantiated decoder first
The above code constructs Decoder
object , And it has been carried out initialization and Build decoder
because NuPlayerDecoder Inherited from NuPlayerDecoderBase
class , therefore Configure The function will execute NuPlayerDecoder Of Configure
function , The final callback onConfigure
function , It is located in NuPlayerDecoder.cpp in
from onConfigure The function can see ,NuPlayerDecoder Introduced MediaCodec
As a decoder . adopt CreateByType/CreateByComponentName
Created Codec
object .
NuPlayer The rendering module of
The main function
1. Cache the original audio and video data to the queue
2. Audio data consumption playback
3. Video data consumption display
4. Audio video synchronization
5. Player control
Cache the original audio and video data to the queue , stay NuPlayerRenderer.h in , There is one. QueueEntry Structure and two queues
The data is in onQueueBuffer
Function to add
And the playback of audio is NuPlayerRenderer
in , Will call first openAudioSink
function
Send a kWhatOpenAudioSink
news , After receiving the message, carry out the following processing
call onOpenAudioSink
function
It's open here AudioSink( Audio back end )
. When putting audio data into the audio queue , Will then call postDrainAudioQueue_l
function
The code above sends a kWhatDrainAudioQueue
There is mainly one to judge onDrainAudioQueue
function , Decide whether you need to go back to AudioSink Write data
Come here , The audio playback process is as follows : Open the audio back end first , Then when sending data to the audio queue , The audio queue writes data to the audio backend at the same time , For audio playback
If the video shows , Also after the original video data enters the video queue , Start execution postDrainVideoQueue
function
Here, first send a kWhatDrainVideoQueue
The news of , Then synchronize audio and video , After sending the message, it will start to call onDrainVideoQueue
NuPlayer::Renderer
It uses Based on video The synchronization mechanism of , If the decoded audio data timestamp is greater than the video data timestamp , Discard audio packets directly , Then render the video directly . The synchronization mechanism is mainly located in the video buffer processing part onDrainVideoQueue
And audio buffer processing onDrainVideoQueue
in . The rendering of audio and video adopts A timer like mechanism , But the video display needs to rely on Actual decoder , Audio playback depends on AudioSink Interface
版权声明
本文为[Panic Lin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230716262339.html
边栏推荐
- 5.6 综合案例-RTU-
- redis主从服务器问题
- Compiler des questions de principe - avec des réponses
- php高精度计算
- 3C装配中的机械臂运动规划
- Install MySQL for Ubuntu and query the average score
- Discussion on ES6 tail tune optimization
- Jetson Xavier NX (3) bazel mediapipe installation
- PHP high precision computing
- Find the largest of 3 strings (no more than 20 characters per string).
猜你喜欢
【学习】从零开始的音视频开发(9)——NuPlayer
校园转转二手市场源码下载
AQS & ReentrantLock 实现原理
利用Js实现一个千分位
Rearranging log files for leetcode simple question
1216_MISRA_C规范学习笔记_控制流的规则要求
Ubuntu安装Mysql并查询平均成绩
Online yaml to XML tool
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
The simple problem of leetcode is to calculate the numerical sum of strings
随机推荐
php生成短链接:将数字转成字母,将字母转成数字
Multi vision slam
C language learning record -- use and analysis of string function (2)
刨析——浏览器如何工作
vslam PPT
Quick rehearsal exercise
freertos学习02-队列 stream buffer message buffer
AAAI 2022招募讲者啦!!
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
Brief description of CPU
Qt读写XML文件
stm32以及freertos 堆栈解析
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
Sword finger offer Day24 math (medium)
The simple problem of leetcode is to calculate the numerical sum of strings
[untitled]
智能名片小程序名片详情页功能实现关键代码
Discussion on ES6 tail tune optimization