From c7fc69be2236a6f3f09680f8b1f1a075b943e31d Mon Sep 17 00:00:00 2001 From: acgist <289547414@qq.com> Date: Sat, 11 May 2024 15:12:23 +0800 Subject: [PATCH] =?UTF-8?q?[*]=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/src/main/cpp/include/Capturer.hpp | 4 +-- .../media/src/main/cpp/include/Player.hpp | 1 + .../src/main/cpp/media/AudioCapturer.cpp | 2 +- .../media/src/main/cpp/media/AudioPlayer.cpp | 25 +++++++++++-------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp index 9e8a63c..199d09e 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Capturer.hpp @@ -106,7 +106,7 @@ public: public: AudioCapturer(); - virtual ~AudioCapturer(); + virtual ~AudioCapturer() override; public: virtual bool start() override; @@ -161,7 +161,7 @@ public: public: VideoCapturer(); - virtual ~VideoCapturer(); + virtual ~VideoCapturer() override; public: void initVulkan(); diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Player.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Player.hpp index 0e5a759..52ae78a 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Player.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Player.hpp @@ -25,6 +25,7 @@ namespace acgist { class Player { protected: + // 是否运行 bool running = false; public: diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp index 653d77f..bb9c479 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioCapturer.cpp @@ -25,7 +25,7 @@ acgist::AudioCapturer::AudioCapturer() { OH_AudioStreamBuilder_SetLatencyMode(this->builder, OH_AudioStream_LatencyMode::AUDIOSTREAM_LATENCY_MODE_NORMAL); OH_AudioStreamBuilder_SetSampleFormat(this->builder, OH_AudioStream_SampleFormat::AUDIOSTREAM_SAMPLE_S16LE); OH_LOG_DEBUG(LOG_APP, "配置音频采集参数:%d %d", acgist::samplingRate, acgist::channelCount); - // 设置采集回调 + // 设置音频采集回调 OH_AudioCapturer_Callbacks callbacks; callbacks.OH_AudioCapturer_OnError = OnError; callbacks.OH_AudioCapturer_OnReadData = OnReadData; diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioPlayer.cpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioPlayer.cpp index 84886bf..96fb67b 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioPlayer.cpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/media/AudioPlayer.cpp @@ -1,7 +1,11 @@ #include "../include/Player.hpp" +#include + #include +static std::recursive_mutex audioMutex; + // 播放回调 static int32_t OnError(OH_AudioRenderer* renderer, void* userData, OH_AudioStream_Result error); static int32_t OnWriteData(OH_AudioRenderer* renderer, void* userData, void* buffer, int32_t length); @@ -10,7 +14,7 @@ static int32_t OnInterruptEvent(OH_AudioRenderer* renderer, void* userData, OH_A acgist::AudioPlayer::AudioPlayer() { OH_AudioStream_Result ret = OH_AudioStreamBuilder_Create(&this->builder, AUDIOSTREAM_TYPE_RENDERER); - OH_LOG_INFO(LOG_APP, "构造音频构造器:%o", ret); + OH_LOG_INFO(LOG_APP, "配置音频构造器:%o", ret); // 配置音频播放参数 OH_AudioStreamBuilder_SetSamplingRate(this->builder, acgist::samplingRate); OH_AudioStreamBuilder_SetChannelCount(this->builder, acgist::channelCount); @@ -18,14 +22,14 @@ acgist::AudioPlayer::AudioPlayer() { OH_AudioStreamBuilder_SetEncodingType(this->builder, OH_AudioStream_EncodingType::AUDIOSTREAM_ENCODING_TYPE_RAW); OH_AudioStreamBuilder_SetRendererInfo(this->builder, OH_AudioStream_Usage::AUDIOSTREAM_USAGE_MUSIC); OH_LOG_DEBUG(LOG_APP, "配置音频播放参数:%d %d", acgist::samplingRate, acgist::channelCount); - // 配置回调函数 + // 配置音频播放回调 OH_AudioRenderer_Callbacks callbacks; callbacks.OH_AudioRenderer_OnError = OnError; callbacks.OH_AudioRenderer_OnWriteData = OnWriteData; callbacks.OH_AudioRenderer_OnStreamEvent = OnStreamEvent; callbacks.OH_AudioRenderer_OnInterruptEvent = OnInterruptEvent; ret = OH_AudioStreamBuilder_SetRendererCallback(this->builder, callbacks, this); - OH_LOG_DEBUG(LOG_APP, "设置播放回调函数:%o", ret); + OH_LOG_DEBUG(LOG_APP, "设置音频播放回调:%o", ret); } acgist::AudioPlayer::~AudioPlayer() { @@ -33,18 +37,19 @@ acgist::AudioPlayer::~AudioPlayer() { if(this->builder != nullptr) { OH_AudioStream_Result ret = OH_AudioStreamBuilder_Destroy(this->builder); this->builder = nullptr; - OH_LOG_INFO(LOG_APP, "释放音频播放:%o", ret); + OH_LOG_INFO(LOG_APP, "释放音频构造器:%o", ret); } } bool acgist::AudioPlayer::start() { + std::lock_guard audioLock(audioMutex); if (this->running) { return true; } this->running = true; - // 构造音频播放器 + // 配置音频播放器 OH_AudioStream_Result ret = OH_AudioStreamBuilder_GenerateRenderer(this->builder, &this->audioRenderer); - OH_LOG_DEBUG(LOG_APP, "构造音频播放器:%o", ret); + OH_LOG_DEBUG(LOG_APP, "配置音频播放器:%o", ret); // 开始音频播放 ret = OH_AudioRenderer_Start(this->audioRenderer); OH_LOG_DEBUG(LOG_APP, "开始音频播放:%o", ret); @@ -52,13 +57,11 @@ bool acgist::AudioPlayer::start() { } bool acgist::AudioPlayer::stop() { + std::lock_guard audioLock(audioMutex); if (!this->running) { return true; } this->running = false; - if (this->audioRenderer == nullptr) { - return true; - } // 停止音频播放 OH_AudioStream_Result ret = OH_AudioRenderer_Stop(this->audioRenderer); OH_LOG_DEBUG(LOG_APP, "停止音频播放:%o", ret); @@ -75,7 +78,7 @@ static int32_t OnError(OH_AudioRenderer* renderer, void* userData, OH_AudioStrea } static int32_t OnWriteData(OH_AudioRenderer* renderer, void* userData, void* buffer, int32_t length) { - // TODO: 混音写入buffer + // TODO: 多个需要混音写入buffer return 0; } @@ -85,6 +88,6 @@ static int32_t OnStreamEvent(OH_AudioRenderer* renderer, void* userData, OH_Audi } static int32_t OnInterruptEvent(OH_AudioRenderer* renderer, void* userData, OH_AudioInterrupt_ForceType type, OH_AudioInterrupt_Hint hint) { - OH_LOG_DEBUG(LOG_APP, "音频播放打断:%o %o", type, hint); + OH_LOG_DEBUG(LOG_APP, "打断音频播放:%o %o", type, hint); return 0; }