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 | |
| 19 | PayloadRouter::PayloadRouter() |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame^] | 20 | : active_(false) {} |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 21 | |
| 22 | PayloadRouter::~PayloadRouter() {} |
| 23 | |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 24 | size_t PayloadRouter::DefaultMaxPayloadLength() { |
| 25 | const size_t kIpUdpSrtpLength = 44; |
| 26 | return IP_PACKET_SIZE - kIpUdpSrtpLength; |
| 27 | } |
| 28 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 29 | void PayloadRouter::SetSendingRtpModules( |
| 30 | const std::list<RtpRtcp*>& rtp_modules) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame^] | 31 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 32 | rtp_modules_.clear(); |
| 33 | rtp_modules_.reserve(rtp_modules.size()); |
| 34 | for (auto* rtp_module : rtp_modules) { |
| 35 | rtp_modules_.push_back(rtp_module); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void PayloadRouter::set_active(bool active) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame^] | 40 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 41 | active_ = active; |
| 42 | } |
| 43 | |
| 44 | bool PayloadRouter::active() { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame^] | 45 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 47d657b | 2015-02-19 10:29:32 +0000 | [diff] [blame] | 46 | return active_ && !rtp_modules_.empty(); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool PayloadRouter::RoutePayload(FrameType frame_type, |
| 50 | int8_t payload_type, |
| 51 | uint32_t time_stamp, |
| 52 | int64_t capture_time_ms, |
| 53 | const uint8_t* payload_data, |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 54 | size_t payload_length, |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 55 | const RTPFragmentationHeader* fragmentation, |
| 56 | const RTPVideoHeader* rtp_video_hdr) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame^] | 57 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 58 | if (!active_ || rtp_modules_.empty()) |
| 59 | return false; |
| 60 | |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 61 | // The simulcast index might actually be larger than the number of modules in |
| 62 | // case the encoder was processing a frame during a codec reconfig. |
| 63 | if (rtp_video_hdr != NULL && |
| 64 | rtp_video_hdr->simulcastIdx >= rtp_modules_.size()) |
| 65 | return false; |
| 66 | |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 67 | int stream_idx = 0; |
| 68 | if (rtp_video_hdr != NULL) |
| 69 | stream_idx = rtp_video_hdr->simulcastIdx; |
| 70 | return rtp_modules_[stream_idx]->SendOutgoingData( |
| 71 | frame_type, payload_type, time_stamp, capture_time_ms, payload_data, |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 72 | payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false; |
| 73 | } |
| 74 | |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 75 | void PayloadRouter::SetTargetSendBitrates( |
| 76 | const std::vector<uint32_t>& stream_bitrates) { |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame^] | 77 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | 50e2816 | 2015-02-23 07:45:11 +0000 | [diff] [blame] | 78 | if (stream_bitrates.size() < rtp_modules_.size()) { |
| 79 | // There can be a size mis-match during codec reconfiguration. |
| 80 | return; |
| 81 | } |
| 82 | int idx = 0; |
| 83 | for (auto* rtp_module : rtp_modules_) { |
| 84 | rtp_module->SetTargetSendBitrate(stream_bitrates[idx++]); |
| 85 | } |
| 86 | } |
| 87 | |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 88 | size_t PayloadRouter::MaxPayloadLength() const { |
| 89 | size_t min_payload_length = DefaultMaxPayloadLength(); |
Tommi | 97888bd | 2016-01-21 23:24:59 +0100 | [diff] [blame^] | 90 | rtc::CritScope lock(&crit_); |
mflodman@webrtc.org | a4ef2ce | 2015-02-12 09:54:18 +0000 | [diff] [blame] | 91 | for (auto* rtp_module : rtp_modules_) { |
| 92 | size_t module_payload_length = rtp_module->MaxDataPayloadLength(); |
| 93 | if (module_payload_length < min_payload_length) |
| 94 | min_payload_length = module_payload_length; |
| 95 | } |
| 96 | return min_payload_length; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | } // namespace webrtc |