blob: 72abdb8a44afc7ecee6e737d838ef4f2481fdc2d [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(
30 const std::list<RtpRtcp*>& rtp_modules) {
Tommi97888bd2016-01-21 23:24:59 +010031 rtc::CritScope lock(&crit_);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000032 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
39void PayloadRouter::set_active(bool active) {
Tommi97888bd2016-01-21 23:24:59 +010040 rtc::CritScope lock(&crit_);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000041 active_ = active;
42}
43
44bool PayloadRouter::active() {
Tommi97888bd2016-01-21 23:24:59 +010045 rtc::CritScope lock(&crit_);
mflodman@webrtc.org47d657b2015-02-19 10:29:32 +000046 return active_ && !rtp_modules_.empty();
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000047}
48
49bool 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.orga4ef2ce2015-02-12 09:54:18 +000054 size_t payload_length,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000055 const RTPFragmentationHeader* fragmentation,
56 const RTPVideoHeader* rtp_video_hdr) {
Tommi97888bd2016-01-21 23:24:59 +010057 rtc::CritScope lock(&crit_);
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000058 if (!active_ || rtp_modules_.empty())
59 return false;
60
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000061 // 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.org02270cd2015-02-06 13:10:19 +000067 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.orga4ef2ce2015-02-12 09:54:18 +000072 payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false;
73}
74
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000075void PayloadRouter::SetTargetSendBitrates(
76 const std::vector<uint32_t>& stream_bitrates) {
Tommi97888bd2016-01-21 23:24:59 +010077 rtc::CritScope lock(&crit_);
mflodman@webrtc.org50e28162015-02-23 07:45:11 +000078 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.orga4ef2ce2015-02-12 09:54:18 +000088size_t PayloadRouter::MaxPayloadLength() const {
89 size_t min_payload_length = DefaultMaxPayloadLength();
Tommi97888bd2016-01-21 23:24:59 +010090 rtc::CritScope lock(&crit_);
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000091 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.org02270cd2015-02-06 13:10:19 +000097}
98
99} // namespace webrtc