当前位置:网站首页>[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
边栏推荐
- 2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
- 常用正则表达式
- 【Appium】测试时遇到手机内嵌H5页面的切换问题
- js常用数组方法
- An article understands variable lifting
- PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
- 基于TCP/IP协议的网络通信实例——文件传输
- Convert object to URL
- Discussion on ES6 tail tune optimization
- [appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
猜你喜欢

clang 如何产生汇编文件

欧圣电气深交所上市:市值52亿 陆为东父女为美国籍

关于ORB——SLAM运行中关键帧位置越来越近的异常说明

浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda

通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解

2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局

Briefly describe the hierarchical strategy of memory

Data security has become a hidden danger. Let's see how vivo can make "user data" armor again

LeetCode简单题之重新排列日志文件

The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
随机推荐
nn.Module类的讲解
Talk about the basic but not simple stock data
Reverse linked list exercise
万物互联下如何对设备进行加密
几种智能机器人室内定位方法对比
CSV column extract column extraction
简述CPU
数据的删除和修改操作(mysql)
Thinkphp6 + JWT realizes login verification
浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
青苹果影视系统源码 影视聚合 影视导航 影视点播网站源码
在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)
5.6 综合案例-RTU-
一个没啥L用,但可以装X的IDEA插件
My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
校园转转二手市场源码下载
AAAI 2022招募讲者啦!!
每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
Ubuntu安装Mysql并查询平均成绩
一篇文章看懂变量提升(hoisting)