150a07fd2Sopenharmony_ci/* 250a07fd2Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 350a07fd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 450a07fd2Sopenharmony_ci * you may not use this file except in compliance with the License. 550a07fd2Sopenharmony_ci * You may obtain a copy of the License at 650a07fd2Sopenharmony_ci * 750a07fd2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 850a07fd2Sopenharmony_ci * 950a07fd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1050a07fd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1150a07fd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1250a07fd2Sopenharmony_ci * See the License for the specific language governing permissions and 1350a07fd2Sopenharmony_ci * limitations under the License. 1450a07fd2Sopenharmony_ci */ 1550a07fd2Sopenharmony_ci 1650a07fd2Sopenharmony_ci#ifndef OHOS_DSPEAKER_CLIENT_H 1750a07fd2Sopenharmony_ci#define OHOS_DSPEAKER_CLIENT_H 1850a07fd2Sopenharmony_ci 1950a07fd2Sopenharmony_ci#include <atomic> 2050a07fd2Sopenharmony_ci#include <condition_variable> 2150a07fd2Sopenharmony_ci#include <cstdint> 2250a07fd2Sopenharmony_ci#include <memory> 2350a07fd2Sopenharmony_ci#include <mutex> 2450a07fd2Sopenharmony_ci#include <queue> 2550a07fd2Sopenharmony_ci#include <securec.h> 2650a07fd2Sopenharmony_ci#include <sstream> 2750a07fd2Sopenharmony_ci#include <string> 2850a07fd2Sopenharmony_ci#include <thread> 2950a07fd2Sopenharmony_ci#include <unistd.h> 3050a07fd2Sopenharmony_ci 3150a07fd2Sopenharmony_ci#include "audio_info.h" 3250a07fd2Sopenharmony_ci#include "audio_renderer.h" 3350a07fd2Sopenharmony_ci#include "audio_system_manager.h" 3450a07fd2Sopenharmony_ci 3550a07fd2Sopenharmony_ci#include "audio_data.h" 3650a07fd2Sopenharmony_ci#include "audio_status.h" 3750a07fd2Sopenharmony_ci#include "audio_event.h" 3850a07fd2Sopenharmony_ci#include "av_receiver_engine_transport.h" 3950a07fd2Sopenharmony_ci#include "daudio_constants.h" 4050a07fd2Sopenharmony_ci#include "daudio_errorcode.h" 4150a07fd2Sopenharmony_ci#include "daudio_log.h" 4250a07fd2Sopenharmony_ci#include "iaudio_data_transport.h" 4350a07fd2Sopenharmony_ci#include "iaudio_datatrans_callback.h" 4450a07fd2Sopenharmony_ci#include "iaudio_event_callback.h" 4550a07fd2Sopenharmony_ci#include "ispk_client.h" 4650a07fd2Sopenharmony_ci 4750a07fd2Sopenharmony_cinamespace OHOS { 4850a07fd2Sopenharmony_cinamespace DistributedHardware { 4950a07fd2Sopenharmony_ciclass DSpeakerClient : public IAudioDataTransCallback, 5050a07fd2Sopenharmony_ci public ISpkClient, public AVReceiverTransportCallback, 5150a07fd2Sopenharmony_ci public AudioStandard::VolumeKeyEventCallback, 5250a07fd2Sopenharmony_ci public AudioStandard::AudioRendererCallback, 5350a07fd2Sopenharmony_ci public AudioStandard::AudioRendererWriteCallback, 5450a07fd2Sopenharmony_ci public std::enable_shared_from_this<DSpeakerClient> { 5550a07fd2Sopenharmony_cipublic: 5650a07fd2Sopenharmony_ci DSpeakerClient(const std::string &devId, const int32_t &dhId, const std::shared_ptr<IAudioEventCallback> &callback) 5750a07fd2Sopenharmony_ci : devId_(devId), dhId_(dhId), eventCallback_(callback) {}; 5850a07fd2Sopenharmony_ci ~DSpeakerClient() override; 5950a07fd2Sopenharmony_ci 6050a07fd2Sopenharmony_ci int32_t OnStateChange(const AudioEventType type) override; 6150a07fd2Sopenharmony_ci void OnStateChange(const AudioStandard::RendererState state, 6250a07fd2Sopenharmony_ci const AudioStandard::StateChangeCmdType __attribute__((unused)) cmdType) override; 6350a07fd2Sopenharmony_ci int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override; 6450a07fd2Sopenharmony_ci void OnVolumeKeyEvent(AudioStandard::VolumeEvent volumeEvent) override; 6550a07fd2Sopenharmony_ci void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override; 6650a07fd2Sopenharmony_ci int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; 6750a07fd2Sopenharmony_ci int32_t SetUp(const AudioParam ¶m) override; 6850a07fd2Sopenharmony_ci int32_t Release() override; 6950a07fd2Sopenharmony_ci int32_t StartRender() override; 7050a07fd2Sopenharmony_ci int32_t StopRender() override; 7150a07fd2Sopenharmony_ci int32_t SetMute(const AudioEvent &event) override; 7250a07fd2Sopenharmony_ci int32_t SetAudioParameters(const AudioEvent &event) override; 7350a07fd2Sopenharmony_ci void PlayStatusChange(const std::string &args) override; 7450a07fd2Sopenharmony_ci void SetAttrs(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback) override; 7550a07fd2Sopenharmony_ci int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; 7650a07fd2Sopenharmony_ci 7750a07fd2Sopenharmony_ci void OnEngineTransEvent(const AVTransEvent &event) override; 7850a07fd2Sopenharmony_ci void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 7950a07fd2Sopenharmony_ci void OnEngineTransDataAvailable(const std::shared_ptr<AudioData> &audioData) override; 8050a07fd2Sopenharmony_ci 8150a07fd2Sopenharmony_ci void OnWriteData(size_t length) override; 8250a07fd2Sopenharmony_ciprivate: 8350a07fd2Sopenharmony_ci std::string GetVolumeLevel(); 8450a07fd2Sopenharmony_ci void PlayThreadRunning(); 8550a07fd2Sopenharmony_ci void Pause(); 8650a07fd2Sopenharmony_ci void ReStart(); 8750a07fd2Sopenharmony_ci void FillJitterQueue(); 8850a07fd2Sopenharmony_ci void FlushJitterQueue(); 8950a07fd2Sopenharmony_ci int32_t CreateAudioRenderer(const AudioParam ¶m); 9050a07fd2Sopenharmony_ci 9150a07fd2Sopenharmony_ciprivate: 9250a07fd2Sopenharmony_ci constexpr static size_t DATA_QUEUE_MAX_SIZE = 12; 9350a07fd2Sopenharmony_ci constexpr static size_t REQUEST_DATA_WAIT = 10; 9450a07fd2Sopenharmony_ci constexpr static size_t DATA_QUEUE_SIZE = 8; 9550a07fd2Sopenharmony_ci constexpr static size_t SLEEP_TIME = 5000; 9650a07fd2Sopenharmony_ci static constexpr const char* RENDERTHREAD = "renderThread"; 9750a07fd2Sopenharmony_ci const std::string DUMP_DAUDIO_SPK_AFTER_TRANS_NAME = "dump_sink_spk_recv_from_trans.pcm"; 9850a07fd2Sopenharmony_ci 9950a07fd2Sopenharmony_ci std::string devId_; 10050a07fd2Sopenharmony_ci const int32_t dhId_; 10150a07fd2Sopenharmony_ci std::thread renderDataThread_; 10250a07fd2Sopenharmony_ci AudioParam audioParam_; 10350a07fd2Sopenharmony_ci std::atomic<bool> isRenderReady_ = false; 10450a07fd2Sopenharmony_ci std::mutex dataQueueMtx_; 10550a07fd2Sopenharmony_ci std::mutex devMtx_; 10650a07fd2Sopenharmony_ci std::queue<std::shared_ptr<AudioData>> dataQueue_; 10750a07fd2Sopenharmony_ci std::condition_variable dataQueueCond_; 10850a07fd2Sopenharmony_ci AudioStatus clientStatus_ = AudioStatus::STATUS_IDLE; 10950a07fd2Sopenharmony_ci 11050a07fd2Sopenharmony_ci std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ = nullptr; 11150a07fd2Sopenharmony_ci std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr; 11250a07fd2Sopenharmony_ci std::weak_ptr<IAudioEventCallback> eventCallback_; 11350a07fd2Sopenharmony_ci int64_t lastPlayStartTime_ = 0; 11450a07fd2Sopenharmony_ci int64_t lastReceiveStartTime_ = 0; 11550a07fd2Sopenharmony_ci FILE *dumpFile_ = nullptr; 11650a07fd2Sopenharmony_ci}; 11750a07fd2Sopenharmony_ci} // DistributedHardware 11850a07fd2Sopenharmony_ci} // OHOS 11950a07fd2Sopenharmony_ci#endif // OHOS_DSPEAKER_CLIENT_H 120