1 /*
2  * Copyright (c) 2023-2024 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 #include "avcast_controller_callback_stub.h"
16 #include "avsession_errors.h"
17 #include "avsession_log.h"
18 #include "avsession_trace.h"
19 
20 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)21 bool AVCastControllerCallbackStub::CheckInterfaceToken(MessageParcel& data)
22 {
23     auto localDescriptor = IAVCastControllerCallback::GetDescriptor();
24     auto remoteDescriptor = data.ReadInterfaceToken();
25     if (remoteDescriptor != localDescriptor) {
26         SLOGI("interface token is not equal");
27         return false;
28     }
29     return true;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t AVCastControllerCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
33     MessageOption &option)
34 {
35     if (!CheckInterfaceToken(data)) {
36         return AVSESSION_ERROR;
37     }
38     if (code >= static_cast<uint32_t>(IAVCastControllerCallback::CAST_CONTROLLER_CMD_ON_CAST_PLAYBACK_STATE_CHANGE)
39         && code < static_cast<uint32_t>(IAVCastControllerCallback::CAST_CONTROLLER_CMD_MAX)) {
40         return handlers[code](data, reply);
41     }
42     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
43 }
44 
HandleOnStateChange(MessageParcel & data,MessageParcel & reply)45 int32_t AVCastControllerCallbackStub::HandleOnStateChange(MessageParcel& data, MessageParcel& reply)
46 {
47     sptr<AVPlaybackState> state = data.ReadParcelable<AVPlaybackState>();
48 
49     CHECK_AND_RETURN_RET_LOG(state != nullptr, ERR_NONE, "read PlaybackState failed");
50     AVSESSION_TRACE_SYNC_START("AVCastControllerCallbackStub::HandleOnStateChange");
51     SLOGI("HandleOnStateChange with state: %{public}d", state->GetState());
52     OnCastPlaybackStateChange(*state);
53     return ERR_NONE;
54 }
55 
HandleOnMediaItemChange(MessageParcel & data,MessageParcel & reply)56 int32_t AVCastControllerCallbackStub::HandleOnMediaItemChange(MessageParcel& data, MessageParcel& reply)
57 {
58     AVSESSION_TRACE_SYNC_START("AVCastControllerCallbackStub::HandleOnMediaItemChange");
59     sptr<AVQueueItem> item = data.ReadParcelable<AVQueueItem>();
60     CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
61     SLOGI("HandleOnMediaItemChange in");
62     OnMediaItemChange(*item);
63     return ERR_NONE;
64 }
65 
HandleOnPlayNext(MessageParcel & data,MessageParcel & reply)66 int32_t AVCastControllerCallbackStub::HandleOnPlayNext(MessageParcel& data, MessageParcel& reply)
67 {
68     OnPlayNext();
69     return ERR_NONE;
70 }
71 
HandleOnPlayPrevious(MessageParcel & data,MessageParcel & reply)72 int32_t AVCastControllerCallbackStub::HandleOnPlayPrevious(MessageParcel& data, MessageParcel& reply)
73 {
74     OnPlayPrevious();
75     return ERR_NONE;
76 }
77 
HandleOnSeekDone(MessageParcel & data,MessageParcel & reply)78 int32_t AVCastControllerCallbackStub::HandleOnSeekDone(MessageParcel& data, MessageParcel& reply)
79 {
80     int32_t seekNumber;
81     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(seekNumber), ERR_NONE, "read seekNumber failed");
82     OnSeekDone(seekNumber);
83     return ERR_NONE;
84 }
85 
HandleOnVideoSizeChange(MessageParcel & data,MessageParcel & reply)86 int32_t AVCastControllerCallbackStub::HandleOnVideoSizeChange(MessageParcel& data, MessageParcel& reply)
87 {
88     int32_t width;
89     int32_t height;
90     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(width), ERR_NONE, "read time failed");
91     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(height), ERR_NONE, "read time failed");
92     OnVideoSizeChange(width, height);
93     return ERR_NONE;
94 }
95 
HandleOnPlayerError(MessageParcel & data,MessageParcel & reply)96 int32_t AVCastControllerCallbackStub::HandleOnPlayerError(MessageParcel& data, MessageParcel& reply)
97 {
98     int32_t errorCode;
99     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(errorCode), ERR_NONE, "read time failed");
100     std::string errorMsg;
101     CHECK_AND_RETURN_RET_LOG(data.ReadString(errorMsg), ERR_NONE, "read time failed");
102     OnPlayerError(errorCode, errorMsg);
103     return ERR_NONE;
104 }
105 
HandleOnEndOfStream(MessageParcel & data,MessageParcel & reply)106 int32_t AVCastControllerCallbackStub::HandleOnEndOfStream(MessageParcel& data, MessageParcel& reply)
107 {
108     int32_t isLooping;
109     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(isLooping), ERR_NONE, "read isLooping failed");
110     OnEndOfStream(isLooping);
111     return ERR_NONE;
112 }
113 
HandleOnPlayRequest(MessageParcel & data,MessageParcel & reply)114 int32_t AVCastControllerCallbackStub::HandleOnPlayRequest(MessageParcel& data, MessageParcel& reply)
115 {
116     AVSESSION_TRACE_SYNC_START("AVCastControllerCallbackStub::HandleOnPlayRequest");
117     sptr<AVQueueItem> item = data.ReadParcelable<AVQueueItem>();
118     CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable preload AVQueueItem failed");
119     SLOGI("HandleOnPlayRequest in");
120     OnPlayRequest(*item);
121     return ERR_NONE;
122 }
123 
HandleOnKeyRequest(MessageParcel & data,MessageParcel & reply)124 int32_t AVCastControllerCallbackStub::HandleOnKeyRequest(MessageParcel& data, MessageParcel& reply)
125 {
126     std::string assetId = data.ReadString();
127     std::vector<uint8_t> request;
128     uint32_t requestSize = static_cast<uint32_t>(data.ReadInt32());
129     uint32_t requestMaxLen = 8 * 1024 * 1024;
130     CHECK_AND_RETURN_RET_LOG(requestSize < requestMaxLen, AVSESSION_ERROR,
131         "The size of request is too large.");
132     if (requestSize != 0) {
133         const uint8_t *requestBuf = static_cast<const uint8_t *>(data.ReadBuffer(requestSize));
134         if (requestBuf == nullptr) {
135             SLOGE("AVCastControllerCallbackStub::HandleOnKeyRequest read request failed");
136             return IPC_STUB_WRITE_PARCEL_ERR;
137         }
138         request.assign(requestBuf, requestBuf + requestSize);
139     }
140     OnKeyRequest(assetId, request);
141     SLOGI("HandleOnKeyRequest out");
142     return ERR_NONE;
143 }
144 
HandleOnCastValidCommandChanged(MessageParcel & data,MessageParcel & reply)145 int32_t AVCastControllerCallbackStub::HandleOnCastValidCommandChanged(MessageParcel& data, MessageParcel& reply)
146 {
147     std::vector<int32_t> cmds;
148     CHECK_AND_RETURN_RET_LOG(data.ReadInt32Vector(&cmds), ERR_NONE, "read int32 vector failed");
149     SLOGI("HandleOnCastValidCommandChanged cmd size:%{public}zd", cmds.size());
150     OnCastValidCommandChanged(cmds);
151     return ERR_NONE;
152 }
153 } // namespace OHOS::AVSession
154