• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 = 0;
37     if (!data.ReadInt32(currentIndex)) {
38         SLOGE("write currentIndex failed");
39         delete result;
40         result = nullptr;
41         return nullptr;
42     }
43 
44     result->currentIndex_ = currentIndex;
45     int32_t playInfosSize = 0;
46     if (!data.ReadInt32(playInfosSize)) {
47         SLOGE("write playInfosSize failed");
48         delete result;
49         result = nullptr;
50         return nullptr;
51     }
52     int32_t maxPlayInfosSize = 1000;
53     if ((playInfosSize < 0) || (playInfosSize >= maxPlayInfosSize)) {
54         SLOGI("playInfosSize is illegal");
55         delete result;
56         result = nullptr;
57         return nullptr;
58     }
59     for (int i = 0; i < playInfosSize; i++) {
60         AVQueueItem* queueItem = AVQueueItem::Unmarshalling(data);
61         if (queueItem == nullptr) {
62             continue;
63         }
64         result->playInfos_.emplace_back(*queueItem);
65     }
66     CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new MediaInfoHolder failed");
67     return result;
68 }
69 
SetCurrentIndex(const int32_t & currentIndex)70 void MediaInfoHolder::SetCurrentIndex(const int32_t& currentIndex)
71 {
72     currentIndex_ = currentIndex;
73 }
74 
GetCurrentIndex() const75 int32_t MediaInfoHolder::GetCurrentIndex() const
76 {
77     return currentIndex_;
78 }
79 
SetPlayInfos(const std::vector<AVQueueItem> & playInfos)80 void MediaInfoHolder::SetPlayInfos(const std::vector<AVQueueItem>& playInfos)
81 {
82     playInfos_ = playInfos;
83 }
84 
GetPlayInfos() const85 const std::vector<AVQueueItem>& MediaInfoHolder::GetPlayInfos() const
86 {
87     return playInfos_;
88 }
89 
IsValid() const90 bool MediaInfoHolder::IsValid() const
91 {
92     return playInfos_.size() > 0;
93 }
94 
Reset()95 void MediaInfoHolder::Reset()
96 {
97     currentIndex_ = 0;
98     playInfos_.clear();
99 }
100 } // namespace OHOS::AVSession
101