blob: 37770b524dc8ac79bba17f3309710858d171a9a8 [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
19RtpTransceiver::RtpTransceiver(cricket::MediaType media_type)
20 : unified_plan_(false), media_type_(media_type) {
21 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
22 media_type == cricket::MEDIA_TYPE_VIDEO);
23}
24
Steve Anton79e79602017-11-20 10:25:56 -080025RtpTransceiver::RtpTransceiver(
26 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
27 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
28 receiver)
29 : unified_plan_(true), media_type_(sender->media_type()) {
30 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
31 media_type_ == cricket::MEDIA_TYPE_VIDEO);
32 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
33 senders_.push_back(sender);
34 receivers_.push_back(receiver);
35}
36
Steve Anton6e634bf2017-11-13 10:44:53 -080037RtpTransceiver::~RtpTransceiver() {
38 Stop();
39}
40
41void RtpTransceiver::SetChannel(cricket::BaseChannel* channel) {
42 if (channel) {
43 RTC_DCHECK_EQ(media_type(), channel->media_type());
44 }
Steve Anton60776752018-01-10 11:51:34 -080045
46 if (channel_) {
47 channel_->SignalFirstPacketReceived.disconnect(this);
48 }
49
Steve Anton6e634bf2017-11-13 10:44:53 -080050 channel_ = channel;
Steve Anton60776752018-01-10 11:51:34 -080051
52 if (channel_) {
53 channel_->SignalFirstPacketReceived.connect(
54 this, &RtpTransceiver::OnFirstPacketReceived);
55 }
56
Steve Anton6e634bf2017-11-13 10:44:53 -080057 for (auto sender : senders_) {
58 if (media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton47136dd2018-01-12 10:49:35 -080059 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
Steve Anton57858b32018-02-15 15:19:50 -080060 sender->internal()->SetVoiceMediaChannel(
61 voice_channel ? voice_channel->media_channel() : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080062 } else {
Steve Anton47136dd2018-01-12 10:49:35 -080063 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
Steve Anton57858b32018-02-15 15:19:50 -080064 sender->internal()->SetVideoMediaChannel(
65 video_channel ? video_channel->media_channel() : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080066 }
67 }
Steve Anton60776752018-01-10 11:51:34 -080068
Steve Anton6e634bf2017-11-13 10:44:53 -080069 for (auto receiver : receivers_) {
70 if (!channel) {
71 receiver->internal()->Stop();
72 }
73 if (media_type() == cricket::MEDIA_TYPE_AUDIO) {
Steve Anton60776752018-01-10 11:51:34 -080074 auto* voice_channel = static_cast<cricket::VoiceChannel*>(channel);
Steve Anton57858b32018-02-15 15:19:50 -080075 receiver->internal()->SetVoiceMediaChannel(
76 voice_channel ? voice_channel->media_channel() : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080077 } else {
Steve Anton60776752018-01-10 11:51:34 -080078 auto* video_channel = static_cast<cricket::VideoChannel*>(channel);
Steve Anton57858b32018-02-15 15:19:50 -080079 receiver->internal()->SetVideoMediaChannel(
80 video_channel ? video_channel->media_channel() : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -080081 }
82 }
83}
84
85void RtpTransceiver::AddSender(
86 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
87 RTC_DCHECK(!unified_plan_);
88 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -080089 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton6e634bf2017-11-13 10:44:53 -080090 RTC_DCHECK(std::find(senders_.begin(), senders_.end(), sender) ==
91 senders_.end());
92 senders_.push_back(sender);
93}
94
95bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
96 RTC_DCHECK(!unified_plan_);
97 if (sender) {
98 RTC_DCHECK_EQ(media_type(), sender->media_type());
99 }
100 auto it = std::find(senders_.begin(), senders_.end(), sender);
101 if (it == senders_.end()) {
102 return false;
103 }
104 (*it)->internal()->Stop();
105 senders_.erase(it);
106 return true;
107}
108
109void RtpTransceiver::AddReceiver(
110 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
111 receiver) {
112 RTC_DCHECK(!unified_plan_);
113 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800114 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton6e634bf2017-11-13 10:44:53 -0800115 RTC_DCHECK(std::find(receivers_.begin(), receivers_.end(), receiver) ==
116 receivers_.end());
117 receivers_.push_back(receiver);
118}
119
120bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
121 RTC_DCHECK(!unified_plan_);
122 if (receiver) {
123 RTC_DCHECK_EQ(media_type(), receiver->media_type());
124 }
125 auto it = std::find(receivers_.begin(), receivers_.end(), receiver);
126 if (it == receivers_.end()) {
127 return false;
128 }
129 (*it)->internal()->Stop();
130 receivers_.erase(it);
131 return true;
132}
133
Steve Antonf9381f02017-12-14 10:23:57 -0800134rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
135 RTC_DCHECK(unified_plan_);
136 RTC_CHECK_EQ(1u, senders_.size());
137 return senders_[0]->internal();
138}
139
140rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
141 const {
142 RTC_DCHECK(unified_plan_);
143 RTC_CHECK_EQ(1u, receivers_.size());
144 return receivers_[0]->internal();
145}
146
Steve Anton69470252018-02-09 11:43:08 -0800147cricket::MediaType RtpTransceiver::media_type() const {
148 return media_type_;
149}
150
Steve Anton6e634bf2017-11-13 10:44:53 -0800151rtc::Optional<std::string> RtpTransceiver::mid() const {
152 return mid_;
153}
154
Steve Anton60776752018-01-10 11:51:34 -0800155void RtpTransceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
156 for (auto receiver : receivers_) {
157 receiver->internal()->NotifyFirstPacketReceived();
158 }
159}
160
Steve Anton6e634bf2017-11-13 10:44:53 -0800161rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
162 RTC_DCHECK(unified_plan_);
163 RTC_CHECK_EQ(1u, senders_.size());
164 return senders_[0];
165}
166
167rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
168 RTC_DCHECK(unified_plan_);
169 RTC_CHECK_EQ(1u, receivers_.size());
170 return receivers_[0];
171}
172
Steve Antondcc3c022017-12-22 16:02:54 -0800173void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700174 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
175 << ") current direction from "
176 << (current_direction_ ? RtpTransceiverDirectionToString(
177 *current_direction_)
178 : "<not set>")
179 << " to " << RtpTransceiverDirectionToString(direction)
180 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800181 current_direction_ = direction;
182 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
183 has_ever_been_used_to_send_ = true;
184 }
185}
186
Steve Anton6e634bf2017-11-13 10:44:53 -0800187bool RtpTransceiver::stopped() const {
188 return stopped_;
189}
190
191RtpTransceiverDirection RtpTransceiver::direction() const {
192 return direction_;
193}
194
195void RtpTransceiver::SetDirection(RtpTransceiverDirection new_direction) {
Steve Anton52d86772018-02-20 15:48:12 -0800196 if (stopped()) {
197 return;
198 }
199 if (new_direction == direction_) {
200 return;
201 }
202 direction_ = new_direction;
203 SignalNegotiationNeeded();
Steve Anton6e634bf2017-11-13 10:44:53 -0800204}
205
206rtc::Optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
207 const {
208 return current_direction_;
209}
210
211void RtpTransceiver::Stop() {
212 for (auto sender : senders_) {
213 sender->internal()->Stop();
214 }
215 for (auto receiver : receivers_) {
216 receiver->internal()->Stop();
217 }
218 stopped_ = true;
Steve Anton54b84072018-02-20 15:19:52 -0800219 current_direction_ = rtc::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800220}
221
222void RtpTransceiver::SetCodecPreferences(
223 rtc::ArrayView<RtpCodecCapability> codecs) {
224 // TODO(steveanton): Implement this.
225 RTC_NOTREACHED() << "Not implemented";
226}
227
228} // namespace webrtc