1 /*
2  * Copyright (c) 2023 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 "avcast_controller_callback_proxy.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
AVCastControllerCallbackProxy(const sptr<IRemoteObject> & impl)20 AVCastControllerCallbackProxy::AVCastControllerCallbackProxy(const sptr<IRemoteObject>& impl)
21     : IRemoteProxy<IAVCastControllerCallback>(impl)
22 {
23     SLOGD("construct");
24 }
25 
OnCastPlaybackStateChange(const AVPlaybackState & state)26 void AVCastControllerCallbackProxy::OnCastPlaybackStateChange(const AVPlaybackState& state)
27 {
28     SLOGI("OnCastPlaybackStateChange in proxy for state %{public}d", state.GetState());
29     MessageParcel parcel;
30     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
31     CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&state), "write PlaybackState failed");
32 
33     MessageParcel reply;
34     MessageOption option = { MessageOption::TF_ASYNC };
35     auto remote = Remote();
36     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
37     SLOGI("OnCastPlaybackStateChange in proxy to send request for state %{public}d", state.GetState());
38     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_CAST_PLAYBACK_STATE_CHANGE,
39         parcel, reply, option) == 0,
40         "send request failed");
41     SLOGI("OnCastPlaybackStateChange done in proxy for state %{public}d", state.GetState());
42 }
43 
OnMediaItemChange(const AVQueueItem & avQueueItem)44 void AVCastControllerCallbackProxy::OnMediaItemChange(const AVQueueItem& avQueueItem)
45 {
46     SLOGI("OnMediaItemChange in proxy");
47     MessageParcel parcel;
48     parcel.SetMaxCapacity(defaultIpcCapacity);
49     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
50     CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&avQueueItem), "Write avQueueItem failed");
51 
52     MessageParcel reply;
53     MessageOption option = { MessageOption::TF_ASYNC };
54     auto remote = Remote();
55     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
56     SLOGI("OnMediaItemChange in proxy to send request");
57     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_MEDIA_ITEM_CHANGE, parcel, reply, option) == 0,
58         "send request failed");
59     SLOGI("OnMediaItemChange in proxy done");
60 }
61 
OnPlayNext()62 void AVCastControllerCallbackProxy::OnPlayNext()
63 {
64     MessageParcel parcel;
65     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
66 
67     MessageParcel reply;
68     MessageOption option = { MessageOption::TF_ASYNC };
69     auto remote = Remote();
70     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
71     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_PLAY_NEXT, parcel, reply, option) == 0,
72         "send request failed");
73 }
74 
OnPlayPrevious()75 void AVCastControllerCallbackProxy::OnPlayPrevious()
76 {
77     MessageParcel parcel;
78     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
79 
80     MessageParcel reply;
81     MessageOption option = { MessageOption::TF_ASYNC };
82     auto remote = Remote();
83     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
84     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_PLAY_PREVIOUS, parcel, reply, option) == 0,
85         "send request failed");
86 }
87 
OnSeekDone(const int32_t seekNumber)88 void AVCastControllerCallbackProxy::OnSeekDone(const int32_t seekNumber)
89 {
90     MessageParcel parcel;
91     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
92     CHECK_AND_RETURN_LOG(parcel.WriteInt32(seekNumber), "write seekNumber failed");
93 
94     MessageParcel reply;
95     MessageOption option = { MessageOption::TF_ASYNC };
96     auto remote = Remote();
97     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
98     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_SEEK_DONE, parcel, reply, option) == 0,
99         "send request failed");
100 }
101 
OnVideoSizeChange(const int32_t width,const int32_t height)102 void AVCastControllerCallbackProxy::OnVideoSizeChange(const int32_t width, const int32_t height)
103 {
104     MessageParcel parcel;
105     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
106     CHECK_AND_RETURN_LOG(parcel.WriteInt32(width), "write width failed");
107     CHECK_AND_RETURN_LOG(parcel.WriteInt32(height), "write height failed");
108 
109     MessageParcel reply;
110     MessageOption option = { MessageOption::TF_ASYNC };
111     auto remote = Remote();
112     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
113     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_VIDEO_SIZE_CHANGE, parcel, reply, option) == 0,
114         "send request failed");
115 }
116 
OnPlayerError(const int32_t errorCode,const std::string & errorMsg)117 void AVCastControllerCallbackProxy::OnPlayerError(const int32_t errorCode, const std::string& errorMsg)
118 {
119     MessageParcel parcel;
120     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
121     CHECK_AND_RETURN_LOG(parcel.WriteInt32(errorCode), "write time failed");
122     CHECK_AND_RETURN_LOG(parcel.WriteString(errorMsg), "write time failed");
123 
124     MessageParcel reply;
125     MessageOption option = { MessageOption::TF_ASYNC };
126     auto remote = Remote();
127     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
128     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_ERROR, parcel, reply, option) == 0,
129         "send request failed");
130 }
131 
OnEndOfStream(const int32_t isLooping)132 void AVCastControllerCallbackProxy::OnEndOfStream(const int32_t isLooping)
133 {
134     MessageParcel parcel;
135     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
136     CHECK_AND_RETURN_LOG(parcel.WriteInt32(isLooping), "write isLooping failed");
137 
138     MessageParcel reply;
139     MessageOption option = { MessageOption::TF_ASYNC };
140     auto remote = Remote();
141     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
142     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_END_OF_STREAM, parcel, reply, option) == 0,
143         "send request failed");
144 }
145 
OnPlayRequest(const AVQueueItem & avQueueItem)146 void AVCastControllerCallbackProxy::OnPlayRequest(const AVQueueItem& avQueueItem)
147 {
148     SLOGI("OnPlayRequest in proxy");
149     MessageParcel parcel;
150     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
151     CHECK_AND_RETURN_LOG(parcel.WriteParcelable(&avQueueItem), "Write avQueueItem failed");
152 
153     MessageParcel reply;
154     MessageOption option = { MessageOption::TF_ASYNC };
155     auto remote = Remote();
156     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
157     SLOGI("OnPlayRequest in proxy to send request");
158     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_PLAY_REQUEST, parcel, reply, option) == 0,
159         "send request failed");
160     SLOGI("OnPlayRequest in proxy done");
161 }
162 
OnKeyRequest(const std::string & assetId,const std::vector<uint8_t> & keyRequestData)163 void AVCastControllerCallbackProxy::OnKeyRequest(const std::string &assetId, const std::vector<uint8_t> &keyRequestData)
164 {
165     SLOGI("OnKeyRequest in proxy");
166     MessageParcel parcel;
167     MessageParcel reply;
168     MessageOption option = { MessageOption::TF_ASYNC };
169     auto len = keyRequestData.size();
170     if (len > parcel.GetDataCapacity()) {
171         SLOGI("OnKeyRequest SetDataCapacity totalSize: %lu", len);
172         parcel.SetMaxCapacity(len + len);
173         parcel.SetDataCapacity(len);
174     }
175     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
176     CHECK_AND_RETURN_LOG(parcel.WriteString(assetId), "write assetId failed");
177     CHECK_AND_RETURN_LOG(parcel.WriteInt32(keyRequestData.size()), "Write keyRequestData failed");
178     if (keyRequestData.size() != 0) {
179         if (!parcel.WriteBuffer(keyRequestData.data(), keyRequestData.size())) {
180             SLOGE("AVCastControllerCallbackProxy OnKeyRequest write keyRequestData failed");
181             return;
182         }
183     }
184     auto remote = Remote();
185     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
186     SLOGI("OnKeyRequest in proxy to send request");
187     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_KEY_REQUEST, parcel, reply, option) == 0,
188         "send request failed");
189     SLOGI("OnKeyRequest in proxy done");
190 }
191 
OnCastValidCommandChanged(const std::vector<int32_t> & cmds)192 void AVCastControllerCallbackProxy::OnCastValidCommandChanged(const std::vector<int32_t> &cmds)
193 {
194     SLOGI("OnCastValidCommandChanged in proxy");
195     MessageParcel parcel;
196     CHECK_AND_RETURN_LOG(parcel.WriteInterfaceToken(GetDescriptor()), "write interface token failed");
197     CHECK_AND_RETURN_LOG(parcel.WriteInt32Vector(cmds), "Write vector failed");
198 
199     MessageParcel reply;
200     MessageOption option = { MessageOption::TF_ASYNC };
201     auto remote = Remote();
202     CHECK_AND_RETURN_LOG(remote != nullptr, "get remote service failed");
203     SLOGI("OnCastValidCommandChanged in proxy to send request");
204     CHECK_AND_RETURN_LOG(remote->SendRequest(CAST_CONTROLLER_CMD_ON_VALID_COMMAND_CHANGED, parcel, reply, option) == 0,
205         "send request failed");
206     SLOGI("OnCastValidCommandChanged in proxy done");
207 }
208 } // namespace OHOS::AVSession
209