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 "avsession_callback_stub.h"
17 #include "avsession_errors.h"
18 #include "iavsession_callback.h"
19 #include "key_event.h"
20 #include "avsession_log.h"
21 #include "avsession_trace.h"
22
23 namespace OHOS::AVSession {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int32_t AVSessionCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
25 MessageOption& option)
26 {
27 if (!CheckInterfaceToken(data)) {
28 return AVSESSION_ERROR;
29 }
30 if (code < SESSION_CALLBACK_MAX) {
31 return (this->*handlers[code])(data, reply);
32 }
33 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
34 }
35
CheckInterfaceToken(MessageParcel & data)36 bool AVSessionCallbackStub::CheckInterfaceToken(MessageParcel& data)
37 {
38 auto localDescriptor = IAVSessionCallback::GetDescriptor();
39 auto remoteDescriptor = data.ReadInterfaceToken();
40 if (remoteDescriptor != localDescriptor) {
41 SLOGI("interface token is not equal");
42 return false;
43 }
44 return true;
45 }
46
HandleOnPlay(MessageParcel & data,MessageParcel & reply)47 int32_t AVSessionCallbackStub::HandleOnPlay(MessageParcel& data, MessageParcel& reply)
48 {
49 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlay");
50 OnPlay();
51 return ERR_NONE;
52 }
53
HandleOnPause(MessageParcel & data,MessageParcel & reply)54 int32_t AVSessionCallbackStub::HandleOnPause(MessageParcel& data, MessageParcel& reply)
55 {
56 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPause");
57 OnPause();
58 return ERR_NONE;
59 }
60
HandleOnStop(MessageParcel & data,MessageParcel & reply)61 int32_t AVSessionCallbackStub::HandleOnStop(MessageParcel& data, MessageParcel& reply)
62 {
63 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnStop");
64 OnStop();
65 return ERR_NONE;
66 }
67
HandleOnPlayNext(MessageParcel & data,MessageParcel & reply)68 int32_t AVSessionCallbackStub::HandleOnPlayNext(MessageParcel& data, MessageParcel& reply)
69 {
70 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayNext");
71 OnPlayNext();
72 return ERR_NONE;
73 }
74
HandleOnPlayPrevious(MessageParcel & data,MessageParcel & reply)75 int32_t AVSessionCallbackStub::HandleOnPlayPrevious(MessageParcel& data, MessageParcel& reply)
76 {
77 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnPlayPrevious");
78 OnPlayPrevious();
79 return ERR_NONE;
80 }
81
HandleOnFastForward(MessageParcel & data,MessageParcel & reply)82 int32_t AVSessionCallbackStub::HandleOnFastForward(MessageParcel& data, MessageParcel& reply)
83 {
84 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnFastForward");
85 OnFastForward();
86 return ERR_NONE;
87 }
88
HandleOnRewind(MessageParcel & data,MessageParcel & reply)89 int32_t AVSessionCallbackStub::HandleOnRewind(MessageParcel& data, MessageParcel& reply)
90 {
91 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnRewind");
92 OnRewind();
93 return ERR_NONE;
94 }
95
HandleOnSeek(MessageParcel & data,MessageParcel & reply)96 int32_t AVSessionCallbackStub::HandleOnSeek(MessageParcel& data, MessageParcel& reply)
97 {
98 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSeek");
99 int32_t time = -1;
100 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(time), ERR_NONE, "read time failed");
101 OnSeek(time);
102 return ERR_NONE;
103 }
104
HandleOnSetSpeed(MessageParcel & data,MessageParcel & reply)105 int32_t AVSessionCallbackStub::HandleOnSetSpeed(MessageParcel& data, MessageParcel& reply)
106 {
107 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSetSpeed");
108 double speed = 0.0;
109 CHECK_AND_RETURN_RET_LOG(data.ReadDouble(speed), ERR_NONE, "read speed failed");
110 OnSetSpeed(speed);
111 return ERR_NONE;
112 }
113
HandleOnSetLoopMode(MessageParcel & data,MessageParcel & reply)114 int32_t AVSessionCallbackStub::HandleOnSetLoopMode(MessageParcel& data, MessageParcel& reply)
115 {
116 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnSetLoopMode");
117 int32_t loopMode = -1;
118 CHECK_AND_RETURN_RET_LOG(data.ReadInt32(loopMode), ERR_NONE, "read loopMode failed");
119 OnSetLoopMode(loopMode);
120 return ERR_NONE;
121 }
122
HandleOnToggleFavorite(MessageParcel & data,MessageParcel & reply)123 int32_t AVSessionCallbackStub::HandleOnToggleFavorite(MessageParcel& data, MessageParcel& reply)
124 {
125 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnToggleFavorite");
126 std::string mediaId;
127 CHECK_AND_RETURN_RET_LOG(data.ReadString(mediaId), ERR_NONE, "read mediaId failed");
128 OnToggleFavorite(mediaId);
129 return ERR_NONE;
130 }
131
HandleOnMediaKeyEvent(MessageParcel & data,MessageParcel & reply)132 int32_t AVSessionCallbackStub::HandleOnMediaKeyEvent(MessageParcel& data, MessageParcel& reply)
133 {
134 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnMediaKeyEvent");
135 auto keyEvent = MMI::KeyEvent::Create();
136 CHECK_AND_RETURN_RET_LOG((*keyEvent).ReadFromParcel(data), ERR_NONE, "read keyEvent failed");
137 OnMediaKeyEvent(*keyEvent);
138 return ERR_NONE;
139 }
140
HandOnOutputDeviceChange(MessageParcel & data,MessageParcel & reply)141 int32_t AVSessionCallbackStub::HandOnOutputDeviceChange(MessageParcel& data, MessageParcel& reply)
142 {
143 AVSESSION_TRACE_SYNC_START("AVSessionCallbackStub::OnOutputDeviceChange");
144 OutputDeviceInfo outputDeviceInfo;
145 CHECK_AND_RETURN_RET_LOG(data.ReadBool(outputDeviceInfo.isRemote_), ERR_NONE, "read isRemote_ failed");
146 CHECK_AND_RETURN_RET_LOG(data.ReadStringVector(&outputDeviceInfo.deviceIds_), ERR_NONE, "read id failed");
147 CHECK_AND_RETURN_RET_LOG(data.ReadStringVector(&outputDeviceInfo.deviceNames_), ERR_NONE, "read name failed");
148 OnOutputDeviceChange(outputDeviceInfo);
149 return ERR_NONE;
150 }
151 } // namespace OHOS::AVSession