blob: b9c4737aeb511627bd1b3697269ef047d8a11ad1 [file] [log] [blame]
kwiberg@webrtc.orgac2d27d2015-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
11#include "webrtc/common_types.h"
12
13#include <string.h>
danilchapef8d7732017-04-19 09:59:4814#include <limits>
15#include <type_traits>
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:2216
magjed10165ab2016-11-22 18:16:5717#include "webrtc/base/checks.h"
18#include "webrtc/base/stringutils.h"
19
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:2220namespace webrtc {
21
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:2222StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {}
23
danilchapef8d7732017-04-19 09:59:4824constexpr size_t StreamId::kMaxSize;
25
26void StreamId::Set(const char* data, size_t size) {
27 // If |data| contains \0, the stream id size might become less than |size|.
28 RTC_DCHECK_LE(size, kMaxSize);
29 memcpy(value_, data, size);
30 if (size < kMaxSize)
31 value_[size] = 0;
32}
33
34// StreamId is used as member of RTPHeader that is sometimes copied with memcpy
35// and thus assume trivial destructibility.
36static_assert(std::is_trivially_destructible<StreamId>::value, "");
37
sprang@webrtc.org30933902015-03-17 14:33:1238RTPHeaderExtension::RTPHeaderExtension()
39 : hasTransmissionTimeOffset(false),
40 transmissionTimeOffset(0),
41 hasAbsoluteSendTime(false),
42 absoluteSendTime(0),
43 hasTransportSequenceNumber(false),
44 transportSequenceNumber(0),
45 hasAudioLevel(false),
Minyue4cee4192015-08-10 13:08:3646 voiceActivity(false),
sprang@webrtc.org30933902015-03-17 14:33:1247 audioLevel(0),
48 hasVideoRotation(false),
ilnik00d802b2017-04-11 17:34:3149 videoRotation(kVideoRotation_0),
50 hasVideoContentType(false),
51 videoContentType(VideoContentType::UNSPECIFIED) {}
sprang@webrtc.org30933902015-03-17 14:33:1252
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:2253RTPHeader::RTPHeader()
54 : markerBit(false),
55 payloadType(0),
56 sequenceNumber(0),
57 timestamp(0),
58 ssrc(0),
59 numCSRCs(0),
tommia6219cc2016-06-15 17:30:1460 arrOfCSRCs(),
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:2261 paddingLength(0),
62 headerLength(0),
63 payload_type_frequency(0),
tommia6219cc2016-06-15 17:30:1464 extension() {}
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:2265
hta257dc392016-10-25 16:05:0666VideoCodec::VideoCodec()
67 : codecType(kVideoCodecUnknown),
68 plName(),
69 plType(0),
70 width(0),
71 height(0),
72 startBitrate(0),
73 maxBitrate(0),
74 minBitrate(0),
75 targetBitrate(0),
76 maxFramerate(0),
77 qpMax(0),
78 numberOfSimulcastStreams(0),
79 simulcastStream(),
80 spatialLayers(),
81 mode(kRealtimeVideo),
Erik Språng08127a92016-11-16 15:41:3082 expect_encode_from_texture(false),
hta527d3472016-11-17 07:23:0483 codec_specific_() {}
hta257dc392016-10-25 16:05:0684
85VideoCodecVP8* VideoCodec::VP8() {
86 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-17 07:23:0487 return &codec_specific_.VP8;
hta257dc392016-10-25 16:05:0688}
89
90const VideoCodecVP8& VideoCodec::VP8() const {
91 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta527d3472016-11-17 07:23:0492 return codec_specific_.VP8;
hta257dc392016-10-25 16:05:0693}
94
95VideoCodecVP9* VideoCodec::VP9() {
96 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-17 07:23:0497 return &codec_specific_.VP9;
hta257dc392016-10-25 16:05:0698}
99
100const VideoCodecVP9& VideoCodec::VP9() const {
101 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta527d3472016-11-17 07:23:04102 return codec_specific_.VP9;
hta257dc392016-10-25 16:05:06103}
104
105VideoCodecH264* VideoCodec::H264() {
106 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-17 07:23:04107 return &codec_specific_.H264;
hta257dc392016-10-25 16:05:06108}
109
110const VideoCodecH264& VideoCodec::H264() const {
111 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta527d3472016-11-17 07:23:04112 return codec_specific_.H264;
hta257dc392016-10-25 16:05:06113}
114
Erik Språng08127a92016-11-16 15:41:30115static const char* kPayloadNameVp8 = "VP8";
116static const char* kPayloadNameVp9 = "VP9";
117static const char* kPayloadNameH264 = "H264";
118static const char* kPayloadNameI420 = "I420";
119static const char* kPayloadNameRED = "RED";
120static const char* kPayloadNameULPFEC = "ULPFEC";
121static const char* kPayloadNameGeneric = "Generic";
122
magjed10165ab2016-11-22 18:16:57123static bool CodecNamesEq(const char* name1, const char* name2) {
124 return _stricmp(name1, name2) == 0;
125}
126
127rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type) {
Erik Språng08127a92016-11-16 15:41:30128 switch (type) {
129 case kVideoCodecVP8:
magjed10165ab2016-11-22 18:16:57130 return rtc::Optional<const char*>(kPayloadNameVp8);
Erik Språng08127a92016-11-16 15:41:30131 case kVideoCodecVP9:
magjed10165ab2016-11-22 18:16:57132 return rtc::Optional<const char*>(kPayloadNameVp9);
Erik Språng08127a92016-11-16 15:41:30133 case kVideoCodecH264:
magjed10165ab2016-11-22 18:16:57134 return rtc::Optional<const char*>(kPayloadNameH264);
Erik Språng08127a92016-11-16 15:41:30135 case kVideoCodecI420:
magjed10165ab2016-11-22 18:16:57136 return rtc::Optional<const char*>(kPayloadNameI420);
Erik Språng08127a92016-11-16 15:41:30137 case kVideoCodecRED:
magjed10165ab2016-11-22 18:16:57138 return rtc::Optional<const char*>(kPayloadNameRED);
Erik Språng08127a92016-11-16 15:41:30139 case kVideoCodecULPFEC:
magjed10165ab2016-11-22 18:16:57140 return rtc::Optional<const char*>(kPayloadNameULPFEC);
Erik Språng08127a92016-11-16 15:41:30141 case kVideoCodecGeneric:
magjed10165ab2016-11-22 18:16:57142 return rtc::Optional<const char*>(kPayloadNameGeneric);
Erik Språng08127a92016-11-16 15:41:30143 default:
magjed10165ab2016-11-22 18:16:57144 return rtc::Optional<const char*>();
Erik Språng08127a92016-11-16 15:41:30145 }
146}
147
148rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name) {
magjed10165ab2016-11-22 18:16:57149 if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
Erik Språng08127a92016-11-16 15:41:30150 return rtc::Optional<VideoCodecType>(kVideoCodecVP8);
magjed10165ab2016-11-22 18:16:57151 if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
Erik Språng08127a92016-11-16 15:41:30152 return rtc::Optional<VideoCodecType>(kVideoCodecVP9);
magjed10165ab2016-11-22 18:16:57153 if (CodecNamesEq(name.c_str(), kPayloadNameH264))
Erik Språng08127a92016-11-16 15:41:30154 return rtc::Optional<VideoCodecType>(kVideoCodecH264);
magjed10165ab2016-11-22 18:16:57155 if (CodecNamesEq(name.c_str(), kPayloadNameI420))
Erik Språng08127a92016-11-16 15:41:30156 return rtc::Optional<VideoCodecType>(kVideoCodecI420);
magjed10165ab2016-11-22 18:16:57157 if (CodecNamesEq(name.c_str(), kPayloadNameRED))
Erik Språng08127a92016-11-16 15:41:30158 return rtc::Optional<VideoCodecType>(kVideoCodecRED);
magjed10165ab2016-11-22 18:16:57159 if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
Erik Språng08127a92016-11-16 15:41:30160 return rtc::Optional<VideoCodecType>(kVideoCodecULPFEC);
magjed10165ab2016-11-22 18:16:57161 if (CodecNamesEq(name.c_str(), kPayloadNameGeneric))
Erik Språng08127a92016-11-16 15:41:30162 return rtc::Optional<VideoCodecType>(kVideoCodecGeneric);
163 return rtc::Optional<VideoCodecType>();
164}
165
166const uint32_t BitrateAllocation::kMaxBitrateBps =
167 std::numeric_limits<uint32_t>::max();
168
169BitrateAllocation::BitrateAllocation() : sum_(0), bitrates_{} {}
170
171bool BitrateAllocation::SetBitrate(size_t spatial_index,
172 size_t temporal_index,
173 uint32_t bitrate_bps) {
sprang6d314c72016-12-06 14:08:53174 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
175 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
176 RTC_CHECK_LE(bitrates_[spatial_index][temporal_index], sum_);
Erik Språng08127a92016-11-16 15:41:30177 uint64_t new_bitrate_sum_bps = sum_;
178 new_bitrate_sum_bps -= bitrates_[spatial_index][temporal_index];
179 new_bitrate_sum_bps += bitrate_bps;
180 if (new_bitrate_sum_bps > kMaxBitrateBps)
181 return false;
182
183 bitrates_[spatial_index][temporal_index] = bitrate_bps;
184 sum_ = static_cast<uint32_t>(new_bitrate_sum_bps);
185 return true;
186}
187
188uint32_t BitrateAllocation::GetBitrate(size_t spatial_index,
189 size_t temporal_index) const {
sprang6d314c72016-12-06 14:08:53190 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
191 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
Erik Språng08127a92016-11-16 15:41:30192 return bitrates_[spatial_index][temporal_index];
193}
194
195// Get the sum of all the temporal layer for a specific spatial layer.
196uint32_t BitrateAllocation::GetSpatialLayerSum(size_t spatial_index) const {
sprang6d314c72016-12-06 14:08:53197 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
Erik Språng08127a92016-11-16 15:41:30198 uint32_t sum = 0;
199 for (int i = 0; i < kMaxTemporalStreams; ++i)
200 sum += bitrates_[spatial_index][i];
201 return sum;
202}
203
kwiberg@webrtc.orgac2d27d2015-02-26 13:59:22204} // namespace webrtc