blob: 4eef06877db12063ec6cb1ed7845d80dac948ed4 [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"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000019
20namespace webrtc {
21
kjellander02b3d272016-04-20 05:05:54 -070022namespace {
23// Map information from info into rtp.
24void CopyCodecSpecific(const CodecSpecificInfo* info, RTPVideoHeader* rtp) {
25 RTC_DCHECK(info);
26 switch (info->codecType) {
27 case kVideoCodecVP8: {
28 rtp->codec = kRtpVideoVp8;
29 rtp->codecHeader.VP8.InitRTPVideoHeaderVP8();
kjellander02b3d272016-04-20 05:05:54 -070030 rtp->codecHeader.VP8.nonReference = info->codecSpecific.VP8.nonReference;
31 rtp->codecHeader.VP8.temporalIdx = info->codecSpecific.VP8.temporalIdx;
32 rtp->codecHeader.VP8.layerSync = info->codecSpecific.VP8.layerSync;
kjellander02b3d272016-04-20 05:05:54 -070033 rtp->codecHeader.VP8.keyIdx = info->codecSpecific.VP8.keyIdx;
34 rtp->simulcastIdx = info->codecSpecific.VP8.simulcastIdx;
35 return;
36 }
37 case kVideoCodecVP9: {
38 rtp->codec = kRtpVideoVp9;
39 rtp->codecHeader.VP9.InitRTPVideoHeaderVP9();
40 rtp->codecHeader.VP9.inter_pic_predicted =
41 info->codecSpecific.VP9.inter_pic_predicted;
42 rtp->codecHeader.VP9.flexible_mode =
43 info->codecSpecific.VP9.flexible_mode;
44 rtp->codecHeader.VP9.ss_data_available =
45 info->codecSpecific.VP9.ss_data_available;
Sergey Silkinc5a131a2018-04-24 20:13:49 +020046 rtp->codecHeader.VP9.non_ref_for_inter_layer_pred =
47 info->codecSpecific.VP9.non_ref_for_inter_layer_pred;
kjellander02b3d272016-04-20 05:05:54 -070048 rtp->codecHeader.VP9.temporal_idx = info->codecSpecific.VP9.temporal_idx;
49 rtp->codecHeader.VP9.spatial_idx = info->codecSpecific.VP9.spatial_idx;
50 rtp->codecHeader.VP9.temporal_up_switch =
51 info->codecSpecific.VP9.temporal_up_switch;
52 rtp->codecHeader.VP9.inter_layer_predicted =
53 info->codecSpecific.VP9.inter_layer_predicted;
54 rtp->codecHeader.VP9.gof_idx = info->codecSpecific.VP9.gof_idx;
55 rtp->codecHeader.VP9.num_spatial_layers =
56 info->codecSpecific.VP9.num_spatial_layers;
57
58 if (info->codecSpecific.VP9.ss_data_available) {
59 rtp->codecHeader.VP9.spatial_layer_resolution_present =
60 info->codecSpecific.VP9.spatial_layer_resolution_present;
61 if (info->codecSpecific.VP9.spatial_layer_resolution_present) {
62 for (size_t i = 0; i < info->codecSpecific.VP9.num_spatial_layers;
63 ++i) {
64 rtp->codecHeader.VP9.width[i] = info->codecSpecific.VP9.width[i];
65 rtp->codecHeader.VP9.height[i] = info->codecSpecific.VP9.height[i];
66 }
67 }
68 rtp->codecHeader.VP9.gof.CopyGofInfoVP9(info->codecSpecific.VP9.gof);
69 }
70
71 rtp->codecHeader.VP9.num_ref_pics = info->codecSpecific.VP9.num_ref_pics;
Sergey Silkin2a1f1832018-04-04 11:45:41 +020072 for (int i = 0; i < info->codecSpecific.VP9.num_ref_pics; ++i) {
kjellander02b3d272016-04-20 05:05:54 -070073 rtp->codecHeader.VP9.pid_diff[i] = info->codecSpecific.VP9.p_diff[i];
Sergey Silkin2a1f1832018-04-04 11:45:41 +020074 }
75 rtp->codecHeader.VP9.end_of_superframe =
76 info->codecSpecific.VP9.end_of_superframe;
kjellander02b3d272016-04-20 05:05:54 -070077 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
Niels Möllerbb894ff2018-03-15 12:28:53 +010096// State for setting picture id and tl0 pic idx, for VP8 and VP9
97// TODO(nisse): Make these properties not codec specific.
Åsa Persson4bece9a2017-10-06 10:04:04 +020098class PayloadRouter::RtpPayloadParams final {
99 public:
100 RtpPayloadParams(const uint32_t ssrc, const RtpPayloadState* state)
101 : ssrc_(ssrc) {
102 Random random(rtc::TimeMicros());
103 state_.picture_id =
104 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
Niels Möllerbb894ff2018-03-15 12:28:53 +0100105 state_.tl0_pic_idx = state ? state->tl0_pic_idx : (random.Rand<uint8_t>());
Åsa Persson4bece9a2017-10-06 10:04:04 +0200106 }
107 ~RtpPayloadParams() {}
108
Niels Möllerbb894ff2018-03-15 12:28:53 +0100109 void Set(RTPVideoHeader* rtp_video_header, bool first_frame_in_picture) {
110 // Always set picture id. Set tl0_pic_idx iff temporal index is set.
111 if (first_frame_in_picture) {
112 state_.picture_id =
113 (static_cast<uint16_t>(state_.picture_id) + 1) & 0x7FFF;
114 }
115 if (rtp_video_header->codec == kRtpVideoVp8) {
Åsa Persson4bece9a2017-10-06 10:04:04 +0200116 rtp_video_header->codecHeader.VP8.pictureId = state_.picture_id;
Niels Möllerbb894ff2018-03-15 12:28:53 +0100117
118 if (rtp_video_header->codecHeader.VP8.temporalIdx != kNoTemporalIdx) {
119 if (rtp_video_header->codecHeader.VP8.temporalIdx == 0) {
120 ++state_.tl0_pic_idx;
121 }
122 rtp_video_header->codecHeader.VP8.tl0PicIdx = state_.tl0_pic_idx;
123 }
124 }
125 if (rtp_video_header->codec == kRtpVideoVp9) {
126 rtp_video_header->codecHeader.VP9.picture_id = state_.picture_id;
127
128 // Note that in the case that we have no temporal layers but we do have
129 // spatial layers, packets will carry layering info with a temporal_idx of
130 // zero, and we then have to set and increment tl0_pic_idx.
131 if (rtp_video_header->codecHeader.VP9.temporal_idx != kNoTemporalIdx ||
132 rtp_video_header->codecHeader.VP9.spatial_idx != kNoSpatialIdx) {
133 if (first_frame_in_picture &&
134 (rtp_video_header->codecHeader.VP9.temporal_idx == 0 ||
135 rtp_video_header->codecHeader.VP9.temporal_idx ==
136 kNoTemporalIdx)) {
137 ++state_.tl0_pic_idx;
138 }
139 rtp_video_header->codecHeader.VP9.tl0_pic_idx = state_.tl0_pic_idx;
140 }
Åsa Persson4bece9a2017-10-06 10:04:04 +0200141 }
142 }
143
144 uint32_t ssrc() const { return ssrc_; }
145
146 RtpPayloadState state() const { return state_; }
147
148 private:
149 const uint32_t ssrc_;
150 RtpPayloadState state_;
151};
152
kjellander02b3d272016-04-20 05:05:54 -0700153PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules,
Åsa Persson4bece9a2017-10-06 10:04:04 +0200154 const std::vector<uint32_t>& ssrcs,
155 int payload_type,
156 const std::map<uint32_t, RtpPayloadState>& states)
Niels Möllerbb894ff2018-03-15 12:28:53 +0100157 : active_(false), rtp_modules_(rtp_modules), payload_type_(payload_type) {
Åsa Persson4bece9a2017-10-06 10:04:04 +0200158 RTC_DCHECK_EQ(ssrcs.size(), rtp_modules.size());
159 // SSRCs are assumed to be sorted in the same order as |rtp_modules|.
160 for (uint32_t ssrc : ssrcs) {
161 // Restore state if it previously existed.
162 const RtpPayloadState* state = nullptr;
163 auto it = states.find(ssrc);
164 if (it != states.end()) {
165 state = &it->second;
166 }
167 params_.push_back(RtpPayloadParams(ssrc, state));
168 }
Per83d09102016-04-15 14:59:13 +0200169}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000170
171PayloadRouter::~PayloadRouter() {}
172
sprang1a646ee2016-12-01 06:34:11 -0800173void PayloadRouter::SetActive(bool active) {
Tommi97888bd2016-01-21 23:24:59 +0100174 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100175 if (active_ == active)
176 return;
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800177 const std::vector<bool> active_modules(rtp_modules_.size(), active);
178 SetActiveModules(active_modules);
179}
Per512ecb32016-09-23 15:52:06 +0200180
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800181void PayloadRouter::SetActiveModules(const std::vector<bool> active_modules) {
182 rtc::CritScope lock(&crit_);
183 RTC_DCHECK_EQ(rtp_modules_.size(), active_modules.size());
184 active_ = false;
185 for (size_t i = 0; i < active_modules.size(); ++i) {
186 if (active_modules[i]) {
187 active_ = true;
188 }
189 // Sends a kRtcpByeCode when going from true to false.
190 rtp_modules_[i]->SetSendingStatus(active_modules[i]);
191 // If set to false this module won't send media.
192 rtp_modules_[i]->SetSendingMediaStatus(active_modules[i]);
Per512ecb32016-09-23 15:52:06 +0200193 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000194}
195
sprang1a646ee2016-12-01 06:34:11 -0800196bool PayloadRouter::IsActive() {
Tommi97888bd2016-01-21 23:24:59 +0100197 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000198 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000199}
200
Åsa Persson4bece9a2017-10-06 10:04:04 +0200201std::map<uint32_t, RtpPayloadState> PayloadRouter::GetRtpPayloadStates() const {
202 rtc::CritScope lock(&crit_);
203 std::map<uint32_t, RtpPayloadState> payload_states;
204 for (const auto& param : params_) {
205 payload_states[param.ssrc()] = param.state();
206 }
207 return payload_states;
208}
209
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700210EncodedImageCallback::Result PayloadRouter::OnEncodedImage(
211 const EncodedImage& encoded_image,
212 const CodecSpecificInfo* codec_specific_info,
213 const RTPFragmentationHeader* fragmentation) {
Tommi97888bd2016-01-21 23:24:59 +0100214 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100215 RTC_DCHECK(!rtp_modules_.empty());
Per512ecb32016-09-23 15:52:06 +0200216 if (!active_)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700217 return Result(Result::ERROR_SEND_FAILED);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000218
kjellander02b3d272016-04-20 05:05:54 -0700219 RTPVideoHeader rtp_video_header;
220 memset(&rtp_video_header, 0, sizeof(RTPVideoHeader));
221 if (codec_specific_info)
222 CopyCodecSpecific(codec_specific_info, &rtp_video_header);
223 rtp_video_header.rotation = encoded_image.rotation_;
ilnik00d802b2017-04-11 10:34:31 -0700224 rtp_video_header.content_type = encoded_image.content_type_;
Niels Möllerc241af92017-11-10 08:51:29 +0100225 if (encoded_image.timing_.flags != TimingFrameFlags::kInvalid &&
226 encoded_image.timing_.flags != TimingFrameFlags::kNotTriggered) {
ilnik04f4d122017-06-19 07:18:55 -0700227 rtp_video_header.video_timing.encode_start_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700228 VideoSendTiming::GetDeltaCappedMs(
229 encoded_image.capture_time_ms_,
230 encoded_image.timing_.encode_start_ms);
ilnik04f4d122017-06-19 07:18:55 -0700231 rtp_video_header.video_timing.encode_finish_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700232 VideoSendTiming::GetDeltaCappedMs(
233 encoded_image.capture_time_ms_,
234 encoded_image.timing_.encode_finish_ms);
ilnik04f4d122017-06-19 07:18:55 -0700235 rtp_video_header.video_timing.packetization_finish_delta_ms = 0;
236 rtp_video_header.video_timing.pacer_exit_delta_ms = 0;
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100237 rtp_video_header.video_timing.network_timestamp_delta_ms = 0;
238 rtp_video_header.video_timing.network2_timestamp_delta_ms = 0;
Niels Möllerc241af92017-11-10 08:51:29 +0100239 rtp_video_header.video_timing.flags = encoded_image.timing_.flags;
240 } else {
241 rtp_video_header.video_timing.flags = TimingFrameFlags::kInvalid;
ilnik04f4d122017-06-19 07:18:55 -0700242 }
isheriff6b4b5f32016-06-08 00:24:21 -0700243 rtp_video_header.playout_delay = encoded_image.playout_delay_;
kjellander02b3d272016-04-20 05:05:54 -0700244
sergeyu7b9feee2016-11-17 16:16:14 -0800245 int stream_index = rtp_video_header.simulcastIdx;
246 RTC_DCHECK_LT(stream_index, rtp_modules_.size());
Niels Möllerbb894ff2018-03-15 12:28:53 +0100247
248 // Sets picture id and tl0 pic idx.
249 const bool first_frame_in_picture =
250 (codec_specific_info && codec_specific_info->codecType == kVideoCodecVP9)
251 ? codec_specific_info->codecSpecific.VP9.first_frame_in_picture
252 : true;
253 params_[stream_index].Set(&rtp_video_header, first_frame_in_picture);
254
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700255 uint32_t frame_id;
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800256 if (!rtp_modules_[stream_index]->Sending()) {
257 // The payload router could be active but this module isn't sending.
258 return Result(Result::ERROR_SEND_FAILED);
259 }
sergeyu7b9feee2016-11-17 16:16:14 -0800260 bool send_result = rtp_modules_[stream_index]->SendOutgoingData(
kjellander02b3d272016-04-20 05:05:54 -0700261 encoded_image._frameType, payload_type_, encoded_image._timeStamp,
262 encoded_image.capture_time_ms_, encoded_image._buffer,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700263 encoded_image._length, fragmentation, &rtp_video_header, &frame_id);
sergeyu7b9feee2016-11-17 16:16:14 -0800264 if (!send_result)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700265 return Result(Result::ERROR_SEND_FAILED);
266
267 return Result(Result::OK, frame_id);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000268}
269
sprang1a646ee2016-12-01 06:34:11 -0800270void PayloadRouter::OnBitrateAllocationUpdated(
Erik Språng566124a2018-04-23 12:32:22 +0200271 const VideoBitrateAllocation& bitrate) {
sprang1a646ee2016-12-01 06:34:11 -0800272 rtc::CritScope lock(&crit_);
273 if (IsActive()) {
274 if (rtp_modules_.size() == 1) {
275 // If spatial scalability is enabled, it is covered by a single stream.
276 rtp_modules_[0]->SetVideoBitrateAllocation(bitrate);
277 } else {
Erik Språng566124a2018-04-23 12:32:22 +0200278 // Simulcast is in use, split the VideoBitrateAllocation into one struct
279 // per rtp stream, moving over the temporal layer allocation.
sprang1a646ee2016-12-01 06:34:11 -0800280 for (size_t si = 0; si < rtp_modules_.size(); ++si) {
sprangd0fc37a2017-06-22 05:40:25 -0700281 // Don't send empty TargetBitrate messages on streams not being relayed.
Seth Hampson46e31ba2018-01-18 10:39:54 -0800282 if (!bitrate.IsSpatialLayerUsed(si)) {
283 // The next spatial layer could be used if the current one is
284 // inactive.
285 continue;
286 }
sprangd0fc37a2017-06-22 05:40:25 -0700287
Erik Språng566124a2018-04-23 12:32:22 +0200288 VideoBitrateAllocation layer_bitrate;
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100289 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
290 if (bitrate.HasBitrate(si, tl))
291 layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl));
292 }
sprang1a646ee2016-12-01 06:34:11 -0800293 rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate);
294 }
295 }
296 }
297}
298
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000299} // namespace webrtc