实现背景
录像有什么难的?无非就是数据过来,编码保存mp4而已,这可能是好多开发者在做录像模块的时候的思考输出。是的,确实不难,但是做好,或者和其他模块有非常好的逻辑配合,确实不容易。
好多开发者希望聊聊录像模块,实际上录像这块,需求层面的东西大家都清楚,无非就是设计的时候,做的更智能,逻辑清晰而已。
设计思路
以大牛直播SDK的录像模块的技术实现为例,我们在设计的时候,确保录像模块和RTMP推送、内置轻量级RTSP服务、转发模块、GB28181设备接入模块完全隔离,可以组合使用,也可以分开始用。
录像数据源,这块很好理解,无非就是编码前的yuv、nv12、nv21、rgb、pcm等( 比如Android camera、camera2,或者otg采集到的数据等),编码成H.264/H.265/AAC,或外部接口直接投递的编码后的264、h265、aac等。
录像模块的功能层面,比较好理解,比如需要支持随时录像,设置单个录像文件大小、录像路径等,并支持纯音频、纯视频、音视频录制模式,此外,最好支持录像过程中,暂停录像、恢复录像。
 从开始录像,到录像结束,需要设计event callback,告诉上层逻辑,什么时候开始录像了,什么时候生成了个录像文件,路径是什么。
- 文件格式:MP4;
- 涉及相关库:libSmartPublisher.so
- 头文件:SmartPublisherJniV2.java
- Jar:smartavengine.jar
接口概述
| Android录像模块接口概述 | |||
| 调用描述 | 接口 | 接口描述 | |
| 录像设置 | 是否录像 | SmartPublisherSetRecorder | 设置是否启用本地录像 | 
| 创建录像目录 | SmartPublisherCreateFileDirectory | 创建录像文件目录 | |
| 设置录像目录 | SmartPublisherSetRecorderDirectory | 设置录像文件目录 | |
| 音频录像 | SmartPublisherSetRecorderAudio | 音频录制开关 | |
| 视频录像 | SmartPublisherSetRecorderVideo | 视频录制开关 | |
| 设置录像文件大小 | SmartPublisherSetRecorderFileMaxSize | 设置每个录像文件的大小,比如100M,超过这个大小后,会自动生成下一个录像文件 | |
| 开始录像 | SmartPublisherStartRecorder | 开始录像 | |
| 暂停/恢复录像 | SmartPublisherPauseRecorder | Pause recorder(暂停/恢复录像) | |
| 停止录像 | SmartPublisherStopRecorder | 停止录像 | |
调用示例
![[技术分享]Android平台实时音视频录像模块设计之道_Android camera2](https://s2.51cto.com/images/blog/202505/03003804_6814f4ec7ee3e14211.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)
录像配置
void ConfigRecorderParam() {
        if (libPublisher != null && publisherHandle != 0) {
            if (recDir != null && !recDir.isEmpty()) {
                int ret = libPublisher.SmartPublisherCreateFileDirectory(recDir);
                if (0 == ret) {
                    if (0 != libPublisher.SmartPublisherSetRecorderDirectory(publisherHandle, recDir)) {
                        Log.e(TAG, "Set record dir failed , path:" + recDir);
                        return;
                    }
                    // 更细粒度控制录像的, 一般情况无需调用
                    //libPublisher.SmartPublisherSetRecorderAudio(publisherHandle, 0);
                    //libPublisher.SmartPublisherSetRecorderVideo(publisherHandle, 0);
                    if (0 != libPublisher.SmartPublisherSetRecorderFileMaxSize(publisherHandle, 200)) {
                        Log.e(TAG, "SmartPublisherSetRecorderFileMaxSize failed.");
                        return;
                    }
                } else {
                    Log.e(TAG, "Create record dir failed, path:" + recDir);
                }
            }
        }
    }开始、停止录像
class ButtonStartRecorderListener implements View.OnClickListener {
        public void onClick(View v) {
            if (isRecording) {
                stopRecorder();
                if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
                    ConfigControlEnable(true);
                }
                btnStartRecorder.setText("实时录像");
                btnPauseRecorder.setText("暂停录像");
                btnPauseRecorder.setEnabled(false);
                isPauseRecording = true;
                return;
            }
            Log.i(TAG, "onClick start recorder..");
            if (libPublisher == null)
                return;
            if (!isPushingRtmp && !isRTSPPublisherRunning&& !isGB28181StreamRunning) {
                InitAndSetConfig();
            }
            ConfigRecorderParam();
            int startRet = libPublisher.SmartPublisherStartRecorder(publisherHandle);
            if (startRet != 0) {
                if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
                    if (publisherHandle != 0) {
                        long handle = publisherHandle;
                        publisherHandle = 0;
                        libPublisher.SmartPublisherClose(handle);
                    }
                }
                Log.e(TAG, "Failed to start recorder.");
                return;
            }
            if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
                CheckInitAudioRecorder();
                ConfigControlEnable(false);
            }
            startLayerPostThread();
            btnStartRecorder.setText("停止录像");
            isRecording = true;
            btnPauseRecorder.setEnabled(true);
            isPauseRecording = true;
        }
    }停止录像封装
//停止录像
    private void stopRecorder() {
        if(!isRecording)
            return;
        isRecording = false;
        if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning)
            stopLayerPostThread();
        if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
            if (audioRecord_ != null) {
                Log.i(TAG, "stopRecorder, call audioRecord_.StopRecording..");
                audioRecord_.Stop();
                if (audioRecordCallback_ != null) {
                    audioRecord_.RemoveCallback(audioRecordCallback_);
                    audioRecordCallback_ = null;
                }
                audioRecord_ = null;
            }
        }
        if (null == libPublisher || 0 == publisherHandle)
            return;
        libPublisher.SmartPublisherStopRecorder(publisherHandle);
        if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
            releasePublisherHandle();
        }
    }暂停/恢复录像
class ButtonPauseRecorderListener implements View.OnClickListener {
        public void onClick(View v) {
            if (isRecording) {
                if(isPauseRecording)
                {
                    int ret = libPublisher.SmartPublisherPauseRecorder(publisherHandle, 1);
                    if (ret == 0)
                    {
                        isPauseRecording = false;
                        btnPauseRecorder.setText("恢复录像");
                    }
                    else if(ret == 3)
                    {
                        Log.e(TAG, "Pause recorder failed, please re-try again..");
                    }
                    else
                    {
                        Log.e(TAG, "Pause recorder failed..");
                    }
                }
                else
                {
                    int ret = libPublisher.SmartPublisherPauseRecorder(publisherHandle, 0);
                    if (ret == 0)
                    {
                        isPauseRecording = true;
                        btnPauseRecorder.setText("暂停录像");
                    }
                    else if(ret == 3)
                    {
                        Log.e(TAG, "Resume recorder failed, please re-try again..");
                    }
                    else
                    {
                        Log.e(TAG, "Resume recorder failed..");
                    }
                }
            }
        }
    }event回调
case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_RECORDER_START_NEW_FILE:
     publisher_event = "开始一个新的录像文件 : " + param3;
     break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_ONE_RECORDER_FILE_FINISHED:
     publisher_event = "已生成一个录像文件 : " + param3;
     break;技术总结
录像模块,单纯地实现不难,如果是需要和GB28181设备接入模块、RTMP推送、轻量级RTSP服务模块一起使用的时候,需要考虑的就多了,感兴趣的开发者,可以酌情参考。
 
 
                     
            
        













 
                    

 
                 
                    