1 /* 2 * Copyright (c) 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 OHOS_AV_TRANSPORT_SENDER_ADAPTER_H 17 #define OHOS_AV_TRANSPORT_SENDER_ADAPTER_H 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <memory> 22 #include <queue> 23 #include <string> 24 #include <thread> 25 26 #include "i_av_engine_provider.h" 27 #include "video_param.h" 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 struct VideoData { 32 uint8_t *data; 33 size_t size; 34 uint32_t width; 35 uint32_t height; 36 int64_t timestamp; 37 std::string format; 38 }; 39 class AVSenderAdapterCallback { 40 public: AVSenderAdapterCallback()41 AVSenderAdapterCallback() {}; 42 virtual ~AVSenderAdapterCallback() = default; 43 virtual void OnEngineEvent(DScreenEventType event, const std::string &content) = 0; 44 virtual void OnEngineMessage(const std::shared_ptr<AVTransMessage> &message) = 0; 45 }; 46 47 class AVTransSenderAdapter : public IAVSenderEngineCallback, 48 public std::enable_shared_from_this<AVTransSenderAdapter> { 49 public: AVTransSenderAdapter()50 AVTransSenderAdapter() {}; ~AVTransSenderAdapter()51 ~AVTransSenderAdapter() override {}; 52 53 int32_t Initialize(IAVEngineProvider *providerPtr, const std::string &peerDevId); 54 int32_t Release(); 55 int32_t Start(); 56 int32_t Stop(); 57 int32_t SetParameter(const AVTransTag &tag, const std::string ¶m); 58 int32_t PushData(const VideoData &video); 59 int32_t SendMessageToRemote(const std::shared_ptr<AVTransMessage> &message); 60 int32_t CreateControlChannel(const std::string &peerDevId); 61 int32_t RegisterAdapterCallback(const std::shared_ptr<AVSenderAdapterCallback> &callback); 62 63 // interfaces from IAVSenderEngineCallback 64 int32_t OnSenderEvent(const AVTransEvent& event) override; 65 int32_t OnMessageReceived(const std::shared_ptr<AVTransMessage> &message) override; 66 67 private: 68 int32_t WaitForChannelCreated(); 69 int32_t WaitForAVTransStarted(); 70 71 private: 72 std::atomic<bool> initialized_ {false}; 73 std::mutex chnCreatedMtx_; 74 std::mutex transStartedMtx_; 75 std::condition_variable chnCreatedCondVar_; 76 std::condition_variable transStartedCondVar_; 77 std::atomic<bool> chnCreateSuccess_ {false}; 78 std::atomic<bool> transStartSuccess_ {false}; 79 80 std::shared_ptr<IAVSenderEngine> senderEngine_; 81 std::shared_ptr<AVSenderAdapterCallback> adapterCallback_; 82 }; 83 } // namespace DistributedHardware 84 } // namespace OHOS 85 #endif