1 /**
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <RtpHeaderExtension.h>
18
19 namespace android
20 {
21
22 namespace telephony
23 {
24
25 namespace imsmedia
26 {
27
RtpHeaderExtension()28 RtpHeaderExtension::RtpHeaderExtension() :
29 mLocalIdentifier(0),
30 mExtensionData(nullptr),
31 mExtensionDataSize(0)
32 {
33 }
34
RtpHeaderExtension(const RtpHeaderExtension & extension)35 RtpHeaderExtension::RtpHeaderExtension(const RtpHeaderExtension& extension) :
36 mLocalIdentifier(extension.mLocalIdentifier),
37 mExtensionData(nullptr),
38 mExtensionDataSize(extension.mExtensionDataSize)
39 {
40 if (mExtensionDataSize > 0)
41 {
42 mExtensionData = new uint8_t[mExtensionDataSize];
43 memcpy(mExtensionData, extension.mExtensionData, mExtensionDataSize);
44 }
45 }
46
~RtpHeaderExtension()47 RtpHeaderExtension::~RtpHeaderExtension()
48 {
49 if (mExtensionData != nullptr)
50 {
51 delete[] mExtensionData;
52 mExtensionData = nullptr;
53 }
54 }
55
operator =(const RtpHeaderExtension & extension)56 RtpHeaderExtension& RtpHeaderExtension::operator=(const RtpHeaderExtension& extension)
57 {
58 if (this != &extension)
59 {
60 mLocalIdentifier = extension.mLocalIdentifier;
61 this->setExtensionData(extension.mExtensionData, extension.mExtensionDataSize);
62 }
63
64 return *this;
65 }
66
operator ==(const RtpHeaderExtension & extension) const67 bool RtpHeaderExtension::operator==(const RtpHeaderExtension& extension) const
68 {
69 return (mLocalIdentifier == extension.mLocalIdentifier &&
70 memcmp(mExtensionData, extension.mExtensionData, mExtensionDataSize) == 0);
71 }
72
operator !=(const RtpHeaderExtension & extension) const73 bool RtpHeaderExtension::operator!=(const RtpHeaderExtension& extension) const
74 {
75 return (mLocalIdentifier != extension.mLocalIdentifier ||
76 memcmp(mExtensionData, extension.mExtensionData, mExtensionDataSize) != 0);
77 }
78
writeToParcel(Parcel * parcel) const79 status_t RtpHeaderExtension::writeToParcel(Parcel* parcel) const
80 {
81 status_t err;
82
83 if (parcel == nullptr)
84 {
85 return BAD_VALUE;
86 }
87
88 err = parcel->writeInt32(mLocalIdentifier);
89
90 if (err != NO_ERROR)
91 {
92 return err;
93 }
94
95 err = parcel->writeInt32(mExtensionDataSize);
96
97 if (err != NO_ERROR)
98 {
99 return err;
100 }
101
102 void* dest = parcel->writeInplace(mExtensionDataSize);
103
104 if (dest == nullptr)
105 {
106 return NO_MEMORY;
107 }
108
109 memcpy(dest, mExtensionData, mExtensionDataSize);
110 return NO_ERROR;
111 }
112
readFromParcel(const Parcel * parcel)113 status_t RtpHeaderExtension::readFromParcel(const Parcel* parcel)
114 {
115 status_t err;
116
117 if (parcel == nullptr)
118 {
119 return BAD_VALUE;
120 }
121
122 err = parcel->readInt32(&mLocalIdentifier);
123
124 if (err != NO_ERROR)
125 {
126 return err;
127 }
128
129 err = parcel->readInt32(&mExtensionDataSize);
130
131 if (err != NO_ERROR)
132 {
133 return err;
134 }
135
136 if (mExtensionDataSize != 0)
137 {
138 if (mExtensionData != nullptr)
139 {
140 delete[] mExtensionData;
141 mExtensionData = nullptr;
142 }
143
144 mExtensionData = new uint8_t[mExtensionDataSize];
145 const void* data = parcel->readInplace(mExtensionDataSize);
146
147 if (data != nullptr)
148 {
149 memcpy(mExtensionData, data, mExtensionDataSize);
150 }
151 }
152
153 return NO_ERROR;
154 }
155
getLocalIdentifier()156 int32_t RtpHeaderExtension::getLocalIdentifier()
157 {
158 return mLocalIdentifier;
159 }
160
setLocalIdentifier(const int32_t id)161 void RtpHeaderExtension::setLocalIdentifier(const int32_t id)
162 {
163 mLocalIdentifier = id;
164 }
165
getExtensionData() const166 uint8_t* RtpHeaderExtension::getExtensionData() const
167 {
168 return mExtensionData;
169 }
170
setExtensionData(const uint8_t * data,const int32_t size)171 void RtpHeaderExtension::setExtensionData(const uint8_t* data, const int32_t size)
172 {
173 if (mExtensionData != nullptr)
174 {
175 delete[] mExtensionData;
176 mExtensionData = nullptr;
177 mExtensionDataSize = 0;
178 }
179
180 if (data != nullptr)
181 {
182 mExtensionDataSize = size;
183 mExtensionData = new uint8_t[mExtensionDataSize];
184 memcpy(mExtensionData, data, mExtensionDataSize);
185 }
186 }
187
getExtensionDataSize()188 int32_t RtpHeaderExtension::getExtensionDataSize()
189 {
190 return mExtensionDataSize;
191 }
setExtensionDataSize(int32_t size)192 void RtpHeaderExtension::setExtensionDataSize(int32_t size)
193 {
194 mExtensionDataSize = size;
195 }
196
197 } // namespace imsmedia
198 } // namespace telephony
199 } // namespace android