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 AUDIO_DECODE_TRANSPORT_H 17 #define AUDIO_DECODE_TRANSPORT_H 18 19 #include <memory> 20 #include <mutex> 21 #include <queue> 22 #include <string> 23 24 #include "audio_event.h" 25 #include "audio_transport_context.h" 26 #include "iaudio_channel.h" 27 #include "iaudio_datatrans_callback.h" 28 #include "iaudio_data_transport.h" 29 #include "iaudio_processor.h" 30 #include "iaudio_processor_callback.h" 31 32 namespace OHOS { 33 namespace DistributedHardware { 34 class AudioDecodeTransport : public IAudioDataTransport, 35 public IAudioChannelListener, 36 public IAudioProcessorCallback, 37 public std::enable_shared_from_this<AudioDecodeTransport> { 38 public: AudioDecodeTransport(const std::string & peerDevId)39 explicit AudioDecodeTransport(const std::string &peerDevId) : peerDevId_(peerDevId) 40 { 41 audioParam_.comParam.sampleRate = AudioSampleRate::SAMPLE_RATE_8000; 42 audioParam_.comParam.channelMask = AudioChannel::MONO; 43 audioParam_.comParam.bitFormat = AudioSampleFormat::SAMPLE_U8; 44 audioParam_.comParam.codecType = AudioCodecType::AUDIO_CODEC_AAC; 45 } 46 ~AudioDecodeTransport() override = default; 47 int32_t SetUp(const AudioParam &localParam, const AudioParam &remoteParam, 48 const std::shared_ptr<IAudioDataTransCallback> &callback, const PortCapType capType) override; 49 int32_t Start() override; 50 int32_t Stop() override; 51 int32_t Release() override; 52 int32_t Pause() override; 53 int32_t Restart(const AudioParam &localParam, const AudioParam &remoteParam) override; 54 int32_t FeedAudioData(std::shared_ptr<AudioData> &audioData) override; 55 int32_t CreateCtrl() override; 56 int32_t InitEngine(IAVEngineProvider *providerPtr) override; 57 int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; 58 59 void OnSessionOpened() override; 60 void OnSessionClosed() override; 61 void OnDataReceived(const std::shared_ptr<AudioData> &data) override; 62 void OnEventReceived(const AudioEvent &event) override; 63 64 void OnAudioDataDone(const std::shared_ptr<AudioData> &outputData) override; 65 void OnStateNotify(const AudioEvent &event) override; 66 67 private: 68 int32_t InitAudioDecodeTransport(const AudioParam &localParam, const AudioParam &remoteParam, 69 const PortCapType capType); 70 int32_t RegisterChannelListener(const PortCapType capType); 71 int32_t RegisterProcessorListener(const AudioParam &localParam, const AudioParam &remoteParam); 72 73 private: 74 static constexpr size_t DATA_QUEUE_MAX_SIZE = 10; 75 static constexpr size_t SLEEP_TIME = 20000; 76 77 std::weak_ptr<IAudioDataTransCallback> dataTransCallback_; 78 std::shared_ptr<IAudioChannel> audioChannel_ = nullptr; 79 std::shared_ptr<IAudioProcessor> processor_ = nullptr; 80 std::shared_ptr<AudioTransportContext> context_ = nullptr; 81 82 std::mutex dataQueueMtx_; 83 std::queue<std::shared_ptr<AudioData>> dataQueue_; 84 std::string peerDevId_; 85 AudioParam audioParam_; 86 PortCapType capType_ = CAP_UNKNOWN; 87 }; 88 } // namespace DistributedHardware 89 } // namespace OHOS 90 #endif // AUDIO_DECODE_TRANSPORT_H 91