1 /*
2 * Copyright (c) 2022 Huawei Device 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 "remote_session_sink_impl.h"
17
18 #include "avsession_sysevent.h"
19 #include "avsession_trace.h"
20
21 namespace OHOS::AVSession {
RemoteSessionSinkImpl()22 RemoteSessionSinkImpl::RemoteSessionSinkImpl()
23 {
24 }
25
CreateRemoteSessionSinkImpl()26 extern "C" RemoteSessionSinkImpl* CreateRemoteSessionSinkImpl()
27 {
28 return new(std::nothrow) RemoteSessionSinkImpl();
29 }
30
DestroyRemoteSessionSinkImpl(RemoteSessionSinkImpl * impl)31 extern "C" void DestroyRemoteSessionSinkImpl(RemoteSessionSinkImpl* impl)
32 {
33 delete(impl);
34 }
35
CastSessionFromRemote(const sptr<AVSessionItem> & session,const std::string & sourceSessionId,const std::string & sourceDevice,const std::string & sinkDevice,const std::string & sourceCap)36 int32_t RemoteSessionSinkImpl::CastSessionFromRemote(const sptr <AVSessionItem>& session,
37 const std::string& sourceSessionId,
38 const std::string& sourceDevice,
39 const std::string& sinkDevice,
40 const std::string& sourceCap)
41 {
42 syncer_ = std::make_shared<RemoteSessionSyncerImpl>(sourceSessionId, sourceDevice, sinkDevice);
43 CHECK_AND_RETURN_RET_LOG(syncer_ != nullptr, AVSESSION_ERROR, "syncer_ is nullptr");
44 int32_t ret = syncer_->Init();
45 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "syncer init failed");
46 session_ = session;
47 sourceDevice_ = sourceDevice;
48 RemoteSessionCapabilitySet::GetInstance().AddRemoteCapability(session->GetSessionId(), sourceDevice, sourceCap);
49
50 ret = syncer_->RegisterDisconnectNotifier([this](const std::string& deviceId) {
51 SLOGE("device %{public}s disconnected, sessionId is %{public}s", deviceId.c_str(),
52 session_->GetSessionId().c_str());
53 HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED",
54 "BUNDLE_NAME", session_->GetDescriptor().elementName_.GetBundleName(),
55 "SESSION_TYPE", session_->GetDescriptor().sessionType_,
56 "AUDIO_STATUS", HISYSEVENT_GET_AUDIO_STATUS(session_->GetUid()),
57 "ERROR_TYPE", "REMOTE_DISCONNECTED",
58 "ERROR_INFO", "remote disconnected");
59 return AVSESSION_SUCCESS;
60 });
61
62 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "AddDisconnectNotifier failed");
63
64 ret = syncer_->RegisterDataNotifier([this](const SessionDataCategory category, const std::string& deviceId) {
65 SLOGE("device %{public}s category %{public}d changed", deviceId.c_str(), category);
66 CHECK_AND_RETURN_RET_LOG(session_ != nullptr && syncer_ != nullptr, AVSESSION_ERROR, "session_ is nullptr");
67 if (category == SESSION_DATA_META) {
68 AVMetaData metaData;
69 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::GetAVMetaData");
70 CHECK_AND_RETURN_RET_LOG(syncer_->GetAVMetaData(metaData) == AVSESSION_SUCCESS, AVSESSION_ERROR,
71 "GetAVMetaData failed");
72 CHECK_AND_RETURN_RET_LOG(session_->SetAVMetaData(metaData) == AVSESSION_SUCCESS, AVSESSION_ERROR,
73 "SetAVMetaData failed");
74 return AVSESSION_SUCCESS;
75 }
76
77 if (category == SESSION_DATA_PLAYBACK_STATE) {
78 AVPlaybackState playbackState;
79 AVSESSION_TRACE_SYNC_START("RemoteSessionSinkImpl::GetAVPlaybackState");
80 CHECK_AND_RETURN_RET_LOG(syncer_->GetAVPlaybackState(playbackState) == AVSESSION_SUCCESS, AVSESSION_ERROR,
81 "GetAVPlaybackState failed");
82 CHECK_AND_RETURN_RET_LOG(session_->SetAVPlaybackState(playbackState) == AVSESSION_SUCCESS, AVSESSION_ERROR,
83 "SetAVPlaybackState failed");
84 return AVSESSION_SUCCESS;
85 }
86 SLOGE("category is illegal");
87 return AVSESSION_ERROR;
88 });
89 CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "AddDataNotifier failed");
90 return AVSESSION_SUCCESS;
91 }
92
CancelCastSession()93 int32_t RemoteSessionSinkImpl::CancelCastSession()
94 {
95 CHECK_AND_RETURN_RET_LOG(session_ != nullptr, AVSESSION_ERROR, "session is nullptr");
96 RemoteSessionCapabilitySet::GetInstance().RemoveRemoteCapability(session_->GetSessionId(), sourceDevice_);
97 syncer_->Destroy();
98 syncer_ = nullptr;
99 return AVSESSION_SUCCESS;
100 }
101
SetControlCommand(const AVControlCommand & command)102 int32_t RemoteSessionSinkImpl::SetControlCommand(const AVControlCommand& command)
103 {
104 CHECK_AND_RETURN_RET_LOG(syncer_ != nullptr, AVSESSION_ERROR, "syncer is nullptr");
105 auto ret = syncer_->PutControlCommand(command);
106 if (ret != AVSESSION_SUCCESS && session_ != nullptr) {
107 HISYSEVENT_FAULT("REMOTE_CONTROL_FAILED",
108 "BUNDLE_NAME", session_->GetDescriptor().elementName_.GetBundleName(),
109 "SESSION_TYPE", session_->GetDescriptor().sessionType_,
110 "AUDIO_STATUS", HISYSEVENT_GET_AUDIO_STATUS(session_->GetUid()),
111 "ERROR_TYPE", "TIME_OUT",
112 "ERROR_INFO", "SetControlCommand time out");
113 }
114 return AVSESSION_SUCCESS;
115 }
116 } // namespace OHOS::AVSession