mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/payload_router.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #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 Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 17 | #include "rtc_base/random.h" |
| 18 | #include "rtc_base/timeutils.h" |
| 19 | #include "system_wrappers/include/field_trial.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | // Map information from info into rtp. |
| 25 | void 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; |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 81 | rtp->codecHeader.H264.packetization_mode = |
| 82 | info->codecSpecific.H264.packetization_mode; |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 83 | return; |
Emircan Uysaler | 90612a6 | 2017-11-28 09:45:25 -0800 | [diff] [blame] | 84 | 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 | } |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 104 | case kVideoCodecGeneric: |
| 105 | rtp->codec = kRtpVideoGeneric; |
| 106 | rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx; |
| 107 | return; |
| 108 | default: |
| 109 | return; |
| 110 | } |
| 111 | } |
perkj | bc75d97 | 2016-05-02 06:31:25 -0700 | [diff] [blame] | 112 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 113 | } // namespace |
| 114 | |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 115 | // 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. |
| 118 | class 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 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 145 | PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules, |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 146 | const std::vector<uint32_t>& ssrcs, |
| 147 | int payload_type, |
| 148 | const std::map<uint32_t, RtpPayloadState>& states) |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 149 | : active_(false), |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 150 | rtp_modules_(rtp_modules), |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 151 | payload_type_(payload_type), |
| 152 | forced_fallback_enabled_((webrtc::field_trial::IsEnabled( |
Åsa Persson | 45bbc8a | 2017-11-13 10:16:47 +0100 | [diff] [blame] | 153 | "WebRTC-VP8-Forced-Fallback-Encoder-v2"))) { |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 154 | 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 | } |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 165 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 166 | |
| 167 | PayloadRouter::~PayloadRouter() {} |
| 168 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 169 | void PayloadRouter::SetActive(bool active) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 170 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 171 | if (active_ == active) |
| 172 | return; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 173 | active_ = active; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 174 | |
| 175 | for (auto& module : rtp_modules_) { |
| 176 | module->SetSendingStatus(active_); |
| 177 | module->SetSendingMediaStatus(active_); |
| 178 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 179 | } |
| 180 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 181 | bool PayloadRouter::IsActive() { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 182 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 47d657b | 2015-02-19 10:29:32 +0000 | [diff] [blame] | 183 | return active_ && !rtp_modules_.empty(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 186 | std::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 Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 195 | EncodedImageCallback::Result PayloadRouter::OnEncodedImage( |
| 196 | const EncodedImage& encoded_image, |
| 197 | const CodecSpecificInfo* codec_specific_info, |
| 198 | const RTPFragmentationHeader* fragmentation) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 199 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 200 | RTC_DCHECK(!rtp_modules_.empty()); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 201 | if (!active_) |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 202 | return Result(Result::ERROR_SEND_FAILED); |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 203 | |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 204 | 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_; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 209 | rtp_video_header.content_type = encoded_image.content_type_; |
Niels Möller | c241af9 | 2017-11-10 08:51:29 +0100 | [diff] [blame] | 210 | if (encoded_image.timing_.flags != TimingFrameFlags::kInvalid && |
| 211 | encoded_image.timing_.flags != TimingFrameFlags::kNotTriggered) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 212 | rtp_video_header.video_timing.encode_start_delta_ms = |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 213 | VideoSendTiming::GetDeltaCappedMs( |
| 214 | encoded_image.capture_time_ms_, |
| 215 | encoded_image.timing_.encode_start_ms); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 216 | rtp_video_header.video_timing.encode_finish_delta_ms = |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 217 | VideoSendTiming::GetDeltaCappedMs( |
| 218 | encoded_image.capture_time_ms_, |
| 219 | encoded_image.timing_.encode_finish_ms); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 220 | rtp_video_header.video_timing.packetization_finish_delta_ms = 0; |
| 221 | rtp_video_header.video_timing.pacer_exit_delta_ms = 0; |
Danil Chapovalov | 996eb9e | 2017-10-30 17:14:41 +0100 | [diff] [blame] | 222 | rtp_video_header.video_timing.network_timestamp_delta_ms = 0; |
| 223 | rtp_video_header.video_timing.network2_timestamp_delta_ms = 0; |
Niels Möller | c241af9 | 2017-11-10 08:51:29 +0100 | [diff] [blame] | 224 | rtp_video_header.video_timing.flags = encoded_image.timing_.flags; |
| 225 | } else { |
| 226 | rtp_video_header.video_timing.flags = TimingFrameFlags::kInvalid; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 227 | } |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 228 | rtp_video_header.playout_delay = encoded_image.playout_delay_; |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 229 | |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 230 | int stream_index = rtp_video_header.simulcastIdx; |
| 231 | RTC_DCHECK_LT(stream_index, rtp_modules_.size()); |
Åsa Persson | 4bece9a | 2017-10-06 10:04:04 +0200 | [diff] [blame] | 232 | 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 Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 238 | uint32_t frame_id; |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 239 | bool send_result = rtp_modules_[stream_index]->SendOutgoingData( |
kjellander | 02b3d27 | 2016-04-20 05:05:54 -0700 | [diff] [blame] | 240 | encoded_image._frameType, payload_type_, encoded_image._timeStamp, |
| 241 | encoded_image.capture_time_ms_, encoded_image._buffer, |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 242 | encoded_image._length, fragmentation, &rtp_video_header, &frame_id); |
sergeyu | 7b9feee | 2016-11-17 16:16:14 -0800 | [diff] [blame] | 243 | if (!send_result) |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 244 | return Result(Result::ERROR_SEND_FAILED); |
| 245 | |
| 246 | return Result(Result::OK, frame_id); |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 247 | } |
| 248 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 249 | void 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) { |
sprang | d0fc37a | 2017-06-22 05:40:25 -0700 | [diff] [blame] | 260 | // Don't send empty TargetBitrate messages on streams not being relayed. |
erikvarga@webrtc.org | 01f2ec3 | 2017-11-15 14:58:23 +0100 | [diff] [blame] | 261 | if (!bitrate.IsSpatialLayerUsed(si)) |
sprang | d0fc37a | 2017-06-22 05:40:25 -0700 | [diff] [blame] | 262 | break; |
| 263 | |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 264 | BitrateAllocation layer_bitrate; |
erikvarga@webrtc.org | 01f2ec3 | 2017-11-15 14:58:23 +0100 | [diff] [blame] | 265 | for (int tl = 0; tl < kMaxTemporalStreams; ++tl) { |
| 266 | if (bitrate.HasBitrate(si, tl)) |
| 267 | layer_bitrate.SetBitrate(0, tl, bitrate.GetBitrate(si, tl)); |
| 268 | } |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 269 | rtp_modules_[si]->SetVideoBitrateAllocation(layer_bitrate); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 275 | } // namespace webrtc |