• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "avqueue_info.h"
17 #include "avsession_log.h"
18 #include "securec.h"
19 
20 namespace OHOS::AVSession {
Marshalling(Parcel & parcel) const21 bool AVQueueInfo::Marshalling(Parcel& parcel) const
22 {
23     return parcel.WriteString(bundleName_) &&
24         parcel.WriteString(avQueueName_) &&
25         parcel.WriteString(avQueueId_) &&
26         parcel.WriteString(avQueueImageUri_) &&
27         parcel.WriteParcelable(avQueueImage_.get());
28 }
29 
Unmarshalling(Parcel & data)30 bool AVQueueInfo::Unmarshalling(Parcel& data)
31 {
32     CHECK_AND_RETURN_RET_LOG(data.ReadString(bundleName_), false, "read bundleName failed");
33     CHECK_AND_RETURN_RET_LOG(data.ReadString(avQueueName_), false, "read avQueueName failed");
34     CHECK_AND_RETURN_RET_LOG(data.ReadString(avQueueId_), false, "read avQueueId failed");
35     CHECK_AND_RETURN_RET_LOG(data.ReadString(avQueueImageUri_), false, "read avQueueImageUri failed");
36 
37     avQueueImage_ = std::shared_ptr<AVSessionPixelMap>(data.ReadParcelable<AVSessionPixelMap>());
38     CHECK_AND_RETURN_RET_LOG(avQueueImage_ != nullptr, false, "read AVQueueInfo PixelMap failed");
39     return true;
40 }
41 
MarshallingMessageParcel(MessageParcel & parcel) const42 bool AVQueueInfo::MarshallingMessageParcel(MessageParcel& parcel) const
43 {
44     return parcel.WriteString(bundleName_) &&
45         parcel.WriteString(avQueueName_) &&
46         parcel.WriteString(avQueueId_) &&
47         parcel.WriteString(avQueueImageUri_) &&
48         MarshallingQueueImage(parcel);
49 }
50 
UnmarshallingMessageParcel(MessageParcel & data)51 AVQueueInfo* AVQueueInfo::UnmarshallingMessageParcel(MessageParcel& data)
52 {
53     auto *result = new (std::nothrow) AVQueueInfo();
54     CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new AVQueueInfo failed");
55 
56     bool ret = data.ReadString(result->bundleName_) && data.ReadString(result->avQueueName_) &&
57         data.ReadString(result->avQueueId_) && data.ReadString(result->avQueueImageUri_);
58     if (!ret) {
59         SLOGE("read AVQueueInfo failed");
60         delete result;
61         return nullptr;
62     }
63 
64     int imageLength = data.ReadInt32();
65     CHECK_AND_RETURN_RET_LOG(imageLength > 0, result, "AVQueueInfo::UnmarshallingMessageParcel image length 0");
66     const char *buffer = nullptr;
67     buffer = reinterpret_cast<const char *>(data.ReadRawData(imageLength));
68     CHECK_AND_RETURN_RET_LOG(buffer != nullptr, result, "read raw data null buffer");
69 
70     std::shared_ptr<AVSessionPixelMap> avQueuePixelMap = std::make_shared<AVSessionPixelMap>();
71     std::vector<uint8_t> mediaImageBuffer(buffer, buffer + imageLength);
72     avQueuePixelMap->SetInnerImgBuffer(mediaImageBuffer);
73     result->avQueueImage_ = avQueuePixelMap;
74     return result;
75 }
76 
MarshallingQueueImage(MessageParcel & parcel) const77 bool AVQueueInfo::MarshallingQueueImage(MessageParcel& parcel) const
78 {
79     int imageLength = 0;
80     std::vector<uint8_t> avQueueImageBuffer;
81     if (avQueueImage_ != nullptr) {
82         avQueueImageBuffer = avQueueImage_->GetInnerImgBuffer();
83         imageLength = static_cast<int>(avQueueImageBuffer.size());
84     }
85     CHECK_AND_RETURN_RET_LOG(parcel.WriteInt32(imageLength), false, "write image length fail");
86     CHECK_AND_RETURN_RET_LOG(imageLength > 0, true, "no image");
87 
88     unsigned char *buffer = new (std::nothrow) unsigned char[imageLength];
89     CHECK_AND_RETURN_RET_LOG(buffer != nullptr, false, "new buffer failed");
90 
91     memcpy_s(buffer, imageLength, avQueueImageBuffer.data(), imageLength);
92 
93     bool ret = parcel.WriteRawData(buffer, imageLength);
94     delete[] buffer;
95     CHECK_AND_RETURN_RET_LOG(ret, false, "WriteRawData failed");
96     return true;
97 }
98 
SetBundleName(const std::string & bundleName)99 void AVQueueInfo::SetBundleName(const std::string& bundleName)
100 {
101     bundleName_ = bundleName;
102 }
103 
GetBundleName() const104 std::string AVQueueInfo::GetBundleName() const
105 {
106     return bundleName_;
107 }
108 
SetAVQueueName(const std::string & avQueueName)109 void AVQueueInfo::SetAVQueueName(const std::string& avQueueName)
110 {
111     avQueueName_ = avQueueName;
112 }
113 
GetAVQueueName() const114 std::string AVQueueInfo::GetAVQueueName() const
115 {
116     return avQueueName_;
117 }
118 
SetAVQueueId(const std::string & avQueueId)119 void AVQueueInfo::SetAVQueueId(const std::string& avQueueId)
120 {
121     avQueueId_ = avQueueId;
122 }
123 
GetAVQueueId() const124 std::string AVQueueInfo::GetAVQueueId() const
125 {
126     return avQueueId_;
127 }
128 
SetAVQueueImage(const std::shared_ptr<AVSessionPixelMap> & avQueueImage)129 void AVQueueInfo::SetAVQueueImage(const std::shared_ptr<AVSessionPixelMap>& avQueueImage)
130 {
131     avQueueImage_ = avQueueImage;
132 }
133 
GetAVQueueImage() const134 std::shared_ptr<AVSessionPixelMap> AVQueueInfo::GetAVQueueImage() const
135 {
136     return avQueueImage_;
137 }
138 
SetAVQueueLength(const int32_t avQueueLength)139 void AVQueueInfo::SetAVQueueLength(const int32_t avQueueLength)
140 {
141     avQueueLength_ = avQueueLength;
142 }
143 
GetAVQueueLength() const144 int32_t AVQueueInfo::GetAVQueueLength() const
145 {
146     return avQueueLength_;
147 }
148 
SetAVQueueImageUri(const std::string & avQueueImageUri)149 void AVQueueInfo::SetAVQueueImageUri(const std::string& avQueueImageUri)
150 {
151     avQueueImageUri_ = avQueueImageUri;
152 }
153 
GetAVQueueImageUri() const154 std::string AVQueueInfo::GetAVQueueImageUri() const
155 {
156     return avQueueImageUri_;
157 }
158 } // namespace OHOS::AVSession