OpenHarmony子系统开发 - 媒体音视频
一、音视频开发概述
OpenHarmony音视频包括音视频播放和录制。
-
OpenHarmony音视频播放模块支持音视频播放业务的开发,主要包括音视频文件和音视频流播放、音量和播放进度控制等。
-
OpenHarmony录制模块支持音视频录制业务的开发,提供音视频录制相关的功能,包括设置录制视频画面尺寸、音视频编码码率、编码器类型、视频帧率、音频采样率、录制文件输出格式等。
基本概念
在进行应用的开发OpenHarmony前,开发者应了解以下基本概念:
-
流媒体技术
流媒体技术是把连续的影像和声音信息进行编码处理后放在网络服务器上,让浏览者一边下载、一边观看与收听,而不需要等整个多媒体文件下载完成就可以即时观看、收听的技术。
-
视频帧率
帧率是用于测量显示帧数的度量,帧数就是在每秒钟时间里传输的图片数量。每秒钟帧数 (FPS) 越多,所显示的画面就会越流畅。
-
码率
码率就是数据传输时单位时间传送的数据位数,常用单位是kbps即千位每秒。
-
采样率
采样率为每秒从连续信号中提取并组成离散信号的采样个数,单位用赫兹(Hz)来表示。
编解码规格
OpenHarmony音视频编解码能力取决于具体设备类型,以当前已支持的开发板为例,规格见下表:
表1 不同开发板编解码规格
设备类型 | 开发板类型 | 解码规格 | 编码规格 |
---|---|---|---|
带屏摄像头类产品 | Hi3516 | - 音频解码:支持MPEG-4 AAC Profile (AAC LC)格式解码,支持单/双声道,支持MPEG-4(.mp4,.m4a)容器格式。 - 视频解码:支持H.265 HEVC/H.264 AVC格式解码(限自身编码码流),支持MPEG-4(.mp4)容器格式。 | - 音频编码:支持音频AAC_LC编码,支持单/双声道,支持MPEG-4(.mp4)容器格式。 - 视频编码:支持视频H.264/H.265编码,支持MPEG-4(.mp4)容器格式。 |
无屏摄像头类产品 | Hi3518 | - 音频解码:支持MPEG-4 AAC Profile (AAC LC)格式解码,支持单/双声道,支持MPEG-4(.mp4,.m4a)容器格式。 - 视频解码:- | - 音频编码:支持音频AAC_LC编码,支持单/双声道,支持MPEG-4(.mp4)容器格式。 - 视频编码:支持视频H.264/H.265编码,支持MPEG-4(.mp4)容器格式。 |
WLAN连接类设备 | Hi3861 | - | - |
Hi3516和Hi3518更多详细的编解码规格请参考开发板自带的资料。
二、音视频播放开发指导
使用场景
音视频播放是将音视频文件或音视频流数据进行解码并通过输出设备进行播放的过程,同时对播放任务进行管理。
接口说明
音视频播放API接口功能如下,具体的API详见接口文档。
表1 音视频播放API接口
类名 | 接口名 | 描述 |
---|---|---|
Player | int32_t SetSource(const Source &source); | 设置播放源。 |
Player | int32_t Prepare(); | 准备播放。 |
Player | int32_t Play(); | 开始播放。 |
Player | bool IsPlaying() | 判断是否播放中。 |
Player | int32_t Pause(); | 暂停播放。 |
Player | int32_t Stop(); | 停止播放。 |
Player | int32_t Rewind(int_64 mSeconds, int32_t mode); | 改变播放位置。 |
Player | int32_t SetVolume(float leftVolume, float rightVolume); | 设置音量,包括左声道和右声道。 |
Player | int32_t SetVideoSurface(Surface *surface) | 设置播放窗口。 |
Player | int32_t EnableSingleLooping(bool loop) | 设置循环播放。 |
Player | bool IsSingleLooping(); | 判断是否循环播放。 |
Player | int32_t GetCurrentTime(int64_t &time) const; | 获取当前播放时长。 |
Player | int32_t GetDuration(int64_t &duration) const; | 获取总播放时长。 |
Player | int32_t GetVideoWidth(int32_t &videoWidth); | 获取视频源宽度。 |
Player | int32_t GetVideoHeight(int32_t &videoHeight); | 获取视频源高度。 |
Player | int32_t Reset(); | 重置播放器。 |
Player | int32_t Release(); | 释放播放器资源。 |
Player | void SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb); | 设置播放回调函数。 |
Source | Source(const std::string& uri); | 基于uri创建Source实例。 |
Source | Source(const std::shared_ptr<StreamSource> &stream, const Format &formats); | 基于流创建Source实例。 |
Source | SourceType GetSourceType() const; | 获取源类型。 |
Source | const std::string &GetSourceUri() const; | 获取音视频uri。 |
Source | const std::shared_ptr<StreamSource> &GetSourceStream() const; | 获取音视频流。 |
Source | const Format &GetSourceStreamFormat() const; | 获取音视频流格式。 |
Format | bool PutIntValue(const std::string &key, int32_t value); | 设置整数类型的元数据。 |
Format | bool PutLongValue(const std::string &key, int64_t value); | 设置长整数类型的元数据。 |
Format | bool PutFloatValue(const std::string &key, float value); | 设置单精度浮点类型的元数据。 |
Format | bool PutDoubleValue(const std::string &key, double value); | 设置双精度浮点类型的元数据。 |
Format | bool PutStringValue(const std::string &key, const std::string &value); | 设置字符串类型的元数据。 |
Format | bool GetIntValue(const std::string &key, int32_t &value) const; | 获取整数类型的元数据值。 |
Format | bool GetLongValue(const std::string &key, int64_t &value) const; | 获取长整数类型的元数据值。 |
Format | bool GetFloatValue(const std::string &key, float &value) const; | 获取单精度浮点类型的元数据值。 |
Format | bool GetDoubleValue(const std::string &key, double &value) const; | 获取双精度浮点类型的元数据值。 |
Format | bool GetStringValue(const std::string &key, std::string &value) const; | 获取字符串类型的元数据值。 |
Format | const std::map<std::string, FormatData *> &GetFormatMap() const; | 获取元数据映射表。 |
Format | bool CopyFrom(const Format &format); | 用输入Format设置所有元数据。 |
约束与限制
输入源为音视频流时,不支持播放进度控制和获取文件时长。
开发步骤
-
实现PlayerCallback回调,通过SetPlayerCallback函数进行绑定,用于事件处理。
class TestPlayerCallback : public PlayerCallback{ void OnPlaybackComplete() override { //此处实现代码用于处理文件播放完成的事件 } void OnError(int32_t errorType, int32_t errorCode) override { //此处实现代码处理错误事件 } void OnInfo(int type, int extra) override { //此处实现代码处理普通事件 } void OnRewindToComplete() override { //此处实现代码处理进度控制完成的事件 } };
-
创建Player实例,设置播放源并开始播放。
Player *player = new Player(); std::shared_ptr<PlayerCallback> callback = std::make_shared<TestPlayerCallback>(); player->SetPlayerCallback(callback);//设置player回调 std::string uri(filePath);//此处filePath为本地文件路径 Source source(uri);//保存uri到source实例 player->SetSource(source);//将source设置到player player->Prepare();//准备播放 player->SetVideoSurface(surface);//设置播放窗口 player->Play();//开始播放
-
根据场景需要进行播放控制。
player->SetVolume(lvolume, rvolume);//设置左右声道声音 player->EnableSingleLooping(true);//设置循环播放 player->Pause();//暂停 player->Play();//继续播放
-
播放任务结束后,进行资源释放。
player->Stop(); //停止播放 player->Release();//释放资源
三、音视频录制开发指导
使用场景
音视频录制的主要功能是录制音视频,并根据设置的编码格式、采样率、码率等参数封装输出文件。
接口说明
音视频录制API接口如下,具体的API详见接口文档。
表1 音视频录制API接口
类名 | 接口名 | 功能 |
---|---|---|
Recorder | int32_t SetVideoSource(VideoSourceType source, int32_t &sourceId) | 设置录制视频源 |
Recorder | int32_t SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder) | 设置录制的视频编码器类型 |
Recorder | int32_t SetVideoSize(int32_t sourceId, int32_t width, int32_t height) | 设置录制的视频宽和高 |
Recorder | int32_t SetVideoFrameRate(int32_t sourceId, int32_t frameRate) | 设置要录制的视频帧率 |
Recorder | int32_t SetVideoEncodingBitRate(int32_t sourceId, int32_t rate) | 设置录制视频编码的码率 |
Recorder | int32_t SetCaptureRate(int32_t sourceId, double fps) | 设置视频帧的捕获帧率 |
Recorder | std::shared_ptr<OHOS::Surface> GetSurface(int32_t sourceId); | 获取对应输入源的surface |
Recorder | int32_t SetAudioSource(AudioSourceType source, int32_t &sourceId) | 设置录制音频源 |
Recorder | int32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder) | 设置录制的音频编码器类型 |
Recorder | int32_t SetAudioSampleRate(int32_t sourceId, int32_t rate) | 设置录制的音频采样率 |
Recorder | int32_t SetAudioChannels(int32_t sourceId, int32_t num) | 设置要录制的音频通道数 |
Recorder | int32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate) | 设置录制音频编码的码率 |
Recorder | int32_t SetMaxDuration(int32_t duration) | 设置录制文件的最大时长 |
Recorder | int32_t SetOutputFormat(OutputFormatType format) | 设置输出文件格式 |
Recorder | int32_t SetOutputPath(const string &path); | 设置输出文件保存路径 |
Recorder | int32_t SetOutputFile(int32_t fd) | 设置输出文件的fd |
Recorder | int32_t SetNextOutputFile(int32_t fd); | 设置下一个输出文件的fd |
Recorder | int32_t SetMaxFileSize(int64_t size) | 设置录制会话的最大文件大小 |
Recorder | int32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback) | 注册录制侦听器回调 |
Recorder | int32_t Prepare() | 准备录制 |
Recorder | int32_t Start() | 开始录制 |
Recorder | int32_t Pause() | 暂停录制 |
Recorder | int32_t Resume() | 恢复录制 |
Recorder | int32_t Stop(bool block) | 停止录制 |
Recorder | int32_t Reset(); | 重置录制 |
Recorder | int32_t Release() | 释放录制资源 |
Recorder | int32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration) | 设置切分录像 |
Recorder | int32_t SetParameter(int32_t sourceId, const Format &format) | 设置录制的扩展参数 |
约束与限制
无。
开发步骤
-
创建Recorder实例。
Recorder *recorder = new Recorder();
-
设置Recorder参数,包括设置音视频源信息,音视频编码格式,采样率,码率,视频宽高等信息。
int32_t sampleRate = 48000; int32_t channelCount = 1; AudioCodecFormat audioFormat = AAC_LC; AudioSourceType inputSource = AUDIO_MIC; int32_t audioEncodingBitRate = sampleRate; VideoSourceType source = VIDEO_SOURCE_SURFACE_ES; int32_t frameRate = 30; double fps = 30; int32_t rate = 4096; int32_t sourceId = 0; int32_t audioSourceId = 0; int32_t width = 1920; int32_t height = 1080; VideoCodecFormat encoder = H264; recorder->SetVideoSource(source, sourceId ); // 设置视频源,获得sourceId recorder->SetVideoEncoder(sourceId, encoder); // 设置视频编码格式 recorder->SetVideoSize(sourceId, width, height); // 设置视频宽高 recorder->SetVideoFrameRate(sourceId, frameRate); // 设置视频帧率 recorder->SetVideoEncodingBitRate(sourceId, rate); // 设置视频编码码率 recorder->SetCaptureRate(sourceId, fps); // 设置视频帧的捕获帧率 recorder->SetAudioSource(inputSource, audioSourceId); // 设置音频源,获得audioSourceId recorder->SetAudioEncoder(audioSourceId, audioFormat); // 设置音频编码格式 recorder->SetAudioSampleRate(audioSourceId, sampleRate); // 设置音频采样率 recorder->SetAudioChannels(audioSourceId, channelCount); // 设置音频通道数 recorder->SetAudioEncodingBitRate(audioSourceId, audioEncodingBitRate); // 设置音频编码码率
-
准备录制,Recorder进行录制前的准备工作。
recorder->Prepare(); // 准备录制
-
开始录制,Recorder会根据设置的音频源和视频源进行录制。
recorder->Start(); // 开始录制
-
结束录制,释放资源。
recorder->Stop(); // 停止录制 recorder->Release(); // 释放录制资源