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 "src_agent.h"
17e0e9324cSopenharmony_ci#include "common/common_macro.h"
18e0e9324cSopenharmony_ci#include "common/event_channel.h"
19e0e9324cSopenharmony_ci#include "magic_enum.hpp"
20e0e9324cSopenharmony_ci
21e0e9324cSopenharmony_cinamespace OHOS {
22e0e9324cSopenharmony_cinamespace Sharing {
23e0e9324cSopenharmony_ci
24e0e9324cSopenharmony_ciSrcAgent::SrcAgent() : Agent(AgentType::SRC_AGENT)
25e0e9324cSopenharmony_ci{
26e0e9324cSopenharmony_ci    SHARING_LOGD("agentId: %{public}u.", GetId());
27e0e9324cSopenharmony_ci}
28e0e9324cSopenharmony_ci
29e0e9324cSopenharmony_ciSrcAgent::~SrcAgent()
30e0e9324cSopenharmony_ci{
31e0e9324cSopenharmony_ci    SHARING_LOGD("agentId: %{public}u.", GetId());
32e0e9324cSopenharmony_ci}
33e0e9324cSopenharmony_ci
34e0e9324cSopenharmony_civoid SrcAgent::OnSessionNotify(SessionStatusMsg::Ptr &statusMsg)
35e0e9324cSopenharmony_ci{
36e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
37e0e9324cSopenharmony_ci    RETURN_IF_NULL(statusMsg);
38e0e9324cSopenharmony_ci    SHARING_LOGI("notify status: %{public}s, agentId: %{public}u.",
39e0e9324cSopenharmony_ci                 std::string(magic_enum::enum_name((SessionNotifyStatus)statusMsg->status)).c_str(), GetId());
40e0e9324cSopenharmony_ci    statusMsg->prosumerId = prosumerId_;
41e0e9324cSopenharmony_ci    switch (statusMsg->status) {
42e0e9324cSopenharmony_ci        case SessionNotifyStatus::NOTIFY_PROSUMER_CREATE:
43e0e9324cSopenharmony_ci            NotifyProducer(statusMsg, EventType::EVENT_MEDIA_PRODUCER_CREATE);
44e0e9324cSopenharmony_ci            break;
45e0e9324cSopenharmony_ci        case SessionNotifyStatus::STATE_SESSION_STARTED:
46e0e9324cSopenharmony_ci            NotifyProducer(statusMsg, EventType::EVENT_MEDIA_PRODUCER_START);
47e0e9324cSopenharmony_ci            break;
48e0e9324cSopenharmony_ci        case SessionNotifyStatus::STATE_SESSION_STOPED:
49e0e9324cSopenharmony_ci            NotifyProducer(statusMsg, EventType::EVENT_MEDIA_PRODUCER_STOP);
50e0e9324cSopenharmony_ci            break;
51e0e9324cSopenharmony_ci        case SessionNotifyStatus::STATE_SESSION_PAUSED:
52e0e9324cSopenharmony_ci            NotifyProducer(statusMsg, EventType::EVENT_MEDIA_PRODUCER_PAUSE);
53e0e9324cSopenharmony_ci            break;
54e0e9324cSopenharmony_ci        case SessionNotifyStatus::STATE_SESSION_RESUMED:
55e0e9324cSopenharmony_ci            NotifyProducer(statusMsg, EventType::EVENT_MEDIA_PRODUCER_RESUME);
56e0e9324cSopenharmony_ci            break;
57e0e9324cSopenharmony_ci        case SessionNotifyStatus::STATE_SESSION_DESTROYED:
58e0e9324cSopenharmony_ci            NotifyProducer(statusMsg, EventType::EVENT_MEDIA_PRODUCER_DESTROY);
59e0e9324cSopenharmony_ci            break;
60e0e9324cSopenharmony_ci        case SessionNotifyStatus::STATE_SESSION_INTERRUPTED:
61e0e9324cSopenharmony_ci            PopNextStep(runStep_, AGENT_STATUS_DONE);
62e0e9324cSopenharmony_ci            break;
63e0e9324cSopenharmony_ci        default:
64e0e9324cSopenharmony_ci            Agent::OnSessionNotify(statusMsg);
65e0e9324cSopenharmony_ci            break;
66e0e9324cSopenharmony_ci    }
67e0e9324cSopenharmony_ci}
68e0e9324cSopenharmony_ci
69e0e9324cSopenharmony_civoid SrcAgent::NotifyProducer(SessionStatusMsg::Ptr &statusMsg, EventType type)
70e0e9324cSopenharmony_ci{
71e0e9324cSopenharmony_ci    SHARING_LOGD("trace.");
72e0e9324cSopenharmony_ci    RETURN_IF_NULL(statusMsg);
73e0e9324cSopenharmony_ci    RETURN_IF_NULL(statusMsg->msg);
74e0e9324cSopenharmony_ci    SHARING_LOGI("agentId: %{public}u, notify status, type: %{public}s.", GetId(),
75e0e9324cSopenharmony_ci                 std::string(magic_enum::enum_name(type)).c_str());
76e0e9324cSopenharmony_ci
77e0e9324cSopenharmony_ci    auto listener = agentListener_.lock();
78e0e9324cSopenharmony_ci    if (listener) {
79e0e9324cSopenharmony_ci        auto channelMsg = std::make_shared<ChannelEventMsg>();
80e0e9324cSopenharmony_ci        RETURN_IF_NULL(channelMsg);
81e0e9324cSopenharmony_ci        channelMsg->agentId = GetId();
82e0e9324cSopenharmony_ci        channelMsg->toMgr = ModuleType::MODULE_MEDIACHANNEL;
83e0e9324cSopenharmony_ci        channelMsg->dstId = mediaChannelId_;
84e0e9324cSopenharmony_ci        channelMsg->prosumerId = statusMsg->prosumerId;
85e0e9324cSopenharmony_ci        channelMsg->type = type;
86e0e9324cSopenharmony_ci        channelMsg->className = statusMsg->className;
87e0e9324cSopenharmony_ci        channelMsg->errorCode = statusMsg->msg->errorCode;
88e0e9324cSopenharmony_ci        channelMsg->requestId = statusMsg->msg->requestId;
89e0e9324cSopenharmony_ci        statusMsg->msg = std::move(channelMsg);
90e0e9324cSopenharmony_ci
91e0e9324cSopenharmony_ci        auto agentMsg = std::static_pointer_cast<AgentStatusMsg>(statusMsg);
92e0e9324cSopenharmony_ci        agentMsg->agentId = GetId();
93e0e9324cSopenharmony_ci        SHARING_LOGI("id: %{public}u, and notify to send event: %{public}s, mediaChannelId: %{public}u.", GetId(),
94e0e9324cSopenharmony_ci                     std::string(magic_enum::enum_name(type)).c_str(), mediaChannelId_);
95e0e9324cSopenharmony_ci        listener->OnAgentNotify(agentMsg);
96e0e9324cSopenharmony_ci    }
97e0e9324cSopenharmony_ci}
98e0e9324cSopenharmony_ci
99e0e9324cSopenharmony_ci} // namespace Sharing
100e0e9324cSopenharmony_ci} // namespace OHOS