blob: f22183287d93171bbb6a8e56c197d3ac4858e350 [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 Uysaler90612a62017-11-28 09:45:25 -080084 case kVideoCodecStereo: {
85 rtp->codec = kRtpVideoStereo;
86 RtpVideoCodecTypes associated_codec_type = kRtpVideoNone;
87 switch (info->codecSpecific.stereo.associated_codec_type) {
88 case kVideoCodecVP8:
89 associated_codec_type = kRtpVideoVp8;
90 break;
91 case kVideoCodecVP9:
92 associated_codec_type = kRtpVideoVp9;
93 break;
94 case kVideoCodecH264:
95 associated_codec_type = kRtpVideoH264;
96 break;
97 default:
98 RTC_NOTREACHED();
99 }
100 rtp->codecHeader.stereo.associated_codec_type = associated_codec_type;
101 rtp->codecHeader.stereo.indices = info->codecSpecific.stereo.indices;
102 return;
103 }
kjellander02b3d272016-04-20 05:05:54 -0700104 case kVideoCodecGeneric:
105 rtp->codec = kRtpVideoGeneric;
106 rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx;
107 return;
108 default:
109 return;
110 }
111}
perkjbc75d972016-05-02 06:31:25 -0700112
kjellander02b3d272016-04-20 05:05:54 -0700113} // namespace
114
Åsa Persson4bece9a2017-10-06 10:04:04 +0200115// Currently only used if forced fallback for VP8 is enabled.
116// Consider adding tl0idx and set for VP8 and VP9.
117// Make picture id not codec specific.
118class PayloadRouter::RtpPayloadParams final {
119 public:
120 RtpPayloadParams(const uint32_t ssrc, const RtpPayloadState* state)
121 : ssrc_(ssrc) {
122 Random random(rtc::TimeMicros());
123 state_.picture_id =
124 state ? state->picture_id : (random.Rand<int16_t>() & 0x7FFF);
125 }
126 ~RtpPayloadParams() {}
127
128 void Set(RTPVideoHeader* rtp_video_header) {
129 if (rtp_video_header->codec == kRtpVideoVp8 &&
130 rtp_video_header->codecHeader.VP8.pictureId != kNoPictureId) {
131 rtp_video_header->codecHeader.VP8.pictureId = state_.picture_id;
132 state_.picture_id = (state_.picture_id + 1) & 0x7FFF;
133 }
134 }
135
136 uint32_t ssrc() const { return ssrc_; }
137
138 RtpPayloadState state() const { return state_; }
139
140 private:
141 const uint32_t ssrc_;
142 RtpPayloadState state_;
143};
144
kjellander02b3d272016-04-20 05:05:54 -0700145PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules,
Åsa Persson4bece9a2017-10-06 10:04:04 +0200146 const std::vector<uint32_t>& ssrcs,
147 int payload_type,
148 const std::map<uint32_t, RtpPayloadState>& states)
kjellander02b3d272016-04-20 05:05:54 -0700149 : active_(false),
kjellander02b3d272016-04-20 05:05:54 -0700150 rtp_modules_(rtp_modules),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200151 payload_type_(payload_type),
152 forced_fallback_enabled_((webrtc::field_trial::IsEnabled(
Åsa Persson45bbc8a2017-11-13 10:16:47 +0100153 "WebRTC-VP8-Forced-Fallback-Encoder-v2"))) {
Åsa Persson4bece9a2017-10-06 10:04:04 +0200154 RTC_DCHECK_EQ(ssrcs.size(), rtp_modules.size());
155 // SSRCs are assumed to be sorted in the same order as |rtp_modules|.
156 for (uint32_t ssrc : ssrcs) {
157 // Restore state if it previously existed.
158 const RtpPayloadState* state = nullptr;
159 auto it = states.find(ssrc);
160 if (it != states.end()) {
161 state = &it->second;
162 }
163 params_.push_back(RtpPayloadParams(ssrc, state));
164 }
Per83d09102016-04-15 14:59:13 +0200165}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000166
167PayloadRouter::~PayloadRouter() {}
168
sprang1a646ee2016-12-01 06:34:11 -0800169void PayloadRouter::SetActive(bool active) {
Tommi97888bd2016-01-21 23:24:59 +0100170 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100171 if (active_ == active)
172 return;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000173 active_ = active;
Per512ecb32016-09-23 15:52:06 +0200174
175 for (auto& module : rtp_modules_) {
176 module->SetSendingStatus(active_);
177 module->SetSendingMediaStatus(active_);
178 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000179}
180
sprang1a646ee2016-12-01 06:34:11 -0800181bool PayloadRouter::IsActive() {
Tommi97888bd2016-01-21 23:24:59 +0100182 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000183 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000184}
185
Åsa Persson4bece9a2017-10-06 10:04:04 +0200186std::map<uint32_t, RtpPayloadState> PayloadRouter::GetRtpPayloadStates() const {
187 rtc::CritScope lock(&crit_);
188 std::map<uint32_t, RtpPayloadState> payload_states;
189 for (const auto& param : params_) {
190 payload_states[param.ssrc()] = param.state();
191 }
192 return payload_states;
193}
194
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700195EncodedImageCallback::Result PayloadRouter::OnEncodedImage(
196 const EncodedImage& encoded_image,
197 const CodecSpecificInfo* codec_specific_info,
198 const RTPFragmentationHeader* fragmentation) {
Tommi97888bd2016-01-21 23:24:59 +0100199 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100200 RTC_DCHECK(!rtp_modules_.empty());
Per512ecb32016-09-23 15:52:06 +0200201 if (!active_)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700202 return Result(Result::ERROR_SEND_FAILED);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000203
kjellander02b3d272016-04-20 05:05:54 -0700204 RTPVideoHeader rtp_video_header;
205 memset(&rtp_video_header, 0, sizeof(RTPVideoHeader));
206 if (codec_specific_info)
207 CopyCodecSpecific(codec_specific_info, &rtp_video_header);
208 rtp_video_header.rotation = encoded_image.rotation_;
ilnik00d802b2017-04-11 10:34:31 -0700209 rtp_video_header.content_type = encoded_image.content_type_;
Niels Möllerc241af92017-11-10 08:51:29 +0100210 if (encoded_image.timing_.flags != TimingFrameFlags::kInvalid &&
211 encoded_image.timing_.flags != TimingFrameFlags::kNotTriggered) {
ilnik04f4d122017-06-19 07:18:55 -0700212 rtp_video_header.video_timing.encode_start_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700213 VideoSendTiming::GetDeltaCappedMs(
214 encoded_image.capture_time_ms_,
215 encoded_image.timing_.encode_start_ms);
ilnik04f4d122017-06-19 07:18:55 -0700216 rtp_video_header.video_timing.encode_finish_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700217 VideoSendTiming::GetDeltaCappedMs(
218 encoded_image.capture_time_ms_,
219 encoded_image.timing_.encode_finish_ms);
ilnik04f4d122017-06-19 07:18:55 -0700220 rtp_video_header.video_timing.packetization_finish_delta_ms = 0;
221 rtp_video_header.video_timing.pacer_exit_delta_ms = 0;
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100222 rtp_video_header.video_timing.network_timestamp_delta_ms = 0;
223 rtp_video_header.video_timing.network2_timestamp_delta_ms = 0;
Niels Möllerc241af92017-11-10 08:51:29 +0100224 rtp_video_header.video_timing.flags = encoded_image.timing_.flags;
225 } else {
226 rtp_video_header.video_timing.flags = TimingFrameFlags::kInvalid;
ilnik04f4d122017-06-19 07:18:55 -0700227 }
isheriff6b4b5f32016-06-08 00:24:21 -0700228 rtp_video_header.playout_delay = encoded_image.playout_delay_;
kjellander02b3d272016-04-20 05:05:54 -0700229
sergeyu7b9feee2016-11-17 16:16:14 -0800230 int stream_index = rtp_video_header.simulcastIdx;
231 RTC_DCHECK_LT(stream_index, rtp_modules_.size());
Åsa Persson4bece9a2017-10-06 10:04:04 +0200232 if (forced_fallback_enabled_) {
233 // Sets picture id. The SW and HW encoder have separate picture id
234 // sequences, set picture id to not cause sequence discontinuties at encoder
235 // changes.
236 params_[stream_index].Set(&rtp_video_header);
237 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700238 uint32_t frame_id;
sergeyu7b9feee2016-11-17 16:16:14 -0800239 bool send_result = rtp_modules_[stream_index]->SendOutgoingData(
kjellander02b3d272016-04-20 05:05:54 -0700240 encoded_image._frameType, payload_type_, encoded_image._timeStamp,
241 encoded_image.capture_time_ms_, encoded_image._buffer,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700242 encoded_image._length, fragmentation, &rtp_video_header, &frame_id);
sergeyu7b9feee2016-11-17 16:16:14 -0800243 if (!send_result)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700244 return Result(Result::ERROR_SEND_FAILED);
245
246 return Result(Result::OK, frame_id);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000247}
248
sprang1a646ee2016-12-01 06:34:11 -0800249void PayloadRouter::OnBitrateAllocationUpdated(
250 const BitrateAllocation& bitrate) {
251 rtc::CritScope lock(&crit_);
252 if (IsActive()) {
253 if (rtp_modules_.size() == 1) {
254 // If spatial scalability is enabled, it is covered by a single stream.
255 rtp_modules_[0]->SetVideoBitrateAllocation(bitrate);
256 } else {
257 // Simulcast is in use, split the BitrateAllocation into one struct per
258 // rtp stream, moving over the temporal layer allocation.
259 for (size_t si = 0; si < rtp_modules_.size(); ++si) {
sprangd0fc37a2017-06-22 05:40:25 -0700260 // Don't send empty TargetBitrate messages on streams not being relayed.
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100261 if (!bitrate.IsSpatialLayerUsed(si))
sprangd0fc37a2017-06-22 05:40:25 -0700262 break;
263
sprang1a646ee2016-12-01 06:34:11 -0800264 BitrateAllocation layer_bitrate;
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100265 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
266 if (bitrate.HasBitrate(si, tl))
267 layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl));
268 }
sprang1a646ee2016-12-01 06:34:11 -0800269 rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate);
270 }
271 }
272 }
273}
274
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000275} // namespace webrtc