blob: a70da244dbe398b0dd64ba4196c6ea39e07fac7f [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"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000016
17namespace webrtc {
18
19PayloadRouter::PayloadRouter()
Tommi97888bd2016-01-21 23:24:59 +010020 : active_(false) {}
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000021
22PayloadRouter::~PayloadRouter() {}
23
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000024size_t PayloadRouter::DefaultMaxPayloadLength() {
25 const size_t kIpUdpSrtpLength = 44;
26 return IP_PACKET_SIZE - kIpUdpSrtpLength;
27}
28
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000029void PayloadRouter::SetSendingRtpModules(
Peter Boström404686a2016-02-11 23:37:26 +010030 const std::vector<RtpRtcp*>& rtp_modules) {
Tommi97888bd2016-01-21 23:24:59 +010031 rtc::CritScope lock(&crit_);
Peter Boström404686a2016-02-11 23:37:26 +010032 rtp_modules_ = rtp_modules;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000033}
34
35void PayloadRouter::set_active(bool active) {
Tommi97888bd2016-01-21 23:24:59 +010036 rtc::CritScope lock(&crit_);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000037 active_ = active;
38}
39
40bool PayloadRouter::active() {
Tommi97888bd2016-01-21 23:24:59 +010041 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +000042 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000043}
44
45bool PayloadRouter::RoutePayload(FrameType frame_type,
46 int8_t payload_type,
47 uint32_t time_stamp,
48 int64_t capture_time_ms,
49 const uint8_t* payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000050 size_t payload_length,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000051 const RTPFragmentationHeader* fragmentation,
52 const RTPVideoHeader* rtp_video_hdr) {
Tommi97888bd2016-01-21 23:24:59 +010053 rtc::CritScope lock(&crit_);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000054 if (!active_ || rtp_modules_.empty())
55 return false;
56
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000057 // The simulcast index might actually be larger than the number of modules in
58 // case the encoder was processing a frame during a codec reconfig.
59 if (rtp_video_hdr != NULL &&
60 rtp_video_hdr->simulcastIdx >= rtp_modules_.size())
61 return false;
62
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000063 int stream_idx = 0;
64 if (rtp_video_hdr != NULL)
65 stream_idx = rtp_video_hdr->simulcastIdx;
66 return rtp_modules_[stream_idx]->SendOutgoingData(
67 frame_type, payload_type, time_stamp, capture_time_ms, payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000068 payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false;
69}
70
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000071void PayloadRouter::SetTargetSendBitrates(
72 const std::vector<uint32_t>& stream_bitrates) {
Tommi97888bd2016-01-21 23:24:59 +010073 rtc::CritScope lock(&crit_);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000074 if (stream_bitrates.size() < rtp_modules_.size()) {
75 // There can be a size mis-match during codec reconfiguration.
76 return;
77 }
78 int idx = 0;
79 for (auto* rtp_module : rtp_modules_) {
80 rtp_module->SetTargetSendBitrate(stream_bitrates[idx++]);
81 }
82}
83
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000084size_t PayloadRouter::MaxPayloadLength() const {
85 size_t min_payload_length = DefaultMaxPayloadLength();
Tommi97888bd2016-01-21 23:24:59 +010086 rtc::CritScope lock(&crit_);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000087 for (auto* rtp_module : rtp_modules_) {
88 size_t module_payload_length = rtp_module->MaxDataPayloadLength();
89 if (module_payload_length < min_payload_length)
90 min_payload_length = module_payload_length;
91 }
92 return min_payload_length;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000093}
94
95} // namespace webrtc