blob: 1d16fbd8c0a627d7d68928e529a05c8d4a2155b8 [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;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000154 active_ = active;
Per512ecb32016-09-23 15:52:06 +0200155
156 for (auto& module : rtp_modules_) {
157 module->SetSendingStatus(active_);
158 module->SetSendingMediaStatus(active_);
159 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000160}
161
sprang1a646ee2016-12-01 06:34:11 -0800162bool PayloadRouter::IsActive() {
Tommi97888bd2016-01-21 23:24:59 +0100163 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000164 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000165}
166
Åsa Persson4bece9a2017-10-06 10:04:04 +0200167std::map<uint32_t, RtpPayloadState> PayloadRouter::GetRtpPayloadStates() const {
168 rtc::CritScope lock(&crit_);
169 std::map<uint32_t, RtpPayloadState> payload_states;
170 for (const auto& param : params_) {
171 payload_states[param.ssrc()] = param.state();
172 }
173 return payload_states;
174}
175
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700176EncodedImageCallback::Result PayloadRouter::OnEncodedImage(
177 const EncodedImage& encoded_image,
178 const CodecSpecificInfo* codec_specific_info,
179 const RTPFragmentationHeader* fragmentation) {
Tommi97888bd2016-01-21 23:24:59 +0100180 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100181 RTC_DCHECK(!rtp_modules_.empty());
Per512ecb32016-09-23 15:52:06 +0200182 if (!active_)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700183 return Result(Result::ERROR_SEND_FAILED);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000184
kjellander02b3d272016-04-20 05:05:54 -0700185 RTPVideoHeader rtp_video_header;
186 memset(&rtp_video_header, 0, sizeof(RTPVideoHeader));
187 if (codec_specific_info)
188 CopyCodecSpecific(codec_specific_info, &rtp_video_header);
189 rtp_video_header.rotation = encoded_image.rotation_;
ilnik00d802b2017-04-11 10:34:31 -0700190 rtp_video_header.content_type = encoded_image.content_type_;
Niels Möllerc241af92017-11-10 08:51:29 +0100191 if (encoded_image.timing_.flags != TimingFrameFlags::kInvalid &&
192 encoded_image.timing_.flags != TimingFrameFlags::kNotTriggered) {
ilnik04f4d122017-06-19 07:18:55 -0700193 rtp_video_header.video_timing.encode_start_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700194 VideoSendTiming::GetDeltaCappedMs(
195 encoded_image.capture_time_ms_,
196 encoded_image.timing_.encode_start_ms);
ilnik04f4d122017-06-19 07:18:55 -0700197 rtp_video_header.video_timing.encode_finish_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700198 VideoSendTiming::GetDeltaCappedMs(
199 encoded_image.capture_time_ms_,
200 encoded_image.timing_.encode_finish_ms);
ilnik04f4d122017-06-19 07:18:55 -0700201 rtp_video_header.video_timing.packetization_finish_delta_ms = 0;
202 rtp_video_header.video_timing.pacer_exit_delta_ms = 0;
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100203 rtp_video_header.video_timing.network_timestamp_delta_ms = 0;
204 rtp_video_header.video_timing.network2_timestamp_delta_ms = 0;
Niels Möllerc241af92017-11-10 08:51:29 +0100205 rtp_video_header.video_timing.flags = encoded_image.timing_.flags;
206 } else {
207 rtp_video_header.video_timing.flags = TimingFrameFlags::kInvalid;
ilnik04f4d122017-06-19 07:18:55 -0700208 }
isheriff6b4b5f32016-06-08 00:24:21 -0700209 rtp_video_header.playout_delay = encoded_image.playout_delay_;
kjellander02b3d272016-04-20 05:05:54 -0700210
sergeyu7b9feee2016-11-17 16:16:14 -0800211 int stream_index = rtp_video_header.simulcastIdx;
212 RTC_DCHECK_LT(stream_index, rtp_modules_.size());
Åsa Persson4bece9a2017-10-06 10:04:04 +0200213 if (forced_fallback_enabled_) {
214 // Sets picture id. The SW and HW encoder have separate picture id
215 // sequences, set picture id to not cause sequence discontinuties at encoder
216 // changes.
217 params_[stream_index].Set(&rtp_video_header);
218 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700219 uint32_t frame_id;
sergeyu7b9feee2016-11-17 16:16:14 -0800220 bool send_result = rtp_modules_[stream_index]->SendOutgoingData(
kjellander02b3d272016-04-20 05:05:54 -0700221 encoded_image._frameType, payload_type_, encoded_image._timeStamp,
222 encoded_image.capture_time_ms_, encoded_image._buffer,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700223 encoded_image._length, fragmentation, &rtp_video_header, &frame_id);
sergeyu7b9feee2016-11-17 16:16:14 -0800224 if (!send_result)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700225 return Result(Result::ERROR_SEND_FAILED);
226
227 return Result(Result::OK, frame_id);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000228}
229
sprang1a646ee2016-12-01 06:34:11 -0800230void PayloadRouter::OnBitrateAllocationUpdated(
231 const BitrateAllocation& bitrate) {
232 rtc::CritScope lock(&crit_);
233 if (IsActive()) {
234 if (rtp_modules_.size() == 1) {
235 // If spatial scalability is enabled, it is covered by a single stream.
236 rtp_modules_[0]->SetVideoBitrateAllocation(bitrate);
237 } else {
238 // Simulcast is in use, split the BitrateAllocation into one struct per
239 // rtp stream, moving over the temporal layer allocation.
240 for (size_t si = 0; si < rtp_modules_.size(); ++si) {
sprangd0fc37a2017-06-22 05:40:25 -0700241 // Don't send empty TargetBitrate messages on streams not being relayed.
Seth Hampson46e31ba2018-01-18 10:39:54 -0800242 if (!bitrate.IsSpatialLayerUsed(si)) {
243 // The next spatial layer could be used if the current one is
244 // inactive.
245 continue;
246 }
sprangd0fc37a2017-06-22 05:40:25 -0700247
sprang1a646ee2016-12-01 06:34:11 -0800248 BitrateAllocation layer_bitrate;
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100249 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
250 if (bitrate.HasBitrate(si, tl))
251 layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl));
252 }
sprang1a646ee2016-12-01 06:34:11 -0800253 rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate);
254 }
255 }
256 }
257}
258
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000259} // namespace webrtc