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 "agent/agent.h" 17e0e9324cSopenharmony_ci#include "agent.h" 18e0e9324cSopenharmony_ci#include "common/common_macro.h" 19e0e9324cSopenharmony_ci#include "common/event_channel.h" 20e0e9324cSopenharmony_ci#include "common/reflect_registration.h" 21e0e9324cSopenharmony_ci#include "common/sharing_log.h" 22e0e9324cSopenharmony_ci#include "magic_enum.hpp" 23e0e9324cSopenharmony_ci 24e0e9324cSopenharmony_cinamespace OHOS { 25e0e9324cSopenharmony_cinamespace Sharing { 26e0e9324cSopenharmony_ci 27e0e9324cSopenharmony_ciAgent::Agent(AgentType agentType) : agentType_(agentType) 28e0e9324cSopenharmony_ci{ 29e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 30e0e9324cSopenharmony_ci} 31e0e9324cSopenharmony_ci 32e0e9324cSopenharmony_ciSharingErrorCode Agent::CreateSession(const std::string &className) 33e0e9324cSopenharmony_ci{ 34e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 35e0e9324cSopenharmony_ci session_ = std::static_pointer_cast<BaseSession>(ReflectRegistration::GetInstance().CreateObject(className)); 36e0e9324cSopenharmony_ci if (session_) { 37e0e9324cSopenharmony_ci session_->SetSessionListener(shared_from_this()); 38e0e9324cSopenharmony_ci SHARING_LOGI("create session classname: %{public}s, agentId: %{public}u, sessionId: %{public}u.", 39e0e9324cSopenharmony_ci className.c_str(), GetId(), session_->GetId()); 40e0e9324cSopenharmony_ci SetRunningStatus(AGENT_STEP_CREATE, AGENT_STATUS_DONE); 41e0e9324cSopenharmony_ci return SharingErrorCode::ERR_OK; 42e0e9324cSopenharmony_ci } 43e0e9324cSopenharmony_ci 44e0e9324cSopenharmony_ci SetRunningStatus(AGENT_STEP_CREATE, AGENT_STATUS_ERROR); 45e0e9324cSopenharmony_ci return SharingErrorCode::ERR_SESSION_CREATE; 46e0e9324cSopenharmony_ci} 47e0e9324cSopenharmony_ci 48e0e9324cSopenharmony_ciint32_t Agent::HandleEvent(SharingEvent &event) 49e0e9324cSopenharmony_ci{ 50e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 51e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(event.eventMsg); 52e0e9324cSopenharmony_ci SHARING_LOGI("contextId: %{public}u, agentId: %{public}u, mediaChannelId: %{public}u, " 53e0e9324cSopenharmony_ci "fromMgr: %{public}u, srcId: %{public}u channel event: %{public}s.", 54e0e9324cSopenharmony_ci event.eventMsg->dstId, GetId(), mediaChannelId_, event.eventMsg->fromMgr, event.eventMsg->srcId, 55e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(event.eventMsg->type)).c_str()); 56e0e9324cSopenharmony_ci 57e0e9324cSopenharmony_ci if (EventType::EVENT_AGENT_PROSUMER_ERROR == event.eventMsg->type) { 58e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(runStepMutex_); 59e0e9324cSopenharmony_ci SetRunningStatus(runStep_, AGENT_STATUS_ERROR); 60e0e9324cSopenharmony_ci } 61e0e9324cSopenharmony_ci 62e0e9324cSopenharmony_ci bool isCached = false; 63e0e9324cSopenharmony_ci SharingErrorCode retCode = CheckRunStep(event, isCached); 64e0e9324cSopenharmony_ci if (((retCode != ERR_OK) || isCached) && runningStatus_ != AGENT_STATUS_INTERRUPT) { 65e0e9324cSopenharmony_ci SHARING_LOGW("check run step return, agentId: %{public}u.", GetId()); 66e0e9324cSopenharmony_ci return retCode; 67e0e9324cSopenharmony_ci } 68e0e9324cSopenharmony_ci 69e0e9324cSopenharmony_ci auto statusMsg = std::make_shared<SessionStatusMsg>(); 70e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(statusMsg); 71e0e9324cSopenharmony_ci statusMsg->msg = std::make_shared<EventMsg>(); 72e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(statusMsg->msg); 73e0e9324cSopenharmony_ci statusMsg->msg->type = event.eventMsg->type; 74e0e9324cSopenharmony_ci statusMsg->msg->requestId = event.eventMsg->requestId; 75e0e9324cSopenharmony_ci statusMsg->msg->errorCode = event.eventMsg->errorCode; 76e0e9324cSopenharmony_ci 77e0e9324cSopenharmony_ci if (runningStatus_ == AGENT_STATUS_INTERRUPT) { 78e0e9324cSopenharmony_ci if (session_) { 79e0e9324cSopenharmony_ci SHARING_LOGW("agentId: %{public}u, interrupt.", GetId()); 80e0e9324cSopenharmony_ci statusMsg->status = SESSION_INTERRUPT; 81e0e9324cSopenharmony_ci session_->UpdateOperation(statusMsg); 82e0e9324cSopenharmony_ci } 83e0e9324cSopenharmony_ci return retCode; 84e0e9324cSopenharmony_ci } 85e0e9324cSopenharmony_ci 86e0e9324cSopenharmony_ci auto inputMsg = ConvertEventMsg<AgentEventMsg>(event); 87e0e9324cSopenharmony_ci if (inputMsg == nullptr) { 88e0e9324cSopenharmony_ci SHARING_LOGE("unknow msg."); 89e0e9324cSopenharmony_ci return -1; 90e0e9324cSopenharmony_ci } 91e0e9324cSopenharmony_ci 92e0e9324cSopenharmony_ci switch (event.eventMsg->type) { 93e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_START: 94e0e9324cSopenharmony_ci statusMsg->status = SESSION_START; 95e0e9324cSopenharmony_ci UpdateSessionStatus(statusMsg); 96e0e9324cSopenharmony_ci break; 97e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_DESTROY: 98e0e9324cSopenharmony_ci statusMsg->status = SESSION_STOP; 99e0e9324cSopenharmony_ci UpdateSessionStatus(statusMsg); 100e0e9324cSopenharmony_ci break; 101e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_PAUSE: 102e0e9324cSopenharmony_ci statusMsg->status = SESSION_PAUSE; 103e0e9324cSopenharmony_ci statusMsg->mediaType = inputMsg->mediaType; 104e0e9324cSopenharmony_ci UpdateSessionStatus(statusMsg); 105e0e9324cSopenharmony_ci break; 106e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_RESUME: 107e0e9324cSopenharmony_ci statusMsg->status = SESSION_RESUME; 108e0e9324cSopenharmony_ci statusMsg->mediaType = inputMsg->mediaType; 109e0e9324cSopenharmony_ci UpdateSessionStatus(statusMsg); 110e0e9324cSopenharmony_ci break; 111e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PROSUMER_CREATE: { 112e0e9324cSopenharmony_ci prosumerId_ = inputMsg->prosumerId; 113e0e9324cSopenharmony_ci statusMsg->prosumerId = prosumerId_; 114e0e9324cSopenharmony_ci HandleProsumerState(statusMsg); 115e0e9324cSopenharmony_ci break; 116e0e9324cSopenharmony_ci } 117e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PROSUMER_START: // fall-through 118e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PROSUMER_PAUSE: // fall-through 119e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PROSUMER_RESUME: // fall-through 120e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PROSUMER_STOP: // fall-through 121e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PROSUMER_DESTROY: 122e0e9324cSopenharmony_ci statusMsg->prosumerId = inputMsg->prosumerId; 123e0e9324cSopenharmony_ci HandleProsumerState(statusMsg); 124e0e9324cSopenharmony_ci break; 125e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_PROSUMER_ERROR: 126e0e9324cSopenharmony_ci statusMsg->prosumerId = inputMsg->prosumerId; 127e0e9324cSopenharmony_ci HandleProsumerError(statusMsg); 128e0e9324cSopenharmony_ci break; 129e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_PLAY_START: 130e0e9324cSopenharmony_ci SendChannelEvent(statusMsg, EVENT_MEDIA_PLAY_START); 131e0e9324cSopenharmony_ci break; 132e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_PLAY_STOP: 133e0e9324cSopenharmony_ci SendChannelEvent(statusMsg, EVENT_MEDIA_PLAY_STOP); 134e0e9324cSopenharmony_ci break; 135e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PLAY_START: 136e0e9324cSopenharmony_ci if (statusMsg->msg->errorCode == ERR_OK) { 137e0e9324cSopenharmony_ci PushNextStep(statusMsg); 138e0e9324cSopenharmony_ci } else { 139e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(runStepMutex_); 140e0e9324cSopenharmony_ci SetRunningStatus(runStep_, AGENT_STATUS_ERROR); 141e0e9324cSopenharmony_ci } 142e0e9324cSopenharmony_ci break; 143e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_PLAY_STOP: 144e0e9324cSopenharmony_ci if (statusMsg->msg->errorCode == ERR_OK) { 145e0e9324cSopenharmony_ci PushNextStep(statusMsg); 146e0e9324cSopenharmony_ci } else { 147e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(runStepMutex_); 148e0e9324cSopenharmony_ci SetRunningStatus(runStep_, AGENT_STATUS_ERROR); 149e0e9324cSopenharmony_ci } 150e0e9324cSopenharmony_ci break; 151e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_CHANNEL_APPENDSURFACE: 152e0e9324cSopenharmony_ci statusMsg->surface = inputMsg->surface; 153e0e9324cSopenharmony_ci statusMsg->sceneType = inputMsg->sceneType; 154e0e9324cSopenharmony_ci SendChannelAppendSurfaceEvent(statusMsg, EVENT_MEDIA_CHANNEL_APPENDSURFACE); 155e0e9324cSopenharmony_ci break; 156e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_CHANNEL_REMOVESURFACE: 157e0e9324cSopenharmony_ci statusMsg->surfaceId = inputMsg->surfaceId; 158e0e9324cSopenharmony_ci SendChannelRemoveSurfaceEvent(statusMsg, EVENT_MEDIA_CHANNEL_REMOVESURFACE); 159e0e9324cSopenharmony_ci break; 160e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_CHANNEL_APPENDSURFACE: 161e0e9324cSopenharmony_ci if (statusMsg->msg->errorCode == ERR_OK) { 162e0e9324cSopenharmony_ci PushNextStep(statusMsg); 163e0e9324cSopenharmony_ci } else { 164e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(runStepMutex_); 165e0e9324cSopenharmony_ci SetRunningStatus(runStep_, AGENT_STATUS_ERROR); 166e0e9324cSopenharmony_ci } 167e0e9324cSopenharmony_ci break; 168e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_STATE_CHANNEL_REMOVESURFACE: 169e0e9324cSopenharmony_ci PushNextStep(statusMsg); 170e0e9324cSopenharmony_ci statusMsg->surfaceId = inputMsg->surfaceId; 171e0e9324cSopenharmony_ci SendInteractionEvent(statusMsg, EVENT_INTERACTION_STATE_REMOVE_SURFACE); 172e0e9324cSopenharmony_ci break; 173e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_CHANNEL_SETSCENETYPE: 174e0e9324cSopenharmony_ci statusMsg->surfaceId = inputMsg->surfaceId; 175e0e9324cSopenharmony_ci statusMsg->sceneType = inputMsg->sceneType; 176e0e9324cSopenharmony_ci SendChannelSceneTypeEvent(statusMsg, EVENT_MEDIA_CHANNEL_SETSCENETYPE); 177e0e9324cSopenharmony_ci break; 178e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_CHANNEL_SETVOLUME: 179e0e9324cSopenharmony_ci statusMsg->volume = inputMsg->volume; 180e0e9324cSopenharmony_ci SendChannelSetVolumeEvent(statusMsg, EVENT_MEDIA_CHANNEL_SETVOLUME); 181e0e9324cSopenharmony_ci break; 182e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_CHANNEL_SETKEYREDIRECT: 183e0e9324cSopenharmony_ci statusMsg->surfaceId = inputMsg->surfaceId; 184e0e9324cSopenharmony_ci statusMsg->keyRedirect = inputMsg->keyRedirect; 185e0e9324cSopenharmony_ci SendChannelKeyRedirectEvent(statusMsg, EVENT_MEDIA_CHANNEL_KEY_REDIRECT); 186e0e9324cSopenharmony_ci break; 187e0e9324cSopenharmony_ci case EventType::EVENT_AGEINT_ACCELERATION_DONE: 188e0e9324cSopenharmony_ci statusMsg->surfaceId = inputMsg->surfaceId; 189e0e9324cSopenharmony_ci SendInteractionEvent(statusMsg, EVENT_INTERACTION_ACCELERATION_DONE); 190e0e9324cSopenharmony_ci break; 191e0e9324cSopenharmony_ci case EventType::EVENT_AGENT_DECODER_DIED: 192e0e9324cSopenharmony_ci statusMsg->surfaceId = inputMsg->surfaceId; 193e0e9324cSopenharmony_ci SendInteractionEvent(statusMsg, EVENT_INTERACTION_DECODER_DIED); 194e0e9324cSopenharmony_ci break; 195e0e9324cSopenharmony_ci default: { 196e0e9324cSopenharmony_ci if (session_) { 197e0e9324cSopenharmony_ci session_->HandleEvent(event); 198e0e9324cSopenharmony_ci } 199e0e9324cSopenharmony_ci } 200e0e9324cSopenharmony_ci } 201e0e9324cSopenharmony_ci 202e0e9324cSopenharmony_ci return 0; 203e0e9324cSopenharmony_ci} 204e0e9324cSopenharmony_ci 205e0e9324cSopenharmony_civoid Agent::HandleProsumerError(SessionStatusMsg::Ptr &statusMsg) 206e0e9324cSopenharmony_ci{ 207e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 208e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 209e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 210e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, handle prosumerId: %{public}u, errorCode: %{public}d.", GetId(), 211e0e9324cSopenharmony_ci statusMsg->prosumerId, statusMsg->msg->errorCode); 212e0e9324cSopenharmony_ci SendInteractionEvent(statusMsg, EVENT_INTERACTION_MSG_ERROR); 213e0e9324cSopenharmony_ci switch (statusMsg->msg->errorCode) { 214e0e9324cSopenharmony_ci case ERR_PROSUMER_INIT: // fall-through 215e0e9324cSopenharmony_ci case ERR_PROSUMER_CREATE: // fall-through 216e0e9324cSopenharmony_ci case ERR_PROSUMER_START: // fall-through 217e0e9324cSopenharmony_ci case ERR_PROSUMER_VIDEO_CAPTURE: // fall-through 218e0e9324cSopenharmony_ci case ERR_PROSUMER_TIMEOUT: 219e0e9324cSopenharmony_ci statusMsg->status = SESSION_STOP; 220e0e9324cSopenharmony_ci UpdateSessionStatus(statusMsg); 221e0e9324cSopenharmony_ci break; 222e0e9324cSopenharmony_ci case ERR_PROSUMER_STOP: 223e0e9324cSopenharmony_ci statusMsg->status = SESSION_DESTROY; 224e0e9324cSopenharmony_ci UpdateSessionStatus(statusMsg); 225e0e9324cSopenharmony_ci break; 226e0e9324cSopenharmony_ci case ERR_PROSUMER_DESTROY: { 227e0e9324cSopenharmony_ci if (agentType_ == SINK_AGENT) { 228e0e9324cSopenharmony_ci SendChannelEvent(statusMsg, EVENT_MEDIA_CHANNEL_DESTROY); 229e0e9324cSopenharmony_ci } else if (agentType_ == SRC_AGENT) { 230e0e9324cSopenharmony_ci SendContextEvent(statusMsg, EVENT_CONTEXT_STATE_AGENT_DESTROY); 231e0e9324cSopenharmony_ci } 232e0e9324cSopenharmony_ci break; 233e0e9324cSopenharmony_ci } 234e0e9324cSopenharmony_ci default: 235e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 236e0e9324cSopenharmony_ci break; 237e0e9324cSopenharmony_ci } 238e0e9324cSopenharmony_ci} 239e0e9324cSopenharmony_ci 240e0e9324cSopenharmony_civoid Agent::HandleProsumerState(SessionStatusMsg::Ptr &statusMsg) 241e0e9324cSopenharmony_ci{ 242e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 243e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 244e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 245e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, handle prosumerId: %{public}u, eventType: %{public}s.", GetId(), 246e0e9324cSopenharmony_ci statusMsg->prosumerId, 247e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(static_cast<EventType>(statusMsg->msg->type))).c_str()); 248e0e9324cSopenharmony_ci if (session_ == nullptr) { 249e0e9324cSopenharmony_ci SHARING_LOGE("session_ is null, agentId: %{public}u.", GetId()); 250e0e9324cSopenharmony_ci return; 251e0e9324cSopenharmony_ci } 252e0e9324cSopenharmony_ci 253e0e9324cSopenharmony_ci MediaNotifyStatus mediaStatus = STATE_PROSUMER_NONE; 254e0e9324cSopenharmony_ci switch (statusMsg->msg->type) { 255e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_CREATE: 256e0e9324cSopenharmony_ci mediaStatus = STATE_PROSUMER_CREATE_SUCCESS; 257e0e9324cSopenharmony_ci break; 258e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_START: 259e0e9324cSopenharmony_ci mediaStatus = STATE_PROSUMER_START_SUCCESS; 260e0e9324cSopenharmony_ci break; 261e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_STOP: 262e0e9324cSopenharmony_ci mediaStatus = STATE_PROSUMER_STOP_SUCCESS; 263e0e9324cSopenharmony_ci break; 264e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_PAUSE: 265e0e9324cSopenharmony_ci mediaStatus = STATE_PROSUMER_PAUSE_SUCCESS; 266e0e9324cSopenharmony_ci break; 267e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_RESUME: 268e0e9324cSopenharmony_ci mediaStatus = STATE_PROSUMER_RESUME_SUCCESS; 269e0e9324cSopenharmony_ci break; 270e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_DESTROY: 271e0e9324cSopenharmony_ci mediaStatus = STATE_PROSUMER_DESTROY_SUCCESS; 272e0e9324cSopenharmony_ci break; 273e0e9324cSopenharmony_ci default: 274e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 275e0e9324cSopenharmony_ci break; 276e0e9324cSopenharmony_ci } 277e0e9324cSopenharmony_ci 278e0e9324cSopenharmony_ci statusMsg->status = mediaStatus; 279e0e9324cSopenharmony_ci session_->UpdateMediaStatus(statusMsg); 280e0e9324cSopenharmony_ci PushNextStep(statusMsg); 281e0e9324cSopenharmony_ci} 282e0e9324cSopenharmony_ci 283e0e9324cSopenharmony_civoid Agent::PushNextStep(SessionStatusMsg::Ptr &statusMsg) 284e0e9324cSopenharmony_ci{ 285e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 286e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 287e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 288e0e9324cSopenharmony_ci switch (statusMsg->msg->type) { 289e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_START: 290e0e9324cSopenharmony_ci PopNextStep(AGENT_STEP_START, AGENT_STATUS_DONE); 291e0e9324cSopenharmony_ci break; 292e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_STOP: 293e0e9324cSopenharmony_ci statusMsg->status = SESSION_DESTROY; 294e0e9324cSopenharmony_ci UpdateSessionStatus(statusMsg); 295e0e9324cSopenharmony_ci break; 296e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PROSUMER_DESTROY: { 297e0e9324cSopenharmony_ci PopNextStep(AGENT_STEP_DESTROY, AGENT_STATUS_DONE); 298e0e9324cSopenharmony_ci if (agentType_ == SINK_AGENT) { 299e0e9324cSopenharmony_ci SendChannelEvent(statusMsg, EVENT_MEDIA_CHANNEL_DESTROY); 300e0e9324cSopenharmony_ci } else if (agentType_ == SRC_AGENT) { 301e0e9324cSopenharmony_ci SendContextEvent(statusMsg, EVENT_CONTEXT_STATE_AGENT_DESTROY); 302e0e9324cSopenharmony_ci } 303e0e9324cSopenharmony_ci break; 304e0e9324cSopenharmony_ci } 305e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PLAY_START: 306e0e9324cSopenharmony_ci PopNextStep(AGENT_STEP_PLAY, AGENT_STATUS_DONE); 307e0e9324cSopenharmony_ci break; 308e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_PLAY_STOP: 309e0e9324cSopenharmony_ci PopNextStep(AGENT_STEP_PLAYSTOP, AGENT_STATUS_DONE); 310e0e9324cSopenharmony_ci break; 311e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_CHANNEL_APPENDSURFACE: 312e0e9324cSopenharmony_ci PopNextStep(AGENT_STEP_APPENDSURFACE, AGENT_STATUS_DONE); 313e0e9324cSopenharmony_ci break; 314e0e9324cSopenharmony_ci case EVENT_AGENT_STATE_CHANNEL_REMOVESURFACE: 315e0e9324cSopenharmony_ci PopNextStep(AGENT_STEP_REMOVESURFACE, AGENT_STATUS_DONE); 316e0e9324cSopenharmony_ci break; 317e0e9324cSopenharmony_ci default: 318e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 319e0e9324cSopenharmony_ci break; 320e0e9324cSopenharmony_ci } 321e0e9324cSopenharmony_ci} 322e0e9324cSopenharmony_ci 323e0e9324cSopenharmony_civoid Agent::OnSessionNotify(SessionStatusMsg::Ptr &statusMsg) 324e0e9324cSopenharmony_ci{ 325e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 326e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 327e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u OnSessionNotify status: %{public}s.", GetId(), 328e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(static_cast<SessionNotifyStatus>(statusMsg->status))).c_str()); 329e0e9324cSopenharmony_ci statusMsg->prosumerId = prosumerId_; 330e0e9324cSopenharmony_ci switch (statusMsg->status) { 331e0e9324cSopenharmony_ci case SessionNotifyStatus::NOTIFY_SESSION_PRIVATE_EVENT: 332e0e9324cSopenharmony_ci NotifyPrivateEvent(statusMsg); 333e0e9324cSopenharmony_ci break; 334e0e9324cSopenharmony_ci case SessionNotifyStatus::STATE_SESSION_ERROR: 335e0e9324cSopenharmony_ci HandleSessionError(statusMsg); 336e0e9324cSopenharmony_ci break; 337e0e9324cSopenharmony_ci default: 338e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 339e0e9324cSopenharmony_ci break; 340e0e9324cSopenharmony_ci } 341e0e9324cSopenharmony_ci} 342e0e9324cSopenharmony_ci 343e0e9324cSopenharmony_civoid Agent::HandleSessionError(SessionStatusMsg::Ptr &statusMsg) 344e0e9324cSopenharmony_ci{ 345e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 346e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 347e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 348e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, error: %{public}d.", GetId(), statusMsg->msg->errorCode); 349e0e9324cSopenharmony_ci SendInteractionEvent(statusMsg, EVENT_INTERACTION_MSG_ERROR); 350e0e9324cSopenharmony_ci switch (statusMsg->msg->errorCode) { 351e0e9324cSopenharmony_ci case ERR_SESSION_START: // fall-through 352e0e9324cSopenharmony_ci case ERR_CONNECTION_FAILURE: // fall-through 353e0e9324cSopenharmony_ci case ERR_INVALID_URL: // fall-through 354e0e9324cSopenharmony_ci case ERR_DECODE_FORMAT: // fall-through 355e0e9324cSopenharmony_ci case ERR_UNAUTHORIZED: // fall-through 356e0e9324cSopenharmony_ci case ERR_INTERACTION_FAILURE: // fall-through 357e0e9324cSopenharmony_ci case ERR_PROTOCOL_INTERACTION_TIMEOUT: // fall-through 358e0e9324cSopenharmony_ci case ERR_NETWORK_ERROR: // fall-through 359e0e9324cSopenharmony_ci case ERR_INTAKE_TIMEOUT: 360e0e9324cSopenharmony_ci PopNextStep(runStep_, AGENT_STATUS_ERROR); 361e0e9324cSopenharmony_ci break; 362e0e9324cSopenharmony_ci default: 363e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 364e0e9324cSopenharmony_ci break; 365e0e9324cSopenharmony_ci } 366e0e9324cSopenharmony_ci} 367e0e9324cSopenharmony_ci 368e0e9324cSopenharmony_civoid Agent::UpdateSessionStatus(SessionStatusMsg::Ptr &statusMsg) 369e0e9324cSopenharmony_ci{ 370e0e9324cSopenharmony_ci SHARING_LOGI("handle session: %{public}s agentId: %{public}u.", 371e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(static_cast<SessionRunningStatus>(statusMsg->status))).c_str(), 372e0e9324cSopenharmony_ci GetId()); 373e0e9324cSopenharmony_ci if (session_) { 374e0e9324cSopenharmony_ci session_->UpdateOperation(statusMsg); 375e0e9324cSopenharmony_ci } 376e0e9324cSopenharmony_ci} 377e0e9324cSopenharmony_ci 378e0e9324cSopenharmony_ciuint32_t Agent::NotifyPrivateEvent(SessionStatusMsg::Ptr &statusMsg) 379e0e9324cSopenharmony_ci{ 380e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 381e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(statusMsg); 382e0e9324cSopenharmony_ci RETURN_INVALID_IF_NULL(statusMsg->msg); 383e0e9324cSopenharmony_ci 384e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, send session event, type: %{public}s.", GetId(), 385e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 386e0e9324cSopenharmony_ci auto toMgr = statusMsg->msg->toMgr; 387e0e9324cSopenharmony_ci if (toMgr != ModuleType::MODULE_MEDIACHANNEL && toMgr != ModuleType::MODULE_INTERACTION 388e0e9324cSopenharmony_ci && toMgr != ModuleType::MODULE_CONTEXT) { 389e0e9324cSopenharmony_ci SHARING_LOGE("agentId: %{public}u, toMgr: %{public}d, eventType: %{public}s, error!", GetId(), toMgr, 390e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 391e0e9324cSopenharmony_ci return ERR_GENERAL_ERROR; 392e0e9324cSopenharmony_ci } 393e0e9324cSopenharmony_ci 394e0e9324cSopenharmony_ci if (toMgr == ModuleType::MODULE_MEDIACHANNEL) { 395e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, send session event to mediachannel mediaChannelId: %{public}u.", GetId(), 396e0e9324cSopenharmony_ci mediaChannelId_); 397e0e9324cSopenharmony_ci statusMsg->msg->dstId = mediaChannelId_; 398e0e9324cSopenharmony_ci auto channelMsg = std::static_pointer_cast<ChannelEventMsg>(statusMsg->msg); 399e0e9324cSopenharmony_ci channelMsg->prosumerId = prosumerId_; 400e0e9324cSopenharmony_ci channelMsg->agentId = GetId(); 401e0e9324cSopenharmony_ci channelMsg->errorCode = statusMsg->msg->errorCode; 402e0e9324cSopenharmony_ci channelMsg->requestId = statusMsg->msg->requestId; 403e0e9324cSopenharmony_ci } else if (toMgr == ModuleType::MODULE_INTERACTION) { 404e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, send session event to interaction.", GetId()); 405e0e9324cSopenharmony_ci } 406e0e9324cSopenharmony_ci 407e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 408e0e9324cSopenharmony_ci if (listener) { 409e0e9324cSopenharmony_ci auto agentMsg = std::static_pointer_cast<AgentStatusMsg>(statusMsg); 410e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 411e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 412e0e9324cSopenharmony_ci } 413e0e9324cSopenharmony_ci 414e0e9324cSopenharmony_ci return ERR_OK; 415e0e9324cSopenharmony_ci} 416e0e9324cSopenharmony_ci 417e0e9324cSopenharmony_civoid Agent::SetRunningStatus(AgentRunStep step, AgentRunningStatus status) 418e0e9324cSopenharmony_ci{ 419e0e9324cSopenharmony_ci SHARING_LOGD("agentId: %{public}u, set agent running step: %{public}s, status: %{public}s.", GetId(), 420e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(step)).c_str(), std::string(magic_enum::enum_name(status)).c_str()); 421e0e9324cSopenharmony_ci runStep_ = step; 422e0e9324cSopenharmony_ci runningStatus_ = status; 423e0e9324cSopenharmony_ci} 424e0e9324cSopenharmony_ci 425e0e9324cSopenharmony_civoid Agent::SendInteractionEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 426e0e9324cSopenharmony_ci{ 427e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 428e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 429e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 430e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 431e0e9324cSopenharmony_ci if (listener) { 432e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify interaction: %{public}s.", GetId(), 433e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 434e0e9324cSopenharmony_ci auto interactionMsg = std::make_shared<InteractionEventMsg>(); 435e0e9324cSopenharmony_ci RETURN_IF_NULL(interactionMsg); 436e0e9324cSopenharmony_ci interactionMsg->agentId = GetId(); 437e0e9324cSopenharmony_ci interactionMsg->agentType = GetAgentType(); 438e0e9324cSopenharmony_ci interactionMsg->toMgr = ModuleType::MODULE_INTERACTION; 439e0e9324cSopenharmony_ci interactionMsg->type = eventType; 440e0e9324cSopenharmony_ci interactionMsg->errorCode = statusMsg->msg->errorCode; 441e0e9324cSopenharmony_ci interactionMsg->requestId = statusMsg->msg->requestId; 442e0e9324cSopenharmony_ci interactionMsg->surfaceId = statusMsg->surfaceId; 443e0e9324cSopenharmony_ci 444e0e9324cSopenharmony_ci auto agentMsg = std::make_shared<AgentStatusMsg>(); 445e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 446e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 447e0e9324cSopenharmony_ci agentMsg->msg = std::move(interactionMsg); 448e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 449e0e9324cSopenharmony_ci } 450e0e9324cSopenharmony_ci} 451e0e9324cSopenharmony_ci 452e0e9324cSopenharmony_civoid Agent::SendChannelEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 453e0e9324cSopenharmony_ci{ 454e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 455e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 456e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 457e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 458e0e9324cSopenharmony_ci if (listener) { 459e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify to channelmgr, eventType: %{public}s.", GetId(), 460e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 461e0e9324cSopenharmony_ci 462e0e9324cSopenharmony_ci auto channelMsg = std::make_shared<ChannelEventMsg>(); 463e0e9324cSopenharmony_ci RETURN_IF_NULL(channelMsg); 464e0e9324cSopenharmony_ci channelMsg->agentId = GetId(); 465e0e9324cSopenharmony_ci channelMsg->toMgr = ModuleType::MODULE_MEDIACHANNEL; 466e0e9324cSopenharmony_ci channelMsg->dstId = mediaChannelId_; 467e0e9324cSopenharmony_ci channelMsg->prosumerId = prosumerId_; 468e0e9324cSopenharmony_ci channelMsg->type = eventType; 469e0e9324cSopenharmony_ci channelMsg->errorCode = statusMsg->msg->errorCode; 470e0e9324cSopenharmony_ci channelMsg->requestId = statusMsg->msg->requestId; 471e0e9324cSopenharmony_ci 472e0e9324cSopenharmony_ci AgentStatusMsg::Ptr agentMsg = std::make_shared<AgentStatusMsg>(); 473e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 474e0e9324cSopenharmony_ci agentMsg->msg = std::move(channelMsg); 475e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 476e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 477e0e9324cSopenharmony_ci } 478e0e9324cSopenharmony_ci} 479e0e9324cSopenharmony_ci 480e0e9324cSopenharmony_civoid Agent::SendChannelAppendSurfaceEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 481e0e9324cSopenharmony_ci{ 482e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 483e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 484e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 485e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 486e0e9324cSopenharmony_ci if (listener) { 487e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify to channelmgr, eventType: %{public}s.", GetId(), 488e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 489e0e9324cSopenharmony_ci 490e0e9324cSopenharmony_ci auto channelMsg = std::make_shared<ChannelAppendSurfaceEventMsg>(); 491e0e9324cSopenharmony_ci RETURN_IF_NULL(channelMsg); 492e0e9324cSopenharmony_ci channelMsg->agentId = GetId(); 493e0e9324cSopenharmony_ci channelMsg->toMgr = ModuleType::MODULE_MEDIACHANNEL; 494e0e9324cSopenharmony_ci channelMsg->dstId = mediaChannelId_; 495e0e9324cSopenharmony_ci channelMsg->prosumerId = prosumerId_; 496e0e9324cSopenharmony_ci channelMsg->type = eventType; 497e0e9324cSopenharmony_ci channelMsg->surface = statusMsg->surface; 498e0e9324cSopenharmony_ci channelMsg->sceneType = statusMsg->sceneType; 499e0e9324cSopenharmony_ci channelMsg->errorCode = statusMsg->msg->errorCode; 500e0e9324cSopenharmony_ci channelMsg->requestId = statusMsg->msg->requestId; 501e0e9324cSopenharmony_ci 502e0e9324cSopenharmony_ci AgentStatusMsg::Ptr agentMsg = std::make_shared<AgentStatusMsg>(); 503e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 504e0e9324cSopenharmony_ci agentMsg->msg = std::move(channelMsg); 505e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 506e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 507e0e9324cSopenharmony_ci } 508e0e9324cSopenharmony_ci} 509e0e9324cSopenharmony_ci 510e0e9324cSopenharmony_civoid Agent::SendChannelRemoveSurfaceEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 511e0e9324cSopenharmony_ci{ 512e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 513e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 514e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 515e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 516e0e9324cSopenharmony_ci if (listener) { 517e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify to channelmgr, eventType: %{public}s.", GetId(), 518e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 519e0e9324cSopenharmony_ci 520e0e9324cSopenharmony_ci auto channelMsg = std::make_shared<ChannelRemoveSurfaceEventMsg>(); 521e0e9324cSopenharmony_ci RETURN_IF_NULL(channelMsg); 522e0e9324cSopenharmony_ci channelMsg->agentId = GetId(); 523e0e9324cSopenharmony_ci channelMsg->toMgr = ModuleType::MODULE_MEDIACHANNEL; 524e0e9324cSopenharmony_ci channelMsg->dstId = mediaChannelId_; 525e0e9324cSopenharmony_ci channelMsg->prosumerId = prosumerId_; 526e0e9324cSopenharmony_ci channelMsg->type = eventType; 527e0e9324cSopenharmony_ci channelMsg->surfaceId = statusMsg->surfaceId; 528e0e9324cSopenharmony_ci channelMsg->errorCode = statusMsg->msg->errorCode; 529e0e9324cSopenharmony_ci channelMsg->requestId = statusMsg->msg->requestId; 530e0e9324cSopenharmony_ci 531e0e9324cSopenharmony_ci AgentStatusMsg::Ptr agentMsg = std::make_shared<AgentStatusMsg>(); 532e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 533e0e9324cSopenharmony_ci agentMsg->msg = std::move(channelMsg); 534e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 535e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 536e0e9324cSopenharmony_ci } 537e0e9324cSopenharmony_ci} 538e0e9324cSopenharmony_ci 539e0e9324cSopenharmony_civoid Agent::SendChannelSceneTypeEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 540e0e9324cSopenharmony_ci{ 541e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 542e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 543e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 544e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 545e0e9324cSopenharmony_ci if (listener) { 546e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify to channelmgr, eventType: %{public}s.", GetId(), 547e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 548e0e9324cSopenharmony_ci 549e0e9324cSopenharmony_ci auto channelMsg = std::make_shared<ChannelSetSceneTypeEventMsg>(); 550e0e9324cSopenharmony_ci RETURN_IF_NULL(channelMsg); 551e0e9324cSopenharmony_ci channelMsg->agentId = GetId(); 552e0e9324cSopenharmony_ci channelMsg->toMgr = ModuleType::MODULE_MEDIACHANNEL; 553e0e9324cSopenharmony_ci channelMsg->dstId = mediaChannelId_; 554e0e9324cSopenharmony_ci channelMsg->prosumerId = prosumerId_; 555e0e9324cSopenharmony_ci channelMsg->type = eventType; 556e0e9324cSopenharmony_ci channelMsg->surfaceId = statusMsg->surfaceId; 557e0e9324cSopenharmony_ci channelMsg->sceneType = statusMsg->sceneType; 558e0e9324cSopenharmony_ci channelMsg->errorCode = statusMsg->msg->errorCode; 559e0e9324cSopenharmony_ci channelMsg->requestId = statusMsg->msg->requestId; 560e0e9324cSopenharmony_ci 561e0e9324cSopenharmony_ci AgentStatusMsg::Ptr agentMsg = std::make_shared<AgentStatusMsg>(); 562e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 563e0e9324cSopenharmony_ci agentMsg->msg = std::move(channelMsg); 564e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 565e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 566e0e9324cSopenharmony_ci } 567e0e9324cSopenharmony_ci} 568e0e9324cSopenharmony_ci 569e0e9324cSopenharmony_civoid Agent::SendChannelKeyRedirectEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 570e0e9324cSopenharmony_ci{ 571e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 572e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 573e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 574e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 575e0e9324cSopenharmony_ci if (listener) { 576e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify to channelmgr, eventType: %{public}s.", GetId(), 577e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 578e0e9324cSopenharmony_ci 579e0e9324cSopenharmony_ci auto channelMsg = std::make_shared<ChannelSetKeyRedirectEventMsg>(); 580e0e9324cSopenharmony_ci RETURN_IF_NULL(channelMsg); 581e0e9324cSopenharmony_ci channelMsg->agentId = GetId(); 582e0e9324cSopenharmony_ci channelMsg->toMgr = ModuleType::MODULE_MEDIACHANNEL; 583e0e9324cSopenharmony_ci channelMsg->dstId = mediaChannelId_; 584e0e9324cSopenharmony_ci channelMsg->prosumerId = prosumerId_; 585e0e9324cSopenharmony_ci channelMsg->type = eventType; 586e0e9324cSopenharmony_ci channelMsg->surfaceId = statusMsg->surfaceId; 587e0e9324cSopenharmony_ci channelMsg->keyRedirect = statusMsg->keyRedirect; 588e0e9324cSopenharmony_ci channelMsg->errorCode = statusMsg->msg->errorCode; 589e0e9324cSopenharmony_ci channelMsg->requestId = statusMsg->msg->requestId; 590e0e9324cSopenharmony_ci 591e0e9324cSopenharmony_ci AgentStatusMsg::Ptr agentMsg = std::make_shared<AgentStatusMsg>(); 592e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 593e0e9324cSopenharmony_ci agentMsg->msg = std::move(channelMsg); 594e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 595e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 596e0e9324cSopenharmony_ci } 597e0e9324cSopenharmony_ci} 598e0e9324cSopenharmony_ci 599e0e9324cSopenharmony_civoid Agent::SendChannelSetVolumeEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 600e0e9324cSopenharmony_ci{ 601e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 602e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 603e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 604e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 605e0e9324cSopenharmony_ci if (listener) { 606e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify to channelmgr, eventType: %{public}s.", GetId(), 607e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 608e0e9324cSopenharmony_ci 609e0e9324cSopenharmony_ci auto channelMsg = std::make_shared<ChannelSetVolumeEventMsg>(); 610e0e9324cSopenharmony_ci RETURN_IF_NULL(channelMsg); 611e0e9324cSopenharmony_ci channelMsg->agentId = GetId(); 612e0e9324cSopenharmony_ci channelMsg->toMgr = ModuleType::MODULE_MEDIACHANNEL; 613e0e9324cSopenharmony_ci channelMsg->dstId = mediaChannelId_; 614e0e9324cSopenharmony_ci channelMsg->prosumerId = prosumerId_; 615e0e9324cSopenharmony_ci channelMsg->type = eventType; 616e0e9324cSopenharmony_ci channelMsg->volume = statusMsg->volume; 617e0e9324cSopenharmony_ci channelMsg->errorCode = statusMsg->msg->errorCode; 618e0e9324cSopenharmony_ci channelMsg->requestId = statusMsg->msg->requestId; 619e0e9324cSopenharmony_ci 620e0e9324cSopenharmony_ci AgentStatusMsg::Ptr agentMsg = std::make_shared<AgentStatusMsg>(); 621e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 622e0e9324cSopenharmony_ci agentMsg->msg = std::move(channelMsg); 623e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 624e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 625e0e9324cSopenharmony_ci } 626e0e9324cSopenharmony_ci} 627e0e9324cSopenharmony_ci 628e0e9324cSopenharmony_civoid Agent::SendContextEvent(SessionStatusMsg::Ptr &statusMsg, EventType eventType) 629e0e9324cSopenharmony_ci{ 630e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 631e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg); 632e0e9324cSopenharmony_ci RETURN_IF_NULL(statusMsg->msg); 633e0e9324cSopenharmony_ci auto listener = agentListener_.lock(); 634e0e9324cSopenharmony_ci if (listener) { 635e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, notify to context, eventType: %{public}s.", GetId(), 636e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(statusMsg->msg->type)).c_str()); 637e0e9324cSopenharmony_ci auto contextMsg = std::make_shared<ContextEventMsg>(); 638e0e9324cSopenharmony_ci RETURN_IF_NULL(contextMsg); 639e0e9324cSopenharmony_ci contextMsg->type = eventType; 640e0e9324cSopenharmony_ci contextMsg->toMgr = ModuleType::MODULE_CONTEXT; 641e0e9324cSopenharmony_ci contextMsg->fromMgr = ModuleType::MODULE_AGENT; 642e0e9324cSopenharmony_ci contextMsg->srcId = GetId(); 643e0e9324cSopenharmony_ci contextMsg->agentId = GetId(); 644e0e9324cSopenharmony_ci contextMsg->agentType = agentType_; 645e0e9324cSopenharmony_ci contextMsg->errorCode = statusMsg->msg->errorCode; 646e0e9324cSopenharmony_ci contextMsg->requestId = statusMsg->msg->requestId; 647e0e9324cSopenharmony_ci 648e0e9324cSopenharmony_ci AgentStatusMsg::Ptr agentMsg = std::make_shared<AgentStatusMsg>(); 649e0e9324cSopenharmony_ci RETURN_IF_NULL(agentMsg); 650e0e9324cSopenharmony_ci agentMsg->msg = std::move(contextMsg); 651e0e9324cSopenharmony_ci agentMsg->agentId = GetId(); 652e0e9324cSopenharmony_ci listener->OnAgentNotify(agentMsg); 653e0e9324cSopenharmony_ci } 654e0e9324cSopenharmony_ci} 655e0e9324cSopenharmony_ci 656e0e9324cSopenharmony_ciAgentRunStep Agent::GetRunStep(EventType eventType) 657e0e9324cSopenharmony_ci{ 658e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 659e0e9324cSopenharmony_ci AgentRunStep step = AGENT_STEP_IDLE; 660e0e9324cSopenharmony_ci switch (eventType) { 661e0e9324cSopenharmony_ci case EVENT_AGENT_START: 662e0e9324cSopenharmony_ci step = AGENT_STEP_START; 663e0e9324cSopenharmony_ci break; 664e0e9324cSopenharmony_ci case EVENT_AGENT_CHANNEL_APPENDSURFACE: 665e0e9324cSopenharmony_ci step = AGENT_STEP_APPENDSURFACE; 666e0e9324cSopenharmony_ci break; 667e0e9324cSopenharmony_ci case EVENT_AGENT_CHANNEL_REMOVESURFACE: 668e0e9324cSopenharmony_ci step = AGENT_STEP_REMOVESURFACE; 669e0e9324cSopenharmony_ci break; 670e0e9324cSopenharmony_ci case EVENT_AGENT_PLAY_START: 671e0e9324cSopenharmony_ci step = AGENT_STEP_PLAY; 672e0e9324cSopenharmony_ci break; 673e0e9324cSopenharmony_ci case EVENT_AGENT_PLAY_STOP: 674e0e9324cSopenharmony_ci step = AGENT_STEP_PLAYSTOP; 675e0e9324cSopenharmony_ci break; 676e0e9324cSopenharmony_ci case EVENT_AGENT_PROSUMER_ERROR: 677e0e9324cSopenharmony_ci step = AGENT_STEP_DESTROY; 678e0e9324cSopenharmony_ci break; 679e0e9324cSopenharmony_ci case EVENT_AGENT_DESTROY: 680e0e9324cSopenharmony_ci step = AGENT_STEP_DESTROY; 681e0e9324cSopenharmony_ci break; 682e0e9324cSopenharmony_ci default: 683e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 684e0e9324cSopenharmony_ci break; 685e0e9324cSopenharmony_ci } 686e0e9324cSopenharmony_ci 687e0e9324cSopenharmony_ci return step; 688e0e9324cSopenharmony_ci} 689e0e9324cSopenharmony_ci 690e0e9324cSopenharmony_ciAgentRunStepWeight Agent::GetRunStepWeight(AgentRunStep runStep) 691e0e9324cSopenharmony_ci{ 692e0e9324cSopenharmony_ci SHARING_LOGD("trace."); 693e0e9324cSopenharmony_ci AgentRunStepWeight weight = AGENT_STEP_WEIGHT_IDLE; 694e0e9324cSopenharmony_ci switch (runStep) { 695e0e9324cSopenharmony_ci case AGENT_STEP_START: 696e0e9324cSopenharmony_ci weight = AGENT_STEP_WEIGHT_START; 697e0e9324cSopenharmony_ci break; 698e0e9324cSopenharmony_ci // case AGENT_STEP_PAUSE: 699e0e9324cSopenharmony_ci // case AGENT_STEP_RESUME: 700e0e9324cSopenharmony_ci case AGENT_STEP_APPENDSURFACE: // fall-through 701e0e9324cSopenharmony_ci case AGENT_STEP_REMOVESURFACE: // fall-through 702e0e9324cSopenharmony_ci case AGENT_STEP_PLAY: // fall-through 703e0e9324cSopenharmony_ci case AGENT_STEP_PLAYSTOP: 704e0e9324cSopenharmony_ci weight = AGENT_STEP_WEIGHT_REUSABLE; 705e0e9324cSopenharmony_ci break; 706e0e9324cSopenharmony_ci case AGENT_STEP_DESTROY: 707e0e9324cSopenharmony_ci weight = AGENT_STEP_WEIGHT_DESTROY; 708e0e9324cSopenharmony_ci break; 709e0e9324cSopenharmony_ci default: 710e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 711e0e9324cSopenharmony_ci break; 712e0e9324cSopenharmony_ci } 713e0e9324cSopenharmony_ci 714e0e9324cSopenharmony_ci return weight; 715e0e9324cSopenharmony_ci} 716e0e9324cSopenharmony_ci 717e0e9324cSopenharmony_ciSharingErrorCode Agent::CheckRunStep(SharingEvent &event, bool &isCached) 718e0e9324cSopenharmony_ci{ 719e0e9324cSopenharmony_ci SHARING_LOGD("check run step, now step: %{public}s, now status: %{public}s, agentId: %{public}u.", 720e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runStep_)).c_str(), 721e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runningStatus_)).c_str(), GetId()); 722e0e9324cSopenharmony_ci SharingErrorCode retCode = ERR_OK; 723e0e9324cSopenharmony_ci AgentRunStep step = GetRunStep(event.eventMsg->type); 724e0e9324cSopenharmony_ci if (step == AGENT_STEP_IDLE) { 725e0e9324cSopenharmony_ci return retCode; 726e0e9324cSopenharmony_ci } 727e0e9324cSopenharmony_ci 728e0e9324cSopenharmony_ci AgentRunStepWeight weight = GetRunStepWeight(step); 729e0e9324cSopenharmony_ci AgentRunStepKey nextKey = {event.eventMsg->requestId, step, weight}; 730e0e9324cSopenharmony_ci 731e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(runStepMutex_); 732e0e9324cSopenharmony_ci switch (runningStatus_) { 733e0e9324cSopenharmony_ci case AGENT_STATUS_DONE: 734e0e9324cSopenharmony_ci if (runStep_ == AGENT_STEP_DESTROY) { 735e0e9324cSopenharmony_ci return ERR_GENERAL_ERROR; 736e0e9324cSopenharmony_ci } 737e0e9324cSopenharmony_ci if ((runStep_ < AGENT_STEP_START) && (step > AGENT_STEP_START) && (step < AGENT_STEP_DESTROY)) { 738e0e9324cSopenharmony_ci runEvents_.emplace(nextKey, event); 739e0e9324cSopenharmony_ci isCached = true; 740e0e9324cSopenharmony_ci SHARING_LOGD("cache event: %{public}s agentId: %{public}u.", 741e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(event.eventMsg->type)).c_str(), GetId()); 742e0e9324cSopenharmony_ci } else { 743e0e9324cSopenharmony_ci SetRunningStatus(step, AGENT_STATUS_RUNNING); 744e0e9324cSopenharmony_ci } 745e0e9324cSopenharmony_ci break; 746e0e9324cSopenharmony_ci case AGENT_STATUS_RUNNING: { 747e0e9324cSopenharmony_ci if ((step == runStep_) || (runStep_ >= AGENT_STEP_DESTROY) || (runEvents_.count(nextKey))) { 748e0e9324cSopenharmony_ci SHARING_LOGW("already has event: %{public}s, agentId: %{public}u.", 749e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(event.eventMsg->type)).c_str(), GetId()); 750e0e9324cSopenharmony_ci retCode = ERR_GENERAL_ERROR; 751e0e9324cSopenharmony_ci } else { 752e0e9324cSopenharmony_ci if (step == AGENT_STEP_DESTROY && runStep_ < step) { 753e0e9324cSopenharmony_ci SHARING_LOGW("agentId: %{public}u, interrupt the step: %{public}d.", GetId(), runStep_); 754e0e9324cSopenharmony_ci SetRunningStatus(runStep_, AGENT_STATUS_INTERRUPT); 755e0e9324cSopenharmony_ci } 756e0e9324cSopenharmony_ci runEvents_.emplace(nextKey, event); 757e0e9324cSopenharmony_ci isCached = true; 758e0e9324cSopenharmony_ci SHARING_LOGW("cache event: %{public}s, agentId: %{public}u.", 759e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(event.eventMsg->type)).c_str(), GetId()); 760e0e9324cSopenharmony_ci } 761e0e9324cSopenharmony_ci break; 762e0e9324cSopenharmony_ci } 763e0e9324cSopenharmony_ci case AGENT_STATUS_ERROR: { 764e0e9324cSopenharmony_ci if (step == AGENT_STEP_DESTROY) { 765e0e9324cSopenharmony_ci SetRunningStatus(step, AGENT_STATUS_RUNNING); 766e0e9324cSopenharmony_ci } else { 767e0e9324cSopenharmony_ci retCode = ERR_GENERAL_ERROR; 768e0e9324cSopenharmony_ci } 769e0e9324cSopenharmony_ci break; 770e0e9324cSopenharmony_ci } 771e0e9324cSopenharmony_ci default: 772e0e9324cSopenharmony_ci SHARING_LOGI("none process case."); 773e0e9324cSopenharmony_ci break; 774e0e9324cSopenharmony_ci } 775e0e9324cSopenharmony_ci 776e0e9324cSopenharmony_ci SHARING_LOGD("check run step, set step: %{public}s, set status: %{public}s, agentId: %{public}u.", 777e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runStep_)).c_str(), 778e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runningStatus_)).c_str(), GetId()); 779e0e9324cSopenharmony_ci return retCode; 780e0e9324cSopenharmony_ci} 781e0e9324cSopenharmony_ci 782e0e9324cSopenharmony_civoid Agent::PopNextStep(AgentRunStep step, AgentRunningStatus status) 783e0e9324cSopenharmony_ci{ 784e0e9324cSopenharmony_ci SHARING_LOGD("pop run step, now step: %{public}s, now status: %{public}s, agentId: %{public}u.", 785e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runStep_)).c_str(), 786e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runningStatus_)).c_str(), GetId()); 787e0e9324cSopenharmony_ci SharingEvent event; 788e0e9324cSopenharmony_ci bool ret = false; 789e0e9324cSopenharmony_ci { 790e0e9324cSopenharmony_ci std::lock_guard<std::mutex> lock(runStepMutex_); 791e0e9324cSopenharmony_ci if (step != runStep_) { 792e0e9324cSopenharmony_ci SHARING_LOGW( 793e0e9324cSopenharmony_ci "pop run step, set step: %{public}s is not equal to now step: %{public}s, agentId: %{public}u.", 794e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(step)).c_str(), std::string(magic_enum::enum_name(runStep_)).c_str(), 795e0e9324cSopenharmony_ci GetId()); 796e0e9324cSopenharmony_ci } else { 797e0e9324cSopenharmony_ci SetRunningStatus(step, status); 798e0e9324cSopenharmony_ci SHARING_LOGD("pop run step, set step: %{public}s, set status: %{public}s, agentId: %{public}u.", 799e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runStep_)).c_str(), 800e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(runningStatus_)).c_str(), GetId()); 801e0e9324cSopenharmony_ci if (runEvents_.size() > 0) { 802e0e9324cSopenharmony_ci auto it = runEvents_.begin(); 803e0e9324cSopenharmony_ci event = std::move(it->second); 804e0e9324cSopenharmony_ci SHARING_LOGD("agentId: %{public}u, next eventType: %{public}s.", GetId(), 805e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(event.eventMsg->type)).c_str()); 806e0e9324cSopenharmony_ci runEvents_.erase(it); 807e0e9324cSopenharmony_ci ret = true; 808e0e9324cSopenharmony_ci } 809e0e9324cSopenharmony_ci } 810e0e9324cSopenharmony_ci } 811e0e9324cSopenharmony_ci 812e0e9324cSopenharmony_ci if (ret) { 813e0e9324cSopenharmony_ci SHARING_LOGI("agentId: %{public}u, pop to handle next event: %{public}s.", GetId(), 814e0e9324cSopenharmony_ci std::string(magic_enum::enum_name(event.eventMsg->type)).c_str()); 815e0e9324cSopenharmony_ci HandleEvent(event); 816e0e9324cSopenharmony_ci } 817e0e9324cSopenharmony_ci} 818e0e9324cSopenharmony_ci 819e0e9324cSopenharmony_ci} // namespace Sharing 820e0e9324cSopenharmony_ci} // namespace OHOS