2015-02-06 13:10:19 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-07-11 17:11:31 +02:00
|
|
|
#include "call/payload_router.h"
|
2015-02-06 13:10:19 +00:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/rtp_rtcp/include/rtp_rtcp.h"
|
|
|
|
|
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
|
|
|
|
|
#include "modules/video_coding/include/video_codec_interface.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
2015-02-06 13:10:19 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-04-20 05:05:54 -07:00
|
|
|
namespace {
|
2018-07-17 10:16:41 +02:00
|
|
|
absl::optional<size_t> GetSimulcastIdx(const CodecSpecificInfo* info) {
|
|
|
|
|
if (!info)
|
|
|
|
|
return absl::nullopt;
|
2016-04-20 05:05:54 -07:00
|
|
|
switch (info->codecType) {
|
2018-07-17 10:16:41 +02:00
|
|
|
case kVideoCodecVP8:
|
|
|
|
|
return absl::optional<size_t>(info->codecSpecific.VP8.simulcastIdx);
|
Revert "Remove RTPVideoHeader::h264() accessors."
This reverts commit dfbced6504720d2c0807d7b92798eb80ba3f8be9.
Reason for revert: Crashes when making a video call.
#9 0x00000001043dd8d8 in webrtc::RTPVideoHeaderH264& absl::variant_internal::TypedThrowBadVariantAccess<webrtc::RTPVideoHeaderH264&>() at /third_party/absl/types/internal/variant.h:315
#10 0x00000001043dd8ac in absl::variant_internal::VariantAccessResultImpl<2ul, absl::variant<webrtc::RTPVideoHeaderVP8, webrtc::RTPVideoHeaderVP9, webrtc::RTPVideoHeaderH264>&&&>::type absl::variant_internal::VariantCoreAccess::CheckedAccess<2ul, absl::variant<webrtc::RTPVideoHeaderVP8, webrtc::RTPVideoHeaderVP9, webrtc::RTPVideoHeaderH264>&>(absl::variant<webrtc::RTPVideoHeaderVP8, webrtc::RTPVideoHeaderVP9, webrtc::RTPVideoHeaderH264>&&&) at /third_party/absl/types/internal/variant.h:597
#11 0x00000001043db778 in webrtc::RTPVideoHeaderH264& absl::get<webrtc::RTPVideoHeaderH264, webrtc::RTPVideoHeaderVP8, webrtc::RTPVideoHeaderVP9, webrtc::RTPVideoHeaderH264>(absl::variant<webrtc::RTPVideoHeaderVP8, webrtc::RTPVideoHeaderVP9, webrtc::RTPVideoHeaderH264>&) at /third_party/absl/types/variant.h:299
#12 0x0000000104558bcc in webrtc::RtpPacketizer::Create(webrtc::VideoCodecType, unsigned long, unsigned long, webrtc::RTPVideoHeader const*, webrtc::FrameType) at webrtc/modules/rtp_rtcp/source/rtp_format.cc:30
Original change's description:
> Remove RTPVideoHeader::h264() accessors.
>
> Bug: none
> Change-Id: I043bcaf358575688b223bc3631506e148b47fd58
> Reviewed-on: https://webrtc-review.googlesource.com/88220
> Reviewed-by: Stefan Holmer <stefan@webrtc.org>
> Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
> Commit-Queue: Philip Eliasson <philipel@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#23971}
TBR=danilchap@webrtc.org,stefan@webrtc.org,philipel@webrtc.org
# Not skipping CQ checks because original CL landed > 1 day ago.
Bug: none
Change-Id: If99bcabdfe3cae7094f24e407bbe2f47233e46e3
Reviewed-on: https://webrtc-review.googlesource.com/88820
Commit-Queue: JT Teh <jtteh@webrtc.org>
Reviewed-by: Zeke Chin <tkchin@webrtc.org>
Reviewed-by: JT Teh <jtteh@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23993}
2018-07-16 17:17:17 +00:00
|
|
|
case kVideoCodecH264:
|
2018-07-17 10:16:41 +02:00
|
|
|
return absl::optional<size_t>(info->codecSpecific.H264.simulcast_idx);
|
2018-01-25 13:01:09 -08:00
|
|
|
case kVideoCodecMultiplex:
|
2016-04-20 05:05:54 -07:00
|
|
|
case kVideoCodecGeneric:
|
2018-07-17 10:16:41 +02:00
|
|
|
return absl::optional<size_t>(info->codecSpecific.generic.simulcast_idx);
|
2016-04-20 05:05:54 -07:00
|
|
|
default:
|
2018-07-17 10:16:41 +02:00
|
|
|
return absl::nullopt;
|
2018-06-13 12:07:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-20 05:05:54 -07:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules,
|
2017-10-06 10:04:04 +02:00
|
|
|
const std::vector<uint32_t>& ssrcs,
|
|
|
|
|
int payload_type,
|
|
|
|
|
const std::map<uint32_t, RtpPayloadState>& states)
|
2018-03-15 12:28:53 +01:00
|
|
|
: active_(false), rtp_modules_(rtp_modules), payload_type_(payload_type) {
|
2017-10-06 10:04:04 +02:00
|
|
|
RTC_DCHECK_EQ(ssrcs.size(), rtp_modules.size());
|
|
|
|
|
// SSRCs are assumed to be sorted in the same order as |rtp_modules|.
|
|
|
|
|
for (uint32_t ssrc : ssrcs) {
|
|
|
|
|
// Restore state if it previously existed.
|
|
|
|
|
const RtpPayloadState* state = nullptr;
|
|
|
|
|
auto it = states.find(ssrc);
|
|
|
|
|
if (it != states.end()) {
|
|
|
|
|
state = &it->second;
|
|
|
|
|
}
|
|
|
|
|
params_.push_back(RtpPayloadParams(ssrc, state));
|
|
|
|
|
}
|
2016-04-15 14:59:13 +02:00
|
|
|
}
|
2015-02-06 13:10:19 +00:00
|
|
|
|
|
|
|
|
PayloadRouter::~PayloadRouter() {}
|
|
|
|
|
|
2016-12-01 06:34:11 -08:00
|
|
|
void PayloadRouter::SetActive(bool active) {
|
2016-01-21 23:24:59 +01:00
|
|
|
rtc::CritScope lock(&crit_);
|
2016-02-26 16:31:37 +01:00
|
|
|
if (active_ == active)
|
|
|
|
|
return;
|
2018-02-02 08:46:16 -08:00
|
|
|
const std::vector<bool> active_modules(rtp_modules_.size(), active);
|
|
|
|
|
SetActiveModules(active_modules);
|
|
|
|
|
}
|
2016-09-23 15:52:06 +02:00
|
|
|
|
2018-02-02 08:46:16 -08:00
|
|
|
void PayloadRouter::SetActiveModules(const std::vector<bool> active_modules) {
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
RTC_DCHECK_EQ(rtp_modules_.size(), active_modules.size());
|
|
|
|
|
active_ = false;
|
|
|
|
|
for (size_t i = 0; i < active_modules.size(); ++i) {
|
|
|
|
|
if (active_modules[i]) {
|
|
|
|
|
active_ = true;
|
|
|
|
|
}
|
|
|
|
|
// Sends a kRtcpByeCode when going from true to false.
|
|
|
|
|
rtp_modules_[i]->SetSendingStatus(active_modules[i]);
|
|
|
|
|
// If set to false this module won't send media.
|
|
|
|
|
rtp_modules_[i]->SetSendingMediaStatus(active_modules[i]);
|
2016-09-23 15:52:06 +02:00
|
|
|
}
|
2015-02-06 13:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-01 06:34:11 -08:00
|
|
|
bool PayloadRouter::IsActive() {
|
2016-01-21 23:24:59 +01:00
|
|
|
rtc::CritScope lock(&crit_);
|
2015-02-19 10:29:32 +00:00
|
|
|
return active_ && !rtp_modules_.empty();
|
2015-02-06 13:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
2017-10-06 10:04:04 +02:00
|
|
|
std::map<uint32_t, RtpPayloadState> PayloadRouter::GetRtpPayloadStates() const {
|
|
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
std::map<uint32_t, RtpPayloadState> payload_states;
|
|
|
|
|
for (const auto& param : params_) {
|
|
|
|
|
payload_states[param.ssrc()] = param.state();
|
|
|
|
|
}
|
|
|
|
|
return payload_states;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 17:46:41 -07:00
|
|
|
EncodedImageCallback::Result PayloadRouter::OnEncodedImage(
|
|
|
|
|
const EncodedImage& encoded_image,
|
|
|
|
|
const CodecSpecificInfo* codec_specific_info,
|
|
|
|
|
const RTPFragmentationHeader* fragmentation) {
|
2016-01-21 23:24:59 +01:00
|
|
|
rtc::CritScope lock(&crit_);
|
2016-02-26 16:31:37 +01:00
|
|
|
RTC_DCHECK(!rtp_modules_.empty());
|
2016-09-23 15:52:06 +02:00
|
|
|
if (!active_)
|
2016-08-02 17:46:41 -07:00
|
|
|
return Result(Result::ERROR_SEND_FAILED);
|
2015-02-23 07:45:11 +00:00
|
|
|
|
2018-07-17 10:16:41 +02:00
|
|
|
size_t stream_index = GetSimulcastIdx(codec_specific_info).value_or(0);
|
2016-11-17 16:16:14 -08:00
|
|
|
RTC_DCHECK_LT(stream_index, rtp_modules_.size());
|
2018-07-17 10:16:41 +02:00
|
|
|
RTPVideoHeader rtp_video_header = params_[stream_index].GetRtpVideoHeader(
|
|
|
|
|
encoded_image, codec_specific_info);
|
2018-03-15 12:28:53 +01:00
|
|
|
|
2016-08-02 17:46:41 -07:00
|
|
|
uint32_t frame_id;
|
2018-02-02 08:46:16 -08:00
|
|
|
if (!rtp_modules_[stream_index]->Sending()) {
|
|
|
|
|
// The payload router could be active but this module isn't sending.
|
|
|
|
|
return Result(Result::ERROR_SEND_FAILED);
|
|
|
|
|
}
|
2016-11-17 16:16:14 -08:00
|
|
|
bool send_result = rtp_modules_[stream_index]->SendOutgoingData(
|
2016-04-20 05:05:54 -07:00
|
|
|
encoded_image._frameType, payload_type_, encoded_image._timeStamp,
|
|
|
|
|
encoded_image.capture_time_ms_, encoded_image._buffer,
|
2016-08-02 17:46:41 -07:00
|
|
|
encoded_image._length, fragmentation, &rtp_video_header, &frame_id);
|
2016-11-17 16:16:14 -08:00
|
|
|
if (!send_result)
|
2016-08-02 17:46:41 -07:00
|
|
|
return Result(Result::ERROR_SEND_FAILED);
|
|
|
|
|
|
|
|
|
|
return Result(Result::OK, frame_id);
|
2015-02-12 09:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-01 06:34:11 -08:00
|
|
|
void PayloadRouter::OnBitrateAllocationUpdated(
|
2018-04-23 12:32:22 +02:00
|
|
|
const VideoBitrateAllocation& bitrate) {
|
2016-12-01 06:34:11 -08:00
|
|
|
rtc::CritScope lock(&crit_);
|
|
|
|
|
if (IsActive()) {
|
|
|
|
|
if (rtp_modules_.size() == 1) {
|
|
|
|
|
// If spatial scalability is enabled, it is covered by a single stream.
|
|
|
|
|
rtp_modules_[0]->SetVideoBitrateAllocation(bitrate);
|
|
|
|
|
} else {
|
2018-07-17 10:16:41 +02:00
|
|
|
std::vector<absl::optional<VideoBitrateAllocation>> layer_bitrates =
|
|
|
|
|
bitrate.GetSimulcastAllocations();
|
2018-04-23 12:32:22 +02:00
|
|
|
// Simulcast is in use, split the VideoBitrateAllocation into one struct
|
|
|
|
|
// per rtp stream, moving over the temporal layer allocation.
|
2018-07-17 10:16:41 +02:00
|
|
|
for (size_t i = 0; i < rtp_modules_.size(); ++i) {
|
|
|
|
|
// The next spatial layer could be used if the current one is
|
|
|
|
|
// inactive.
|
|
|
|
|
if (layer_bitrates[i]) {
|
|
|
|
|
rtp_modules_[i]->SetVideoBitrateAllocation(*layer_bitrates[i]);
|
2017-11-15 14:58:23 +01:00
|
|
|
}
|
2016-12-01 06:34:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 13:10:19 +00:00
|
|
|
} // namespace webrtc
|