1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DSPEAKER_CLIENT_H 17 #define OHOS_DSPEAKER_CLIENT_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <cstdint> 22 #include <memory> 23 #include <mutex> 24 #include <securec.h> 25 #include <sstream> 26 #include <string> 27 #include <thread> 28 #include <unistd.h> 29 30 #include "audio_info.h" 31 #include "audio_renderer.h" 32 #include "audio_system_manager.h" 33 34 #include "audio_data.h" 35 #include "audio_status.h" 36 #include "audio_decode_transport.h" 37 #include "audio_event.h" 38 #include "av_receiver_engine_transport.h" 39 #include "daudio_errorcode.h" 40 #include "daudio_log.h" 41 #include "iaudio_data_transport.h" 42 #include "iaudio_datatrans_callback.h" 43 #include "iaudio_event_callback.h" 44 #include "ispk_client.h" 45 46 namespace OHOS { 47 namespace DistributedHardware { 48 class DSpeakerClient : public IAudioDataTransCallback, 49 public ISpkClient, public AVReceiverTransportCallback, 50 public AudioStandard::VolumeKeyEventCallback, 51 public AudioStandard::AudioRendererCallback, 52 public AudioStandard::AudioRendererWriteCallback, 53 public std::enable_shared_from_this<DSpeakerClient> { 54 public: DSpeakerClient(const std::string & devId,const std::shared_ptr<IAudioEventCallback> & callback)55 DSpeakerClient(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback) 56 : devId_(devId), eventCallback_(callback) {}; 57 ~DSpeakerClient() override; 58 59 int32_t OnStateChange(const AudioEventType type) override; 60 void OnStateChange(const AudioStandard::RendererState state, 61 const AudioStandard::StateChangeCmdType __attribute__((unused)) cmdType) override; 62 int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override; 63 void OnVolumeKeyEvent(AudioStandard::VolumeEvent volumeEvent) override; 64 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 65 int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; 66 int32_t SetUp(const AudioParam ¶m) override; 67 int32_t Release() override; 68 int32_t StartRender() override; 69 int32_t StopRender() override; 70 int32_t SetMute(const AudioEvent &event) override; 71 int32_t SetAudioParameters(const AudioEvent &event) override; 72 void PlayStatusChange(const std::string &args) override; 73 void SetAttrs(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback) override; 74 int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; 75 76 void OnEngineTransEvent(const AVTransEvent &event) override; 77 void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 78 void OnEngineTransDataAvailable(const std::shared_ptr<AudioData> &audioData) override; 79 80 void OnWriteData(size_t length) override; 81 private: 82 std::string GetVolumeLevel(); 83 void PlayThreadRunning(); 84 void Pause(); 85 void ReStart(); 86 void FillJitterQueue(); 87 void FlushJitterQueue(); 88 int32_t CreateAudioRenderer(const AudioParam ¶m); 89 90 private: 91 constexpr static size_t DATA_QUEUE_MAX_SIZE = 12; 92 constexpr static size_t REQUEST_DATA_WAIT = 10; 93 constexpr static size_t DATA_QUEUE_SIZE = 8; 94 constexpr static size_t SLEEP_TIME = 5000; 95 static constexpr const char* RENDERTHREAD = "renderThread"; 96 97 std::string devId_; 98 std::thread renderDataThread_; 99 AudioParam audioParam_; 100 std::atomic<bool> isRenderReady_ = false; 101 std::mutex dataQueueMtx_; 102 std::mutex devMtx_; 103 std::queue<std::shared_ptr<AudioData>> dataQueue_; 104 std::condition_variable dataQueueCond_; 105 AudioStatus clientStatus_ = AudioStatus::STATUS_IDLE; 106 107 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ = nullptr; 108 std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr; 109 std::weak_ptr<IAudioEventCallback> eventCallback_; 110 }; 111 } // DistributedHardware 112 } // OHOS 113 #endif // OHOS_DSPEAKER_CLIENT_H 114