blob: f5b487f6be0a5280c1e0a7c84f0037edd8e2db4f [file] [log] [blame]
kwiberg@webrtc.orgc4e2cd02015-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
Erik Språngfafcfc02016-11-16 15:41:3013#include <limits>
kwiberg@webrtc.orgc4e2cd02015-02-26 13:59:2214#include <string.h>
15
magjed3fcf7852016-11-22 18:16:5716#include "webrtc/base/checks.h"
17#include "webrtc/base/stringutils.h"
18
kwiberg@webrtc.orgc4e2cd02015-02-26 13:59:2219namespace webrtc {
20
kwiberg@webrtc.orgc4e2cd02015-02-26 13:59:2221StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {}
22
sprang@webrtc.org25ec20f2015-03-17 14:33:1223RTPHeaderExtension::RTPHeaderExtension()
24 : hasTransmissionTimeOffset(false),
25 transmissionTimeOffset(0),
26 hasAbsoluteSendTime(false),
27 absoluteSendTime(0),
28 hasTransportSequenceNumber(false),
29 transportSequenceNumber(0),
30 hasAudioLevel(false),
Minyueda4c0f02015-08-10 13:08:3631 voiceActivity(false),
sprang@webrtc.org25ec20f2015-03-17 14:33:1232 audioLevel(0),
33 hasVideoRotation(false),
magjed78706f42016-09-08 10:24:5834 videoRotation(kVideoRotation_0) {}
sprang@webrtc.org25ec20f2015-03-17 14:33:1235
kwiberg@webrtc.orgc4e2cd02015-02-26 13:59:2236RTPHeader::RTPHeader()
37 : markerBit(false),
38 payloadType(0),
39 sequenceNumber(0),
40 timestamp(0),
41 ssrc(0),
42 numCSRCs(0),
tommia6d985c2016-06-15 17:30:1443 arrOfCSRCs(),
kwiberg@webrtc.orgc4e2cd02015-02-26 13:59:2244 paddingLength(0),
45 headerLength(0),
46 payload_type_frequency(0),
tommia6d985c2016-06-15 17:30:1447 extension() {}
kwiberg@webrtc.orgc4e2cd02015-02-26 13:59:2248
hta078cd6c2016-10-25 16:05:0649VideoCodec::VideoCodec()
50 : codecType(kVideoCodecUnknown),
51 plName(),
52 plType(0),
53 width(0),
54 height(0),
55 startBitrate(0),
56 maxBitrate(0),
57 minBitrate(0),
58 targetBitrate(0),
59 maxFramerate(0),
60 qpMax(0),
61 numberOfSimulcastStreams(0),
62 simulcastStream(),
63 spatialLayers(),
64 mode(kRealtimeVideo),
Erik Språngfafcfc02016-11-16 15:41:3065 expect_encode_from_texture(false),
hta8ee78142016-11-17 07:23:0466 codec_specific_() {}
hta078cd6c2016-10-25 16:05:0667
68VideoCodecVP8* VideoCodec::VP8() {
69 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta8ee78142016-11-17 07:23:0470 return &codec_specific_.VP8;
hta078cd6c2016-10-25 16:05:0671}
72
73const VideoCodecVP8& VideoCodec::VP8() const {
74 RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
hta8ee78142016-11-17 07:23:0475 return codec_specific_.VP8;
hta078cd6c2016-10-25 16:05:0676}
77
78VideoCodecVP9* VideoCodec::VP9() {
79 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta8ee78142016-11-17 07:23:0480 return &codec_specific_.VP9;
hta078cd6c2016-10-25 16:05:0681}
82
83const VideoCodecVP9& VideoCodec::VP9() const {
84 RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
hta8ee78142016-11-17 07:23:0485 return codec_specific_.VP9;
hta078cd6c2016-10-25 16:05:0686}
87
88VideoCodecH264* VideoCodec::H264() {
89 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta8ee78142016-11-17 07:23:0490 return &codec_specific_.H264;
hta078cd6c2016-10-25 16:05:0691}
92
93const VideoCodecH264& VideoCodec::H264() const {
94 RTC_DCHECK_EQ(codecType, kVideoCodecH264);
hta8ee78142016-11-17 07:23:0495 return codec_specific_.H264;
hta078cd6c2016-10-25 16:05:0696}
97
Erik Språngfafcfc02016-11-16 15:41:3098static const char* kPayloadNameVp8 = "VP8";
99static const char* kPayloadNameVp9 = "VP9";
100static const char* kPayloadNameH264 = "H264";
101static const char* kPayloadNameI420 = "I420";
102static const char* kPayloadNameRED = "RED";
103static const char* kPayloadNameULPFEC = "ULPFEC";
104static const char* kPayloadNameGeneric = "Generic";
105
magjed3fcf7852016-11-22 18:16:57106static bool CodecNamesEq(const char* name1, const char* name2) {
107 return _stricmp(name1, name2) == 0;
108}
109
110rtc::Optional<const char*> CodecTypeToPayloadName(VideoCodecType type) {
Erik Språngfafcfc02016-11-16 15:41:30111 switch (type) {
112 case kVideoCodecVP8:
magjed3fcf7852016-11-22 18:16:57113 return rtc::Optional<const char*>(kPayloadNameVp8);
Erik Språngfafcfc02016-11-16 15:41:30114 case kVideoCodecVP9:
magjed3fcf7852016-11-22 18:16:57115 return rtc::Optional<const char*>(kPayloadNameVp9);
Erik Språngfafcfc02016-11-16 15:41:30116 case kVideoCodecH264:
magjed3fcf7852016-11-22 18:16:57117 return rtc::Optional<const char*>(kPayloadNameH264);
Erik Språngfafcfc02016-11-16 15:41:30118 case kVideoCodecI420:
magjed3fcf7852016-11-22 18:16:57119 return rtc::Optional<const char*>(kPayloadNameI420);
Erik Språngfafcfc02016-11-16 15:41:30120 case kVideoCodecRED:
magjed3fcf7852016-11-22 18:16:57121 return rtc::Optional<const char*>(kPayloadNameRED);
Erik Språngfafcfc02016-11-16 15:41:30122 case kVideoCodecULPFEC:
magjed3fcf7852016-11-22 18:16:57123 return rtc::Optional<const char*>(kPayloadNameULPFEC);
Erik Språngfafcfc02016-11-16 15:41:30124 case kVideoCodecGeneric:
magjed3fcf7852016-11-22 18:16:57125 return rtc::Optional<const char*>(kPayloadNameGeneric);
Erik Språngfafcfc02016-11-16 15:41:30126 default:
magjed3fcf7852016-11-22 18:16:57127 return rtc::Optional<const char*>();
Erik Språngfafcfc02016-11-16 15:41:30128 }
129}
130
131rtc::Optional<VideoCodecType> PayloadNameToCodecType(const std::string& name) {
magjed3fcf7852016-11-22 18:16:57132 if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
Erik Språngfafcfc02016-11-16 15:41:30133 return rtc::Optional<VideoCodecType>(kVideoCodecVP8);
magjed3fcf7852016-11-22 18:16:57134 if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
Erik Språngfafcfc02016-11-16 15:41:30135 return rtc::Optional<VideoCodecType>(kVideoCodecVP9);
magjed3fcf7852016-11-22 18:16:57136 if (CodecNamesEq(name.c_str(), kPayloadNameH264))
Erik Språngfafcfc02016-11-16 15:41:30137 return rtc::Optional<VideoCodecType>(kVideoCodecH264);
magjed3fcf7852016-11-22 18:16:57138 if (CodecNamesEq(name.c_str(), kPayloadNameI420))
Erik Språngfafcfc02016-11-16 15:41:30139 return rtc::Optional<VideoCodecType>(kVideoCodecI420);
magjed3fcf7852016-11-22 18:16:57140 if (CodecNamesEq(name.c_str(), kPayloadNameRED))
Erik Språngfafcfc02016-11-16 15:41:30141 return rtc::Optional<VideoCodecType>(kVideoCodecRED);
magjed3fcf7852016-11-22 18:16:57142 if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
Erik Språngfafcfc02016-11-16 15:41:30143 return rtc::Optional<VideoCodecType>(kVideoCodecULPFEC);
magjed3fcf7852016-11-22 18:16:57144 if (CodecNamesEq(name.c_str(), kPayloadNameGeneric))
Erik Språngfafcfc02016-11-16 15:41:30145 return rtc::Optional<VideoCodecType>(kVideoCodecGeneric);
146 return rtc::Optional<VideoCodecType>();
147}
148
149const uint32_t BitrateAllocation::kMaxBitrateBps =
150 std::numeric_limits<uint32_t>::max();
151
152BitrateAllocation::BitrateAllocation() : sum_(0), bitrates_{} {}
153
154bool BitrateAllocation::SetBitrate(size_t spatial_index,
155 size_t temporal_index,
156 uint32_t bitrate_bps) {
sprangafc16ca2016-12-06 14:08:53157 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
158 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
159 RTC_CHECK_LE(bitrates_[spatial_index][temporal_index], sum_);
Erik Språngfafcfc02016-11-16 15:41:30160 uint64_t new_bitrate_sum_bps = sum_;
161 new_bitrate_sum_bps -= bitrates_[spatial_index][temporal_index];
162 new_bitrate_sum_bps += bitrate_bps;
163 if (new_bitrate_sum_bps > kMaxBitrateBps)
164 return false;
165
166 bitrates_[spatial_index][temporal_index] = bitrate_bps;
167 sum_ = static_cast<uint32_t>(new_bitrate_sum_bps);
168 return true;
169}
170
171uint32_t BitrateAllocation::GetBitrate(size_t spatial_index,
172 size_t temporal_index) const {
sprangafc16ca2016-12-06 14:08:53173 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
174 RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
Erik Språngfafcfc02016-11-16 15:41:30175 return bitrates_[spatial_index][temporal_index];
176}
177
178// Get the sum of all the temporal layer for a specific spatial layer.
179uint32_t BitrateAllocation::GetSpatialLayerSum(size_t spatial_index) const {
sprangafc16ca2016-12-06 14:08:53180 RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
Erik Språngfafcfc02016-11-16 15:41:30181 uint32_t sum = 0;
182 for (int i = 0; i < kMaxTemporalStreams; ++i)
183 sum += bitrates_[spatial_index][i];
184 return sum;
185}
186
kwiberg@webrtc.orgc4e2cd02015-02-26 13:59:22187} // namespace webrtc