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