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