blob: 3be5882cdbf2172928fe312caf2a9b3401d1ea9b [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
Peter Boström7623ce42015-12-09 12:13:30 +010011#include "webrtc/video/payload_router.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000012
13#include "webrtc/base/checks.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010014#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
15#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
kjellander02b3d272016-04-20 05:05:54 -070016#include "webrtc/modules/video_coding/include/video_codec_interface.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000017
18namespace webrtc {
19
kjellander02b3d272016-04-20 05:05:54 -070020namespace {
21// Map information from info into rtp.
22void CopyCodecSpecific(const CodecSpecificInfo* info, RTPVideoHeader* rtp) {
23 RTC_DCHECK(info);
24 switch (info->codecType) {
25 case kVideoCodecVP8: {
26 rtp->codec = kRtpVideoVp8;
27 rtp->codecHeader.VP8.InitRTPVideoHeaderVP8();
28 rtp->codecHeader.VP8.pictureId = info->codecSpecific.VP8.pictureId;
29 rtp->codecHeader.VP8.nonReference = info->codecSpecific.VP8.nonReference;
30 rtp->codecHeader.VP8.temporalIdx = info->codecSpecific.VP8.temporalIdx;
31 rtp->codecHeader.VP8.layerSync = info->codecSpecific.VP8.layerSync;
32 rtp->codecHeader.VP8.tl0PicIdx = info->codecSpecific.VP8.tl0PicIdx;
33 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;
46 rtp->codecHeader.VP9.picture_id = info->codecSpecific.VP9.picture_id;
47 rtp->codecHeader.VP9.tl0_pic_idx = info->codecSpecific.VP9.tl0_pic_idx;
48 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;
72 for (int i = 0; i < info->codecSpecific.VP9.num_ref_pics; ++i)
73 rtp->codecHeader.VP9.pid_diff[i] = info->codecSpecific.VP9.p_diff[i];
74 return;
75 }
76 case kVideoCodecH264:
77 rtp->codec = kRtpVideoH264;
78 return;
79 case kVideoCodecGeneric:
80 rtp->codec = kRtpVideoGeneric;
81 rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx;
82 return;
83 default:
84 return;
85 }
86}
perkjbc75d972016-05-02 06:31:25 -070087
kjellander02b3d272016-04-20 05:05:54 -070088} // namespace
89
90PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules,
91 int payload_type)
92 : active_(false),
93 num_sending_modules_(1),
94 rtp_modules_(rtp_modules),
95 payload_type_(payload_type) {
Per83d09102016-04-15 14:59:13 +020096 UpdateModuleSendingState();
97}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000098
99PayloadRouter::~PayloadRouter() {}
100
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000101size_t PayloadRouter::DefaultMaxPayloadLength() {
102 const size_t kIpUdpSrtpLength = 44;
103 return IP_PACKET_SIZE - kIpUdpSrtpLength;
104}
105
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000106void PayloadRouter::set_active(bool active) {
Tommi97888bd2016-01-21 23:24:59 +0100107 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100108 if (active_ == active)
109 return;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000110 active_ = active;
Peter Boström8b79b072016-02-26 16:31:37 +0100111 UpdateModuleSendingState();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000112}
113
114bool PayloadRouter::active() {
Tommi97888bd2016-01-21 23:24:59 +0100115 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +0000116 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000117}
118
perkjbc75d972016-05-02 06:31:25 -0700119void PayloadRouter::SetSendStreams(const std::vector<VideoStream>& streams) {
120 RTC_DCHECK_LE(streams.size(), rtp_modules_.size());
Peter Boström8b79b072016-02-26 16:31:37 +0100121 rtc::CritScope lock(&crit_);
perkjbc75d972016-05-02 06:31:25 -0700122 num_sending_modules_ = streams.size();
123 streams_ = streams;
124 // TODO(perkj): Should SetSendStreams also call SetTargetSendBitrate?
Peter Boström8b79b072016-02-26 16:31:37 +0100125 UpdateModuleSendingState();
126}
127
128void PayloadRouter::UpdateModuleSendingState() {
129 for (size_t i = 0; i < num_sending_modules_; ++i) {
130 rtp_modules_[i]->SetSendingStatus(active_);
131 rtp_modules_[i]->SetSendingMediaStatus(active_);
132 }
133 // Disable inactive modules.
134 for (size_t i = num_sending_modules_; i < rtp_modules_.size(); ++i) {
135 rtp_modules_[i]->SetSendingStatus(false);
136 rtp_modules_[i]->SetSendingMediaStatus(false);
137 }
138}
139
kjellander02b3d272016-04-20 05:05:54 -0700140int32_t PayloadRouter::Encoded(const EncodedImage& encoded_image,
141 const CodecSpecificInfo* codec_specific_info,
142 const RTPFragmentationHeader* fragmentation) {
Tommi97888bd2016-01-21 23:24:59 +0100143 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100144 RTC_DCHECK(!rtp_modules_.empty());
145 if (!active_ || num_sending_modules_ == 0)
kjellander02b3d272016-04-20 05:05:54 -0700146 return -1;
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000147
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000148 int stream_idx = 0;
kjellander02b3d272016-04-20 05:05:54 -0700149
150 RTPVideoHeader rtp_video_header;
151 memset(&rtp_video_header, 0, sizeof(RTPVideoHeader));
152 if (codec_specific_info)
153 CopyCodecSpecific(codec_specific_info, &rtp_video_header);
154 rtp_video_header.rotation = encoded_image.rotation_;
155
156 RTC_DCHECK_LT(rtp_video_header.simulcastIdx, rtp_modules_.size());
157 // The simulcast index might actually be larger than the number of modules
158 // in case the encoder was processing a frame during a codec reconfig.
159 if (rtp_video_header.simulcastIdx >= num_sending_modules_)
160 return -1;
161 stream_idx = rtp_video_header.simulcastIdx;
162
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000163 return rtp_modules_[stream_idx]->SendOutgoingData(
kjellander02b3d272016-04-20 05:05:54 -0700164 encoded_image._frameType, payload_type_, encoded_image._timeStamp,
165 encoded_image.capture_time_ms_, encoded_image._buffer,
166 encoded_image._length, fragmentation, &rtp_video_header);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000167}
168
perkjbc75d972016-05-02 06:31:25 -0700169void PayloadRouter::SetTargetSendBitrate(uint32_t bitrate_bps) {
Tommi97888bd2016-01-21 23:24:59 +0100170 rtc::CritScope lock(&crit_);
perkjbc75d972016-05-02 06:31:25 -0700171 RTC_DCHECK_LE(streams_.size(), rtp_modules_.size());
172
173 // TODO(sprang): Rebase https://codereview.webrtc.org/1913073002/ on top of
174 // this.
175 int bitrate_remainder = bitrate_bps;
176 for (size_t i = 0; i < streams_.size() && bitrate_remainder > 0; ++i) {
177 int stream_bitrate = 0;
178 if (streams_[i].max_bitrate_bps > bitrate_remainder) {
179 stream_bitrate = bitrate_remainder;
180 } else {
181 stream_bitrate = streams_[i].max_bitrate_bps;
182 }
183 bitrate_remainder -= stream_bitrate;
184 rtp_modules_[i]->SetTargetSendBitrate(stream_bitrate);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +0000185 }
186}
187
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000188size_t PayloadRouter::MaxPayloadLength() const {
189 size_t min_payload_length = DefaultMaxPayloadLength();
Tommi97888bd2016-01-21 23:24:59 +0100190 rtc::CritScope lock(&crit_);
Peter Boström8b79b072016-02-26 16:31:37 +0100191 for (size_t i = 0; i < num_sending_modules_; ++i) {
192 size_t module_payload_length = rtp_modules_[i]->MaxDataPayloadLength();
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +0000193 if (module_payload_length < min_payload_length)
194 min_payload_length = module_payload_length;
195 }
196 return min_payload_length;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000197}
198
199} // namespace webrtc