• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "avcontroller_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 AVControllerCallbackStub::CheckInterfaceToken(MessageParcel& data)
22 {
23     auto localDescriptor = IAVControllerCallback::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 AVControllerCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
33     MessageOption &option)
34 {
35     if (!CheckInterfaceToken(data)) {
36         return AVSESSION_ERROR;
37     }
38     if (code < CONTROLLER_CMD_MAX) {
39         return (this->*handlers[code])(data, reply);
40     }
41     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42 }
43 
HandleOnSessionDestroy(MessageParcel & data,MessageParcel & reply)44 int32_t AVControllerCallbackStub::HandleOnSessionDestroy(MessageParcel& data, MessageParcel& reply)
45 {
46     OnSessionDestroy();
47     return ERR_NONE;
48 }
49 
HandleOnPlaybackStateChange(MessageParcel & data,MessageParcel & reply)50 int32_t AVControllerCallbackStub::HandleOnPlaybackStateChange(MessageParcel& data, MessageParcel& reply)
51 {
52     sptr<AVPlaybackState> state = data.ReadParcelable<AVPlaybackState>();
53 
54     CHECK_AND_RETURN_RET_LOG(state != nullptr, ERR_NONE, "read PlaybackState failed");
55     AVSESSION_TRACE_SYNC_START("AVControllerCallbackStub::OnPlaybackStateChange");
56     OnPlaybackStateChange(*state);
57     return ERR_NONE;
58 }
59 
HandleOnMetadataChange(MessageParcel & data,MessageParcel & reply)60 int32_t AVControllerCallbackStub::HandleOnMetadataChange(MessageParcel& data, MessageParcel& reply)
61 {
62     sptr<AVMetaData> metaData = data.ReadParcelable<AVMetaData>();
63 
64     CHECK_AND_RETURN_RET_LOG(metaData != nullptr, ERR_NONE, "read MetaData failed");
65     AVSESSION_TRACE_SYNC_START("AVControllerCallbackStub::OnMetaDataChange");
66     OnMetaDataChange(*metaData);
67     return ERR_NONE;
68 }
69 
HandleOnActiveStateChange(MessageParcel & data,MessageParcel & reply)70 int32_t AVControllerCallbackStub::HandleOnActiveStateChange(MessageParcel& data, MessageParcel& reply)
71 {
72     bool isActive = false;
73     CHECK_AND_RETURN_RET_LOG(data.ReadBool(isActive), ERR_NONE, "read isActive failed");
74     OnActiveStateChange(isActive);
75     return ERR_NONE;
76 }
77 
HandleOnValidCommandChange(MessageParcel & data,MessageParcel & reply)78 int32_t AVControllerCallbackStub::HandleOnValidCommandChange(MessageParcel& data, MessageParcel& reply)
79 {
80     std::vector<int32_t> cmds;
81     CHECK_AND_RETURN_RET_LOG(data.ReadInt32Vector(&cmds), ERR_NONE, "read int32 vector failed");
82     OnValidCommandChange(cmds);
83     return ERR_NONE;
84 }
85 
HandleOnOutputDeviceChange(MessageParcel & data,MessageParcel & reply)86 int32_t AVControllerCallbackStub::HandleOnOutputDeviceChange(MessageParcel& data, MessageParcel& reply)
87 {
88     int32_t connectionState;
89     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(connectionState), false, "write deviceInfoSize failed");
90     OutputDeviceInfo outputDeviceInfo;
91     int32_t deviceInfoSize;
92     CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfoSize), false, "write deviceInfoSize failed");
93     int32_t maxDeviceInfoSize = 1000; // A maximum of 1000 device change events can be processed at a time
94     CHECK_AND_RETURN_RET_LOG((deviceInfoSize >= 0) && (deviceInfoSize < maxDeviceInfoSize),
95         false, "deviceInfoSize is illegal");
96     for (int i = 0; i < deviceInfoSize; i++) {
97         DeviceInfo deviceInfo;
98         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.castCategory_), false, "Read castCategory failed");
99         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceId_), false, "Read deviceId failed");
100         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.deviceName_), false, "Read deviceName failed");
101         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.deviceType_), false, "Read deviceType failed");
102         CHECK_AND_RETURN_RET_LOG(data.ReadString(deviceInfo.ipAddress_), false, "Read ipAddress failed");
103         CHECK_AND_RETURN_RET_LOG(data.ReadInt32(deviceInfo.providerId_), false, "Read providerId failed");
104         outputDeviceInfo.deviceInfos_.emplace_back(deviceInfo);
105     }
106 
107     OnOutputDeviceChange(connectionState, outputDeviceInfo);
108     return ERR_NONE;
109 }
110 
HandleOnSessionEventChange(MessageParcel & data,MessageParcel & reply)111 int32_t AVControllerCallbackStub::HandleOnSessionEventChange(MessageParcel& data, MessageParcel& reply)
112 {
113     auto event = data.ReadString();
114     sptr want = data.ReadParcelable<AAFwk::WantParams>();
115 
116     CHECK_AND_RETURN_RET_LOG(want != nullptr, ERR_NONE, "read want args failed");
117     AVSESSION_TRACE_SYNC_START("AVControllerCallbackStub::OnSessionEventChange");
118     OnSessionEventChange(event, *want);
119     return ERR_NONE;
120 }
121 
HandleOnQueueItemsChange(MessageParcel & data,MessageParcel & reply)122 int32_t AVControllerCallbackStub::HandleOnQueueItemsChange(MessageParcel& data, MessageParcel& reply)
123 {
124     std::vector<AVQueueItem> items_;
125     int32_t maxItemNumber = 1000; // A maximum of 1000 queue items can be processed at a time
126     int32_t itemNum = data.ReadInt32();
127     CHECK_AND_RETURN_RET_LOG((itemNum >= 0) && (itemNum < maxItemNumber), ERR_NONE, "read int32 itemNum failed");
128     for (int32_t i = 0; i < itemNum; i++) {
129         AVQueueItem *item = data.ReadParcelable<AVQueueItem>();
130         CHECK_AND_RETURN_RET_LOG(item != nullptr, ERR_UNMARSHALLING, "read parcelable AVQueueItem failed");
131         items_.emplace_back(*item);
132         delete item;
133     }
134     AVSESSION_TRACE_SYNC_START("AVControllerCallbackStub::OnQueueItemsChange");
135     OnQueueItemsChange(items_);
136     return ERR_NONE;
137 }
138 
HandleOnQueueTitleChange(MessageParcel & data,MessageParcel & reply)139 int32_t AVControllerCallbackStub::HandleOnQueueTitleChange(MessageParcel& data, MessageParcel& reply)
140 {
141     auto title = data.ReadString();
142     AVSESSION_TRACE_SYNC_START("AVControllerCallbackStub::OnQueueTitleChange");
143     OnQueueTitleChange(title);
144     return ERR_NONE;
145 }
146 
HandleOnExtrasChange(MessageParcel & data,MessageParcel & reply)147 int32_t AVControllerCallbackStub::HandleOnExtrasChange(MessageParcel& data, MessageParcel& reply)
148 {
149     sptr extras = data.ReadParcelable<AAFwk::WantParams>();
150     CHECK_AND_RETURN_RET_LOG(extras != nullptr, ERR_NONE, "read extras failed");
151     AVSESSION_TRACE_SYNC_START("AVControllerCallbackStub::OnExtrasChange");
152     OnExtrasChange(*extras);
153     return ERR_NONE;
154 }
155 } // namespace OHOS::AVSession
156