OpenHarmony子系统开发 - 媒体音视频

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接口

类名接口名描述
Playerint32_t SetSource(const Source &source);设置播放源。
Playerint32_t Prepare();准备播放。
Playerint32_t Play();开始播放。
Playerbool IsPlaying()判断是否播放中。
Playerint32_t Pause();暂停播放。
Playerint32_t Stop();停止播放。
Playerint32_t Rewind(int_64 mSeconds, int32_t mode);改变播放位置。
Playerint32_t SetVolume(float leftVolume, float rightVolume);设置音量,包括左声道和右声道。
Playerint32_t SetVideoSurface(Surface *surface)设置播放窗口。
Playerint32_t EnableSingleLooping(bool loop)设置循环播放。
Playerbool IsSingleLooping();判断是否循环播放。
Playerint32_t GetCurrentTime(int64_t &time) const;获取当前播放时长。
Playerint32_t GetDuration(int64_t &duration) const;获取总播放时长。
Playerint32_t GetVideoWidth(int32_t &videoWidth);获取视频源宽度。
Playerint32_t GetVideoHeight(int32_t &videoHeight);获取视频源高度。
Playerint32_t Reset();重置播放器。
Playerint32_t Release();释放播放器资源。
Playervoid SetPlayerCallback(const std::shared_ptr<PlayerCallback> &cb);设置播放回调函数。
SourceSource(const std::string& uri);基于uri创建Source实例。
SourceSource(const std::shared_ptr<StreamSource> &stream, const Format &formats);基于流创建Source实例。
SourceSourceType GetSourceType() const;获取源类型。
Sourceconst std::string &GetSourceUri() const;获取音视频uri。
Sourceconst std::shared_ptr<StreamSource> &GetSourceStream() const;获取音视频流。
Sourceconst Format &GetSourceStreamFormat() const;获取音视频流格式。
Formatbool PutIntValue(const std::string &key, int32_t value);设置整数类型的元数据。
Formatbool PutLongValue(const std::string &key, int64_t value);设置长整数类型的元数据。
Formatbool PutFloatValue(const std::string &key, float value);设置单精度浮点类型的元数据。
Formatbool PutDoubleValue(const std::string &key, double value);设置双精度浮点类型的元数据。
Formatbool PutStringValue(const std::string &key, const std::string &value);设置字符串类型的元数据。
Formatbool GetIntValue(const std::string &key, int32_t &value) const;获取整数类型的元数据值。
Formatbool GetLongValue(const std::string &key, int64_t &value) const;获取长整数类型的元数据值。
Formatbool GetFloatValue(const std::string &key, float &value) const;获取单精度浮点类型的元数据值。
Formatbool GetDoubleValue(const std::string &key, double &value) const;获取双精度浮点类型的元数据值。
Formatbool GetStringValue(const std::string &key, std::string &value) const;获取字符串类型的元数据值。
Formatconst std::map<std::string, FormatData *> &GetFormatMap() const;获取元数据映射表。
Formatbool CopyFrom(const Format &format);用输入Format设置所有元数据。

约束与限制

输入源为音视频流时,不支持播放进度控制和获取文件时长。

开发步骤

  1. 实现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 
        { 
            //此处实现代码处理进度控制完成的事件 
        } 
    };
    
    
  2. 创建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();//开始播放
    
  3. 根据场景需要进行播放控制。

    player->SetVolume(lvolume, rvolume);//设置左右声道声音 
    player->EnableSingleLooping(true);//设置循环播放 
    player->Pause();//暂停 
    player->Play();//继续播放
    
  4. 播放任务结束后,进行资源释放。

    player->Stop(); //停止播放
    player->Release();//释放资源
    

三、音视频录制开发指导

使用场景

音视频录制的主要功能是录制音视频,并根据设置的编码格式、采样率、码率等参数封装输出文件。

接口说明

音视频录制API接口如下,具体的API详见接口文档。

表1 音视频录制API接口

类名接口名功能
Recorderint32_t SetVideoSource(VideoSourceType source, int32_t &sourceId)设置录制视频源
Recorderint32_t SetVideoEncoder(int32_t sourceId, VideoCodecFormat encoder)设置录制的视频编码器类型
Recorderint32_t SetVideoSize(int32_t sourceId, int32_t width, int32_t height)设置录制的视频宽和高
Recorderint32_t SetVideoFrameRate(int32_t sourceId, int32_t frameRate)设置要录制的视频帧率
Recorderint32_t SetVideoEncodingBitRate(int32_t sourceId, int32_t rate)设置录制视频编码的码率
Recorderint32_t SetCaptureRate(int32_t sourceId, double fps)设置视频帧的捕获帧率
Recorderstd::shared_ptr<OHOS::Surface> GetSurface(int32_t sourceId);获取对应输入源的surface
Recorderint32_t SetAudioSource(AudioSourceType source, int32_t &sourceId)设置录制音频源
Recorderint32_t SetAudioEncoder(int32_t sourceId, AudioCodecFormat encoder)设置录制的音频编码器类型
Recorderint32_t SetAudioSampleRate(int32_t sourceId, int32_t rate)设置录制的音频采样率
Recorderint32_t SetAudioChannels(int32_t sourceId, int32_t num)设置要录制的音频通道数
Recorderint32_t SetAudioEncodingBitRate(int32_t sourceId, int32_t bitRate)设置录制音频编码的码率
Recorderint32_t SetMaxDuration(int32_t duration)设置录制文件的最大时长
Recorderint32_t SetOutputFormat(OutputFormatType format)设置输出文件格式
Recorderint32_t SetOutputPath(const string &path);设置输出文件保存路径
Recorderint32_t SetOutputFile(int32_t fd)设置输出文件的fd
Recorderint32_t SetNextOutputFile(int32_t fd);设置下一个输出文件的fd
Recorderint32_t SetMaxFileSize(int64_t size)设置录制会话的最大文件大小
Recorderint32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback)注册录制侦听器回调
Recorderint32_t Prepare()准备录制
Recorderint32_t Start()开始录制
Recorderint32_t Pause()暂停录制
Recorderint32_t Resume()恢复录制
Recorderint32_t Stop(bool block)停止录制
Recorderint32_t Reset();重置录制
Recorderint32_t Release()释放录制资源
Recorderint32_t SetFileSplitDuration(FileSplitType type, int64_t timestamp, uint32_t duration)设置切分录像
Recorderint32_t SetParameter(int32_t sourceId, const Format &format)设置录制的扩展参数

