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_DEV_H 1750a07fd2Sopenharmony_ci#define OHOS_DSPEAKER_DEV_H 1850a07fd2Sopenharmony_ci 1950a07fd2Sopenharmony_ci#include <condition_variable> 2050a07fd2Sopenharmony_ci#include <set> 2150a07fd2Sopenharmony_ci#include <thread> 2250a07fd2Sopenharmony_ci#include "cJSON.h" 2350a07fd2Sopenharmony_ci 2450a07fd2Sopenharmony_ci#include "audio_param.h" 2550a07fd2Sopenharmony_ci#include "ashmem.h" 2650a07fd2Sopenharmony_ci#include "av_sender_engine_transport.h" 2750a07fd2Sopenharmony_ci#include "daudio_constants.h" 2850a07fd2Sopenharmony_ci#include "daudio_hdi_handler.h" 2950a07fd2Sopenharmony_ci#include "daudio_io_dev.h" 3050a07fd2Sopenharmony_ci#include "iaudio_event_callback.h" 3150a07fd2Sopenharmony_ci#include "iaudio_data_transport.h" 3250a07fd2Sopenharmony_ci#include "iaudio_datatrans_callback.h" 3350a07fd2Sopenharmony_ci#include "idaudio_hdi_callback.h" 3450a07fd2Sopenharmony_ci 3550a07fd2Sopenharmony_cinamespace OHOS { 3650a07fd2Sopenharmony_cinamespace DistributedHardware { 3750a07fd2Sopenharmony_ciclass DSpeakerDev : public DAudioIoDev, 3850a07fd2Sopenharmony_ci public IAudioDataTransCallback, 3950a07fd2Sopenharmony_ci public AVSenderTransportCallback, 4050a07fd2Sopenharmony_ci public std::enable_shared_from_this<DSpeakerDev> { 4150a07fd2Sopenharmony_cipublic: 4250a07fd2Sopenharmony_ci DSpeakerDev(const std::string &devId, std::shared_ptr<IAudioEventCallback> callback) 4350a07fd2Sopenharmony_ci : DAudioIoDev(devId), audioEventCallback_(callback) {}; 4450a07fd2Sopenharmony_ci ~DSpeakerDev() override = default; 4550a07fd2Sopenharmony_ci 4650a07fd2Sopenharmony_ci void OnEngineTransEvent(const AVTransEvent &event) override; 4750a07fd2Sopenharmony_ci void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 4850a07fd2Sopenharmony_ci 4950a07fd2Sopenharmony_ci int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; 5050a07fd2Sopenharmony_ci int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override; 5150a07fd2Sopenharmony_ci 5250a07fd2Sopenharmony_ci int32_t EnableDevice(const int32_t dhId, const std::string &capability) override; 5350a07fd2Sopenharmony_ci int32_t DisableDevice(const int32_t dhId) override; 5450a07fd2Sopenharmony_ci int32_t CreateStream(const int32_t streamId) override; 5550a07fd2Sopenharmony_ci int32_t DestroyStream(const int32_t streamId) override; 5650a07fd2Sopenharmony_ci int32_t SetParameters(const int32_t streamId, const AudioParamHDF ¶m) override; 5750a07fd2Sopenharmony_ci int32_t WriteStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override; 5850a07fd2Sopenharmony_ci int32_t ReadStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override; 5950a07fd2Sopenharmony_ci int32_t NotifyEvent(const int32_t streamId, const AudioEvent &event) override; 6050a07fd2Sopenharmony_ci int32_t ReadMmapPosition(const int32_t streamId, uint64_t &frames, CurrentTimeHDF &time) override; 6150a07fd2Sopenharmony_ci int32_t RefreshAshmemInfo(const int32_t streamId, 6250a07fd2Sopenharmony_ci int32_t fd, int32_t ashmemLength, int32_t lengthPerTrans) override; 6350a07fd2Sopenharmony_ci 6450a07fd2Sopenharmony_ci int32_t MmapStart() override; 6550a07fd2Sopenharmony_ci int32_t MmapStop() override; 6650a07fd2Sopenharmony_ci 6750a07fd2Sopenharmony_ci int32_t OnStateChange(const AudioEventType type) override; 6850a07fd2Sopenharmony_ci int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override; 6950a07fd2Sopenharmony_ci 7050a07fd2Sopenharmony_ci int32_t SetUp() override; 7150a07fd2Sopenharmony_ci int32_t Start() override; 7250a07fd2Sopenharmony_ci int32_t Stop() override; 7350a07fd2Sopenharmony_ci int32_t Release() override; 7450a07fd2Sopenharmony_ci bool IsOpened() override; 7550a07fd2Sopenharmony_ci int32_t Pause() override; 7650a07fd2Sopenharmony_ci int32_t Restart() override; 7750a07fd2Sopenharmony_ci int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; 7850a07fd2Sopenharmony_ci 7950a07fd2Sopenharmony_ci AudioParam GetAudioParam() const override; 8050a07fd2Sopenharmony_ci int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) override; 8150a07fd2Sopenharmony_ci 8250a07fd2Sopenharmony_ciprivate: 8350a07fd2Sopenharmony_ci void EnqueueThread(); 8450a07fd2Sopenharmony_ci 8550a07fd2Sopenharmony_ciprivate: 8650a07fd2Sopenharmony_ci static constexpr const char* ENQUEUE_THREAD = "spkEnqueueTh"; 8750a07fd2Sopenharmony_ci const std::string SPK_DEV_FILENAME = "dump_source_spk_write_to_trans.pcm"; 8850a07fd2Sopenharmony_ci const std::string SPK_LOWLATENCY_FILENAME = "dump_source_spk_fast_read_from_ashmem.pcm"; 8950a07fd2Sopenharmony_ci const int32_t ASHMEM_MAX_LEN = 2 * 4096; 9050a07fd2Sopenharmony_ci 9150a07fd2Sopenharmony_ci std::weak_ptr<IAudioEventCallback> audioEventCallback_; 9250a07fd2Sopenharmony_ci std::mutex channelWaitMutex_; 9350a07fd2Sopenharmony_ci std::condition_variable channelWaitCond_; 9450a07fd2Sopenharmony_ci std::atomic<bool> isTransReady_ = false; 9550a07fd2Sopenharmony_ci std::atomic<bool> isOpened_ = false; 9650a07fd2Sopenharmony_ci int32_t curPort_ = 0; 9750a07fd2Sopenharmony_ci int32_t streamId_ = 0; 9850a07fd2Sopenharmony_ci std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr; 9950a07fd2Sopenharmony_ci 10050a07fd2Sopenharmony_ci // Speaker render parameters 10150a07fd2Sopenharmony_ci AudioParamHDF paramHDF_; 10250a07fd2Sopenharmony_ci AudioParam param_; 10350a07fd2Sopenharmony_ci 10450a07fd2Sopenharmony_ci sptr<Ashmem> ashmem_ = nullptr; 10550a07fd2Sopenharmony_ci std::atomic<bool> isEnqueueRunning_ = false; 10650a07fd2Sopenharmony_ci int32_t ashmemLength_ = -1; 10750a07fd2Sopenharmony_ci int32_t lengthPerTrans_ = -1; 10850a07fd2Sopenharmony_ci int32_t readIndex_ = -1; 10950a07fd2Sopenharmony_ci int64_t frameIndex_ = 0; 11050a07fd2Sopenharmony_ci int64_t startTime_ = 0; 11150a07fd2Sopenharmony_ci uint64_t readNum_ = 0; 11250a07fd2Sopenharmony_ci int64_t readTvSec_ = 0; 11350a07fd2Sopenharmony_ci int64_t readTvNSec_ = 0; 11450a07fd2Sopenharmony_ci std::thread enqueueDataThread_; 11550a07fd2Sopenharmony_ci int64_t lastwriteStartTime_ = 0; 11650a07fd2Sopenharmony_ci int32_t dhId_ = -1; 11750a07fd2Sopenharmony_ci FILE *dumpFileCommn_ = nullptr; 11850a07fd2Sopenharmony_ci FILE *dumpFileFast_ = nullptr; 11950a07fd2Sopenharmony_ci}; 12050a07fd2Sopenharmony_ci} // DistributedHardware 12150a07fd2Sopenharmony_ci} // OHOS 12250a07fd2Sopenharmony_ci#endif // OHOS_DAUDIO_DSPEAKER_DEV_H