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-16 15:03:08 +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-16 15:03:08 +02:00
|
|
|
case kVideoCodecVP8:
|
|
|
|
|
return absl::optional<size_t>(info->codecSpecific.VP8.simulcastIdx);
|
|
|
|
|
case kVideoCodecH264:
|
|
|
|
|
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-16 15:03:08 +02:00
|
|
|
return absl::optional<size_t>(info->codecSpecific.generic.simulcast_idx);
|
2016-04-20 05:05:54 -07:00
|
|
|
default:
|
2018-07-16 15:03:08 +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-16 15:03:08 +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-16 15:03:08 +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-16 15:03:08 +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-16 15:03:08 +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
|