1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_MEDIA_CHANNEL_H
17 #define OHOS_SHARING_MEDIA_CHANNEL_H
18 
19 #include <memory>
20 #include <unordered_map>
21 #include "base_channel.h"
22 #include "base_consumer.h"
23 #include "common/event_comm.h"
24 #include "media_controller.h"
25 
26 namespace OHOS {
27 namespace Sharing {
28 
29 enum MediaChannelNotifyStatus {
30     NOTIFY_VIDEO_STOP,
31 };
32 
33 class IMediaChannelListener {
34 public:
35     virtual ~IMediaChannelListener() = default;
36 
37     virtual void OnMediaChannelNotify(MediaChannelNotifyStatus status) = 0;
38 };
39 
40 class MediaChannel : public IProducerListener,
41                      public IConsumerListener,
42                      public BufferDispatcherListener,
43                      public BaseChannel,
44                      public std::enable_shared_from_this<MediaChannel> {
45 public:
46     using Ptr = std::shared_ptr<MediaChannel>;
47 
48     MediaChannel();
49     ~MediaChannel() override;
50 
51     void OnWriteTimeout() override;
SetContextId(uint32_t contextId)52     void SetContextId(uint32_t contextId)
53     {
54         SHARING_LOGD("trace.");
55         srcContextId_ = contextId;
56     }
57 
58     BufferDispatcher::Ptr GetDispatcher() override
59     {
60         MEDIA_LOGD("trace.");
61         return dispatcher_;
62     }
63 
64 public:
65     uint32_t GetSinkAgentId();
66     uint32_t CreateProducer(std::string &className) override;
67 
68     int32_t Release();
69     int32_t HandleEvent(SharingEvent &event) override;
70 
71     void OnMediaControllerNotify(ProsumerStatusMsg::Ptr &statusMsg);
72     void OnProducerNotify(ProsumerStatusMsg::Ptr &statusMsg) override;
73     void OnConsumerNotify(ProsumerStatusMsg::Ptr &statusMsg) override;
74     void SendAgentEvent(ProsumerStatusMsg::Ptr &msg, EventType eventType);
75     void SetMediaChannelListener(std::weak_ptr<IMediaChannelListener> listener);
76 
77     SharingErrorCode CreateConsumer(std::string &className) override;
78 
79 private:
80     SharingErrorCode HandleStopConsumer(SharingEvent &event);
81     SharingErrorCode HandleStartConsumer(SharingEvent &event);
82     SharingErrorCode HandlePauseConsumer(SharingEvent &event);
83     SharingErrorCode HandleCreateConsumer(SharingEvent &event);
84     SharingErrorCode HandleResumeConsumer(SharingEvent &event);
85     SharingErrorCode HandleDestroyConsumer(SharingEvent &event);
86 
87     SharingErrorCode HandleStopProducer(SharingEvent &event);
88     SharingErrorCode HandlePauseProducer(SharingEvent &event);
89     SharingErrorCode HandleStartProducer(SharingEvent &event);
90     SharingErrorCode HandleCreateProducer(SharingEvent &event);
91     SharingErrorCode HandleResumeProducer(SharingEvent &event);
92     SharingErrorCode HandleDestroyProducer(SharingEvent &event);
93 
94     SharingErrorCode InitPlayController();
95     SharingErrorCode HandleStopPlay(SharingEvent &event);
96     SharingErrorCode HandleSetVolume(SharingEvent &event);
97     SharingErrorCode HandleStartPlay(SharingEvent &event);
98     SharingErrorCode HandleSetSceneType(SharingEvent &event);
99     SharingErrorCode HandleAppendSurface(SharingEvent &event);
100     SharingErrorCode HandleRemoveSurface(SharingEvent &event);
101     SharingErrorCode HandleSetKeyRedirect(SharingEvent &event);
102 
103 private:
104     uint32_t srcContextId_ = INVALID_ID;
105 
106     std::mutex mutex_;
107     std::weak_ptr<IMediaChannelListener> listener_;
108 
109     BufferDispatcher::Ptr dispatcher_ = nullptr;
110     MediaController::Ptr playController_ = nullptr;
111 };
112 
113 } // namespace Sharing
114 } // namespace OHOS
115 #endif