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 "avcontrol_command.h"
17 #include "avplayback_state.h"
18 #include "avsession_errors.h"
19 #include "avsession_log.h"
20
21 namespace OHOS::AVSession {
AVControlCommand()22 AVControlCommand::AVControlCommand()
23 : cmd_(SESSION_CMD_INVALID)
24 {
25 }
26
~AVControlCommand()27 AVControlCommand::~AVControlCommand()
28 {
29 }
30
Unmarshalling(Parcel & data)31 AVControlCommand *AVControlCommand::Unmarshalling(Parcel& data)
32 {
33 auto result = new (std::nothrow) AVControlCommand();
34 if (result != nullptr) {
35 int32_t cmd = data.ReadInt32();
36 result->SetCommand(cmd);
37 switch (cmd) {
38 case SESSION_CMD_SEEK:
39 result->SetSeekTime(data.ReadInt64());
40 break;
41 case SESSION_CMD_SET_SPEED:
42 result->SetSpeed(data.ReadDouble());
43 break;
44 case SESSION_CMD_SET_LOOP_MODE:
45 result->SetLoopMode(data.ReadInt32());
46 break;
47 case SESSION_CMD_TOGGLE_FAVORITE:
48 result->SetAssetId(data.ReadString());
49 break;
50 default:
51 break;
52 }
53 }
54 return result;
55 }
56
Marshalling(Parcel & parcel) const57 bool AVControlCommand::Marshalling(Parcel& parcel) const
58 {
59 if (!parcel.WriteInt32(cmd_)) {
60 return false;
61 }
62 switch (cmd_) {
63 case SESSION_CMD_SEEK:
64 CHECK_AND_RETURN_RET_LOG(std::holds_alternative<int64_t>(param_)
65 && parcel.WriteInt64(std::get<int64_t>(param_)), false, "write seek time failed");
66 break;
67 case SESSION_CMD_SET_SPEED:
68 CHECK_AND_RETURN_RET_LOG(std::holds_alternative<double>(param_)
69 && parcel.WriteDouble(std::get<double>(param_)), false, "write speed failed");
70 break;
71 case SESSION_CMD_SET_LOOP_MODE:
72 CHECK_AND_RETURN_RET_LOG(std::holds_alternative<int32_t>(param_)
73 && parcel.WriteInt32(std::get<int32_t>(param_)), false, "write loop mode failed");
74 break;
75 case SESSION_CMD_TOGGLE_FAVORITE:
76 CHECK_AND_RETURN_RET_LOG(std::holds_alternative<std::string>(param_)
77 && parcel.WriteString(std::get<std::string>(param_)), false, "write toggle favorite failed");
78 break;
79 default:
80 break;
81 }
82 return true;
83 }
84
IsValid() const85 bool AVControlCommand::IsValid() const
86 {
87 return cmd_ > SESSION_CMD_INVALID && cmd_ < SESSION_CMD_MAX;
88 }
89
SetCommand(int32_t cmd)90 int32_t AVControlCommand::SetCommand(int32_t cmd)
91 {
92 if (cmd <= SESSION_CMD_INVALID || cmd >= SESSION_CMD_MAX) {
93 return ERR_INVALID_PARAM;
94 }
95 cmd_ = cmd;
96 return AVSESSION_SUCCESS;
97 }
98
GetCommand() const99 int32_t AVControlCommand::GetCommand() const
100 {
101 return cmd_;
102 }
103
SetSpeed(double speed)104 int32_t AVControlCommand::SetSpeed(double speed)
105 {
106 if (speed <= 0) {
107 return ERR_INVALID_PARAM;
108 }
109 param_ = speed;
110 return AVSESSION_SUCCESS;
111 }
112
GetSpeed(double & speed) const113 int32_t AVControlCommand::GetSpeed(double& speed) const
114 {
115 if (!std::holds_alternative<double>(param_)) {
116 return AVSESSION_ERROR;
117 }
118 speed = std::get<double>(param_);
119 return AVSESSION_SUCCESS;
120 }
121
SetSeekTime(int64_t time)122 int32_t AVControlCommand::SetSeekTime(int64_t time)
123 {
124 if (time < 0) {
125 return ERR_INVALID_PARAM;
126 }
127 param_ = time;
128 return AVSESSION_SUCCESS;
129 }
130
GetSeekTime(int64_t & time) const131 int32_t AVControlCommand::GetSeekTime(int64_t& time) const
132 {
133 if (!std::holds_alternative<int64_t>(param_)) {
134 return AVSESSION_ERROR;
135 }
136 time = std::get<int64_t>(param_);
137 return AVSESSION_SUCCESS;
138 }
139
SetLoopMode(int32_t mode)140 int32_t AVControlCommand::SetLoopMode(int32_t mode)
141 {
142 if (mode < AVPlaybackState::LOOP_MODE_SEQUENCE || mode > AVPlaybackState::LOOP_MODE_SHUFFLE) {
143 return ERR_INVALID_PARAM;
144 }
145 param_ = mode;
146 return AVSESSION_SUCCESS;
147 }
148
GetLoopMode(int32_t & mode) const149 int32_t AVControlCommand::GetLoopMode(int32_t& mode) const
150 {
151 if (!std::holds_alternative<int32_t>(param_)) {
152 return AVSESSION_ERROR;
153 }
154 mode = std::get<int32_t>(param_);
155 return AVSESSION_SUCCESS;
156 }
157
SetAssetId(const std::string & assetId)158 int32_t AVControlCommand::SetAssetId(const std::string& assetId)
159 {
160 if (assetId.empty()) {
161 return ERR_INVALID_PARAM;
162 }
163 param_ = assetId;
164 return AVSESSION_SUCCESS;
165 }
166
GetAssetId(std::string & assetId) const167 int32_t AVControlCommand::GetAssetId(std::string& assetId) const
168 {
169 if (!std::holds_alternative<std::string>(param_)) {
170 return AVSESSION_ERROR;
171 }
172 assetId = std::get<std::string>(param_);
173 return AVSESSION_SUCCESS;
174 }
175 } // OHOS::AVSession