2015-02-26 13:59:22 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "webrtc/common_types.h"
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
2017-06-08 04:19:13 -07:00
|
|
|
#include <algorithm>
|
2017-04-19 02:59:48 -07:00
|
|
|
#include <limits>
|
|
|
|
|
#include <type_traits>
|
2015-02-26 13:59:22 +00:00
|
|
|
|
2017-07-06 19:44:34 +02:00
|
|
|
#include "webrtc/rtc_base/checks.h"
|
|
|
|
|
#include "webrtc/rtc_base/stringutils.h"
|
2016-11-22 10:16:57 -08:00
|
|
|
|
2015-02-26 13:59:22 +00:00
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
StreamDataCounters::StreamDataCounters() : first_packet_time_ms(-1) {}
|
|
|
|
|
|
2017-04-19 02:59:48 -07:00
|
|
|
constexpr size_t StreamId::kMaxSize;
|
|
|
|
|
|
2017-06-08 04:19:13 -07:00
|
|
|
bool StreamId::IsLegalName(rtc::ArrayView<const char> name) {
|
|
|
|
|
return (name.size() <= kMaxSize && name.size() > 0 &&
|
|
|
|
|
std::all_of(name.data(), name.data() + name.size(), isalnum));
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 02:59:48 -07:00
|
|
|
void StreamId::Set(const char* data, size_t size) {
|
|
|
|
|
// If |data| contains \0, the stream id size might become less than |size|.
|
2017-06-02 03:37:48 -07:00
|
|
|
RTC_CHECK_LE(size, kMaxSize);
|
2017-04-19 02:59:48 -07:00
|
|
|
memcpy(value_, data, size);
|
|
|
|
|
if (size < kMaxSize)
|
|
|
|
|
value_[size] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StreamId is used as member of RTPHeader that is sometimes copied with memcpy
|
|
|
|
|
// and thus assume trivial destructibility.
|
|
|
|
|
static_assert(std::is_trivially_destructible<StreamId>::value, "");
|
|
|
|
|
|
2015-03-17 14:33:12 +00:00
|
|
|
RTPHeaderExtension::RTPHeaderExtension()
|
|
|
|
|
: hasTransmissionTimeOffset(false),
|
|
|
|
|
transmissionTimeOffset(0),
|
|
|
|
|
hasAbsoluteSendTime(false),
|
|
|
|
|
absoluteSendTime(0),
|
|
|
|
|
hasTransportSequenceNumber(false),
|
|
|
|
|
transportSequenceNumber(0),
|
|
|
|
|
hasAudioLevel(false),
|
2015-08-10 15:08:36 +02:00
|
|
|
voiceActivity(false),
|
2015-03-17 14:33:12 +00:00
|
|
|
audioLevel(0),
|
|
|
|
|
hasVideoRotation(false),
|
2017-04-11 10:34:31 -07:00
|
|
|
videoRotation(kVideoRotation_0),
|
|
|
|
|
hasVideoContentType(false),
|
2017-06-19 07:18:55 -07:00
|
|
|
videoContentType(VideoContentType::UNSPECIFIED),
|
|
|
|
|
has_video_timing(false) {}
|
2015-03-17 14:33:12 +00:00
|
|
|
|
2017-09-11 08:48:26 -07:00
|
|
|
RTPHeaderExtension::RTPHeaderExtension(const RTPHeaderExtension& other) =
|
|
|
|
|
default;
|
|
|
|
|
|
|
|
|
|
RTPHeaderExtension& RTPHeaderExtension::operator=(
|
|
|
|
|
const RTPHeaderExtension& other) = default;
|
|
|
|
|
|
2015-02-26 13:59:22 +00:00
|
|
|
RTPHeader::RTPHeader()
|
|
|
|
|
: markerBit(false),
|
|
|
|
|
payloadType(0),
|
|
|
|
|
sequenceNumber(0),
|
|
|
|
|
timestamp(0),
|
|
|
|
|
ssrc(0),
|
|
|
|
|
numCSRCs(0),
|
FileWrapper[Impl] modifications and actually remove the "Impl" class.
This is a somewhat involved refactoring of this class. Here's an overview of the changes:
* FileWrapper can now be used as a regular class and instances allocated on the stack.
* The type now has support for move semantics and copy isn't allowed.
* New public ctor with FILE* that can be used instead of OpenFromFileHandle.
* New static Open() method. The intent of this is to allow opening a file and getting back a FileWrapper instance. Using this method instead of Create(), will allow us in the future to make the FILE* member pointer, to be const and simplify threading (get rid of the lock).
* Rename the Open() method to is_open() and make it inline.
* The FileWrapper interface is no longer a pure virtual interface. There's only one implementation so there's no need to go through a vtable for everything.
* Functionality offered by the class, is now reduced. No support for looping (not clear if that was actually useful to users of that flag), no need to implement the 'read_only_' functionality in the class, since file APIs implement that already, no support for *not* managing the file handle (this wasn't used). OpenFromFileHandle always "manages" the file.
* Delete the unused WriteText() method and don't support opening files in text mode. Text mode is only different on Windows and on Windows it translates \n to \r\n, which means that files such as log files, could have a slightly different format on Windows than other platforms. Besides, tools on Windows can handle UNIX line endings.
* Remove FileName(), change Trace code to manage its own path.
* Rename id_ member variable to file_.
* Removed the open_ member variable since the same functionality can be gotten from just checking the file pointer.
* Don't call CloseFile inside of Write. Write shouldn't be changing the state of the class beyond just attempting to write.
* Remove concept of looping from FileWrapper and never close inside of Read()
* Changed stream base classes to inherit from a common base class instead of both defining the Rewind method. Ultimately, Id' like to remove these interfaces and just have FileWrapper.
* Remove read_only param from OpenFromFileHandle
* Renamed size_in_bytes_ to position_, since it gets set to 0 when Rewind() is called (and the size actually does not change).
* Switch out rw lock for CriticalSection. The r/w lock was only used for reading when checking the open_ flag.
BUG=
Review-Url: https://codereview.webrtc.org/2054373002
Cr-Commit-Position: refs/heads/master@{#13155}
2016-06-15 10:30:14 -07:00
|
|
|
arrOfCSRCs(),
|
2015-02-26 13:59:22 +00:00
|
|
|
paddingLength(0),
|
|
|
|
|
headerLength(0),
|
|
|
|
|
payload_type_frequency(0),
|
FileWrapper[Impl] modifications and actually remove the "Impl" class.
This is a somewhat involved refactoring of this class. Here's an overview of the changes:
* FileWrapper can now be used as a regular class and instances allocated on the stack.
* The type now has support for move semantics and copy isn't allowed.
* New public ctor with FILE* that can be used instead of OpenFromFileHandle.
* New static Open() method. The intent of this is to allow opening a file and getting back a FileWrapper instance. Using this method instead of Create(), will allow us in the future to make the FILE* member pointer, to be const and simplify threading (get rid of the lock).
* Rename the Open() method to is_open() and make it inline.
* The FileWrapper interface is no longer a pure virtual interface. There's only one implementation so there's no need to go through a vtable for everything.
* Functionality offered by the class, is now reduced. No support for looping (not clear if that was actually useful to users of that flag), no need to implement the 'read_only_' functionality in the class, since file APIs implement that already, no support for *not* managing the file handle (this wasn't used). OpenFromFileHandle always "manages" the file.
* Delete the unused WriteText() method and don't support opening files in text mode. Text mode is only different on Windows and on Windows it translates \n to \r\n, which means that files such as log files, could have a slightly different format on Windows than other platforms. Besides, tools on Windows can handle UNIX line endings.
* Remove FileName(), change Trace code to manage its own path.
* Rename id_ member variable to file_.
* Removed the open_ member variable since the same functionality can be gotten from just checking the file pointer.
* Don't call CloseFile inside of Write. Write shouldn't be changing the state of the class beyond just attempting to write.
* Remove concept of looping from FileWrapper and never close inside of Read()
* Changed stream base classes to inherit from a common base class instead of both defining the Rewind method. Ultimately, Id' like to remove these interfaces and just have FileWrapper.
* Remove read_only param from OpenFromFileHandle
* Renamed size_in_bytes_ to position_, since it gets set to 0 when Rewind() is called (and the size actually does not change).
* Switch out rw lock for CriticalSection. The r/w lock was only used for reading when checking the open_ flag.
BUG=
Review-Url: https://codereview.webrtc.org/2054373002
Cr-Commit-Position: refs/heads/master@{#13155}
2016-06-15 10:30:14 -07:00
|
|
|
extension() {}
|
2015-02-26 13:59:22 +00:00
|
|
|
|
2017-09-11 08:48:26 -07:00
|
|
|
RTPHeader::RTPHeader(const RTPHeader& other) = default;
|
|
|
|
|
|
|
|
|
|
RTPHeader& RTPHeader::operator=(const RTPHeader& other) = default;
|
|
|
|
|
|
2016-10-25 09:05:06 -07:00
|
|
|
VideoCodec::VideoCodec()
|
|
|
|
|
: codecType(kVideoCodecUnknown),
|
|
|
|
|
plName(),
|
|
|
|
|
plType(0),
|
|
|
|
|
width(0),
|
|
|
|
|
height(0),
|
|
|
|
|
startBitrate(0),
|
|
|
|
|
maxBitrate(0),
|
|
|
|
|
minBitrate(0),
|
|
|
|
|
targetBitrate(0),
|
|
|
|
|
maxFramerate(0),
|
|
|
|
|
qpMax(0),
|
|
|
|
|
numberOfSimulcastStreams(0),
|
|
|
|
|
simulcastStream(),
|
|
|
|
|
spatialLayers(),
|
|
|
|
|
mode(kRealtimeVideo),
|
2016-11-16 16:41:30 +01:00
|
|
|
expect_encode_from_texture(false),
|
2017-06-19 07:18:55 -07:00
|
|
|
timing_frame_thresholds({0, 0}),
|
2016-11-16 23:23:04 -08:00
|
|
|
codec_specific_() {}
|
2016-10-25 09:05:06 -07:00
|
|
|
|
|
|
|
|
VideoCodecVP8* VideoCodec::VP8() {
|
|
|
|
|
RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
|
2016-11-16 23:23:04 -08:00
|
|
|
return &codec_specific_.VP8;
|
2016-10-25 09:05:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const VideoCodecVP8& VideoCodec::VP8() const {
|
|
|
|
|
RTC_DCHECK_EQ(codecType, kVideoCodecVP8);
|
2016-11-16 23:23:04 -08:00
|
|
|
return codec_specific_.VP8;
|
2016-10-25 09:05:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoCodecVP9* VideoCodec::VP9() {
|
|
|
|
|
RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
|
2016-11-16 23:23:04 -08:00
|
|
|
return &codec_specific_.VP9;
|
2016-10-25 09:05:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const VideoCodecVP9& VideoCodec::VP9() const {
|
|
|
|
|
RTC_DCHECK_EQ(codecType, kVideoCodecVP9);
|
2016-11-16 23:23:04 -08:00
|
|
|
return codec_specific_.VP9;
|
2016-10-25 09:05:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoCodecH264* VideoCodec::H264() {
|
|
|
|
|
RTC_DCHECK_EQ(codecType, kVideoCodecH264);
|
2016-11-16 23:23:04 -08:00
|
|
|
return &codec_specific_.H264;
|
2016-10-25 09:05:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const VideoCodecH264& VideoCodec::H264() const {
|
|
|
|
|
RTC_DCHECK_EQ(codecType, kVideoCodecH264);
|
2016-11-16 23:23:04 -08:00
|
|
|
return codec_specific_.H264;
|
2016-10-25 09:05:06 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-16 16:41:30 +01:00
|
|
|
static const char* kPayloadNameVp8 = "VP8";
|
|
|
|
|
static const char* kPayloadNameVp9 = "VP9";
|
|
|
|
|
static const char* kPayloadNameH264 = "H264";
|
|
|
|
|
static const char* kPayloadNameI420 = "I420";
|
|
|
|
|
static const char* kPayloadNameRED = "RED";
|
|
|
|
|
static const char* kPayloadNameULPFEC = "ULPFEC";
|
|
|
|
|
static const char* kPayloadNameGeneric = "Generic";
|
|
|
|
|
|
2016-11-22 10:16:57 -08:00
|
|
|
static bool CodecNamesEq(const char* name1, const char* name2) {
|
|
|
|
|
return _stricmp(name1, name2) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 03:52:48 -07:00
|
|
|
const char* CodecTypeToPayloadString(VideoCodecType type) {
|
2016-11-16 16:41:30 +01:00
|
|
|
switch (type) {
|
|
|
|
|
case kVideoCodecVP8:
|
2017-08-24 03:52:48 -07:00
|
|
|
return kPayloadNameVp8;
|
2016-11-16 16:41:30 +01:00
|
|
|
case kVideoCodecVP9:
|
2017-08-24 03:52:48 -07:00
|
|
|
return kPayloadNameVp9;
|
2016-11-16 16:41:30 +01:00
|
|
|
case kVideoCodecH264:
|
2017-08-24 03:52:48 -07:00
|
|
|
return kPayloadNameH264;
|
2016-11-16 16:41:30 +01:00
|
|
|
case kVideoCodecI420:
|
2017-08-24 03:52:48 -07:00
|
|
|
return kPayloadNameI420;
|
2016-11-16 16:41:30 +01:00
|
|
|
case kVideoCodecRED:
|
2017-08-24 03:52:48 -07:00
|
|
|
return kPayloadNameRED;
|
2016-11-16 16:41:30 +01:00
|
|
|
case kVideoCodecULPFEC:
|
2017-08-24 03:52:48 -07:00
|
|
|
return kPayloadNameULPFEC;
|
2016-11-16 16:41:30 +01:00
|
|
|
default:
|
2017-08-24 03:52:48 -07:00
|
|
|
// Unrecognized codecs default to generic.
|
|
|
|
|
return kPayloadNameGeneric;
|
2016-11-16 16:41:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 03:52:48 -07:00
|
|
|
VideoCodecType PayloadStringToCodecType(const std::string& name) {
|
2016-11-22 10:16:57 -08:00
|
|
|
if (CodecNamesEq(name.c_str(), kPayloadNameVp8))
|
2017-08-24 03:52:48 -07:00
|
|
|
return kVideoCodecVP8;
|
2016-11-22 10:16:57 -08:00
|
|
|
if (CodecNamesEq(name.c_str(), kPayloadNameVp9))
|
2017-08-24 03:52:48 -07:00
|
|
|
return kVideoCodecVP9;
|
2016-11-22 10:16:57 -08:00
|
|
|
if (CodecNamesEq(name.c_str(), kPayloadNameH264))
|
2017-08-24 03:52:48 -07:00
|
|
|
return kVideoCodecH264;
|
2016-11-22 10:16:57 -08:00
|
|
|
if (CodecNamesEq(name.c_str(), kPayloadNameI420))
|
2017-08-24 03:52:48 -07:00
|
|
|
return kVideoCodecI420;
|
2016-11-22 10:16:57 -08:00
|
|
|
if (CodecNamesEq(name.c_str(), kPayloadNameRED))
|
2017-08-24 03:52:48 -07:00
|
|
|
return kVideoCodecRED;
|
2016-11-22 10:16:57 -08:00
|
|
|
if (CodecNamesEq(name.c_str(), kPayloadNameULPFEC))
|
2017-08-24 03:52:48 -07:00
|
|
|
return kVideoCodecULPFEC;
|
|
|
|
|
return kVideoCodecGeneric;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-16 16:41:30 +01:00
|
|
|
const uint32_t BitrateAllocation::kMaxBitrateBps =
|
|
|
|
|
std::numeric_limits<uint32_t>::max();
|
|
|
|
|
|
|
|
|
|
BitrateAllocation::BitrateAllocation() : sum_(0), bitrates_{} {}
|
|
|
|
|
|
|
|
|
|
bool BitrateAllocation::SetBitrate(size_t spatial_index,
|
|
|
|
|
size_t temporal_index,
|
|
|
|
|
uint32_t bitrate_bps) {
|
2016-12-06 06:08:53 -08:00
|
|
|
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
|
|
|
|
|
RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
|
|
|
|
|
RTC_CHECK_LE(bitrates_[spatial_index][temporal_index], sum_);
|
2016-11-16 16:41:30 +01:00
|
|
|
uint64_t new_bitrate_sum_bps = sum_;
|
|
|
|
|
new_bitrate_sum_bps -= bitrates_[spatial_index][temporal_index];
|
|
|
|
|
new_bitrate_sum_bps += bitrate_bps;
|
|
|
|
|
if (new_bitrate_sum_bps > kMaxBitrateBps)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bitrates_[spatial_index][temporal_index] = bitrate_bps;
|
|
|
|
|
sum_ = static_cast<uint32_t>(new_bitrate_sum_bps);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t BitrateAllocation::GetBitrate(size_t spatial_index,
|
|
|
|
|
size_t temporal_index) const {
|
2016-12-06 06:08:53 -08:00
|
|
|
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
|
|
|
|
|
RTC_CHECK_LT(temporal_index, kMaxTemporalStreams);
|
2016-11-16 16:41:30 +01:00
|
|
|
return bitrates_[spatial_index][temporal_index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the sum of all the temporal layer for a specific spatial layer.
|
|
|
|
|
uint32_t BitrateAllocation::GetSpatialLayerSum(size_t spatial_index) const {
|
2016-12-06 06:08:53 -08:00
|
|
|
RTC_CHECK_LT(spatial_index, kMaxSpatialLayers);
|
2016-11-16 16:41:30 +01:00
|
|
|
uint32_t sum = 0;
|
|
|
|
|
for (int i = 0; i < kMaxTemporalStreams; ++i)
|
|
|
|
|
sum += bitrates_[spatial_index][i];
|
|
|
|
|
return sum;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 05:40:25 -07:00
|
|
|
std::string BitrateAllocation::ToString() const {
|
|
|
|
|
if (sum_ == 0)
|
|
|
|
|
return "BitrateAllocation [ [] ]";
|
|
|
|
|
|
|
|
|
|
// TODO(sprang): Replace this stringstream with something cheaper.
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
oss << "BitrateAllocation [";
|
|
|
|
|
uint32_t spatial_cumulator = 0;
|
|
|
|
|
for (int si = 0; si < kMaxSpatialLayers; ++si) {
|
|
|
|
|
RTC_DCHECK_LE(spatial_cumulator, sum_);
|
|
|
|
|
if (spatial_cumulator == sum_)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
const uint32_t layer_sum = GetSpatialLayerSum(si);
|
|
|
|
|
if (layer_sum == sum_) {
|
|
|
|
|
oss << " [";
|
|
|
|
|
} else {
|
|
|
|
|
if (si > 0)
|
|
|
|
|
oss << ",";
|
|
|
|
|
oss << std::endl << " [";
|
|
|
|
|
}
|
|
|
|
|
spatial_cumulator += layer_sum;
|
|
|
|
|
|
|
|
|
|
uint32_t temporal_cumulator = 0;
|
|
|
|
|
for (int ti = 0; ti < kMaxTemporalStreams; ++ti) {
|
|
|
|
|
RTC_DCHECK_LE(temporal_cumulator, layer_sum);
|
|
|
|
|
if (temporal_cumulator == layer_sum)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (ti > 0)
|
|
|
|
|
oss << ", ";
|
|
|
|
|
|
|
|
|
|
uint32_t bitrate = bitrates_[si][ti];
|
|
|
|
|
oss << bitrate;
|
|
|
|
|
temporal_cumulator += bitrate;
|
|
|
|
|
}
|
|
|
|
|
oss << "]";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RTC_DCHECK_EQ(spatial_cumulator, sum_);
|
|
|
|
|
oss << " ]";
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ostream& BitrateAllocation::operator<<(std::ostream& os) const {
|
|
|
|
|
os << ToString();
|
|
|
|
|
return os;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 13:59:22 +00:00
|
|
|
} // namespace webrtc
|