Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: f1b2f1e13ed77618b025243c061db144a6c17390 [file] [log] [blame]
[email protected]ac2d27d2015-02-26 13:59:221/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei71207422017-09-15 11:58:0911#include "common_types.h" // NOLINT(build/include)
[email protected]ac2d27d2015-02-26 13:59:2212
13#include <string.h>
eladalond0244c22017-06-08 11:19:1314#include <algorithm>
danilchapef8d7732017-04-19 09:59:4815#include <limits>
16#include <type_traits>
[email protected]ac2d27d2015-02-26 13:59:2217
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
19#include "rtc_base/stringutils.h"
magjed10165ab2016-11-22 18:16:5720
[email protected]ac2d27d2015-02-26 13:59:2221namespace webrtc {
22
[email protected]ac2d27d2015-02-26 13:59:2223StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {}
24
danilchapef8d7732017-04-19 09:59:4825constexpr size_t StreamId::kMaxSize;
26
eladalond0244c22017-06-08 11:19:1327bool StreamId::IsLegalName(rtc::ArrayView<const char> name) {
28 return (name.size() <= kMaxSize && name.size() > 0 &&
29 std::all_of(name.data(), name.data() + name.size(), isalnum));
30}
31
danilchapef8d7732017-04-19 09:59:4832void StreamId::Set(const char* data, size_t size) {
33 // If |data| contains \0, the stream id size might become less than |size|.
eladalon4a3c9f62017-06-02 10:37:4834 RTC_CHECK_LE(size, kMaxSize);
danilchapef8d7732017-04-19 09:59:4835 memcpy(value_, data, size);
36 if (size < kMaxSize)
37 value_[size] = 0;
38}
39
40// StreamId is used as member of RTPHeader that is sometimes copied with memcpy
41// and thus assume trivial destructibility.
42static_assert(std::is_trivially_destructible<StreamId>::value, "");
43
[email protected]30933902015-03-17 14:33:1244RTPHeaderExtension::RTPHeaderExtension()
45 : hasTransmissionTimeOffset(false),
46 transmissionTimeOffset(0),
47 hasAbsoluteSendTime(false),
48 absoluteSendTime(0),
49 hasTransportSequenceNumber(false),
50 transportSequenceNumber(0),
51 hasAudioLevel(false),
Minyue4cee4192015-08-10 13:08:3652 voiceActivity(false),
[email protected]30933902015-03-17 14:33:1253 audioLevel(0),
54 hasVideoRotation(false),
ilnik00d802b2017-04-11 17:34:3155 videoRotation(kVideoRotation_0),
56 hasVideoContentType(false),
ilnik04f4d122017-06-19 14:18:5557 videoContentType(VideoContentType::UNSPECIFIED),
58 has_video_timing(false) {}
[email protected]30933902015-03-17 14:33:1259
eladalon98b1b7d2017-09-11 15:48:2660RTPHeaderExtension::RTPHeaderExtension(const RTPHeaderExtension& other) =
61 default;
62
63RTPHeaderExtension& RTPHeaderExtension::operator=(
64 const RTPHeaderExtension& other) = default;
65
[email protected]ac2d27d2015-02-26 13:59:2266RTPHeader::RTPHeader()
67 : markerBit(false),
68 payloadType(0),
69 sequenceNumber(0),
70 timestamp(0),
71 ssrc(0),
72 numCSRCs(0),
tommia6219cc2016-06-15 17:30:1473 arrOfCSRCs(),
[email protected]ac2d27d2015-02-26 13:59:2274 paddingLength(0),
75 headerLength(0),
76 payload_type_frequency(0),
tommia6219cc2016-06-15 17:30:1477 extension() {}
[email protected]ac2d27d2015-02-26 13:59:2278
eladalon98b1b7d2017-09-11 15:48:2679RTPHeader::RTPHeader(const RTPHeader& other) = default;
80
81RTPHeader& RTPHeader::operator=(const RTPHeader& other) = default;
82
hta257dc392016-10-25 16:05:0683VideoCodec::VideoCodec()
84 : codecType(kVideoCodecUnknown),
85 plName(),
86 plType(0),
87 width(0),
88 height(0),
89 startBitrate(0),
90 maxBitrate(0),
91 minBitrate(0),
92 targetBitrate(0),
93 maxFramerate(0),
94 qpMax(0),
95 numberOfSimulcastStreams(0),
96 simulcastStream(),
97 spatialLayers(),
98 mode(kRealtimeVideo),
Erik Språng08127a92016-11-16 15:41:3099 expect_encode_from_texture(false),
ilnik04f4d122017-06-19 14:18:55100 timing_frame_thresholds({0, 0}),
hta527d3472016-11-17 07:23:04101 codec_specific_() {}
hta257dc392016-10-25 16:05:06102
103VideoCodecVP8* VideoCodec::VP8() {
104 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-17 07:23:04105 return &codec_specific_.VP8;
hta257dc392016-10-25 16:05:06106}
107
108const VideoCodecVP8& VideoCodec::VP8() const {
109 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-17 07:23:04110 return codec_specific_.VP8;
hta257dc392016-10-25 16:05:06111}
112
113VideoCodecVP9* VideoCodec::VP9() {
114 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-17 07:23:04115 return &codec_specific_.VP9;
hta257dc392016-10-25 16:05:06116}
117
118const VideoCodecVP9& VideoCodec::VP9() const {
119 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-17 07:23:04120 return codec_specific_.VP9;
hta257dc392016-10-25 16:05:06121}
122
123VideoCodecH264* VideoCodec::H264() {
124 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-17 07:23:04125 return &codec_specific_.H264;
hta257dc392016-10-25 16:05:06126}
127
128const VideoCodecH264& VideoCodec::H264() const {
129 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-17 07:23:04130 return codec_specific_.H264;
hta257dc392016-10-25 16:05:06131}
132
Erik Språng08127a92016-11-16 15:41:30133static const char* kPayloadNameVp8 = "VP8";
134static const char* kPayloadNameVp9 = "VP9";
135static const char* kPayloadNameH264 = "H264";
136static const char* kPayloadNameI420 = "I420";
137static const char* kPayloadNameRED = "RED";
138static const char* kPayloadNameULPFEC = "ULPFEC";
139static const char* kPayloadNameGeneric = "Generic";
Emircan Uysaler0a375472017-12-11 06:51:02140static const char* kPayloadNameStereo = "Stereo";
Erik Språng08127a92016-11-16 15:41:30141
magjed10165ab2016-11-22 18:16:57142static bool CodecNamesEq(const char* name1, const char* name2) {
143 return _stricmp(name1, name2) == 0;
144}
145
kthelgason1cdddc92017-08-24 10:52:48146const char* CodecTypeToPayloadString(VideoCodecType type) {
Erik Språng08127a92016-11-16 15:41:30147 switch (type) {
148 case kVideoCodecVP8:
kthelgason1cdddc92017-08-24 10:52:48149 return kPayloadNameVp8;
Erik Språng08127a92016-11-16 15:41:30150 case kVideoCodecVP9:
kthelgason1cdddc92017-08-24 10:52:48151 return kPayloadNameVp9;
Erik Språng08127a92016-11-16 15:41:30152 case kVideoCodecH264:
kthelgason1cdddc92017-08-24 10:52:48153 return kPayloadNameH264;
Erik Språng08127a92016-11-16 15:41:30154 case kVideoCodecI420:
kthelgason1cdddc92017-08-24 10:52:48155 return kPayloadNameI420;
Erik Språng08127a92016-11-16 15:41:30156 case kVideoCodecRED:
kthelgason1cdddc92017-08-24 10:52:48157 return kPayloadNameRED;
Erik Språng08127a92016-11-16 15:41:30158 case kVideoCodecULPFEC:
kthelgason1cdddc92017-08-24 10:52:48159 return kPayloadNameULPFEC;
Emircan Uysaler0a375472017-12-11 06:51:02160 // Other codecs default to generic.
161 case kVideoCodecStereo:
162 case kVideoCodecFlexfec:
163 case kVideoCodecGeneric:
164 case kVideoCodecUnknown:
kthelgason1cdddc92017-08-24 10:52:48165 return kPayloadNameGeneric;
Erik Språng08127a92016-11-16 15:41:30166 }
Emircan Uysaler0a375472017-12-11 06:51:02167 return kPayloadNameGeneric;
Erik Språng08127a92016-11-16 15:41:30168}
169
kthelgason1cdddc92017-08-24 10:52:48170VideoCodecType PayloadStringToCodecType(const std::string& name) {
magjed10165ab2016-11-22 18:16:57171 if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
kthelgason1cdddc92017-08-24 10:52:48172 return kVideoCodecVP8;
magjed10165ab2016-11-22 18:16:57173 if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
kthelgason1cdddc92017-08-24 10:52:48174 return kVideoCodecVP9;
magjed10165ab2016-11-22 18:16:57175 if (CodecNamesEq(name.c_str(), kPayloadNameH264))
kthelgason1cdddc92017-08-24 10:52:48176 return kVideoCodecH264;
magjed10165ab2016-11-22 18:16:57177 if (CodecNamesEq(name.c_str(), kPayloadNameI420))
kthelgason1cdddc92017-08-24 10:52:48178 return kVideoCodecI420;
magjed10165ab2016-11-22 18:16:57179 if (CodecNamesEq(name.c_str(), kPayloadNameRED))
kthelgason1cdddc92017-08-24 10:52:48180 return kVideoCodecRED;
magjed10165ab2016-11-22 18:16:57181 if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
kthelgason1cdddc92017-08-24 10:52:48182 return kVideoCodecULPFEC;
Emircan Uysaler0a375472017-12-11 06:51:02183 if (CodecNamesEq(name.c_str(), kPayloadNameStereo))
184 return kVideoCodecStereo;
kthelgason1cdddc92017-08-24 10:52:48185 return kVideoCodecGeneric;
186}
187
Erik Språng08127a92016-11-16 15:41:30188const uint32_t BitrateAllocation::kMaxBitrateBps =
189 std::numeric_limits<uint32_t>::max();
190
[email protected]01f2ec32017-11-15 13:58:23191BitrateAllocation::BitrateAllocation() : sum_(0), bitrates_{}, has_bitrate_{} {}
Erik Språng08127a92016-11-16 15:41:30192
193bool BitrateAllocation::SetBitrate(size_t spatial_index,
194 size_t temporal_index,
195 uint32_t bitrate_bps) {
sprang6d314c72016-12-06 14:08:53196 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
197 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
198 RTC_CHECK_LE(bitrates_[spatial_index][temporal_index], sum_);
Erik Språng08127a92016-11-16 15:41:30199 uint64_t new_bitrate_sum_bps = sum_;
200 new_bitrate_sum_bps -= bitrates_[spatial_index][temporal_index];
201 new_bitrate_sum_bps += bitrate_bps;
202 if (new_bitrate_sum_bps > kMaxBitrateBps)
203 return false;
204
205 bitrates_[spatial_index][temporal_index] = bitrate_bps;
[email protected]01f2ec32017-11-15 13:58:23206 has_bitrate_[spatial_index][temporal_index] = true;
Erik Språng08127a92016-11-16 15:41:30207 sum_ = static_cast<uint32_t>(new_bitrate_sum_bps);
208 return true;
209}
210
[email protected]01f2ec32017-11-15 13:58:23211bool BitrateAllocation::HasBitrate(size_t spatial_index,
212 size_t temporal_index) const {
213 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
214 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
215 return has_bitrate_[spatial_index][temporal_index];
216}
217
Erik Språng08127a92016-11-16 15:41:30218uint32_t BitrateAllocation::GetBitrate(size_t spatial_index,
219 size_t temporal_index) const {
sprang6d314c72016-12-06 14:08:53220 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
221 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
Erik Språng08127a92016-11-16 15:41:30222 return bitrates_[spatial_index][temporal_index];
223}
224
[email protected]01f2ec32017-11-15 13:58:23225// Whether the specific spatial layers has the bitrate set in any of its
226// temporal layers.
227bool BitrateAllocation::IsSpatialLayerUsed(size_t spatial_index) const {
228 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
229 for (int i = 0; i < kMaxTemporalStreams; ++i) {
230 if (has_bitrate_[spatial_index][i])
231 return true;
232 }
233 return false;
234}
235
Erik Språng08127a92016-11-16 15:41:30236// Get the sum of all the temporal layer for a specific spatial layer.
237uint32_t BitrateAllocation::GetSpatialLayerSum(size_t spatial_index) const {
sprang6d314c72016-12-06 14:08:53238 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
Erik Språng08127a92016-11-16 15:41:30239 uint32_t sum = 0;
240 for (int i = 0; i < kMaxTemporalStreams; ++i)
241 sum += bitrates_[spatial_index][i];
242 return sum;
243}
244
sprangd0fc37a2017-06-22 12:40:25245std::string BitrateAllocation::ToString() const {
246 if (sum_ == 0)
247 return "BitrateAllocation [ [] ]";
248
249 // TODO(sprang): Replace this stringstream with something cheaper.
250 std::ostringstream oss;
251 oss << "BitrateAllocation [";
252 uint32_t spatial_cumulator = 0;
253 for (int si = 0; si < kMaxSpatialLayers; ++si) {
254 RTC_DCHECK_LE(spatial_cumulator, sum_);
255 if (spatial_cumulator == sum_)
256 break;
257
258 const uint32_t layer_sum = GetSpatialLayerSum(si);
259 if (layer_sum == sum_) {
260 oss << " [";
261 } else {
262 if (si > 0)
263 oss << ",";
264 oss << std::endl << " [";
265 }
266 spatial_cumulator += layer_sum;
267
268 uint32_t temporal_cumulator = 0;
269 for (int ti = 0; ti < kMaxTemporalStreams; ++ti) {
270 RTC_DCHECK_LE(temporal_cumulator, layer_sum);
271 if (temporal_cumulator == layer_sum)
272 break;
273
274 if (ti > 0)
275 oss << ", ";
276
277 uint32_t bitrate = bitrates_[si][ti];
278 oss << bitrate;
279 temporal_cumulator += bitrate;
280 }
281 oss << "]";
282 }
283
284 RTC_DCHECK_EQ(spatial_cumulator, sum_);
285 oss << " ]";
286 return oss.str();
287}
288
289std::ostream& BitrateAllocation::operator<<(std::ostream& os) const {
290 os << ToString();
291 return os;
292}
293
[email protected]ac2d27d2015-02-26 13:59:22294} // namespace webrtc