[Android][MediaRecorder] Android MediaRecorder框架简洁梳理

(Base on Android Q)
主要是为了梳理MediaRecorder模块代码,熟悉流程
本文的简化图由draw.io绘制,详细时序图由plantUml绘制
绘制原材料在百度云盘中 链接: https://pan.baidu.com/s/1IJ7Qo2Jl3o6GthPtAiD95Q 提取码: qu4w
,可以自行down下来修改添加

一.MediaRecorder整体架构

主要简单梳理数据流向,方便复习记忆

1.1 MediaRecorder录制数据流框架

在这里插入图片描述
简单过程:
1.Camera应用中至少有两个Surface,一个使用于preview的,另一个使用来record的,record的surface是PersistentSurface类型,PersistentSurface中的GraphicBufferSource类型的成员变量mBufferSource最终由编码器创建引用;
2.CameraServer中持有Record Surface的producer引用和Preview Surface的producer引用,因此预览和录制CameraServer都充当着生产者的角色;
3.在向CameraProvider发request的时候先dequeueBuffer送至HAL去填充,填充完HAL发回result这时queueBuffer将数据填充至BufferQueue中,由BufferQueue的原理,可知这时候BufferQueue的Consumer将回调onFrameAvailable函数去收到数据准备完成通知,接下来Consumer使用acquireBuffer去消费即可,消费完即releaseBuffer去释放Buffer;
4.对于preview,消费者就是Surfaceflinger了,合成消费后拿去显示,对于record,消费者就是编码器了,举例OMX一种,编码器获取到数据消费用于编码;
5.编码器编完码之后将调用Framework中MediaServer的回调,将编码后的数据传递至MediaRecorder;
6.MediaRecorder在start之后将启动一个WriteThread,两个TrackThread(分别是Video和Audio),当TrackThread跟踪到有相应数据后将video或audio的数据分装成Chunk数据结构,保存在MPEG4Writer成员变量mChunks中.这时WriteThread发现有数据可写会将mChunks中的chunk写到文件中.

1.2 PersistentSurface及GraphicBufferSource实现的BufferQueue框架

在这里插入图片描述
1.CameraApp使用的Java层PersistentSurface继承Java层的Surface,因此它首先是个Surface,其次他一个成员变量指向Native层的 PersistentSurface 实例对象,也指向Natvie层创建的Surface对象;
2.Surface一般用来作为生产者的容器,这里一样,Java PersistentSurface的 natvie Surface给到CameraServer用于生产数据容器,Producer是CameraServer;
3.Native PersistentSurface的两个参数mBufferProducer和mBufferSource最终是由编码器创建,其mBufferProducer指向CameraServer中的Record Surface,mBufferSource作为消费者引用在编码器内部实现,mBufferSource是GraphicBufferSource类型,在其走在构造函数时会创建BufferQueue;
4.mBufferSource应是指向引用编码器内部的数据结构或实例,Acodec可以通过GraphicBufferSource类型的成员变量(由PersistentSurface的mBufferSource赋值来)对编码器进行一些参数设置.

1.3 写文件的过程及重要类

在这里插入图片描述

二.MediaRecorder init,prepare,start,stop简单过程

上面已经将重要过程过基本讲述了完了,下面是简单地梳理MediaRecorder的代码过程,文末将附上详细的代码跟进过程,简化图为了方便记忆主要过程,都基于详细代码跟进之后归纳重点的地方.

2.1 MediaRecorder init

在这里插入图片描述
详细过程见附件,其中附上PersistentSurface的创建及设置详细过程

2.2 MediaRecorder prepare

在这里插入图片描述
MediaCodecSource 即对应videoEncoder和audioEncoder的初始化:
在这里插入图片描述
prepare的详细过程见附件.

2.3 MediaRecorder start

在这里插入图片描述
MediaRecord Start 与编码器数据传递至recorder的详细过程见附件

2.4 MediaRecorder stop

在这里插入图片描述

三.MediaRecorder 代码详细跟进

3.1 MediaRecorder init flow

在这里插入图片描述

3.2 PersistentSurface init and setInputSurface flow

在这里插入图片描述

3.3 PersistentSurface prepare flow

在这里插入图片描述

3.4 MediaRecorder start flow

在这里插入图片描述

3.5 MediaRecorder Buffer Callback from OMX to MediaRecorder

在这里插入图片描述

3.6 MediaRecorder stop flow

在这里插入图片描述

### Backtracking Algorithm Code Template in C Language Below is a general backtracking algorithm template implemented in the C programming language. This implementation can be adapted for various problems such as solving puzzles, generating permutations, or finding subsets. ```c #include <stdio.h> #include <stdbool.h> #define MAX_SIZE 100 // Function prototype declarations bool isValid(int step); void processSolution(); void backtrack(int step); int n; // Size of the problem int solution[MAX_SIZE]; // Array to store current partial solutions // Main function that initializes variables and starts the recursion void solveBacktrackingProblem(int size) { n = size; int step = 0; // Start from the first step backtrack(step); // Begin the recursive backtracking process } // Recursive backtracking function void backtrack(int step) { if (step == n) { // Base case: If all steps are completed processSolution(); // Process the final valid solution } else { for (int i = 0; i < n; ++i) { // Iterate through possible choices at this step solution[step] = i; // Make a choice if (isValid(step)) { // Check if the current state is valid backtrack(step + 1); // Recur with next step } } } } // Placeholder function to check validity of the current configuration bool isValid(int step) { // Add specific validation logic here based on your problem requirements. return true; // For now, assume everything is valid } // Placeholder function to handle processing when a full solution is found void processSolution() { printf("Found Solution:\n"); for (int i = 0; i < n; ++i) { printf("%d ", solution[i]); } printf("\n"); } ``` This generic backtracking framework includes placeholders (`processSolution` and `isValid`) where you would insert custom logic depending on the particular problem being solved. The example demonstrates how to recursively explore potential configurations while pruning invalid ones early using constraints defined within `isValid`. For instance, consider applying it towards generating all unique binary strings of length N by modifying these helper functions accordingly: #### Example Application – Generating Binary Strings Using Backtrack Methodology Modify the above program slightly so each position has only two options—either '0' or '1'. Update both utility methods thusly: ```c bool isValid(int step){ /* No additional checks needed since every assignment will result into either zero or one */ return true ; } void processSolution(){ char str[n+1]; memset(str,' ',sizeof(char)*(n)); for(int idx=0 ;idx<n;++idx ){ str[idx]=(solution[idx]==0)?’0’:’1'; } str[n]='\0'; // Null terminate string after filling characters up until index ‘n-1' puts(str); } ``` Now calling `solveBacktrackingProblem(3)` generates outputs like `"000", "001", ..., "111"` representing all combinations exhaustively explored via depth-first traversal strategy inherent inside our generalized approach presented earlier. ---
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值