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