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 "avmeta_data.h"
17 #include "avsession_log.h"
18
19 namespace OHOS::AVSession {
Marshalling(Parcel & parcel) const20 bool AVMetaData::Marshalling(Parcel& parcel) const
21 {
22 return parcel.WriteString(metaMask_.to_string()) &&
23 parcel.WriteString(assetId_) &&
24 parcel.WriteString(title_) &&
25 parcel.WriteString(artist_) &&
26 parcel.WriteString(author_) &&
27 parcel.WriteString(album_) &&
28 parcel.WriteString(writer_) &&
29 parcel.WriteString(composer_) &&
30 parcel.WriteInt64(duration_) &&
31 parcel.WriteString(mediaImageUri_) &&
32 parcel.WriteDouble(publishDate_) &&
33 parcel.WriteString(subTitle_) &&
34 parcel.WriteString(description_) &&
35 parcel.WriteString(lyric_) &&
36 parcel.WriteString(previousAssetId_) &&
37 parcel.WriteString(nextAssetId_) &&
38 parcel.WriteParcelable(mediaImage_.get());
39 }
40
Unmarshalling(Parcel & data)41 AVMetaData *AVMetaData::Unmarshalling(Parcel& data)
42 {
43 std::string mask;
44 CHECK_AND_RETURN_RET_LOG(data.ReadString(mask) && mask.length() == META_KEY_MAX, nullptr, "mask not valid");
45 CHECK_AND_RETURN_RET_LOG(mask.find_first_not_of("01") == std::string::npos, nullptr, "mask string not 0 or 1");
46
47 auto *result = new (std::nothrow) AVMetaData();
48 CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new AVMetaData failed");
49 result->metaMask_ = MetaMaskType(mask);
50 if (!data.ReadString(result->assetId_) ||
51 !data.ReadString(result->title_) ||
52 !data.ReadString(result->artist_) ||
53 !data.ReadString(result->author_) ||
54 !data.ReadString(result->album_) ||
55 !data.ReadString(result->writer_) ||
56 !data.ReadString(result->composer_) ||
57 !data.ReadInt64(result->duration_) ||
58 !data.ReadString(result->mediaImageUri_) ||
59 !data.ReadDouble(result->publishDate_) ||
60 !data.ReadString(result->subTitle_) ||
61 !data.ReadString(result->description_) ||
62 !data.ReadString(result->lyric_) ||
63 !data.ReadString(result->previousAssetId_) ||
64 !data.ReadString(result->nextAssetId_)) {
65 SLOGE("read AVMetaData failed");
66 delete result;
67 return nullptr;
68 }
69 result->mediaImage_ = std::shared_ptr<AVSessionPixelMap>(data.ReadParcelable<AVSessionPixelMap>());
70 if (result->metaMask_.test(META_KEY_MEDIA_IMAGE) && result->mediaImage_ == nullptr) {
71 SLOGE("read PixelMap failed");
72 delete result;
73 return nullptr;
74 }
75 return result;
76 }
77
SetAssetId(const std::string & assetId)78 void AVMetaData::SetAssetId(const std::string& assetId)
79 {
80 assetId_ = assetId;
81 metaMask_.set(META_KEY_ASSET_ID);
82 }
83
GetAssetId() const84 std::string AVMetaData::GetAssetId() const
85 {
86 return assetId_;
87 }
88
SetTitle(const std::string & title)89 void AVMetaData::SetTitle(const std::string& title)
90 {
91 title_ = title;
92 metaMask_.set(META_KEY_TITLE);
93 }
94
GetTitle() const95 std::string AVMetaData::GetTitle() const
96 {
97 return title_;
98 }
99
SetArtist(const std::string & artist)100 void AVMetaData::SetArtist(const std::string& artist)
101 {
102 artist_ = artist;
103 metaMask_.set(META_KEY_ARTIST);
104 }
105
GetArtist() const106 std::string AVMetaData::GetArtist() const
107 {
108 return artist_;
109 }
110
SetAuthor(const std::string & author)111 void AVMetaData::SetAuthor(const std::string& author)
112 {
113 author_ = author;
114 metaMask_.set(META_KEY_AUTHOR);
115 }
116
GetAuthor() const117 std::string AVMetaData::GetAuthor() const
118 {
119 return author_;
120 }
121
SetAlbum(const std::string & album)122 void AVMetaData::SetAlbum(const std::string& album)
123 {
124 album_ = album;
125 metaMask_.set(META_KEY_ALBUM);
126 }
127
GetAlbum() const128 std::string AVMetaData::GetAlbum() const
129 {
130 return album_;
131 }
132
SetWriter(const std::string & writer)133 void AVMetaData::SetWriter(const std::string& writer)
134 {
135 writer_ = writer;
136 metaMask_.set(META_KEY_WRITER);
137 }
138
GetWriter() const139 std::string AVMetaData::GetWriter() const
140 {
141 return writer_;
142 }
143
SetComposer(const std::string & composer)144 void AVMetaData::SetComposer(const std::string& composer)
145 {
146 composer_ = composer;
147 metaMask_.set(META_KEY_COMPOSER);
148 }
149
GetComposer() const150 std::string AVMetaData::GetComposer() const
151 {
152 return composer_;
153 }
154
SetDuration(int64_t duration)155 void AVMetaData::SetDuration(int64_t duration)
156 {
157 if (duration < AVMetaData::DURATION_ALWAYS_PLAY) {
158 SLOGW("invalid duration");
159 }
160 duration_ = duration;
161 metaMask_.set(META_KEY_DURATION);
162 }
163
GetDuration() const164 int64_t AVMetaData::GetDuration() const
165 {
166 return duration_;
167 }
168
SetMediaImage(const std::shared_ptr<AVSessionPixelMap> & mediaImage)169 void AVMetaData::SetMediaImage(const std::shared_ptr<AVSessionPixelMap>& mediaImage)
170 {
171 mediaImage_ = mediaImage;
172 metaMask_.set(META_KEY_MEDIA_IMAGE);
173 }
174
GetMediaImage() const175 std::shared_ptr<AVSessionPixelMap> AVMetaData::GetMediaImage() const
176 {
177 return mediaImage_;
178 }
179
SetMediaImageUri(const std::string & mediaImageUri)180 void AVMetaData::SetMediaImageUri(const std::string& mediaImageUri)
181 {
182 mediaImageUri_ = mediaImageUri;
183 metaMask_.set(META_KEY_MEDIA_IMAGE_URI);
184 }
185
GetMediaImageUri() const186 std::string AVMetaData::GetMediaImageUri() const
187 {
188 return mediaImageUri_;
189 }
190
SetPublishDate(double date)191 void AVMetaData::SetPublishDate(double date)
192 {
193 if (date < 0) {
194 SLOGW("invalid publish date");
195 }
196 publishDate_ = date;
197 metaMask_.set(META_KEY_PUBLISH_DATE);
198 }
199
GetPublishDate() const200 double AVMetaData::GetPublishDate() const
201 {
202 return publishDate_;
203 }
204
SetSubTitle(const std::string & subTitle)205 void AVMetaData::SetSubTitle(const std::string& subTitle)
206 {
207 subTitle_ = subTitle;
208 metaMask_.set(META_KEY_SUBTITLE);
209 }
210
GetSubTitle() const211 std::string AVMetaData::GetSubTitle() const
212 {
213 return subTitle_;
214 }
215
SetDescription(const std::string & description)216 void AVMetaData::SetDescription(const std::string& description)
217 {
218 description_ = description;
219 metaMask_.set(META_KEY_DESCRIPTION);
220 }
221
GetDescription() const222 std::string AVMetaData::GetDescription() const
223 {
224 return description_;
225 }
226
SetLyric(const std::string & lyric)227 void AVMetaData::SetLyric(const std::string& lyric)
228 {
229 lyric_ = lyric;
230 metaMask_.set(META_KEY_LYRIC);
231 }
232
GetLyric() const233 std::string AVMetaData::GetLyric() const
234 {
235 return lyric_;
236 }
237
SetPreviousAssetId(const std::string & assetId)238 void AVMetaData::SetPreviousAssetId(const std::string& assetId)
239 {
240 previousAssetId_ = assetId;
241 metaMask_.set(META_KEY_PREVIOUS_ASSET_ID);
242 }
243
GetPreviousAssetId() const244 std::string AVMetaData::GetPreviousAssetId() const
245 {
246 return previousAssetId_;
247 }
248
SetNextAssetId(const std::string & assetId)249 void AVMetaData::SetNextAssetId(const std::string& assetId)
250 {
251 nextAssetId_ = assetId;
252 metaMask_.set(META_KEY_NEXT_ASSET_ID);
253 }
254
GetNextAssetId() const255 std::string AVMetaData::GetNextAssetId() const
256 {
257 return nextAssetId_;
258 }
259
GetMetaMask() const260 AVMetaData::MetaMaskType AVMetaData::GetMetaMask() const
261 {
262 return metaMask_;
263 }
264
Reset()265 void AVMetaData::Reset()
266 {
267 metaMask_.reset();
268 assetId_ = "";
269 title_ = "";
270 artist_ = "";
271 author_ = "";
272 album_ = "";
273 writer_ = "";
274 composer_ = "";
275 duration_ = 0;
276 mediaImage_ = nullptr;
277 mediaImageUri_ = "";
278 publishDate_ = 0;
279 subTitle_ = "";
280 description_ = "";
281 lyric_ = "";
282 previousAssetId_ = "";
283 nextAssetId_ = "";
284 }
285
CopyToByMask(MetaMaskType & mask,AVMetaData & metaOut) const286 bool AVMetaData::CopyToByMask(MetaMaskType& mask, AVMetaData& metaOut) const
287 {
288 bool result = false;
289 auto intersection = metaMask_ & mask;
290 for (int i = 0; i < META_KEY_MAX; i++) {
291 if (intersection.test(i)) {
292 cloneActions[i](*this, metaOut);
293 metaOut.metaMask_.set(i);
294 result = true;
295 }
296 }
297
298 return result;
299 }
300
CopyFrom(const AVMetaData & metaIn)301 bool AVMetaData::CopyFrom(const AVMetaData& metaIn)
302 {
303 if (metaIn.assetId_.empty()) {
304 SLOGE("assetId is empty");
305 return false;
306 }
307
308 if (metaIn.assetId_ != assetId_) {
309 SLOGD("assetId not equal");
310 *this = metaIn;
311 return true;
312 }
313
314 bool result = false;
315 for (int i = 0; i < META_KEY_MAX; i++) {
316 if (metaIn.metaMask_.test(i)) {
317 cloneActions[i](metaIn, *this);
318 metaMask_.set(i);
319 result = true;
320 }
321 }
322
323 return result;
324 }
325
IsValid() const326 bool AVMetaData::IsValid() const
327 {
328 return duration_ >= AVMetaData::DURATION_ALWAYS_PLAY && publishDate_ >= 0;
329 }
330
CloneAssetId(const AVMetaData & from,AVMetaData & to)331 void AVMetaData::CloneAssetId(const AVMetaData& from, AVMetaData& to)
332 {
333 to.assetId_ = from.assetId_;
334 }
335
CloneTitle(const AVMetaData & from,AVMetaData & to)336 void AVMetaData::CloneTitle(const AVMetaData& from, AVMetaData& to)
337 {
338 to.title_ = from.title_;
339 }
340
CloneArtist(const AVMetaData & from,AVMetaData & to)341 void AVMetaData::CloneArtist(const AVMetaData& from, AVMetaData& to)
342 {
343 to.artist_ = from.artist_;
344 }
345
CloneAuthor(const AVMetaData & from,AVMetaData & to)346 void AVMetaData::CloneAuthor(const AVMetaData& from, AVMetaData& to)
347 {
348 to.author_ = from.author_;
349 }
350
CloneAlbum(const AVMetaData & from,AVMetaData & to)351 void AVMetaData::CloneAlbum(const AVMetaData& from, AVMetaData& to)
352 {
353 to.album_ = from.album_;
354 }
355
CloneWriter(const AVMetaData & from,AVMetaData & to)356 void AVMetaData::CloneWriter(const AVMetaData& from, AVMetaData& to)
357 {
358 to.writer_ = from.writer_;
359 }
360
CloneComposer(const AVMetaData & from,AVMetaData & to)361 void AVMetaData::CloneComposer(const AVMetaData& from, AVMetaData& to)
362 {
363 to.composer_ = from.composer_;
364 }
365
CloneDuration(const AVMetaData & from,AVMetaData & to)366 void AVMetaData::CloneDuration(const AVMetaData& from, AVMetaData& to)
367 {
368 to.duration_ = from.duration_;
369 }
370
CloneMediaImage(const AVMetaData & from,AVMetaData & to)371 void AVMetaData::CloneMediaImage(const AVMetaData& from, AVMetaData& to)
372 {
373 to.mediaImage_ = from.mediaImage_;
374 }
375
CloneMediaImageUri(const AVMetaData & from,AVMetaData & to)376 void AVMetaData::CloneMediaImageUri(const AVMetaData& from, AVMetaData& to)
377 {
378 to.mediaImageUri_ = from.mediaImageUri_;
379 }
380
ClonePublishData(const AVMetaData & from,AVMetaData & to)381 void AVMetaData::ClonePublishData(const AVMetaData& from, AVMetaData& to)
382 {
383 to.publishDate_ = from.publishDate_;
384 }
385
CloneSubTitle(const AVMetaData & from,AVMetaData & to)386 void AVMetaData::CloneSubTitle(const AVMetaData& from, AVMetaData& to)
387 {
388 to.subTitle_ = from.subTitle_;
389 }
390
CloneDescription(const AVMetaData & from,AVMetaData & to)391 void AVMetaData::CloneDescription(const AVMetaData& from, AVMetaData& to)
392 {
393 to.description_ = from.description_;
394 }
395
CloneLyric(const AVMetaData & from,AVMetaData & to)396 void AVMetaData::CloneLyric(const AVMetaData& from, AVMetaData& to)
397 {
398 to.lyric_ = from.lyric_;
399 }
400
ClonePreviousAssetId(const AVMetaData & from,AVMetaData & to)401 void AVMetaData::ClonePreviousAssetId(const AVMetaData& from, AVMetaData& to)
402 {
403 to.previousAssetId_ = from.previousAssetId_;
404 }
405
CloneNextAssetId(const AVMetaData & from,AVMetaData & to)406 void AVMetaData::CloneNextAssetId(const AVMetaData& from, AVMetaData& to)
407 {
408 to.nextAssetId_ = from.nextAssetId_;
409 }
410 } // namespace OHOS::AVSession