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