1 /* 2 * Copyright (c) 2024 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 <queue> 25 #include <securec.h> 26 #include <sstream> 27 #include <string> 28 #include <thread> 29 #include <unistd.h> 30 31 #include "audio_info.h" 32 #include "audio_renderer.h" 33 #include "audio_system_manager.h" 34 35 #include "audio_data.h" 36 #include "audio_status.h" 37 #include "audio_event.h" 38 #include "av_receiver_engine_transport.h" 39 #include "daudio_constants.h" 40 #include "daudio_errorcode.h" 41 #include "daudio_log.h" 42 #include "daudio_sink_ctrl_trans.h" 43 #include "iaudio_data_transport.h" 44 #include "iaudio_datatrans_callback.h" 45 #include "iaudio_event_callback.h" 46 #include "ispk_client.h" 47 48 namespace OHOS { 49 namespace DistributedHardware { 50 class DSpeakerClient : public IAudioDataTransCallback, 51 public ISpkClient, public AVReceiverTransportCallback, 52 public IAudioCtrlTransCallback, 53 public AudioStandard::VolumeKeyEventCallback, 54 public AudioStandard::AudioRendererCallback, 55 public AudioStandard::AudioRendererWriteCallback, 56 public std::enable_shared_from_this<DSpeakerClient> { 57 public: DSpeakerClient(const std::string & devId,const int32_t & dhId,const std::shared_ptr<IAudioEventCallback> & callback)58 DSpeakerClient(const std::string &devId, const int32_t &dhId, const std::shared_ptr<IAudioEventCallback> &callback) 59 : devId_(devId), dhId_(dhId), eventCallback_(callback) {}; 60 ~DSpeakerClient() override; 61 62 int32_t OnStateChange(const AudioEventType type) override; 63 void OnStateChange(const AudioStandard::RendererState state, 64 const AudioStandard::StateChangeCmdType __attribute__((unused)) cmdType) override; 65 int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override; 66 void OnVolumeKeyEvent(AudioStandard::VolumeEvent volumeEvent) override; 67 void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 68 int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; 69 int32_t InitCtrlTrans() override; 70 int32_t SetUp(const AudioParam ¶m) override; 71 int32_t Release() override; 72 int32_t StartRender() override; 73 int32_t StopRender() override; 74 int32_t SetMute(const AudioEvent &event) override; 75 int32_t SetAudioParameters(const AudioEvent &event) override; 76 void PlayStatusChange(const std::string &args) override; 77 void SetAttrs(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback) override; 78 int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; 79 80 void OnEngineTransEvent(const AVTransEvent &event) override; 81 void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 82 void OnEngineTransDataAvailable(const std::shared_ptr<AudioData> &audioData) override; 83 84 void OnCtrlTransEvent(const AVTransEvent &event) override; 85 void OnCtrlTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 86 87 void OnWriteData(size_t length) override; 88 private: 89 std::string GetVolumeLevel(); 90 void PlayThreadRunning(); 91 void Pause(); 92 void ReStart(); 93 void FillJitterQueue(); 94 void FlushJitterQueue(); 95 int32_t CreateAudioRenderer(const AudioParam ¶m); 96 97 private: 98 constexpr static size_t DATA_QUEUE_MAX_SIZE = 12; 99 constexpr static size_t REQUEST_DATA_WAIT = 10; 100 constexpr static size_t DATA_QUEUE_SIZE = 8; 101 constexpr static size_t SLEEP_TIME = 5000; 102 static constexpr const char* RENDERTHREAD = "renderThread"; 103 const std::string DUMP_DAUDIO_SPK_AFTER_TRANS_NAME = "dump_sink_spk_recv_from_trans.pcm"; 104 105 std::string devId_; 106 const int32_t dhId_; 107 std::thread renderDataThread_; 108 AudioParam audioParam_; 109 std::atomic<bool> isRenderReady_ = false; 110 std::mutex dataQueueMtx_; 111 std::mutex devMtx_; 112 std::queue<std::shared_ptr<AudioData>> dataQueue_; 113 std::condition_variable dataQueueCond_; 114 AudioStatus clientStatus_ = AudioStatus::STATUS_IDLE; 115 116 std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ = nullptr; 117 std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr; 118 std::shared_ptr<IAudioCtrlTransport> speakerCtrlTrans_ = nullptr; 119 std::weak_ptr<IAudioEventCallback> eventCallback_; 120 int64_t lastPlayStartTime_ = 0; 121 int64_t lastReceiveStartTime_ = 0; 122 FILE *dumpFile_ = nullptr; 123 }; 124 } // DistributedHardware 125 } // OHOS 126 #endif // OHOS_DSPEAKER_CLIENT_H 127