技术背景
RTSP、RTMP直播播放,这里不再赘述,我们可以很轻松的实现毫秒级的延迟体验,这里讲的是如何实现RTSP、RTSP流的实时录像功能。
我们理解的录像,可能觉得,只要有个开始录像、停止录像接口就够了,实际上的录像,如果需要细粒度设计,需要支持设置录像保存路径、开始录像停止录像录像状态回调、录像文件名是不是需要添加时间或前缀(便于查询遍历)、PCMA|PCMU|Speex的audio是不是要转AAC后保存、是不是只录制纯音频或纯视频、单个文件大小等。
技术设计
无图无真相,先说设置界面:
对应的代码如下:
/* * RecordConfigForm.cs * Created by daniusdk.com on 2017/04/19. * WeChat: xinsheng120 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SmartPlayer { public partial class RecordConfigForm : Form { private bool is_rec_video_ = true; private bool is_rec_audio_ = true; private String rec_dir_ = ""; private String rec_name_file_prefix_ = ""; private UInt32 max_file_size_ = 200 * 1024; // 单位是KByte, 默认200MB private bool is_append_date_ = true; private bool is_append_time_ = true; private bool is_audio_transcode_aac_ = true; public bool IsRecVideo() { return is_rec_video_; } public bool IsRecAudio() { return is_rec_audio_; } public String RecDir() { return rec_dir_; } public String RecNameFilePrefix() { return rec_name_file_prefix_; } public UInt32 MaxFileSize() { return max_file_size_; } public bool IsAppendDate() { return is_append_date_; } public bool IsAppendTime() { return is_append_time_; } public bool IsAudioTanscodeAAC() { return is_audio_transcode_aac_; } public RecordConfigForm(bool is_rec_video, bool is_rec_audio, String rec_dir, String rec_name_file_prefix, UInt32 max_file_size, bool is_append_date, bool is_append_time, bool is_audio_transcode_aac) { InitializeComponent(); text_rec_max_file_size.Text = "200"; if (is_rec_video) { checkbox_rec_video.CheckState = CheckState.Checked; } else { checkbox_rec_video.CheckState = CheckState.Unchecked; } if (is_rec_audio) { checkbox_rec_audio.CheckState = CheckState.Checked; } else { checkbox_rec_audio.CheckState = CheckState.Unchecked; } text_rec_dir.Text = rec_dir; text_rec_name_file_prefix.Text = rec_name_file_prefix; text_rec_max_file_size.Text = (max_file_size/1024).ToString(); if (is_append_date) { checkbox_append_date.CheckState = CheckState.Checked; } else { checkbox_append_date.CheckState = CheckState.Unchecked; } if (is_append_time) { checkbox_append_time.CheckState = CheckState.Checked; } else { checkbox_append_time.CheckState = CheckState.Unchecked; } if (is_audio_transcode_aac) { check_rec_audio_transcode_aac.CheckState = CheckState.Checked; } else { check_rec_audio_transcode_aac.CheckState = CheckState.Unchecked; } } private void btn_record_dir_Click(object sender, EventArgs e) { FolderBrowserDialog dlg = new FolderBrowserDialog(); if (dlg.ShowDialog() == DialogResult.OK) { text_rec_dir.Text = dlg.SelectedPath.ToString(); } } private void btn_ok_Click(object sender, EventArgs e) { is_rec_video_ = checkbox_rec_video.Checked; is_rec_audio_ = checkbox_rec_audio.Checked; rec_dir_ = text_rec_dir.Text; rec_name_file_prefix_ = text_rec_name_file_prefix.Text; max_file_size_ = UInt32.Parse(text_rec_max_file_size.Text)*1024; is_append_date_ = checkbox_append_date.Checked; is_append_time_ = checkbox_append_time.Checked; is_audio_transcode_aac_ = check_rec_audio_transcode_aac.Checked; this.Close(); this.Dispose(); } private void btn_cancel_Click(object sender, EventArgs e) { this.Close(); this.Dispose(); } } }
录像设置按钮如下:
private void btn_record_config_Click(object sender, EventArgs e) { RecordConfigForm record_config_dlg = new RecordConfigForm(is_rec_video_, is_rec_audio_, rec_dir_, rec_name_file_prefix_, max_file_size_, is_append_date_, is_append_time_, is_audio_transcode_aac_); record_config_dlg.ShowDialog(); String rec_dir = record_config_dlg.RecDir(); if (!String.IsNullOrEmpty(rec_dir)) { rec_dir_ = rec_dir; } else { MessageBox.Show("未设置录像保存路径,默认保存到rec文件夹下.."); } is_rec_video_ = record_config_dlg.IsRecVideo(); is_rec_audio_ = record_config_dlg.IsRecAudio(); rec_name_file_prefix_ = record_config_dlg.RecNameFilePrefix(); max_file_size_ = record_config_dlg.MaxFileSize(); is_append_date_ = record_config_dlg.IsAppendDate(); is_append_time_ = record_config_dlg.IsAppendTime(); is_audio_transcode_aac_ = record_config_dlg.IsAudioTanscodeAAC(); }
开始录像、结束录像设计如下:
/* * SmartPlayerForm.cs * Created by daniusdk.com on 2017/04/19. * WeChat: xinsheng120 */ private void btn_record_Click(object sender, EventArgs e) { if (player_handle_ == IntPtr.Zero) return; if (btn_record.Text == "录像") { if (!is_rec_video_ && !is_rec_audio_) { MessageBox.Show("音频录制选项和视频录制选项至少需要选择一个!"); return; } if (!is_playing_) { if (!InitCommonSDKParam()) { MessageBox.Show("设置参数错误!"); return; } } NTSmartPlayerSDK.NT_SP_SetRecorderVideo(player_handle_, is_rec_video_ ? 1 : 0); NTSmartPlayerSDK.NT_SP_SetRecorderAudio(player_handle_, is_rec_audio_ ? 1 : 0); UInt32 ret = NTSmartPlayerSDK.NT_SP_SetRecorderDirectoryW(player_handle_, rec_dir_); if (NT.NTBaseCodeDefine.NT_ERC_OK != ret) { MessageBox.Show("设置录像目录失败"); return; } NTSmartPlayerSDK.NT_SP_SetRecorderFileMaxSize(player_handle_, max_file_size_); NT_SP_RecorderFileNameRuler rec_name_ruler = new NT_SP_RecorderFileNameRuler(); rec_name_ruler.type_ = 0; rec_name_ruler.file_name_prefix_ = rec_name_file_prefix_; rec_name_ruler.append_date_ = is_append_date_ ? 1 : 0; rec_name_ruler.append_time_ = is_append_time_ ? 1 : 0; NTSmartPlayerSDK.NT_SP_SetRecorderFileNameRuler(player_handle_, ref rec_name_ruler); record_call_back_ = new SP_SDKRecorderCallBack(SDKRecorderCallBack); NTSmartPlayerSDK.NT_SP_SetRecorderCallBack(player_handle_, IntPtr.Zero, record_call_back_); NTSmartPlayerSDK.NT_SP_SetRecorderAudioTranscodeAAC(player_handle_, is_audio_transcode_aac_ ? 1 : 0); if (NT.NTBaseCodeDefine.NT_ERC_OK != NTSmartPlayerSDK.NT_SP_StartRecorder(player_handle_)) { MessageBox.Show("录像失败!"); return; } btn_record.Text = "停止录像"; is_recording_ = true; } else { StopRecorder(); } }
录像状态回调处理如下,包含了开始录像回调和单个文件录制结束回调:
public void SDKRecorderCallBack(IntPtr handle, IntPtr userData, UInt32 status, [MarshalAs(UnmanagedType.LPStr)] String file_name) { if (playWnd.InvokeRequired) { BeginInvoke(set_record_call_back_, status, file_name); } else { set_record_call_back_(status, file_name); } } private void RecordCallBack(UInt32 status, [MarshalAs(UnmanagedType.LPStr)] String file_name) { byte[] utf8_bytes = Encoding.Default.GetBytes(file_name); byte[] default_bytes = Encoding.Convert(Encoding.UTF8, Encoding.Default, utf8_bytes); String recorder_file_name = Encoding.Default.GetString(default_bytes); StringBuilder sb = new StringBuilder(); sb.Append("录像状态:"); if (status == 1) { sb.Append("new file: "); } else if(status == 2) { sb.Append("finished file: "); } sb.Append(recorder_file_name); MessageBox.Show(sb.ToString()); }
总结
以上是Windows平台RTSP、RTMP播放器实时录像接口设计,实际上,除了Windows平台,我们Linux、Android、iOS平台也是一样的设计,单纯的录像模块,如果做的全面,也不是一两个接口可以搞定的,此外,录像设计,需要和RTSP|RTMP拉流播放设计,可以做到一起,也可以拆分使用,如果同时录像和直播播放,要注意的是,这时候只需要在一个实例操作,不要播放一个实例,录像一个实例,造成下载两路试试RTSP|RTMP流下来。