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_TRANS_CONTROL_CENTER_H
17 #define OHOS_AV_TRANS_CONTROL_CENTER_H
18 
19 #include "av_sync_manager.h"
20 #include "av_trans_constants.h"
21 #include "av_trans_message.h"
22 #include "iav_trans_control_center_callback.h"
23 #include "single_instance.h"
24 #include "softbus_channel_adapter.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 class AVTransControlCenter : public ISoftbusChannelListener {
29     DECLARE_SINGLE_INSTANCE_BASE(AVTransControlCenter);
30 public:
31     AVTransControlCenter();
32     virtual ~AVTransControlCenter();
33 
34     int32_t InitializeAVCenter(const TransRole &transRole, int32_t &engineId);
35     int32_t ReleaseAVCenter(int32_t engineId);
36     int32_t CreateControlChannel(int32_t engineId, const std::string &peerDevId);
37     int32_t NotifyAVCenter(int32_t engineId, const AVTransEvent &event);
38     int32_t RegisterCtlCenterCallback(int32_t engineId, const sptr<IAVTransControlCenterCallback> &callback);
39 
40     // interfaces from ISoftbusChannelListener
41     void OnChannelEvent(const AVTransEvent &event) override;
42     void OnStreamReceived(const StreamData *data, const StreamData *ext) override;
43 
44 public:
45     int32_t SendMessage(const std::shared_ptr<AVTransMessage> &message);
46     void SetParam2Engines(AVTransTag tag, const std::string &value);
47     void SetParam2Engines(const AVTransSharedMemory &memory);
48 
49 private:
50     void HandleChannelEvent(const AVTransEvent &event);
51     void HandleDataReceived(const std::string &content, const std::string &peerDevId);
52     bool IsInvalidEngineId(int32_t engineId);
53 
54 private:
55     TransRole transRole_;
56     std::string sessionName_;
57     std::atomic<int32_t> rootEngineId_;
58     std::atomic<bool> initialized_ {false};
59     std::shared_ptr<AVSyncManager> syncManager_ = nullptr;
60 
61     std::mutex devIdMutex_;
62     std::mutex engineIdMutex_;
63     std::mutex callbackMutex_;
64 
65     std::vector<std::string> connectedDevIds_;
66     std::map<int32_t, std::string> engine2DevIdMap_;
67     std::map<int32_t, sptr<IAVTransControlCenterCallback>> callbackMap_;
68 };
69 }
70 }
71 #endif