blob: ecb8ec879fb1915c6bf85fef32c91c4f9e380445 [file] [log] [blame]
Steve Anton6e634bf2017-11-13 10:44:53 -08001/*
2 * Copyright 2017 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 "pc/rtptransceiver.h"
12
13#include <string>
14
Steve Antondcc3c022017-12-22 16:02:54 -080015#include "pc/rtpmediautils.h"
16
Steve Anton6e634bf2017-11-13 10:44:53 -080017namespace webrtc {
18
Steve Antondcc3c022017-12-22 16:02:54 -080019std::ostream& operator<<(std::ostream& os, RtpTransceiverDirection direction) {
20 return os << RtpTransceiverDirectionToString(direction);
21}
22
Steve Anton6e634bf2017-11-13 10:44:53 -080023RtpTransceiver::RtpTransceiver(cricket::MediaType media_type)
24 : unified_plan_(false), media_type_(media_type) {
25 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
26 media_type == cricket::MEDIA_TYPE_VIDEO);
27}
28
Steve Anton79e79602017-11-20 10:25:56 -080029RtpTransceiver::RtpTransceiver(
30 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
31 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
32 receiver)
33 : unified_plan_(true), media_type_(sender->media_type()) {
34 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
35 media_type_ == cricket::MEDIA_TYPE_VIDEO);
36 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
37 senders_.push_back(sender);
38 receivers_.push_back(receiver);
39}
40
Steve Anton6e634bf2017-11-13 10:44:53 -080041RtpTransceiver::~RtpTransceiver() {
42 Stop();
43}
44
45void RtpTransceiver::SetChannel(cricket::BaseChannel* channel) {
46 if (channel) {
47 RTC_DCHECK_EQ(media_type(), channel->media_type());
48 }
Steve Anton60776752018-01-10 11:51:34 -080049
50 if (channel_) {
51 channel_->SignalFirstPacketReceived.disconnect(this);
52 }
53
Steve Anton6e634bf2017-11-13 10:44:53 -080054 channel_ = channel;
Steve Anton60776752018-01-10 11:51:34 -080055
56 if (channel_) {
57 channel_->SignalFirstPacketReceived.connect(
58 this, &RtpTransceiver::OnFirstPacketReceived);
59 }
60
Steve Anton6e634bf2017-11-13 10:44:53 -080061 for (auto sender : senders_) {
62 if (media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton47136dd2018-01-12 10:49:35 -080063 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
Steve Anton6e634bf2017-11-13 10:44:53 -080064 static_cast<AudioRtpSender*>(sender->internal())
Steve Anton47136dd2018-01-12 10:49:35 -080065 ->SetMediaChannel(voice_channel ? voice_channel->media_channel()
66 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080067 } else {
Steve Anton47136dd2018-01-12 10:49:35 -080068 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
Steve Anton6e634bf2017-11-13 10:44:53 -080069 static_cast<VideoRtpSender*>(sender->internal())
Steve Anton47136dd2018-01-12 10:49:35 -080070 ->SetMediaChannel(video_channel ? video_channel->media_channel()
71 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080072 }
73 }
Steve Anton60776752018-01-10 11:51:34 -080074
Steve Anton6e634bf2017-11-13 10:44:53 -080075 for (auto receiver : receivers_) {
76 if (!channel) {
77 receiver->internal()->Stop();
78 }
79 if (media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton60776752018-01-10 11:51:34 -080080 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
Steve Anton6e634bf2017-11-13 10:44:53 -080081 static_cast<AudioRtpReceiver*>(receiver->internal())
Steve Anton60776752018-01-10 11:51:34 -080082 ->SetMediaChannel(voice_channel ? voice_channel->media_channel()
83 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080084 } else {
Steve Anton60776752018-01-10 11:51:34 -080085 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
Steve Anton6e634bf2017-11-13 10:44:53 -080086 static_cast<VideoRtpReceiver*>(receiver->internal())
Steve Anton60776752018-01-10 11:51:34 -080087 ->SetMediaChannel(video_channel ? video_channel->media_channel()
88 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080089 }
90 }
91}
92
93void RtpTransceiver::AddSender(
94 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
95 RTC_DCHECK(!unified_plan_);
96 RTC_DCHECK(sender);
97 RTC_DCHECK_EQ(media_type(), sender->internal()->media_type());
98 RTC_DCHECK(std::find(senders_.begin(), senders_.end(), sender) ==
99 senders_.end());
100 senders_.push_back(sender);
101}
102
103bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
104 RTC_DCHECK(!unified_plan_);
105 if (sender) {
106 RTC_DCHECK_EQ(media_type(), sender->media_type());
107 }
108 auto it = std::find(senders_.begin(), senders_.end(), sender);
109 if (it == senders_.end()) {
110 return false;
111 }
112 (*it)->internal()->Stop();
113 senders_.erase(it);
114 return true;
115}
116
117void RtpTransceiver::AddReceiver(
118 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
119 receiver) {
120 RTC_DCHECK(!unified_plan_);
121 RTC_DCHECK(receiver);
122 RTC_DCHECK_EQ(media_type(), receiver->internal()->media_type());
123 RTC_DCHECK(std::find(receivers_.begin(), receivers_.end(), receiver) ==
124 receivers_.end());
125 receivers_.push_back(receiver);
126}
127
128bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
129 RTC_DCHECK(!unified_plan_);
130 if (receiver) {
131 RTC_DCHECK_EQ(media_type(), receiver->media_type());
132 }
133 auto it = std::find(receivers_.begin(), receivers_.end(), receiver);
134 if (it == receivers_.end()) {
135 return false;
136 }
137 (*it)->internal()->Stop();
138 receivers_.erase(it);
139 return true;
140}
141
Steve Antonf9381f02017-12-14 10:23:57 -0800142rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
143 RTC_DCHECK(unified_plan_);
144 RTC_CHECK_EQ(1u, senders_.size());
145 return senders_[0]->internal();
146}
147
148rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
149 const {
150 RTC_DCHECK(unified_plan_);
151 RTC_CHECK_EQ(1u, receivers_.size());
152 return receivers_[0]->internal();
153}
154
Steve Anton6e634bf2017-11-13 10:44:53 -0800155rtc::Optional<std::string> RtpTransceiver::mid() const {
156 return mid_;
157}
158
Steve Anton60776752018-01-10 11:51:34 -0800159void RtpTransceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
160 for (auto receiver : receivers_) {
161 receiver->internal()->NotifyFirstPacketReceived();
162 }
163}
164
Steve Anton6e634bf2017-11-13 10:44:53 -0800165rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
166 RTC_DCHECK(unified_plan_);
167 RTC_CHECK_EQ(1u, senders_.size());
168 return senders_[0];
169}
170
171rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
172 RTC_DCHECK(unified_plan_);
173 RTC_CHECK_EQ(1u, receivers_.size());
174 return receivers_[0];
175}
176
Steve Antondcc3c022017-12-22 16:02:54 -0800177void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
178 current_direction_ = direction;
179 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
180 has_ever_been_used_to_send_ = true;
181 }
182}
183
Steve Anton6e634bf2017-11-13 10:44:53 -0800184bool RtpTransceiver::stopped() const {
185 return stopped_;
186}
187
188RtpTransceiverDirection RtpTransceiver::direction() const {
189 return direction_;
190}
191
192void RtpTransceiver::SetDirection(RtpTransceiverDirection new_direction) {
193 // TODO(steveanton): This should fire OnNegotiationNeeded.
Steve Antondcc3c022017-12-22 16:02:54 -0800194 set_direction(new_direction);
Steve Anton6e634bf2017-11-13 10:44:53 -0800195}
196
197rtc::Optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
198 const {
199 return current_direction_;
200}
201
202void RtpTransceiver::Stop() {
203 for (auto sender : senders_) {
204 sender->internal()->Stop();
205 }
206 for (auto receiver : receivers_) {
207 receiver->internal()->Stop();
208 }
209 stopped_ = true;
210}
211
212void RtpTransceiver::SetCodecPreferences(
213 rtc::ArrayView<RtpCodecCapability> codecs) {
214 // TODO(steveanton): Implement this.
215 RTC_NOTREACHED() << "Not implemented";
216}
217
218} // namespace webrtc