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 | |
Peter Boström | 7623ce4 | 2015-12-09 12:13:30 +0100 | [diff] [blame] | 11 | #include "webrtc/video/payload_router.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 12 | |
| 13 | #include "webrtc/base/checks.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 14 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" |
| 15 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame^] | 19 | PayloadRouter::PayloadRouter(const std::vector<RtpRtcp*>& rtp_modules) |
| 20 | : active_(false), num_sending_modules_(1), rtp_modules_(rtp_modules) { |
| 21 | UpdateModuleSendingState(); |
| 22 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 23 | |
| 24 | PayloadRouter::~PayloadRouter() {} |
| 25 | |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 26 | size_t PayloadRouter::DefaultMaxPayloadLength() { |
| 27 | const size_t kIpUdpSrtpLength = 44; |
| 28 | return IP_PACKET_SIZE - kIpUdpSrtpLength; |
| 29 | } |
| 30 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 31 | void PayloadRouter::set_active(bool active) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 32 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 33 | if (active_ == active) |
| 34 | return; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 35 | active_ = active; |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 36 | UpdateModuleSendingState(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | bool PayloadRouter::active() { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 40 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 47d657b | 2015-02-19 10:29:32 +0000 | [diff] [blame] | 41 | return active_ && !rtp_modules_.empty(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 44 | void PayloadRouter::SetSendingRtpModules(size_t num_sending_modules) { |
| 45 | RTC_DCHECK_LE(num_sending_modules, rtp_modules_.size()); |
| 46 | rtc::CritScope lock(&crit_); |
| 47 | num_sending_modules_ = num_sending_modules; |
| 48 | UpdateModuleSendingState(); |
| 49 | } |
| 50 | |
| 51 | void PayloadRouter::UpdateModuleSendingState() { |
| 52 | for (size_t i = 0; i < num_sending_modules_; ++i) { |
| 53 | rtp_modules_[i]->SetSendingStatus(active_); |
| 54 | rtp_modules_[i]->SetSendingMediaStatus(active_); |
| 55 | } |
| 56 | // Disable inactive modules. |
| 57 | for (size_t i = num_sending_modules_; i < rtp_modules_.size(); ++i) { |
| 58 | rtp_modules_[i]->SetSendingStatus(false); |
| 59 | rtp_modules_[i]->SetSendingMediaStatus(false); |
| 60 | } |
| 61 | } |
| 62 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 63 | bool PayloadRouter::RoutePayload(FrameType frame_type, |
| 64 | int8_t payload_type, |
| 65 | uint32_t time_stamp, |
| 66 | int64_t capture_time_ms, |
| 67 | const uint8_t* payload_data, |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 68 | size_t payload_length, |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 69 | const RTPFragmentationHeader* fragmentation, |
| 70 | const RTPVideoHeader* rtp_video_hdr) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 71 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 72 | RTC_DCHECK(!rtp_modules_.empty()); |
| 73 | if (!active_ || num_sending_modules_ == 0) |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 74 | return false; |
| 75 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 76 | int stream_idx = 0; |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 77 | if (rtp_video_hdr) { |
| 78 | RTC_DCHECK_LT(rtp_video_hdr->simulcastIdx, rtp_modules_.size()); |
| 79 | // The simulcast index might actually be larger than the number of modules |
| 80 | // in case the encoder was processing a frame during a codec reconfig. |
| 81 | if (rtp_video_hdr->simulcastIdx >= num_sending_modules_) |
| 82 | return false; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 83 | stream_idx = rtp_video_hdr->simulcastIdx; |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 84 | } |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 85 | return rtp_modules_[stream_idx]->SendOutgoingData( |
| 86 | frame_type, payload_type, time_stamp, capture_time_ms, payload_data, |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 87 | payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false; |
| 88 | } |
| 89 | |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 90 | void PayloadRouter::SetTargetSendBitrates( |
| 91 | const std::vector<uint32_t>& stream_bitrates) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 92 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 93 | RTC_DCHECK_LE(stream_bitrates.size(), rtp_modules_.size()); |
| 94 | for (size_t i = 0; i < stream_bitrates.size(); ++i) { |
| 95 | rtp_modules_[i]->SetTargetSendBitrate(stream_bitrates[i]); |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 99 | size_t PayloadRouter::MaxPayloadLength() const { |
| 100 | size_t min_payload_length = DefaultMaxPayloadLength(); |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame] | 101 | rtc::CritScope lock(&crit_); |
Peter Boström | 8b79b07 | 2016-02-26 16:31:37 +0100 | [diff] [blame] | 102 | for (size_t i = 0; i < num_sending_modules_; ++i) { |
| 103 | size_t module_payload_length = rtp_modules_[i]->MaxDataPayloadLength(); |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 104 | if (module_payload_length < min_payload_length) |
| 105 | min_payload_length = module_payload_length; |
| 106 | } |
| 107 | return min_payload_length; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // namespace webrtc |