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 "media_info_holder.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
Marshalling(Parcel & parcel) const20 bool MediaInfoHolder::Marshalling(Parcel& parcel) const
21 {
22     CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(currentIndex_), false, "write currentIndex failed");
23     int32_t playInfosSize = static_cast<int32_t>(playInfos_.size());
24     CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(playInfosSize), false, "write playInfoSize failed");
25     for (auto playInfo : playInfos_) {
26         CHECK_AND_RETURN_RET_LOG(playInfo.Marshalling(parcel), false, "write playInfoSize failed");
27     }
28     return true;
29 }
30 
Unmarshalling(Parcel & data)31 MediaInfoHolder *MediaInfoHolder::Unmarshalling(Parcel& data)
32 {
33     auto *result = new (std::nothrow) MediaInfoHolder();
34     CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "result new fail");
35 
36     int32_t currentIndex;
37     if (!data.ReadInt32(currentIndex)) {
38         SLOGE("write currentIndex failed");
39         delete result;
40         return nullptr;
41     }
42 
43     result->currentIndex_ = currentIndex;
44     int32_t playInfosSize;
45     if (!data.ReadInt32(playInfosSize)) {
46         SLOGE("write playInfosSize failed");
47         delete result;
48         return nullptr;
49     }
50     int32_t maxPlayInfosSize = 1000;
51     if ((playInfosSize < 0) || (playInfosSize >= maxPlayInfosSize)) {
52         SLOGI("playInfosSize is illegal");
53         delete result;
54         return nullptr;
55     }
56     for (int i = 0; i < playInfosSize; i++) {
57         AVQueueItem* queueItem = AVQueueItem::Unmarshalling(data);
58         if (queueItem == nullptr) {
59             continue;
60         }
61         result->playInfos_.emplace_back(*queueItem);
62     }
63     CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new MediaInfoHolder failed");
64     return result;
65 }
66 
SetCurrentIndex(const int32_t & currentIndex)67 void MediaInfoHolder::SetCurrentIndex(const int32_t& currentIndex)
68 {
69     currentIndex_ = currentIndex;
70 }
71 
GetCurrentIndex() const72 int32_t MediaInfoHolder::GetCurrentIndex() const
73 {
74     return currentIndex_;
75 }
76 
SetPlayInfos(const std::vector<AVQueueItem> & playInfos)77 void MediaInfoHolder::SetPlayInfos(const std::vector<AVQueueItem>& playInfos)
78 {
79     playInfos_ = playInfos;
80 }
81 
GetPlayInfos() const82 const std::vector<AVQueueItem>& MediaInfoHolder::GetPlayInfos() const
83 {
84     return playInfos_;
85 }
86 
IsValid() const87 bool MediaInfoHolder::IsValid() const
88 {
89     return playInfos_.size() > 0;
90 }
91 
Reset()92 void MediaInfoHolder::Reset()
93 {
94     currentIndex_ = 0;
95     playInfos_.clear();
96 }
97 } // namespace OHOS::AVSession
98