blob: 4ecb620c7c03cb29e51c6b7ce70faff103fbb065 [file] [log] [blame]
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +00001/*
2 * Copyright (c) 2015 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/payload_router.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/rtp_rtcp/include/rtp_rtcp.h"
14#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
15#include "modules/video_coding/include/video_codec_interface.h"
16#include "rtc_base/checks.h"
Åsa Persson4bece9a2017-10-06 10:04:04 +020017#include "rtc_base/random.h"
18#include "rtc_base/timeutils.h"
19#include "system_wrappers/include/field_trial.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000020
21namespace webrtc {
22
kjellander02b3d272016-04-20 05:05:54 -070023namespace {
24// Map information from info into rtp.
25void CopyCodecSpecific(const CodecSpecificInfo* info, RTPVideoHeader* rtp) {
26 RTC_DCHECK(info);
27 switch (info->codecType) {
28 case kVideoCodecVP8: {
29 rtp->codec = kRtpVideoVp8;
30 rtp->codecHeader.VP8.InitRTPVideoHeaderVP8();
31 rtp->codecHeader.VP8.pictureId = info->codecSpecific.VP8.pictureId;
32 rtp->codecHeader.VP8.nonReference = info->codecSpecific.VP8.nonReference;
33 rtp->codecHeader.VP8.temporalIdx = info->codecSpecific.VP8.temporalIdx;
34 rtp->codecHeader.VP8.layerSync = info->codecSpecific.VP8.layerSync;
35 rtp->codecHeader.VP8.tl0PicIdx = info->codecSpecific.VP8.tl0PicIdx;
36 rtp->codecHeader.VP8.keyIdx = info->codecSpecific.VP8.keyIdx;
37 rtp->simulcastIdx = info->codecSpecific.VP8.simulcastIdx;
38 return;
39 }
40 case kVideoCodecVP9: {
41 rtp->codec = kRtpVideoVp9;
42 rtp->codecHeader.VP9.InitRTPVideoHeaderVP9();
43 rtp->codecHeader.VP9.inter_pic_predicted =
44 info->codecSpecific.VP9.inter_pic_predicted;
45 rtp->codecHeader.VP9.flexible_mode =
46 info->codecSpecific.VP9.flexible_mode;
47 rtp->codecHeader.VP9.ss_data_available =
48 info->codecSpecific.VP9.ss_data_available;
49 rtp->codecHeader.VP9.picture_id = info->codecSpecific.VP9.picture_id;
50 rtp->codecHeader.VP9.tl0_pic_idx = info->codecSpecific.VP9.tl0_pic_idx;
51 rtp->codecHeader.VP9.temporal_idx = info->codecSpecific.VP9.temporal_idx;
52 rtp->codecHeader.VP9.spatial_idx = info->codecSpecific.VP9.spatial_idx;
53 rtp->codecHeader.VP9.temporal_up_switch =
54 info->codecSpecific.VP9.temporal_up_switch;
55 rtp->codecHeader.VP9.inter_layer_predicted =
56 info->codecSpecific.VP9.inter_layer_predicted;
57 rtp->codecHeader.VP9.gof_idx = info->codecSpecific.VP9.gof_idx;
58 rtp->codecHeader.VP9.num_spatial_layers =
59 info->codecSpecific.VP9.num_spatial_layers;
60
61 if (info->codecSpecific.VP9.ss_data_available) {
62 rtp->codecHeader.VP9.spatial_layer_resolution_present =
63 info->codecSpecific.VP9.spatial_layer_resolution_present;
64 if (info->codecSpecific.VP9.spatial_layer_resolution_present) {
65 for (size_t i = 0; i < info->codecSpecific.VP9.num_spatial_layers;
66 ++i) {
67 rtp->codecHeader.VP9.width[i] = info->codecSpecific.VP9.width[i];
68 rtp->codecHeader.VP9.height[i] = info->codecSpecific.VP9.height[i];
69 }
70 }
71 rtp->codecHeader.VP9.gof.CopyGofInfoVP9(info->codecSpecific.VP9.gof);
72 }
73
74 rtp->codecHeader.VP9.num_ref_pics = info->codecSpecific.VP9.num_ref_pics;
75 for (int i = 0; i < info->codecSpecific.VP9.num_ref_pics; ++i)
76 rtp->codecHeader.VP9.pid_diff[i] = info->codecSpecific.VP9.p_diff[i];
77 return;
78 }
79 case kVideoCodecH264:
80 rtp->codec = kRtpVideoH264;
hta9aa96882016-12-06 05:36:03 -080081 rtp->codecHeader.H264.packetization_mode =
82 info->codecSpecific.H264.packetization_mode;
kjellander02b3d272016-04-20 05:05:54 -070083 return;
Emircan Uysalerd7ae3c32018-01-25 13:01:09 -080084 case kVideoCodecMultiplex:
kjellander02b3d272016-04-20 05:05:54 -070085 case kVideoCodecGeneric:
86 rtp->codec = kRtpVideoGeneric;
87 rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx;
88 return;
89 default:
90 return;
91 }
92}
perkjbc75d972016-05-02 06:31:25 -070093
kjellander02b3d272016-04-20 05:05:54 -070094} // namespace
95
Åsa Persson4bece9a2017-10-06 10:04:04 +020096// Currently only used if forced fallback for VP8 is enabled.
97// Consider adding tl0idx and set for VP8 and VP9.
98// Make picture id not codec specific.
99class PayloadRouter::RtpPayloadParams final {
100 public:
101 RtpPayloadParams(const uint32_t ssrc, const RtpPayloadState* state)
102 : ssrc_(ssrc) {
103 Random random(rtc::TimeMicros());
104 state_.picture_id =
105 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
106 }
107 ~RtpPayloadParams() {}
108
109 void Set(RTPVideoHeader* rtp_video_header) {
110 if (rtp_video_header->codec == kRtpVideoVp8 &&
111 rtp_video_header->codecHeader.VP8.pictureId != kNoPictureId) {
112 rtp_video_header->codecHeader.VP8.pictureId = state_.picture_id;
113 state_.picture_id = (state_.picture_id + 1) & 0x7FFF;
114 }
115 }
116
117 uint32_t ssrc() const { return ssrc_; }
118
119 RtpPayloadState state() const { return state_; }
120
121 private:
122 const uint32_t ssrc_;
123 RtpPayloadState state_;
124};
125
kjellander02b3d272016-04-20 05:05:54 -0700126PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules,
Åsa Persson4bece9a2017-10-06 10:04:04 +0200127 const std::vector<uint32_t>& ssrcs,
128 int payload_type,
129 const std::map<uint32_t, RtpPayloadState>& states)
kjellander02b3d272016-04-20 05:05:54 -0700130 : active_(false),
kjellander02b3d272016-04-20 05:05:54 -0700131 rtp_modules_(rtp_modules),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200132 payload_type_(payload_type),
133 forced_fallback_enabled_((webrtc::field_trial::IsEnabled(
Åsa Persson45bbc8a2017-11-13 10:16:47 +0100134 "WebRTC-VP8-Forced-Fallback-Encoder-v2"))) {
Åsa Persson4bece9a2017-10-06 10:04:04 +0200135 RTC_DCHECK_EQ(ssrcs.size(), rtp_modules.size());
136 // SSRCs are assumed to be sorted in the same order as |rtp_modules|.
137 for (uint32_t ssrc : ssrcs) {
138 // Restore state if it previously existed.
139 const RtpPayloadState* state = nullptr;
140 auto it = states.find(ssrc);
141 if (it != states.end()) {
142 state = &it->second;
143 }
144 params_.push_back(RtpPayloadParams(ssrc, state));
145 }
Per83d09102016-04-15 14:59:13 +0200146}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000147
148PayloadRouter::~PayloadRouter() {}
149
sprang1a646ee2016-12-01 06:34:11 -0800150void PayloadRouter::SetActive(bool active) {
Tommi97888bd2016-01-21 23:24:59 +0100151 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100152 if (active_ == active)
153 return;
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800154 const std::vector<bool> active_modules(rtp_modules_.size(), active);
155 SetActiveModules(active_modules);
156}
Per512ecb32016-09-23 15:52:06 +0200157
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800158void PayloadRouter::SetActiveModules(const std::vector<bool> active_modules) {
159 rtc::CritScope lock(&crit_);
160 RTC_DCHECK_EQ(rtp_modules_.size(), active_modules.size());
161 active_ = false;
162 for (size_t i = 0; i < active_modules.size(); ++i) {
163 if (active_modules[i]) {
164 active_ = true;
165 }
166 // Sends a kRtcpByeCode when going from true to false.
167 rtp_modules_[i]->SetSendingStatus(active_modules[i]);
168 // If set to false this module won't send media.
169 rtp_modules_[i]->SetSendingMediaStatus(active_modules[i]);
Per512ecb32016-09-23 15:52:06 +0200170 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000171}
172
sprang1a646ee2016-12-01 06:34:11 -0800173bool PayloadRouter::IsActive() {
Tommi97888bd2016-01-21 23:24:59 +0100174 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000175 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000176}
177
Åsa Persson4bece9a2017-10-06 10:04:04 +0200178std::map<uint32_t, RtpPayloadState> PayloadRouter::GetRtpPayloadStates() const {
179 rtc::CritScope lock(&crit_);
180 std::map<uint32_t, RtpPayloadState> payload_states;
181 for (const auto& param : params_) {
182 payload_states[param.ssrc()] = param.state();
183 }
184 return payload_states;
185}
186
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700187EncodedImageCallback::Result PayloadRouter::OnEncodedImage(
188 const EncodedImage& encoded_image,
189 const CodecSpecificInfo* codec_specific_info,
190 const RTPFragmentationHeader* fragmentation) {
Tommi97888bd2016-01-21 23:24:59 +0100191 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100192 RTC_DCHECK(!rtp_modules_.empty());
Per512ecb32016-09-23 15:52:06 +0200193 if (!active_)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700194 return Result(Result::ERROR_SEND_FAILED);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000195
kjellander02b3d272016-04-20 05:05:54 -0700196 RTPVideoHeader rtp_video_header;
197 memset(&rtp_video_header, 0, sizeof(RTPVideoHeader));
198 if (codec_specific_info)
199 CopyCodecSpecific(codec_specific_info, &rtp_video_header);
200 rtp_video_header.rotation = encoded_image.rotation_;
ilnik00d802b2017-04-11 10:34:31 -0700201 rtp_video_header.content_type = encoded_image.content_type_;
Niels Möllerc241af92017-11-10 08:51:29 +0100202 if (encoded_image.timing_.flags != TimingFrameFlags::kInvalid &&
203 encoded_image.timing_.flags != TimingFrameFlags::kNotTriggered) {
ilnik04f4d122017-06-19 07:18:55 -0700204 rtp_video_header.video_timing.encode_start_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700205 VideoSendTiming::GetDeltaCappedMs(
206 encoded_image.capture_time_ms_,
207 encoded_image.timing_.encode_start_ms);
ilnik04f4d122017-06-19 07:18:55 -0700208 rtp_video_header.video_timing.encode_finish_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700209 VideoSendTiming::GetDeltaCappedMs(
210 encoded_image.capture_time_ms_,
211 encoded_image.timing_.encode_finish_ms);
ilnik04f4d122017-06-19 07:18:55 -0700212 rtp_video_header.video_timing.packetization_finish_delta_ms = 0;
213 rtp_video_header.video_timing.pacer_exit_delta_ms = 0;
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100214 rtp_video_header.video_timing.network_timestamp_delta_ms = 0;
215 rtp_video_header.video_timing.network2_timestamp_delta_ms = 0;
Niels Möllerc241af92017-11-10 08:51:29 +0100216 rtp_video_header.video_timing.flags = encoded_image.timing_.flags;
217 } else {
218 rtp_video_header.video_timing.flags = TimingFrameFlags::kInvalid;
ilnik04f4d122017-06-19 07:18:55 -0700219 }
isheriff6b4b5f32016-06-08 00:24:21 -0700220 rtp_video_header.playout_delay = encoded_image.playout_delay_;
kjellander02b3d272016-04-20 05:05:54 -0700221
sergeyu7b9feee2016-11-17 16:16:14 -0800222 int stream_index = rtp_video_header.simulcastIdx;
223 RTC_DCHECK_LT(stream_index, rtp_modules_.size());
Åsa Persson4bece9a2017-10-06 10:04:04 +0200224 if (forced_fallback_enabled_) {
225 // Sets picture id. The SW and HW encoder have separate picture id
226 // sequences, set picture id to not cause sequence discontinuties at encoder
227 // changes.
228 params_[stream_index].Set(&rtp_video_header);
229 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700230 uint32_t frame_id;
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800231 if (!rtp_modules_[stream_index]->Sending()) {
232 // The payload router could be active but this module isn't sending.
233 return Result(Result::ERROR_SEND_FAILED);
234 }
sergeyu7b9feee2016-11-17 16:16:14 -0800235 bool send_result = rtp_modules_[stream_index]->SendOutgoingData(
kjellander02b3d272016-04-20 05:05:54 -0700236 encoded_image._frameType, payload_type_, encoded_image._timeStamp,
237 encoded_image.capture_time_ms_, encoded_image._buffer,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700238 encoded_image._length, fragmentation, &rtp_video_header, &frame_id);
sergeyu7b9feee2016-11-17 16:16:14 -0800239 if (!send_result)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700240 return Result(Result::ERROR_SEND_FAILED);
241
242 return Result(Result::OK, frame_id);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000243}
244
sprang1a646ee2016-12-01 06:34:11 -0800245void PayloadRouter::OnBitrateAllocationUpdated(
246 const BitrateAllocation& bitrate) {
247 rtc::CritScope lock(&crit_);
248 if (IsActive()) {
249 if (rtp_modules_.size() == 1) {
250 // If spatial scalability is enabled, it is covered by a single stream.
251 rtp_modules_[0]->SetVideoBitrateAllocation(bitrate);
252 } else {
253 // Simulcast is in use, split the BitrateAllocation into one struct per
254 // rtp stream, moving over the temporal layer allocation.
255 for (size_t si = 0; si < rtp_modules_.size(); ++si) {
sprangd0fc37a2017-06-22 05:40:25 -0700256 // Don't send empty TargetBitrate messages on streams not being relayed.
Seth Hampson46e31ba2018-01-18 10:39:54 -0800257 if (!bitrate.IsSpatialLayerUsed(si)) {
258 // The next spatial layer could be used if the current one is
259 // inactive.
260 continue;
261 }
sprangd0fc37a2017-06-22 05:40:25 -0700262
sprang1a646ee2016-12-01 06:34:11 -0800263 BitrateAllocation layer_bitrate;
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100264 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
265 if (bitrate.HasBitrate(si, tl))
266 layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl));
267 }
sprang1a646ee2016-12-01 06:34:11 -0800268 rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate);
269 }
270 }
271 }
272}
273
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000274} // namespace webrtc