约束与限制

无。

开发步骤

  1. 创建Recorder实例。

    Recorder *recorder = new Recorder();
    
  2. 设置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); // 设置音频编码码率
    
  3. 准备录制,Recorder进行录制前的准备工作。

    recorder->Prepare(); // 准备录制
    
  4. 开始录制,Recorder会根据设置的音频源和视频源进行录制。

    recorder->Start(); // 开始录制
    
  5. 结束录制,释放资源。

    recorder->Stop(); // 停止录制
    recorder->Release(); // 释放录制资源
    
### OpenHarmony 中 PJSIP 的集成与使用 #### 背景概述 PJSIP 是一个开源的多媒体通信库,支持 SIP 协议栈以及音视频处理功能。要在 OpenHarmony 平台上集成和使用 PJSIP 库,需要完成以下几个方面的配置工作:环境搭建、依赖项管理、交叉编译设置以及最终的应用程序开发--- #### 环境搭建 为了在 OpenHarmony 上成功运行 PJSIP,首先需要构建适合目标硬件平台的系统镜像。根据已有信息,OpenHarmony 提供了一个预编译好的系统镜像路径 `OpenHarmony/out/rk3568/packages/phone/images`[^1]。此路径下的文件可以作为基础环境的一部分用于后续操作。 --- #### 依赖项管理 PJSIP 的正常运行依赖于多个外部组件,例如 OpenSSL(提供加密支持)、FFmpeg 或 GStreamer(音频编码解码)。这些依赖项需通过以下方式获取: 1. **源码下载** 可以从官方网站或其他可信仓库下载所需版本的依赖项源码。 2. **交叉编译工具链** 使用适用于目标架构(如 ARM)的交叉编译器对上述依赖项进行编译。具体步骤可参考鸿蒙社区关于 configure 和交叉编译的相关文档[^2]。 --- #### 交叉编译 PJSIP 以下是针对 OpenHarmony 进行 PJSIP 交叉编译的主要流程: 1. **安装必要的工具链** 确保已正确安装 GNU 工具链及相关头文件。如果尚未准备好,可以从 OpenHarmony 官方文档找到对应的目标板工具链说明。 2. **修改 Makefile 文件** 编辑 PJSIP 源代码目录中的 `Makefile` 文件,指定目标平台参数。例如: ```makefile TARGET=arm-openharmony-linux-gnueabi CC=/path/to/cross_compiler/bin/arm-openharmony-linux-gcc CXX=/path/to/cross_compiler/bin/arm-openharmony-linux-g++ ``` 3. **执行编译命令** 在终端中进入 PJSIP 源码根目录并运行以下指令: ```bash ./configure --host=arm-openharmony-linux-gnueabi \ --disable-video \ --enable-shared=no \ --prefix=$(pwd)/output make dep && make clean && make ``` 4. **验证生成产物** 成功完成后,在指定输出路径下会生成静态链接库 `.a` 文件或动态共享对象 `.so` 文件。 --- #### 开发应用程序 当所有准备工作就绪后,即可基于 OpenHarmony SDK 创建一个新的项目来调用 PJSIP 功能模块。下面是一个简单的示例代码片段展示如何初始化 PJSIP 实例: ```c #include <pjlib.h> #include <pjlib-util.h> #include <pjmedia.h> #include <pjsip.h> int main(int argc, char *argv[]) { pj_caching_pool cachingPool; pj_pool_t* pool; /* 初始化 PJLIB */ pj_init(); /* 启动日志子系统 */ pj_log_set_level(5); /* 设置缓存池 */ pj_caching_pool_init(&cachingPool, &pj_pool_factory_default_policy, 0); pool = pj_pool_create(&cachingPool.factory, "app", 4096, 4096, NULL); /* 执行其他逻辑... */ pj_pool_release(pool); pj_caching_pool_destroy(&cachingPool); return 0; } ``` 注意以上仅为基本框架示意;实际实现还需补充更多细节部分,比如网络传输层绑定地址设定等。 --- #### 常见问题排查 - 如果遇到缺少某些函数定义错误,则可能是遗漏了特定选项开关或者未加载完整的第三方插件集合。 - 对于性能优化方面的需求,建议启用 NEON SIMD 加速特性以便更好地适配 RK3568 类型 SoC 架构特点。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__Benco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值