blob: 58a2fb4dc73520734449ef08171c5c8a80dc30e9 [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
11#include "webrtc/video_engine/payload_router.h"
12
13#include "webrtc/base/checks.h"
14#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000015#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000016#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17
18namespace webrtc {
19
20PayloadRouter::PayloadRouter()
21 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
22 active_(false) {}
23
24PayloadRouter::~PayloadRouter() {}
25
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000026size_t PayloadRouter::DefaultMaxPayloadLength() {
27 const size_t kIpUdpSrtpLength = 44;
28 return IP_PACKET_SIZE - kIpUdpSrtpLength;
29}
30
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000031void PayloadRouter::SetSendingRtpModules(
32 const std::list<RtpRtcp*>& rtp_modules) {
33 CriticalSectionScoped cs(crit_.get());
34 rtp_modules_.clear();
35 rtp_modules_.reserve(rtp_modules.size());
36 for (auto* rtp_module : rtp_modules) {
37 rtp_modules_.push_back(rtp_module);
38 }
39}
40
41void PayloadRouter::set_active(bool active) {
42 CriticalSectionScoped cs(crit_.get());
43 active_ = active;
44}
45
46bool PayloadRouter::active() {
47 CriticalSectionScoped cs(crit_.get());
48 return active_;
49}
50
51bool PayloadRouter::RoutePayload(FrameType frame_type,
52 int8_t payload_type,
53 uint32_t time_stamp,
54 int64_t capture_time_ms,
55 const uint8_t* payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000056 size_t payload_length,
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000057 const RTPFragmentationHeader* fragmentation,
58 const RTPVideoHeader* rtp_video_hdr) {
59 CriticalSectionScoped cs(crit_.get());
60 DCHECK(rtp_video_hdr == NULL ||
61 rtp_video_hdr->simulcastIdx <= rtp_modules_.size());
62
63 if (!active_ || rtp_modules_.empty())
64 return false;
65
66 int stream_idx = 0;
67 if (rtp_video_hdr != NULL)
68 stream_idx = rtp_video_hdr->simulcastIdx;
69 return rtp_modules_[stream_idx]->SendOutgoingData(
70 frame_type, payload_type, time_stamp, capture_time_ms, payload_data,
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000071 payload_length, fragmentation, rtp_video_hdr) == 0 ? true : false;
72}
73
mflodman@webrtc.org290cb562015-02-17 10:15:06 +000074bool PayloadRouter::TimeToSendPacket(uint32_t ssrc,
75 uint16_t sequence_number,
76 int64_t capture_timestamp,
77 bool retransmission) {
78 CriticalSectionScoped cs(crit_.get());
79 for (auto* rtp_module : rtp_modules_) {
80 if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) {
81 return rtp_module->TimeToSendPacket(ssrc, sequence_number,
82 capture_timestamp, retransmission);
83 }
84 }
85 return true;
86}
87
88size_t PayloadRouter::TimeToSendPadding(size_t bytes) {
89 CriticalSectionScoped cs(crit_.get());
90 for(auto* rtp_module : rtp_modules_) {
91 if (rtp_module->SendingMedia())
92 return rtp_module->TimeToSendPadding(bytes);
93 }
94 return 0;
95}
96
mflodman@webrtc.orga4ef2ce2015-02-12 09:54:18 +000097size_t PayloadRouter::MaxPayloadLength() const {
98 size_t min_payload_length = DefaultMaxPayloadLength();
99 CriticalSectionScoped cs(crit_.get());
100 for (auto* rtp_module : rtp_modules_) {
101 size_t module_payload_length = rtp_module->MaxDataPayloadLength();
102 if (module_payload_length < min_payload_length)
103 min_payload_length = module_payload_length;
104 }
105 return min_payload_length;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +0000106}
107
108} // namespace webrtc