1e0e9324cSopenharmony_ci/*
2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License.
5e0e9324cSopenharmony_ci * You may obtain a copy of the License at
6e0e9324cSopenharmony_ci *
7e0e9324cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0e9324cSopenharmony_ci *
9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and
13e0e9324cSopenharmony_ci * limitations under the License.
14e0e9324cSopenharmony_ci */
15e0e9324cSopenharmony_ci
16e0e9324cSopenharmony_ci#include "media_channel.h"
17e0e9324cSopenharmony_ci#include <chrono>
18e0e9324cSopenharmony_ci#include "common/common_macro.h"
19e0e9324cSopenharmony_ci#include "common/const_def.h"
20e0e9324cSopenharmony_ci#include "common/event_comm.h"
21e0e9324cSopenharmony_ci#include "common/media_log.h"
22e0e9324cSopenharmony_ci#include "common/reflect_registration.h"
23e0e9324cSopenharmony_ci#include "configuration/include/config.h"
24e0e9324cSopenharmony_ci#include "magic_enum.hpp"
25e0e9324cSopenharmony_ci#include "mediachannel/channel_manager.h"
26e0e9324cSopenharmony_ci
27e0e9324cSopenharmony_ciusing namespace std::chrono_literals;
28e0e9324cSopenharmony_ci
29e0e9324cSopenharmony_cinamespace OHOS {
30e0e9324cSopenharmony_cinamespace Sharing {
31e0e9324cSopenharmony_ci
32e0e9324cSopenharmony_ciMediaChannel::MediaChannel()
33e0e9324cSopenharmony_ci{
34e0e9324cSopenharmony_ci    SHARING_LOGD("mediachannelId: %{public}u.", GetId());
35e0e9324cSopenharmony_ci    SharingValue::Ptr values = nullptr;
36e0e9324cSopenharmony_ci    int32_t maxBufferCapacity = MAX_BUFFER_CAPACITY;
37e0e9324cSopenharmony_ci    auto ret = Config::GetInstance().GetConfig("mediachannel", "bufferDispatcher", "maxBufferCapacity", values);
38e0e9324cSopenharmony_ci    if (ret == CONFIGURE_ERROR_NONE) {
39e0e9324cSopenharmony_ci        values->GetValue<int32_t>(maxBufferCapacity);
40e0e9324cSopenharmony_ci    }
41e0e9324cSopenharmony_ci
42e0e9324cSopenharmony_ci    int32_t bufferCapacityIncrement = BUFFER_CAPACITY_INCREMENT;
43e0e9324cSopenharmony_ci    ret = Config::GetInstance().GetConfig("mediachannel", "bufferDispatcher", "bufferCapacityIncrement", values);
44e0e9324cSopenharmony_ci    if (ret == CONFIGURE_ERROR_NONE) {
45e0e9324cSopenharmony_ci        values->GetValue<int32_t>(bufferCapacityIncrement);
46e0e9324cSopenharmony_ci    }
47e0e9324cSopenharmony_ci
48e0e9324cSopenharmony_ci    dispatcher_ = std::make_shared<BufferDispatcher>(maxBufferCapacity, bufferCapacityIncrement);
49e0e9324cSopenharmony_ci    playController_ = std::make_shared<MediaController>(GetId());
50e0e9324cSopenharmony_ci}
51e0e9324cSopenharmony_ci
52e0e9324cSopenharmony_ciMediaChannel::~MediaChannel()
53e0e9324cSopenharmony_ci{
54e0e9324cSopenharmony_ci    SHARING_LOGD("mediachannelId: %{public}u.", GetId());
55e0e9324cSopenharmony_ci    if (consumer_) {
56e0e9324cSopenharmony_ci        consumer_.reset();
57e0e9324cSopenharmony_ci    }
58e0e9324cSopenharmony_ci
59e0e9324cSopenharmony_ci    if (dispatcher_ != nullptr) {
60e0e9324cSopenharmony_ci        dispatcher_->StopDispatch();
61e0e9324cSopenharmony_ci        dispatcher_->FlushBuffer();
62e0e9324cSopenharmony_ci        dispatcher_->ReleaseAllReceiver();
63e0e9324cSopenharmony_ci        dispatcher_.reset();
64e0e9324cSopenharmony_ci    }
65e0e9324cSopenharmony_ci
66e0e9324cSopenharmony_ci    SHARING_LOGD("leave, mediachannelId: %{public}u.", GetId());
67e0e9324cSopenharmony_ci}
68e0e9324cSopenharmony_ci
69e0e9324cSopenharmony_civoid MediaChannel::OnWriteTimeout()
70e0e9324cSopenharmony_ci{
71e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
72e0e9324cSopenharmony_ci    auto msg = std::make_shared<EventMsg>();
73e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
74e0e9324cSopenharmony_ci    statusMsg->eventMsg = msg;
75e0e9324cSopenharmony_ci    statusMsg->errorCode = ERR_INTAKE_TIMEOUT;
76e0e9324cSopenharmony_ci    statusMsg->agentId = GetSinkAgentId();
77e0e9324cSopenharmony_ci    statusMsg->prosumerId = consumer_ ? consumer_->GetId() : INVALID_ID;
78e0e9324cSopenharmony_ci
79e0e9324cSopenharmony_ci    SendAgentEvent(statusMsg, EVENT_AGENT_STATE_WRITE_WARNING);
80e0e9324cSopenharmony_ci}
81e0e9324cSopenharmony_ci
82e0e9324cSopenharmony_ciuint32_t MediaChannel::GetSinkAgentId()
83e0e9324cSopenharmony_ci{
84e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
85e0e9324cSopenharmony_ci    if (consumer_)
86e0e9324cSopenharmony_ci        return consumer_->GetSinkAgentId();
87e0e9324cSopenharmony_ci    else
88e0e9324cSopenharmony_ci        return INVALID_ID;
89e0e9324cSopenharmony_ci}
90e0e9324cSopenharmony_ci
91e0e9324cSopenharmony_civoid MediaChannel::OnProducerNotify(ProsumerStatusMsg::Ptr &msg)
92e0e9324cSopenharmony_ci{
93e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
94e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg);
95e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg->eventMsg);
96e0e9324cSopenharmony_ci    SHARING_LOGI("mediachannelId: %{public}u, producerId: %{public}u, status: %{public}s.", GetId(), msg->prosumerId,
97e0e9324cSopenharmony_ci                 std::string(magic_enum::enum_name(static_cast<ProsumerNotifyStatus>(msg->status))).c_str());
98e0e9324cSopenharmony_ci    switch (msg->status) {
99e0e9324cSopenharmony_ci        case ProsumerNotifyStatus::PROSUMER_NOTIFY_INIT_SUCCESS:
100e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_CREATE);
101e0e9324cSopenharmony_ci            break;
102e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_START_SUCCESS:
103e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_START);
104e0e9324cSopenharmony_ci            break;
105e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_STOP_SUCCESS:
106e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_STOP);
107e0e9324cSopenharmony_ci            break;
108e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_PAUSE_SUCCESS:
109e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_PAUSE);
110e0e9324cSopenharmony_ci            break;
111e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_RESUME_SUCCESS:
112e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_RESUME);
113e0e9324cSopenharmony_ci            break;
114e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_DESTROY_SUCCESS: {
115e0e9324cSopenharmony_ci            SHARING_LOGD("destroy producer, mediachannelId: %{public}u.", GetId());
116e0e9324cSopenharmony_ci            std::unique_lock<std::mutex> lock(mutex_);
117e0e9324cSopenharmony_ci            auto iter = producers_.find(msg->prosumerId);
118e0e9324cSopenharmony_ci            if (iter != producers_.end()) {
119e0e9324cSopenharmony_ci                producers_.erase(iter);
120e0e9324cSopenharmony_ci            }
121e0e9324cSopenharmony_ci            SHARING_LOGD("erase producer, mediachannelId: %{public}u.", GetId());
122e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_DESTROY);
123e0e9324cSopenharmony_ci            break;
124e0e9324cSopenharmony_ci        }
125e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_ERROR:
126e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_PROSUMER_ERROR);
127e0e9324cSopenharmony_ci            break;
128e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_PRIVATE_EVENT: {
129e0e9324cSopenharmony_ci            SharingEvent agentEvent;
130e0e9324cSopenharmony_ci            agentEvent.eventMsg = msg->eventMsg;
131e0e9324cSopenharmony_ci            SendEvent(agentEvent);
132e0e9324cSopenharmony_ci            return;
133e0e9324cSopenharmony_ci        }
134e0e9324cSopenharmony_ci        default:
135e0e9324cSopenharmony_ci            break;
136e0e9324cSopenharmony_ci    }
137e0e9324cSopenharmony_ci}
138e0e9324cSopenharmony_ci
139e0e9324cSopenharmony_civoid MediaChannel::OnConsumerNotify(ProsumerStatusMsg::Ptr &msg)
140e0e9324cSopenharmony_ci{
141e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
142e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg);
143e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg->eventMsg);
144e0e9324cSopenharmony_ci    SHARING_LOGI("mediachannelId: %{public}u, consumerId: %{public}u, status: %{public}s.", GetId(), msg->prosumerId,
145e0e9324cSopenharmony_ci                 std::string(magic_enum::enum_name(static_cast<ProsumerNotifyStatus>(msg->status))).c_str());
146e0e9324cSopenharmony_ci
147e0e9324cSopenharmony_ci    switch (msg->status) {
148e0e9324cSopenharmony_ci        case ProsumerNotifyStatus::PROSUMER_NOTIFY_INIT_SUCCESS:
149e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_CREATE);
150e0e9324cSopenharmony_ci            break;
151e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_START_SUCCESS: {
152e0e9324cSopenharmony_ci            msg->errorCode = InitPlayController();
153e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_START);
154e0e9324cSopenharmony_ci            break;
155e0e9324cSopenharmony_ci        }
156e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_STOP_SUCCESS:
157e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_STOP);
158e0e9324cSopenharmony_ci            break;
159e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_PAUSE_SUCCESS:
160e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_PAUSE);
161e0e9324cSopenharmony_ci            break;
162e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_RESUME_SUCCESS:
163e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_RESUME);
164e0e9324cSopenharmony_ci            break;
165e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_DESTROY_SUCCESS:
166e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_STATE_PROSUMER_DESTROY);
167e0e9324cSopenharmony_ci            break;
168e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_ERROR:
169e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_PROSUMER_ERROR);
170e0e9324cSopenharmony_ci            break;
171e0e9324cSopenharmony_ci        case PROSUMER_NOTIFY_PRIVATE_EVENT: {
172e0e9324cSopenharmony_ci            SharingEvent agentEvent;
173e0e9324cSopenharmony_ci            agentEvent.eventMsg = msg->eventMsg;
174e0e9324cSopenharmony_ci            SendEvent(agentEvent);
175e0e9324cSopenharmony_ci            return;
176e0e9324cSopenharmony_ci        }
177e0e9324cSopenharmony_ci        default:
178e0e9324cSopenharmony_ci            break;
179e0e9324cSopenharmony_ci    }
180e0e9324cSopenharmony_ci}
181e0e9324cSopenharmony_ci
182e0e9324cSopenharmony_civoid MediaChannel::OnMediaControllerNotify(ProsumerStatusMsg::Ptr &msg)
183e0e9324cSopenharmony_ci{
184e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
185e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg);
186e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg->eventMsg);
187e0e9324cSopenharmony_ci    SHARING_LOGI("mediachannelId: %{public}u, prosumerId: %{public}u, status: %{public}s.", GetId(), msg->prosumerId,
188e0e9324cSopenharmony_ci                 std::string(magic_enum::enum_name(static_cast<PlayConntrollerNotifyStatus>(msg->status))).c_str());
189e0e9324cSopenharmony_ci
190e0e9324cSopenharmony_ci    switch (msg->status) {
191e0e9324cSopenharmony_ci        case CONNTROLLER_NOTIFY_ACCELERATION:
192e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGEINT_ACCELERATION_DONE);
193e0e9324cSopenharmony_ci            break;
194e0e9324cSopenharmony_ci        case CONNTROLLER_NOTIFY_DECODER_DIED:
195e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_DECODER_DIED);
196e0e9324cSopenharmony_ci            break;
197e0e9324cSopenharmony_ci        case CONNTROLLER_NOTIFY_KEYMOD_START:
198e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_KEYMODE_START);
199e0e9324cSopenharmony_ci            break;
200e0e9324cSopenharmony_ci        case CONNTROLLER_NOTIFY_KEYMOD_STOP:
201e0e9324cSopenharmony_ci            SendAgentEvent(msg, EVENT_AGENT_KEYMODE_STOP);
202e0e9324cSopenharmony_ci            break;
203e0e9324cSopenharmony_ci        default:
204e0e9324cSopenharmony_ci            break;
205e0e9324cSopenharmony_ci    }
206e0e9324cSopenharmony_ci}
207e0e9324cSopenharmony_ci
208e0e9324cSopenharmony_ciuint32_t MediaChannel::CreateProducer(std::string &className)
209e0e9324cSopenharmony_ci{
210e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
211e0e9324cSopenharmony_ci    BaseProducer::Ptr producer = ClassReflector<BaseProducer>::Class2Instance(className);
212e0e9324cSopenharmony_ci    if (producer) {
213e0e9324cSopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
214e0e9324cSopenharmony_ci        producer->SetProducerListener(shared_from_this());
215e0e9324cSopenharmony_ci        producers_[producer->GetId()] = producer;
216e0e9324cSopenharmony_ci        SHARING_LOGD("create producer success, mediachannelId: %{public}u.", GetId());
217e0e9324cSopenharmony_ci        return producer->GetId();
218e0e9324cSopenharmony_ci    }
219e0e9324cSopenharmony_ci
220e0e9324cSopenharmony_ci    return INVALID_ID;
221e0e9324cSopenharmony_ci}
222e0e9324cSopenharmony_ci
223e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::CreateConsumer(std::string &className)
224e0e9324cSopenharmony_ci{
225e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
226e0e9324cSopenharmony_ci    if (consumer_) {
227e0e9324cSopenharmony_ci        consumer_.reset();
228e0e9324cSopenharmony_ci        SHARING_LOGW("create consumer. The consumer is reseted.");
229e0e9324cSopenharmony_ci    }
230e0e9324cSopenharmony_ci    consumer_ = ClassReflector<BaseConsumer>::Class2Instance(className);
231e0e9324cSopenharmony_ci    if (consumer_) {
232e0e9324cSopenharmony_ci        consumer_->SetConsumerListener(shared_from_this());
233e0e9324cSopenharmony_ci        if (dispatcher_) {
234e0e9324cSopenharmony_ci            dispatcher_->SetBufferDispatcherListener(shared_from_this());
235e0e9324cSopenharmony_ci        }
236e0e9324cSopenharmony_ci        SHARING_LOGD("create consumer success name: %{public}s, mediachannelId: %{public}u.", className.c_str(),
237e0e9324cSopenharmony_ci                     GetId());
238e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
239e0e9324cSopenharmony_ci    }
240e0e9324cSopenharmony_ci
241e0e9324cSopenharmony_ci    SHARING_LOGD("create consumer error name: %{public}s.", className.c_str());
242e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_GENERAL_ERROR;
243e0e9324cSopenharmony_ci}
244e0e9324cSopenharmony_ci
245e0e9324cSopenharmony_ciint32_t MediaChannel::HandleEvent(SharingEvent &event)
246e0e9324cSopenharmony_ci{
247e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
248e0e9324cSopenharmony_ci    RETURN_INVALID_IF_NULL(event.eventMsg);
249e0e9324cSopenharmony_ci    SHARING_LOGI(
250e0e9324cSopenharmony_ci        "HandleEvent mediachannelId: %{public}u, fromMgr: %{public}u, srcId: %{public}u, handle event: %{public}s.",
251e0e9324cSopenharmony_ci        event.eventMsg->dstId, event.eventMsg->fromMgr, event.eventMsg->srcId,
252e0e9324cSopenharmony_ci        std::string(magic_enum::enum_name(event.eventMsg->type)).c_str());
253e0e9324cSopenharmony_ci
254e0e9324cSopenharmony_ci    switch (event.eventMsg->type) {
255e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CONSUMER_CREATE:
256e0e9324cSopenharmony_ci            HandleCreateConsumer(event);
257e0e9324cSopenharmony_ci            break;
258e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CONSUMER_DESTROY:
259e0e9324cSopenharmony_ci            HandleDestroyConsumer(event);
260e0e9324cSopenharmony_ci            break;
261e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CONSUMER_START:
262e0e9324cSopenharmony_ci            HandleStartConsumer(event);
263e0e9324cSopenharmony_ci            break;
264e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CONSUMER_STOP:
265e0e9324cSopenharmony_ci            HandleStopConsumer(event);
266e0e9324cSopenharmony_ci            break;
267e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CONSUMER_PAUSE:
268e0e9324cSopenharmony_ci            HandlePauseConsumer(event);
269e0e9324cSopenharmony_ci            break;
270e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CONSUMER_RESUME:
271e0e9324cSopenharmony_ci            HandleResumeConsumer(event);
272e0e9324cSopenharmony_ci            break;
273e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PRODUCER_CREATE:
274e0e9324cSopenharmony_ci            HandleCreateProducer(event);
275e0e9324cSopenharmony_ci            break;
276e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PRODUCER_DESTROY:
277e0e9324cSopenharmony_ci            HandleDestroyProducer(event);
278e0e9324cSopenharmony_ci            break;
279e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PRODUCER_START:
280e0e9324cSopenharmony_ci            HandleStartProducer(event);
281e0e9324cSopenharmony_ci            break;
282e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PRODUCER_STOP:
283e0e9324cSopenharmony_ci            HandleStopProducer(event);
284e0e9324cSopenharmony_ci            break;
285e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PRODUCER_PAUSE:
286e0e9324cSopenharmony_ci            HandlePauseProducer(event);
287e0e9324cSopenharmony_ci            break;
288e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PRODUCER_RESUME:
289e0e9324cSopenharmony_ci            HandleResumeProducer(event);
290e0e9324cSopenharmony_ci            break;
291e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CHANNEL_APPENDSURFACE:
292e0e9324cSopenharmony_ci            HandleAppendSurface(event);
293e0e9324cSopenharmony_ci            break;
294e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CHANNEL_REMOVESURFACE:
295e0e9324cSopenharmony_ci            HandleRemoveSurface(event);
296e0e9324cSopenharmony_ci            break;
297e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CHANNEL_SETSCENETYPE:
298e0e9324cSopenharmony_ci            HandleSetSceneType(event);
299e0e9324cSopenharmony_ci            break;
300e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CHANNEL_SETVOLUME:
301e0e9324cSopenharmony_ci            HandleSetVolume(event);
302e0e9324cSopenharmony_ci            break;
303e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PLAY_START:
304e0e9324cSopenharmony_ci            HandleStartPlay(event);
305e0e9324cSopenharmony_ci            break;
306e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_PLAY_STOP:
307e0e9324cSopenharmony_ci            HandleStopPlay(event);
308e0e9324cSopenharmony_ci            break;
309e0e9324cSopenharmony_ci        case EventType::EVENT_MEDIA_CHANNEL_KEY_REDIRECT:
310e0e9324cSopenharmony_ci            HandleSetKeyRedirect(event);
311e0e9324cSopenharmony_ci            break;
312e0e9324cSopenharmony_ci        default: {
313e0e9324cSopenharmony_ci            auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
314e0e9324cSopenharmony_ci            if (channelMsg) {
315e0e9324cSopenharmony_ci                if (consumer_ && (consumer_->GetId() == channelMsg->prosumerId)) {
316e0e9324cSopenharmony_ci                    SHARING_LOGD("send event to consumer.");
317e0e9324cSopenharmony_ci                    consumer_->HandleEvent(event);
318e0e9324cSopenharmony_ci                } else if (producers_.find(channelMsg->prosumerId) != producers_.end()) {
319e0e9324cSopenharmony_ci                    SHARING_LOGD("send event to producer.");
320e0e9324cSopenharmony_ci                    producers_[channelMsg->prosumerId]->HandleEvent(event);
321e0e9324cSopenharmony_ci                    return SharingErrorCode::ERR_OK;
322e0e9324cSopenharmony_ci                } else {
323e0e9324cSopenharmony_ci                    SHARING_LOGW("unknow prosumerId: %{public}u.", channelMsg->prosumerId);
324e0e9324cSopenharmony_ci                }
325e0e9324cSopenharmony_ci            } else {
326e0e9324cSopenharmony_ci                SHARING_LOGW("unknown event msg.");
327e0e9324cSopenharmony_ci            }
328e0e9324cSopenharmony_ci
329e0e9324cSopenharmony_ci            break;
330e0e9324cSopenharmony_ci        }
331e0e9324cSopenharmony_ci    }
332e0e9324cSopenharmony_ci
333e0e9324cSopenharmony_ci    return 0;
334e0e9324cSopenharmony_ci}
335e0e9324cSopenharmony_ci
336e0e9324cSopenharmony_ciint32_t MediaChannel::Release()
337e0e9324cSopenharmony_ci{
338e0e9324cSopenharmony_ci    SHARING_LOGD("trace, mediachannelId: %{public}u.", GetId());
339e0e9324cSopenharmony_ci    if (nullptr != playController_) {
340e0e9324cSopenharmony_ci        SHARING_LOGD("release playController, mediachannelId: %{public}u.", GetId());
341e0e9324cSopenharmony_ci        playController_->Release();
342e0e9324cSopenharmony_ci        playController_.reset();
343e0e9324cSopenharmony_ci    } else {
344e0e9324cSopenharmony_ci        SHARING_LOGD("playerController has been released, mediachannelId: %{public}u.", GetId());
345e0e9324cSopenharmony_ci    }
346e0e9324cSopenharmony_ci    if (nullptr != consumer_) {
347e0e9324cSopenharmony_ci        SHARING_LOGD("release Consumer, mediachannelId: %{public}u.", GetId());
348e0e9324cSopenharmony_ci        consumer_.reset();
349e0e9324cSopenharmony_ci    }
350e0e9324cSopenharmony_ci
351e0e9324cSopenharmony_ci    std::unique_lock<std::mutex> lock(mutex_);
352e0e9324cSopenharmony_ci    producers_.clear();
353e0e9324cSopenharmony_ci    SHARING_LOGD("end mediachannelId: %{public}u.", GetId());
354e0e9324cSopenharmony_ci    return 0;
355e0e9324cSopenharmony_ci}
356e0e9324cSopenharmony_ci
357e0e9324cSopenharmony_civoid MediaChannel::SetMediaChannelListener(std::weak_ptr<IMediaChannelListener> listener)
358e0e9324cSopenharmony_ci{
359e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
360e0e9324cSopenharmony_ci    listener_ = listener;
361e0e9324cSopenharmony_ci}
362e0e9324cSopenharmony_ci
363e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleAppendSurface(SharingEvent &event)
364e0e9324cSopenharmony_ci{
365e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
366e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelAppendSurfaceEventMsg>(event);
367e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
368e0e9324cSopenharmony_ci        SHARING_LOGD("unknow msg.");
369e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
370e0e9324cSopenharmony_ci    }
371e0e9324cSopenharmony_ci
372e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
373e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
374e0e9324cSopenharmony_ci    if ((nullptr != channelMsg->surface) && playController_) {
375e0e9324cSopenharmony_ci        if (playController_->AppendSurface(channelMsg->surface, channelMsg->sceneType)) {
376e0e9324cSopenharmony_ci            statusMsg->errorCode = ERR_OK;
377e0e9324cSopenharmony_ci            statusMsg->prosumerId = consumer_->GetId();
378e0e9324cSopenharmony_ci            statusMsg->agentId = consumer_->GetSinkAgentId();
379e0e9324cSopenharmony_ci            SendAgentEvent(statusMsg, EVENT_AGENT_STATE_CHANNEL_APPENDSURFACE);
380e0e9324cSopenharmony_ci            return ERR_OK;
381e0e9324cSopenharmony_ci        } else {
382e0e9324cSopenharmony_ci            SHARING_LOGW("AppendSurface failed.");
383e0e9324cSopenharmony_ci            statusMsg->errorCode = ERR_INVALID_SURFACE_ID;
384e0e9324cSopenharmony_ci        }
385e0e9324cSopenharmony_ci    } else {
386e0e9324cSopenharmony_ci        SHARING_LOGW("playerController or surface is nullptr.");
387e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PLAY_START;
388e0e9324cSopenharmony_ci    }
389e0e9324cSopenharmony_ci
390e0e9324cSopenharmony_ci    statusMsg->prosumerId = channelMsg->prosumerId;
391e0e9324cSopenharmony_ci    statusMsg->agentId = channelMsg->agentId;
392e0e9324cSopenharmony_ci    SendAgentEvent(statusMsg, EVENT_AGENT_STATE_CHANNEL_APPENDSURFACE);
393e0e9324cSopenharmony_ci    return ERR_GENERAL_ERROR;
394e0e9324cSopenharmony_ci}
395e0e9324cSopenharmony_ci
396e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleRemoveSurface(SharingEvent &event)
397e0e9324cSopenharmony_ci{
398e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
399e0e9324cSopenharmony_ci    if (consumer_ == nullptr) {
400e0e9324cSopenharmony_ci        SHARING_LOGE("consumer is not inited.");
401e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
402e0e9324cSopenharmony_ci    }
403e0e9324cSopenharmony_ci
404e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelRemoveSurfaceEventMsg>(event);
405e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
406e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
407e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
408e0e9324cSopenharmony_ci    }
409e0e9324cSopenharmony_ci
410e0e9324cSopenharmony_ci    if (playController_) {
411e0e9324cSopenharmony_ci        playController_->RemoveSurface(channelMsg->surfaceId);
412e0e9324cSopenharmony_ci    } else {
413e0e9324cSopenharmony_ci        SHARING_LOGW("remove surface error, mediachannelId: %{public}u.", GetId());
414e0e9324cSopenharmony_ci    }
415e0e9324cSopenharmony_ci
416e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
417e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
418e0e9324cSopenharmony_ci    statusMsg->errorCode = ERR_OK;
419e0e9324cSopenharmony_ci    statusMsg->prosumerId = consumer_->GetId();
420e0e9324cSopenharmony_ci    statusMsg->agentId = consumer_->GetSinkAgentId();
421e0e9324cSopenharmony_ci    statusMsg->surfaceId = channelMsg->surfaceId;
422e0e9324cSopenharmony_ci    SendAgentEvent(statusMsg, EVENT_AGENT_STATE_CHANNEL_REMOVESURFACE);
423e0e9324cSopenharmony_ci
424e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_OK;
425e0e9324cSopenharmony_ci}
426e0e9324cSopenharmony_ci
427e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleSetSceneType(SharingEvent &event)
428e0e9324cSopenharmony_ci{
429e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
430e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelSetSceneTypeEventMsg>(event);
431e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
432e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
433e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
434e0e9324cSopenharmony_ci    }
435e0e9324cSopenharmony_ci
436e0e9324cSopenharmony_ci    if (playController_) {
437e0e9324cSopenharmony_ci        if (channelMsg->sceneType == OHOS::Sharing::SceneType::FOREGROUND) {
438e0e9324cSopenharmony_ci            playController_->SetKeyMode(channelMsg->surfaceId, false);
439e0e9324cSopenharmony_ci        } else if (channelMsg->sceneType == OHOS::Sharing::SceneType::BACKGROUND) {
440e0e9324cSopenharmony_ci            playController_->SetKeyMode(channelMsg->surfaceId, true);
441e0e9324cSopenharmony_ci        }
442e0e9324cSopenharmony_ci    }
443e0e9324cSopenharmony_ci
444e0e9324cSopenharmony_ci    SHARING_LOGW("playerController is nullptr, mediaChannelId: %{public}u.", GetId());
445e0e9324cSopenharmony_ci    return ERR_OK;
446e0e9324cSopenharmony_ci}
447e0e9324cSopenharmony_ci
448e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleSetKeyRedirect(SharingEvent &event)
449e0e9324cSopenharmony_ci{
450e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
451e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelSetKeyRedirectEventMsg>(event);
452e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
453e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
454e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
455e0e9324cSopenharmony_ci    }
456e0e9324cSopenharmony_ci
457e0e9324cSopenharmony_ci    if (playController_) {
458e0e9324cSopenharmony_ci        playController_->SetKeyRedirect(channelMsg->surfaceId, channelMsg->keyRedirect);
459e0e9324cSopenharmony_ci    } else {
460e0e9324cSopenharmony_ci        SHARING_LOGW("playerController is nullptr, mediaChannelId: %{public}u.", GetId());
461e0e9324cSopenharmony_ci    }
462e0e9324cSopenharmony_ci
463e0e9324cSopenharmony_ci    return ERR_OK;
464e0e9324cSopenharmony_ci}
465e0e9324cSopenharmony_ci
466e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleSetVolume(SharingEvent &event)
467e0e9324cSopenharmony_ci{
468e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
469e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelSetVolumeEventMsg>(event);
470e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
471e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
472e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
473e0e9324cSopenharmony_ci    }
474e0e9324cSopenharmony_ci
475e0e9324cSopenharmony_ci    float volume = channelMsg->volume;
476e0e9324cSopenharmony_ci    if (playController_) {
477e0e9324cSopenharmony_ci        playController_->SetVolume(volume);
478e0e9324cSopenharmony_ci    } else {
479e0e9324cSopenharmony_ci        SHARING_LOGW("playerController is nullptr, mediaChannelId: %{public}u.", GetId());
480e0e9324cSopenharmony_ci    }
481e0e9324cSopenharmony_ci
482e0e9324cSopenharmony_ci    return ERR_OK;
483e0e9324cSopenharmony_ci}
484e0e9324cSopenharmony_ci
485e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleStartPlay(SharingEvent &event)
486e0e9324cSopenharmony_ci{
487e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
488e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
489e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
490e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
491e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
492e0e9324cSopenharmony_ci    }
493e0e9324cSopenharmony_ci
494e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
495e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
496e0e9324cSopenharmony_ci    statusMsg->prosumerId = consumer_ ? consumer_->GetId() : channelMsg->prosumerId;
497e0e9324cSopenharmony_ci    statusMsg->agentId = channelMsg->agentId;
498e0e9324cSopenharmony_ci
499e0e9324cSopenharmony_ci    if (playController_) {
500e0e9324cSopenharmony_ci        playController_->Start();
501e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_OK;
502e0e9324cSopenharmony_ci    } else {
503e0e9324cSopenharmony_ci        SHARING_LOGW("playerController is nullptr, mediaChannelId: %{public}u.", GetId());
504e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PLAY_START;
505e0e9324cSopenharmony_ci    }
506e0e9324cSopenharmony_ci
507e0e9324cSopenharmony_ci    SendAgentEvent(statusMsg, EVENT_AGENT_STATE_PLAY_START);
508e0e9324cSopenharmony_ci    return ERR_OK;
509e0e9324cSopenharmony_ci}
510e0e9324cSopenharmony_ci
511e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleStopPlay(SharingEvent &event)
512e0e9324cSopenharmony_ci{
513e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
514e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
515e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
516e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
517e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
518e0e9324cSopenharmony_ci    }
519e0e9324cSopenharmony_ci
520e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
521e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
522e0e9324cSopenharmony_ci    statusMsg->prosumerId = consumer_ ? consumer_->GetId() : channelMsg->prosumerId;
523e0e9324cSopenharmony_ci    statusMsg->agentId = channelMsg->agentId;
524e0e9324cSopenharmony_ci
525e0e9324cSopenharmony_ci    if (playController_) {
526e0e9324cSopenharmony_ci        playController_->Stop();
527e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_OK;
528e0e9324cSopenharmony_ci    } else {
529e0e9324cSopenharmony_ci        SHARING_LOGW("playerController is nullptr, mediaChannelId: %{public}u.", GetId());
530e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PLAY_STOP;
531e0e9324cSopenharmony_ci    }
532e0e9324cSopenharmony_ci
533e0e9324cSopenharmony_ci    SendAgentEvent(statusMsg, EVENT_AGENT_STATE_PLAY_STOP);
534e0e9324cSopenharmony_ci    return ERR_OK;
535e0e9324cSopenharmony_ci}
536e0e9324cSopenharmony_ci
537e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleCreateConsumer(SharingEvent &event)
538e0e9324cSopenharmony_ci{
539e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
540e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
541e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
542e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
543e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
544e0e9324cSopenharmony_ci    }
545e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
546e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
547e0e9324cSopenharmony_ci
548e0e9324cSopenharmony_ci    if (CreateConsumer(channelMsg->className) == ERR_OK) {
549e0e9324cSopenharmony_ci        if (consumer_) {
550e0e9324cSopenharmony_ci            SHARING_LOGD("create consumer success, consumerId: %{public}u, mediachannelId: %{public}u.",
551e0e9324cSopenharmony_ci                         consumer_->GetId(), GetId());
552e0e9324cSopenharmony_ci            statusMsg->status = PROSUMER_INIT;
553e0e9324cSopenharmony_ci            consumer_->SetSinkAgentId(channelMsg->agentId);
554e0e9324cSopenharmony_ci            consumer_->UpdateOperation(statusMsg);
555e0e9324cSopenharmony_ci        }
556e0e9324cSopenharmony_ci        return ERR_OK;
557e0e9324cSopenharmony_ci    } else {
558e0e9324cSopenharmony_ci        SHARING_LOGW("create consumer failed.");
559e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PROSUMER_CREATE;
560e0e9324cSopenharmony_ci        statusMsg->prosumerId = INVALID_ID;
561e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
562e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_PROSUMER_ERROR);
563e0e9324cSopenharmony_ci        return ERR_GENERAL_ERROR;
564e0e9324cSopenharmony_ci    }
565e0e9324cSopenharmony_ci}
566e0e9324cSopenharmony_ci
567e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleDestroyConsumer(SharingEvent &event)
568e0e9324cSopenharmony_ci{
569e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
570e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
571e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
572e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
573e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
574e0e9324cSopenharmony_ci    }
575e0e9324cSopenharmony_ci
576e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
577e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
578e0e9324cSopenharmony_ci
579e0e9324cSopenharmony_ci    if (consumer_ == nullptr) {
580e0e9324cSopenharmony_ci        SHARING_LOGW("consumer is null, mediachannelId: %{public}u.", GetId());
581e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_OK;
582e0e9324cSopenharmony_ci        statusMsg->prosumerId = INVALID_ID;
583e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
584e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_STATE_PROSUMER_DESTROY);
585e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
586e0e9324cSopenharmony_ci    }
587e0e9324cSopenharmony_ci
588e0e9324cSopenharmony_ci    if (consumer_->IsRunning()) {
589e0e9324cSopenharmony_ci        SHARING_LOGD("consumer is running, mediachannelId: %{public}u.", GetId());
590e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PROSUMER_DESTROY;
591e0e9324cSopenharmony_ci        statusMsg->prosumerId = consumer_ ? consumer_->GetId() : INVALID_ID;
592e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
593e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_PROSUMER_ERROR);
594e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
595e0e9324cSopenharmony_ci    } else {
596e0e9324cSopenharmony_ci        statusMsg->status = PROSUMER_DESTROY;
597e0e9324cSopenharmony_ci        consumer_->UpdateOperation(statusMsg);
598e0e9324cSopenharmony_ci        SHARING_LOGD("ERR_OK.");
599e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
600e0e9324cSopenharmony_ci    }
601e0e9324cSopenharmony_ci}
602e0e9324cSopenharmony_ci
603e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleStartConsumer(SharingEvent &event)
604e0e9324cSopenharmony_ci{
605e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
606e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
607e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
608e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
609e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
610e0e9324cSopenharmony_ci    }
611e0e9324cSopenharmony_ci
612e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
613e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
614e0e9324cSopenharmony_ci
615e0e9324cSopenharmony_ci    if (consumer_) {
616e0e9324cSopenharmony_ci        statusMsg->status = PROSUMER_START;
617e0e9324cSopenharmony_ci        consumer_->UpdateOperation(statusMsg);
618e0e9324cSopenharmony_ci        SHARING_LOGD("update consumer start success, mediaChannelId: %{public}u.", GetId());
619e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
620e0e9324cSopenharmony_ci    } else {
621e0e9324cSopenharmony_ci        SHARING_LOGE("consumer is null, mediachannelId: %{public}u.", GetId());
622e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PROSUMER_START;
623e0e9324cSopenharmony_ci        statusMsg->prosumerId = INVALID_ID;
624e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
625e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_PROSUMER_ERROR);
626e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
627e0e9324cSopenharmony_ci    }
628e0e9324cSopenharmony_ci}
629e0e9324cSopenharmony_ci
630e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleStopConsumer(SharingEvent &event)
631e0e9324cSopenharmony_ci{
632e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
633e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
634e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
635e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
636e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
637e0e9324cSopenharmony_ci    }
638e0e9324cSopenharmony_ci
639e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
640e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
641e0e9324cSopenharmony_ci
642e0e9324cSopenharmony_ci    if (nullptr != playController_) {
643e0e9324cSopenharmony_ci        playController_->Stop();
644e0e9324cSopenharmony_ci        SHARING_LOGD("play controller stop success, mediaChannelId: %{public}u.", GetId());
645e0e9324cSopenharmony_ci    } else {
646e0e9324cSopenharmony_ci        SHARING_LOGW("playerController is nullptr, mediaChannelId: %{public}u.", GetId());
647e0e9324cSopenharmony_ci    }
648e0e9324cSopenharmony_ci
649e0e9324cSopenharmony_ci    if (consumer_) {
650e0e9324cSopenharmony_ci        statusMsg->status = PROSUMER_STOP;
651e0e9324cSopenharmony_ci        consumer_->UpdateOperation(statusMsg);
652e0e9324cSopenharmony_ci        SHARING_LOGD("update consumer stop success, mediaChannelId: %{public}u.", GetId());
653e0e9324cSopenharmony_ci    } else {
654e0e9324cSopenharmony_ci        SHARING_LOGW("consumer null mediaChannelId: %{public}u.", GetId());
655e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_OK;
656e0e9324cSopenharmony_ci        statusMsg->prosumerId = INVALID_ID;
657e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
658e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_STATE_PROSUMER_STOP);
659e0e9324cSopenharmony_ci    }
660e0e9324cSopenharmony_ci
661e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_OK;
662e0e9324cSopenharmony_ci}
663e0e9324cSopenharmony_ci
664e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandlePauseConsumer(SharingEvent &event)
665e0e9324cSopenharmony_ci{
666e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
667e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
668e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
669e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
670e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
671e0e9324cSopenharmony_ci    }
672e0e9324cSopenharmony_ci
673e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
674e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
675e0e9324cSopenharmony_ci    statusMsg->mediaType = channelMsg->mediaType;
676e0e9324cSopenharmony_ci
677e0e9324cSopenharmony_ci    if (consumer_) {
678e0e9324cSopenharmony_ci        statusMsg->status = PROSUMER_PAUSE;
679e0e9324cSopenharmony_ci        consumer_->UpdateOperation(statusMsg);
680e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
681e0e9324cSopenharmony_ci    }
682e0e9324cSopenharmony_ci
683e0e9324cSopenharmony_ci    SHARING_LOGD("pasue consumer error mediachannelId: %{public}u.", GetId());
684e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_GENERAL_ERROR;
685e0e9324cSopenharmony_ci}
686e0e9324cSopenharmony_ci
687e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleResumeConsumer(SharingEvent &event)
688e0e9324cSopenharmony_ci{
689e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
690e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
691e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
692e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
693e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
694e0e9324cSopenharmony_ci    }
695e0e9324cSopenharmony_ci
696e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
697e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
698e0e9324cSopenharmony_ci    statusMsg->mediaType = channelMsg->mediaType;
699e0e9324cSopenharmony_ci
700e0e9324cSopenharmony_ci    if (consumer_) {
701e0e9324cSopenharmony_ci        statusMsg->status = PROSUMER_RESUME;
702e0e9324cSopenharmony_ci        consumer_->UpdateOperation(statusMsg);
703e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
704e0e9324cSopenharmony_ci    }
705e0e9324cSopenharmony_ci
706e0e9324cSopenharmony_ci    SHARING_LOGD("resume consumer error mediachannelId: %{public}u.", GetId());
707e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_GENERAL_ERROR;
708e0e9324cSopenharmony_ci}
709e0e9324cSopenharmony_ci
710e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleCreateProducer(SharingEvent &event)
711e0e9324cSopenharmony_ci{
712e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
713e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
714e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
715e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
716e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
717e0e9324cSopenharmony_ci    }
718e0e9324cSopenharmony_ci
719e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
720e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
721e0e9324cSopenharmony_ci
722e0e9324cSopenharmony_ci    uint32_t retId = CreateProducer(channelMsg->className);
723e0e9324cSopenharmony_ci    if (retId != INVALID_ID) {
724e0e9324cSopenharmony_ci        SHARING_LOGD("create prosumer success.");
725e0e9324cSopenharmony_ci        producers_[retId]->SetSrcAgentId(channelMsg->agentId);
726e0e9324cSopenharmony_ci        statusMsg->status = PROSUMER_INIT;
727e0e9324cSopenharmony_ci        producers_[retId]->UpdateOperation(statusMsg);
728e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
729e0e9324cSopenharmony_ci    } else {
730e0e9324cSopenharmony_ci        SHARING_LOGW("create prosumer failed.");
731e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PROSUMER_CREATE;
732e0e9324cSopenharmony_ci        statusMsg->prosumerId = INVALID_ID;
733e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
734e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_PROSUMER_ERROR);
735e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
736e0e9324cSopenharmony_ci    }
737e0e9324cSopenharmony_ci}
738e0e9324cSopenharmony_ci
739e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleDestroyProducer(SharingEvent &event)
740e0e9324cSopenharmony_ci{
741e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
742e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
743e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
744e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
745e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
746e0e9324cSopenharmony_ci    }
747e0e9324cSopenharmony_ci
748e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
749e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
750e0e9324cSopenharmony_ci
751e0e9324cSopenharmony_ci    uint32_t id = channelMsg->prosumerId;
752e0e9324cSopenharmony_ci    BaseProducer::Ptr pProducer = nullptr;
753e0e9324cSopenharmony_ci    {
754e0e9324cSopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
755e0e9324cSopenharmony_ci        auto iter = producers_.find(id);
756e0e9324cSopenharmony_ci        if (iter != producers_.end()) {
757e0e9324cSopenharmony_ci            pProducer = iter->second;
758e0e9324cSopenharmony_ci        } else {
759e0e9324cSopenharmony_ci            SHARING_LOGE("cann't find producerId: %{public}d, channelId: %{public}d.", id, GetId());
760e0e9324cSopenharmony_ci        }
761e0e9324cSopenharmony_ci    }
762e0e9324cSopenharmony_ci
763e0e9324cSopenharmony_ci    if (nullptr == pProducer) {
764e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_OK;
765e0e9324cSopenharmony_ci        statusMsg->prosumerId = id;
766e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
767e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_STATE_PROSUMER_DESTROY);
768e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_OK;
769e0e9324cSopenharmony_ci    }
770e0e9324cSopenharmony_ci
771e0e9324cSopenharmony_ci    if (pProducer->IsRunning()) {
772e0e9324cSopenharmony_ci        SHARING_LOGD("producer is running, producerId: %{public}d, mediachannelId: %{public}u.", id, GetId());
773e0e9324cSopenharmony_ci        statusMsg->errorCode = ERR_PROSUMER_DESTROY;
774e0e9324cSopenharmony_ci        statusMsg->prosumerId = id;
775e0e9324cSopenharmony_ci        statusMsg->agentId = channelMsg->agentId;
776e0e9324cSopenharmony_ci        SendAgentEvent(statusMsg, EVENT_AGENT_PROSUMER_ERROR);
777e0e9324cSopenharmony_ci    } else {
778e0e9324cSopenharmony_ci        statusMsg->status = PROSUMER_DESTROY;
779e0e9324cSopenharmony_ci        pProducer->UpdateOperation(statusMsg);
780e0e9324cSopenharmony_ci    }
781e0e9324cSopenharmony_ci
782e0e9324cSopenharmony_ci    SHARING_LOGD("producer destroy exit, mediachannelId: %{public}u.", GetId());
783e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_OK;
784e0e9324cSopenharmony_ci}
785e0e9324cSopenharmony_ci
786e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleStartProducer(SharingEvent &event)
787e0e9324cSopenharmony_ci{
788e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
789e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
790e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
791e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
792e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
793e0e9324cSopenharmony_ci    }
794e0e9324cSopenharmony_ci
795e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
796e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
797e0e9324cSopenharmony_ci
798e0e9324cSopenharmony_ci    {
799e0e9324cSopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
800e0e9324cSopenharmony_ci        auto iter = producers_.find(channelMsg->prosumerId);
801e0e9324cSopenharmony_ci        if (iter != producers_.end() && (iter->second)) {
802e0e9324cSopenharmony_ci            auto producer = iter->second;
803e0e9324cSopenharmony_ci            dispatcher_->AttachReceiver(producer);
804e0e9324cSopenharmony_ci            statusMsg->status = PROSUMER_START;
805e0e9324cSopenharmony_ci            producer->UpdateOperation(statusMsg);
806e0e9324cSopenharmony_ci            producer->StartDispatchThread();
807e0e9324cSopenharmony_ci            SHARING_LOGD("media start producer success, mediachannelId: %{public}u.", GetId());
808e0e9324cSopenharmony_ci            return SharingErrorCode::ERR_OK;
809e0e9324cSopenharmony_ci        } else {
810e0e9324cSopenharmony_ci            SHARING_LOGW("cann't find producerId: %{public}d, mediachannelId: %{public}d, agentId: %{public}d.",
811e0e9324cSopenharmony_ci                         channelMsg->prosumerId, GetId(), channelMsg->agentId);
812e0e9324cSopenharmony_ci            statusMsg->errorCode = ERR_PROSUMER_START;
813e0e9324cSopenharmony_ci            statusMsg->prosumerId = channelMsg->prosumerId;
814e0e9324cSopenharmony_ci            statusMsg->agentId = channelMsg->agentId;
815e0e9324cSopenharmony_ci            SendAgentEvent(statusMsg, EVENT_AGENT_PROSUMER_ERROR);
816e0e9324cSopenharmony_ci        }
817e0e9324cSopenharmony_ci    }
818e0e9324cSopenharmony_ci
819e0e9324cSopenharmony_ci    SHARING_LOGD("media start producer end, mediachannelId: %{public}u.", GetId());
820e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_GENERAL_ERROR;
821e0e9324cSopenharmony_ci}
822e0e9324cSopenharmony_ci
823e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleStopProducer(SharingEvent &event)
824e0e9324cSopenharmony_ci{
825e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
826e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
827e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
828e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
829e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
830e0e9324cSopenharmony_ci    }
831e0e9324cSopenharmony_ci
832e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
833e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
834e0e9324cSopenharmony_ci    {
835e0e9324cSopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
836e0e9324cSopenharmony_ci        auto iter = producers_.find(channelMsg->prosumerId);
837e0e9324cSopenharmony_ci        if (iter != producers_.end() && (iter->second)) {
838e0e9324cSopenharmony_ci            dispatcher_->DetachReceiver(iter->second);
839e0e9324cSopenharmony_ci            statusMsg->status = PROSUMER_STOP;
840e0e9324cSopenharmony_ci            iter->second->UpdateOperation(statusMsg);
841e0e9324cSopenharmony_ci            SHARING_LOGD("media stop producer success, mediachannelId: %{public}u.", GetId());
842e0e9324cSopenharmony_ci            return SharingErrorCode::ERR_OK;
843e0e9324cSopenharmony_ci        } else {
844e0e9324cSopenharmony_ci            SHARING_LOGW("cann't find producerId: %{public}d, mediachannelId: %{public}d, agentId: %{public}d.",
845e0e9324cSopenharmony_ci                         channelMsg->prosumerId, GetId(), channelMsg->agentId);
846e0e9324cSopenharmony_ci            statusMsg->errorCode = ERR_OK;
847e0e9324cSopenharmony_ci            statusMsg->prosumerId = channelMsg->prosumerId;
848e0e9324cSopenharmony_ci            statusMsg->agentId = channelMsg->agentId;
849e0e9324cSopenharmony_ci            SendAgentEvent(statusMsg, EVENT_AGENT_STATE_PROSUMER_STOP);
850e0e9324cSopenharmony_ci        }
851e0e9324cSopenharmony_ci    }
852e0e9324cSopenharmony_ci
853e0e9324cSopenharmony_ci    SHARING_LOGD("media stop producer end, mediachannelId: %{public}u.", GetId());
854e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_GENERAL_ERROR;
855e0e9324cSopenharmony_ci}
856e0e9324cSopenharmony_ci
857e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandlePauseProducer(SharingEvent &event)
858e0e9324cSopenharmony_ci{
859e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
860e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
861e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
862e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
863e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
864e0e9324cSopenharmony_ci    }
865e0e9324cSopenharmony_ci
866e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
867e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
868e0e9324cSopenharmony_ci    statusMsg->mediaType = channelMsg->mediaType;
869e0e9324cSopenharmony_ci
870e0e9324cSopenharmony_ci    {
871e0e9324cSopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
872e0e9324cSopenharmony_ci        auto iter = producers_.find(channelMsg->prosumerId);
873e0e9324cSopenharmony_ci        if (iter != producers_.end() && (iter->second)) {
874e0e9324cSopenharmony_ci            statusMsg->status = PROSUMER_PAUSE;
875e0e9324cSopenharmony_ci            iter->second->UpdateOperation(statusMsg);
876e0e9324cSopenharmony_ci            return SharingErrorCode::ERR_OK;
877e0e9324cSopenharmony_ci        } else {
878e0e9324cSopenharmony_ci            SHARING_LOGW("cann't find producerId: %{public}d, mediachannelId: %{public}d, agentId: %{public}d.",
879e0e9324cSopenharmony_ci                         channelMsg->prosumerId, GetId(), channelMsg->agentId);
880e0e9324cSopenharmony_ci        }
881e0e9324cSopenharmony_ci    }
882e0e9324cSopenharmony_ci
883e0e9324cSopenharmony_ci    SHARING_LOGD("media pause producer end, mediachannelId: %{public}u.", GetId());
884e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_GENERAL_ERROR;
885e0e9324cSopenharmony_ci}
886e0e9324cSopenharmony_ci
887e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::HandleResumeProducer(SharingEvent &event)
888e0e9324cSopenharmony_ci{
889e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
890e0e9324cSopenharmony_ci    auto channelMsg = ConvertEventMsg<ChannelEventMsg>(event);
891e0e9324cSopenharmony_ci    if (channelMsg == nullptr) {
892e0e9324cSopenharmony_ci        SHARING_LOGE("unknow msg.");
893e0e9324cSopenharmony_ci        return SharingErrorCode::ERR_GENERAL_ERROR;
894e0e9324cSopenharmony_ci    }
895e0e9324cSopenharmony_ci
896e0e9324cSopenharmony_ci    auto statusMsg = std::make_shared<ProsumerStatusMsg>();
897e0e9324cSopenharmony_ci    statusMsg->eventMsg = channelMsg;
898e0e9324cSopenharmony_ci    statusMsg->mediaType = channelMsg->mediaType;
899e0e9324cSopenharmony_ci
900e0e9324cSopenharmony_ci    {
901e0e9324cSopenharmony_ci        std::unique_lock<std::mutex> lock(mutex_);
902e0e9324cSopenharmony_ci        auto iter = producers_.find(channelMsg->prosumerId);
903e0e9324cSopenharmony_ci        if (iter != producers_.end() && (iter->second)) {
904e0e9324cSopenharmony_ci            statusMsg->status = PROSUMER_RESUME;
905e0e9324cSopenharmony_ci            iter->second->UpdateOperation(statusMsg);
906e0e9324cSopenharmony_ci            return SharingErrorCode::ERR_OK;
907e0e9324cSopenharmony_ci        } else {
908e0e9324cSopenharmony_ci            SHARING_LOGW("cann't find producerId: %{public}d, mediachannelId: %{public}d, agentId: %{public}d.",
909e0e9324cSopenharmony_ci                         channelMsg->prosumerId, GetId(), channelMsg->agentId);
910e0e9324cSopenharmony_ci        }
911e0e9324cSopenharmony_ci    }
912e0e9324cSopenharmony_ci
913e0e9324cSopenharmony_ci    SHARING_LOGD("media resume producer end, mediachannelId: %{public}u.", GetId());
914e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_GENERAL_ERROR;
915e0e9324cSopenharmony_ci}
916e0e9324cSopenharmony_ci
917e0e9324cSopenharmony_civoid MediaChannel::SendAgentEvent(ProsumerStatusMsg::Ptr &msg, EventType eventType)
918e0e9324cSopenharmony_ci{
919e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
920e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg);
921e0e9324cSopenharmony_ci    RETURN_IF_NULL(msg->eventMsg);
922e0e9324cSopenharmony_ci    SHARING_LOGD("send event: %{public}s ,mediachannelId: %{public}u, contextId: %{public}u, agentId: %{public}u.",
923e0e9324cSopenharmony_ci                 std::string(magic_enum::enum_name(eventType)).c_str(), GetId(), srcContextId_, msg->agentId);
924e0e9324cSopenharmony_ci
925e0e9324cSopenharmony_ci    auto agentMsg = std::make_shared<AgentEventMsg>();
926e0e9324cSopenharmony_ci    agentMsg->toMgr = ModuleType::MODULE_CONTEXT;
927e0e9324cSopenharmony_ci    agentMsg->fromMgr = ModuleType::MODULE_MEDIACHANNEL;
928e0e9324cSopenharmony_ci    agentMsg->srcId = GetId();
929e0e9324cSopenharmony_ci    agentMsg->dstId = srcContextId_;
930e0e9324cSopenharmony_ci    agentMsg->type = eventType;
931e0e9324cSopenharmony_ci    agentMsg->requestId = msg->eventMsg->requestId;
932e0e9324cSopenharmony_ci    agentMsg->agentId = msg->agentId;
933e0e9324cSopenharmony_ci    agentMsg->prosumerId = msg->prosumerId;
934e0e9324cSopenharmony_ci    agentMsg->errorCode = msg->errorCode;
935e0e9324cSopenharmony_ci    agentMsg->surfaceId = msg->surfaceId;
936e0e9324cSopenharmony_ci
937e0e9324cSopenharmony_ci    SharingEvent agentEvent;
938e0e9324cSopenharmony_ci    agentEvent.eventMsg = std::move(agentMsg);
939e0e9324cSopenharmony_ci    SendEvent(agentEvent);
940e0e9324cSopenharmony_ci}
941e0e9324cSopenharmony_ci
942e0e9324cSopenharmony_ciSharingErrorCode MediaChannel::InitPlayController()
943e0e9324cSopenharmony_ci{
944e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
945e0e9324cSopenharmony_ci    if (playController_ != nullptr) {
946e0e9324cSopenharmony_ci        playController_->SetMediaChannel(shared_from_this());
947e0e9324cSopenharmony_ci        AudioTrack audioTrack;
948e0e9324cSopenharmony_ci        if (!(consumer_->IsCapture())) {
949e0e9324cSopenharmony_ci            audioTrack = consumer_->GetAudioTrack();
950e0e9324cSopenharmony_ci        }
951e0e9324cSopenharmony_ci        VideoTrack videoTrack = consumer_->GetVideoTrack();
952e0e9324cSopenharmony_ci        if (!playController_->Init(audioTrack, videoTrack)) {
953e0e9324cSopenharmony_ci            SHARING_LOGE("player controller Init error.");
954e0e9324cSopenharmony_ci            playController_ = nullptr;
955e0e9324cSopenharmony_ci            return SharingErrorCode::ERR_DECODE_FORMAT;
956e0e9324cSopenharmony_ci        }
957e0e9324cSopenharmony_ci    }
958e0e9324cSopenharmony_ci
959e0e9324cSopenharmony_ci    SHARING_LOGD("create playcontroller end, mediachannelId: %{public}u.", GetId());
960e0e9324cSopenharmony_ci    return SharingErrorCode::ERR_OK;
961e0e9324cSopenharmony_ci}
962e0e9324cSopenharmony_ci
963e0e9324cSopenharmony_ci} // namespace Sharing
964e0e9324cSopenharmony_ci} // namespace OHOS