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