blob: 24627e73a3bfc90e3a22894a4478b7c811474bb2 [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;
84 case kVideoCodecGeneric:
85 rtp->codec = kRtpVideoGeneric;
86 rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx;
87 return;
88 default:
89 return;
90 }
91}
perkjbc75d972016-05-02 06:31:25 -070092
kjellander02b3d272016-04-20 05:05:54 -070093} // namespace
94
Åsa Persson4bece9a2017-10-06 10:04:04 +020095// Currently only used if forced fallback for VP8 is enabled.
96// Consider adding tl0idx and set for VP8 and VP9.
97// Make picture id not codec specific.
98class 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);
105 }
106 ~RtpPayloadParams() {}
107
108 void Set(RTPVideoHeader* rtp_video_header) {
109 if (rtp_video_header->codec == kRtpVideoVp8 &&
110 rtp_video_header->codecHeader.VP8.pictureId != kNoPictureId) {
111 rtp_video_header->codecHeader.VP8.pictureId = state_.picture_id;
112 state_.picture_id = (state_.picture_id + 1) & 0x7FFF;
113 }
114 }
115
116 uint32_t ssrc() const { return ssrc_; }
117
118 RtpPayloadState state() const { return state_; }
119
120 private:
121 const uint32_t ssrc_;
122 RtpPayloadState state_;
123};
124
kjellander02b3d272016-04-20 05:05:54 -0700125PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules,
Åsa Persson4bece9a2017-10-06 10:04:04 +0200126 const std::vector<uint32_t>& ssrcs,
127 int payload_type,
128 const std::map<uint32_t, RtpPayloadState>& states)
kjellander02b3d272016-04-20 05:05:54 -0700129 : active_(false),
kjellander02b3d272016-04-20 05:05:54 -0700130 rtp_modules_(rtp_modules),
Åsa Persson4bece9a2017-10-06 10:04:04 +0200131 payload_type_(payload_type),
132 forced_fallback_enabled_((webrtc::field_trial::IsEnabled(
133 "WebRTC-VP8-Forced-Fallback-Encoder"))) {
134 RTC_DCHECK_EQ(ssrcs.size(), rtp_modules.size());
135 // SSRCs are assumed to be sorted in the same order as |rtp_modules|.
136 for (uint32_t ssrc : ssrcs) {
137 // Restore state if it previously existed.
138 const RtpPayloadState* state = nullptr;
139 auto it = states.find(ssrc);
140 if (it != states.end()) {
141 state = &it->second;
142 }
143 params_.push_back(RtpPayloadParams(ssrc, state));
144 }
Per83d09102016-04-15 14:59:13 +0200145}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000146
147PayloadRouter::~PayloadRouter() {}
148
sprang1a646ee2016-12-01 06:34:11 -0800149void PayloadRouter::SetActive(bool active) {
Tommi97888bd2016-01-21 23:24:59 +0100150 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100151 if (active_ == active)
152 return;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000153 active_ = active;
Per512ecb32016-09-23 15:52:06 +0200154
155 for (auto& module : rtp_modules_) {
156 module->SetSendingStatus(active_);
157 module->SetSendingMediaStatus(active_);
158 }
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000159}
160
sprang1a646ee2016-12-01 06:34:11 -0800161bool PayloadRouter::IsActive() {
Tommi97888bd2016-01-21 23:24:59 +0100162 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000163 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000164}
165
Åsa Persson4bece9a2017-10-06 10:04:04 +0200166std::map<uint32_t, RtpPayloadState> PayloadRouter::GetRtpPayloadStates() const {
167 rtc::CritScope lock(&crit_);
168 std::map<uint32_t, RtpPayloadState> payload_states;
169 for (const auto& param : params_) {
170 payload_states[param.ssrc()] = param.state();
171 }
172 return payload_states;
173}
174
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700175EncodedImageCallback::Result PayloadRouter::OnEncodedImage(
176 const EncodedImage& encoded_image,
177 const CodecSpecificInfo* codec_specific_info,
178 const RTPFragmentationHeader* fragmentation) {
Tommi97888bd2016-01-21 23:24:59 +0100179 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100180 RTC_DCHECK(!rtp_modules_.empty());
Per512ecb32016-09-23 15:52:06 +0200181 if (!active_)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700182 return Result(Result::ERROR_SEND_FAILED);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000183
kjellander02b3d272016-04-20 05:05:54 -0700184 RTPVideoHeader rtp_video_header;
185 memset(&rtp_video_header, 0, sizeof(RTPVideoHeader));
186 if (codec_specific_info)
187 CopyCodecSpecific(codec_specific_info, &rtp_video_header);
188 rtp_video_header.rotation = encoded_image.rotation_;
ilnik00d802b2017-04-11 10:34:31 -0700189 rtp_video_header.content_type = encoded_image.content_type_;
sprangba050a62017-08-18 02:51:12 -0700190 if (encoded_image.timing_.flags != TimingFrameFlags::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -0700191 rtp_video_header.video_timing.encode_start_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700192 VideoSendTiming::GetDeltaCappedMs(
193 encoded_image.capture_time_ms_,
194 encoded_image.timing_.encode_start_ms);
ilnik04f4d122017-06-19 07:18:55 -0700195 rtp_video_header.video_timing.encode_finish_delta_ms =
ilnik2edc6842017-07-06 03:06:50 -0700196 VideoSendTiming::GetDeltaCappedMs(
197 encoded_image.capture_time_ms_,
198 encoded_image.timing_.encode_finish_ms);
ilnik04f4d122017-06-19 07:18:55 -0700199 rtp_video_header.video_timing.packetization_finish_delta_ms = 0;
200 rtp_video_header.video_timing.pacer_exit_delta_ms = 0;
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100201 rtp_video_header.video_timing.network_timestamp_delta_ms = 0;
202 rtp_video_header.video_timing.network2_timestamp_delta_ms = 0;
ilnik04f4d122017-06-19 07:18:55 -0700203 }
sprangba050a62017-08-18 02:51:12 -0700204 rtp_video_header.video_timing.flags = encoded_image.timing_.flags;
isheriff6b4b5f32016-06-08 00:24:21 -0700205 rtp_video_header.playout_delay = encoded_image.playout_delay_;
kjellander02b3d272016-04-20 05:05:54 -0700206
sergeyu7b9feee2016-11-17 16:16:14 -0800207 int stream_index = rtp_video_header.simulcastIdx;
208 RTC_DCHECK_LT(stream_index, rtp_modules_.size());
Åsa Persson4bece9a2017-10-06 10:04:04 +0200209 if (forced_fallback_enabled_) {
210 // Sets picture id. The SW and HW encoder have separate picture id
211 // sequences, set picture id to not cause sequence discontinuties at encoder
212 // changes.
213 params_[stream_index].Set(&rtp_video_header);
214 }
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700215 uint32_t frame_id;
sergeyu7b9feee2016-11-17 16:16:14 -0800216 bool send_result = rtp_modules_[stream_index]->SendOutgoingData(
kjellander02b3d272016-04-20 05:05:54 -0700217 encoded_image._frameType, payload_type_, encoded_image._timeStamp,
218 encoded_image.capture_time_ms_, encoded_image._buffer,
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700219 encoded_image._length, fragmentation, &rtp_video_header, &frame_id);
sergeyu7b9feee2016-11-17 16:16:14 -0800220 if (!send_result)
Sergey Ulanov525df3f2016-08-02 17:46:41 -0700221 return Result(Result::ERROR_SEND_FAILED);
222
223 return Result(Result::OK, frame_id);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000224}
225
sprang1a646ee2016-12-01 06:34:11 -0800226void PayloadRouter::OnBitrateAllocationUpdated(
227 const BitrateAllocation& bitrate) {
228 rtc::CritScope lock(&crit_);
229 if (IsActive()) {
230 if (rtp_modules_.size() == 1) {
231 // If spatial scalability is enabled, it is covered by a single stream.
232 rtp_modules_[0]->SetVideoBitrateAllocation(bitrate);
233 } else {
234 // Simulcast is in use, split the BitrateAllocation into one struct per
235 // rtp stream, moving over the temporal layer allocation.
236 for (size_t si = 0; si < rtp_modules_.size(); ++si) {
sprangd0fc37a2017-06-22 05:40:25 -0700237 // Don't send empty TargetBitrate messages on streams not being relayed.
238 if (bitrate.GetSpatialLayerSum(si) == 0)
239 break;
240
sprang1a646ee2016-12-01 06:34:11 -0800241 BitrateAllocation layer_bitrate;
242 for (int tl = 0; tl < kMaxTemporalStreams; ++tl)
243 layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl));
244 rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate);
245 }
246 }
247 }
248}
249
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000250} // namespace webrtc