• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "avcontroller_item.h"
17 #include "ipc_skeleton.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20 #include "avsession_trace.h"
21 #include "command_send_limit.h"
22 #include "avsession_utils.h"
23 
24 #if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM) and !defined(IOS_PLATFORM)
25 #include <malloc.h>
26 #endif
27 
28 namespace OHOS::AVSession {
AVControllerItem(pid_t pid,const sptr<AVSessionItem> & session)29 AVControllerItem::AVControllerItem(pid_t pid, const sptr<AVSessionItem>& session)
30     : pid_(pid), session_(session)
31 {
32     sessionId_ = session_->GetSessionId();
33     SLOGD("construct sessionId=%{public}s", sessionId_.c_str());
34 }
35 
~AVControllerItem()36 AVControllerItem::~AVControllerItem()
37 {
38     SLOGI("destroy sessionId=%{public}s", sessionId_.c_str());
39 }
40 
RegisterCallbackInner(const sptr<IRemoteObject> & callback)41 int32_t AVControllerItem::RegisterCallbackInner(const sptr<IRemoteObject>& callback)
42 {
43     callback_ = iface_cast<AVControllerCallbackProxy>(callback);
44     CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, AVSESSION_ERROR, "callback_ is nullptr");
45     return AVSESSION_SUCCESS;
46 }
47 
GetAVPlaybackState(AVPlaybackState & state)48 int32_t AVControllerItem::GetAVPlaybackState(AVPlaybackState& state)
49 {
50     CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
51     state = session_->GetPlaybackState();
52     return AVSESSION_SUCCESS;
53 }
54 
GetAVMetaData(AVMetaData & data)55 int32_t AVControllerItem::GetAVMetaData(AVMetaData& data)
56 {
57     CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
58     data = session_->GetMetaData();
59     return AVSESSION_SUCCESS;
60 }
61 
SendAVKeyEvent(const MMI::KeyEvent & keyEvent)62 int32_t AVControllerItem::SendAVKeyEvent(const MMI::KeyEvent& keyEvent)
63 {
64     CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
65     session_->HandleMediaKeyEvent(keyEvent);
66     return AVSESSION_SUCCESS;
67 }
68 
GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent & ability)69 int32_t AVControllerItem::GetLaunchAbility(AbilityRuntime::WantAgent::WantAgent& ability)
70 {
71     CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
72     ability = session_->GetLaunchAbility();
73     return AVSESSION_SUCCESS;
74 }
75 
GetValidCommands(std::vector<int32_t> & cmds)76 int32_t AVControllerItem::GetValidCommands(std::vector<int32_t>& cmds)
77 {
78     CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
79     cmds = session_->GetSupportCommand();
80     return AVSESSION_SUCCESS;
81 }
82 
IsSessionActive(bool & isActive)83 int32_t AVControllerItem::IsSessionActive(bool& isActive)
84 {
85     CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
86     isActive = session_->IsActive();
87     return AVSESSION_SUCCESS;
88 }
89 
SendControlCommand(const AVControlCommand & cmd)90 int32_t AVControllerItem::SendControlCommand(const AVControlCommand& cmd)
91 {
92     CHECK_AND_RETURN_RET_LOG(session_ != nullptr, ERR_SESSION_NOT_EXIST, "session not exist");
93     std::vector<int32_t> cmds = session_->GetSupportCommand();
94     CHECK_AND_RETURN_RET_LOG(std::find(cmds.begin(), cmds.end(), cmd.GetCommand()) != cmds.end(),
95                              ERR_COMMAND_NOT_SUPPORT, "command not support");
96     CHECK_AND_RETURN_RET_LOG(CommandSendLimit::GetInstance().IsCommandSendEnable(OHOS::IPCSkeleton::GetCallingPid()),
97         ERR_COMMAND_SEND_EXCEED_MAX, "command send number exceed max");
98     session_->ExecuteControllerCommand(cmd);
99     return AVSESSION_SUCCESS;
100 }
101 
SetMetaFilter(const AVMetaData::MetaMaskType & filter)102 int32_t AVControllerItem::SetMetaFilter(const AVMetaData::MetaMaskType& filter)
103 {
104     metaMask_ = filter;
105     return AVSESSION_SUCCESS;
106 }
107 
SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType & filter)108 int32_t AVControllerItem::SetPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter)
109 {
110     playbackMask_ = filter;
111     return AVSESSION_SUCCESS;
112 }
113 
Destroy()114 int32_t AVControllerItem::Destroy()
115 {
116     callback_ = nullptr;
117     if (session_ != nullptr) {
118         session_->HandleControllerRelease(pid_);
119         session_ = nullptr;
120         sessionId_.clear();
121     }
122     if (serviceCallback_) {
123         serviceCallback_(*this);
124     }
125     return AVSESSION_SUCCESS;
126 }
127 
GetSessionId()128 std::string AVControllerItem::GetSessionId()
129 {
130     return sessionId_;
131 }
132 
HandleSessionDestroy()133 void AVControllerItem::HandleSessionDestroy()
134 {
135     if (callback_ != nullptr) {
136         callback_->OnSessionDestroy();
137     }
138     session_ = nullptr;
139     sessionId_.clear();
140 }
141 
HandlePlaybackStateChange(const AVPlaybackState & state)142 void AVControllerItem::HandlePlaybackStateChange(const AVPlaybackState& state)
143 {
144     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
145     AVPlaybackState stateOut;
146     if (state.CopyToByMask(playbackMask_, stateOut)) {
147         SLOGI("update playback state");
148         AVSESSION_TRACE_SYNC_START("AVControllerItem::OnPlaybackStateChange");
149         callback_->OnPlaybackStateChange(stateOut);
150     }
151 }
152 
HandleMetaDataChange(const AVMetaData & data)153 void AVControllerItem::HandleMetaDataChange(const AVMetaData& data)
154 {
155     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
156     AVMetaData metaOut;
157     if (data.CopyToByMask(metaMask_, metaOut)) {
158         if ((metaMask_.test(AVMetaData::META_KEY_MEDIA_IMAGE)) && (metaOut.GetMediaImage() != nullptr)) {
159             std::string fileName = AVSessionUtils::GetCachePathName() + sessionId_ + AVSessionUtils::GetFileSuffix();
160             std::shared_ptr<AVSessionPixelMap> innerPixelMap = metaOut.GetMediaImage();
161             AVSessionUtils::ReadImageFromFile(innerPixelMap, fileName);
162             metaOut.SetMediaImage(innerPixelMap);
163         }
164         SLOGI("update meta data");
165         AVSESSION_TRACE_SYNC_START("AVControllerItem::OnMetaDataChange");
166         callback_->OnMetaDataChange(metaOut);
167     }
168 }
169 
HandleOutputDeviceChange(const OutputDeviceInfo & outputDeviceInfo)170 void AVControllerItem::HandleOutputDeviceChange(const OutputDeviceInfo& outputDeviceInfo)
171 {
172     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
173     callback_->OnOutputDeviceChange(outputDeviceInfo);
174 }
175 
HandleActiveStateChange(bool isActive)176 void AVControllerItem::HandleActiveStateChange(bool isActive)
177 {
178     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
179     callback_->OnActiveStateChange(isActive);
180 }
181 
HandleValidCommandChange(const std::vector<int32_t> & cmds)182 void AVControllerItem::HandleValidCommandChange(const std::vector<int32_t>& cmds)
183 {
184     CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback_ is nullptr");
185     callback_->OnValidCommandChange(cmds);
186 }
187 
GetPid() const188 pid_t AVControllerItem::GetPid() const
189 {
190     return pid_;
191 }
192 
HasSession(const std::string & sessionId)193 bool AVControllerItem::HasSession(const std::string& sessionId)
194 {
195     return sessionId_ == sessionId;
196 }
197 
SetServiceCallbackForRelease(const std::function<void (AVControllerItem &)> & callback)198 void AVControllerItem::SetServiceCallbackForRelease(const std::function<void(AVControllerItem&)>& callback)
199 {
200     serviceCallback_ = callback;
201 }
202 } // OHOS::AVSession