blob: 19f393f3fec72f7016e71f0c80b8fe3660dc857d [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#ifndef PC_RTPTRANSCEIVER_H_
12#define PC_RTPTRANSCEIVER_H_
13
14#include <string>
15#include <vector>
16
17#include "api/rtptransceiverinterface.h"
18#include "pc/rtpreceiver.h"
19#include "pc/rtpsender.h"
20
21namespace webrtc {
22
23// Implementation of the public RtpTransceiverInterface.
24//
25// The RtpTransceiverInterface is only intended to be used with a PeerConnection
26// that enables Unified Plan SDP. Thus, the methods that only need to implement
27// public API features and are not used internally can assume exactly one sender
28// and receiver.
29//
30// Since the RtpTransceiver is used internally by PeerConnection for tracking
31// RtpSenders, RtpReceivers, and BaseChannels, and PeerConnection needs to be
32// backwards compatible with Plan B SDP, this implementation is more flexible
33// than that required by the WebRTC specification.
34//
35// With Plan B SDP, an RtpTransceiver can have any number of senders and
36// receivers which map to a=ssrc lines in the m= section.
37// With Unified Plan SDP, an RtpTransceiver will have exactly one sender and one
38// receiver which are encapsulated by the m= section.
39//
40// This class manages the RtpSenders, RtpReceivers, and BaseChannel associated
41// with this m= section. Since the transceiver, senders, and receivers are
42// reference counted and can be referenced from JavaScript (in Chromium), these
43// objects must be ready to live for an arbitrary amount of time. The
44// BaseChannel is not reference counted and is owned by the ChannelManager, so
45// the PeerConnection must take care of creating/deleting the BaseChannel and
46// setting the channel reference in the transceiver to null when it has been
47// deleted.
48//
49// The RtpTransceiver is specialized to either audio or video according to the
50// MediaType specified in the constructor. Audio RtpTransceivers will have
51// AudioRtpSenders, AudioRtpReceivers, and a VoiceChannel. Video RtpTransceivers
52// will have VideoRtpSenders, VideoRtpReceivers, and a VideoChannel.
53class RtpTransceiver final
54 : public rtc::RefCountedObject<RtpTransceiverInterface> {
55 public:
Steve Anton79e79602017-11-20 10:25:56 -080056 // Construct a Plan B-style RtpTransceiver with no senders, receivers, or
57 // channel set.
Steve Anton6e634bf2017-11-13 10:44:53 -080058 // |media_type| specifies the type of RtpTransceiver (and, by transitivity,
59 // the type of senders, receivers, and channel). Can either by audio or video.
60 explicit RtpTransceiver(cricket::MediaType media_type);
Steve Anton79e79602017-11-20 10:25:56 -080061 // Construct a Unified Plan-style RtpTransceiver with the given sender and
62 // receiver. The media type will be derived from the media types of the sender
63 // and receiver. The sender and receiver should have the same media type.
64 RtpTransceiver(
65 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
66 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
67 receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -080068 ~RtpTransceiver() override;
69
70 cricket::MediaType media_type() const { return media_type_; }
71
72 // Returns the Voice/VideoChannel set for this transceiver. May be null if
73 // the transceiver is not in the currently set local/remote description.
74 cricket::BaseChannel* channel() const { return channel_; }
75
76 // Sets the Voice/VideoChannel. The caller must pass in the correct channel
77 // implementation based on the type of the transceiver.
78 void SetChannel(cricket::BaseChannel* channel);
79
80 // Adds an RtpSender of the appropriate type to be owned by this transceiver.
81 // Must not be null.
82 void AddSender(
83 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender);
84
85 // Removes the given RtpSender. Returns false if the sender is not owned by
86 // this transceiver.
87 bool RemoveSender(RtpSenderInterface* sender);
88
89 // Returns a vector of the senders owned by this transceiver.
90 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
91 senders() const {
92 return senders_;
93 }
94
95 // Adds an RtpReceiver of the appropriate type to be owned by this
96 // transceiver. Must not be null.
97 void AddReceiver(
98 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
99 receiver);
100
101 // Removes the given RtpReceiver. Returns false if the sender is not owned by
102 // this transceiver.
103 bool RemoveReceiver(RtpReceiverInterface* receiver);
104
105 // Returns a vector of the receivers owned by this transceiver.
106 std::vector<
107 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
108 receivers() const {
109 return receivers_;
110 }
111
Steve Antonf9381f02017-12-14 10:23:57 -0800112 // Returns the backing object for the transceiver's Unified Plan sender.
113 rtc::scoped_refptr<RtpSenderInternal> sender_internal() const;
114
115 // Returns the backing object for the transceiver's Unified Plan receiver.
116 rtc::scoped_refptr<RtpReceiverInternal> receiver_internal() const;
117
118 // According to JSEP rules for SetRemoteDescription, RtpTransceivers can be
119 // reused only if they were added by AddTrack.
120 void set_created_by_addtrack(bool created_by_addtrack) {
121 created_by_addtrack_ = created_by_addtrack;
122 }
123 bool created_by_addtrack() const { return created_by_addtrack_; }
124
125 // Returns true if this transceiver has ever had the current direction set to
126 // sendonly or sendrecv.
127 bool has_ever_been_used_to_send() const {
128 return has_ever_been_used_to_send_;
129 }
130
Steve Anton6e634bf2017-11-13 10:44:53 -0800131 // RtpTransceiverInterface implementation.
132 rtc::Optional<std::string> mid() const override;
133 rtc::scoped_refptr<RtpSenderInterface> sender() const override;
134 rtc::scoped_refptr<RtpReceiverInterface> receiver() const override;
135 bool stopped() const override;
136 RtpTransceiverDirection direction() const override;
137 void SetDirection(RtpTransceiverDirection new_direction) override;
138 rtc::Optional<RtpTransceiverDirection> current_direction() const override;
139 void Stop() override;
140 void SetCodecPreferences(rtc::ArrayView<RtpCodecCapability> codecs) override;
141
142 private:
143 const bool unified_plan_;
144 const cricket::MediaType media_type_;
145 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
146 senders_;
147 std::vector<
148 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
149 receivers_;
150
151 bool stopped_ = false;
152 RtpTransceiverDirection direction_ = RtpTransceiverDirection::kInactive;
153 rtc::Optional<RtpTransceiverDirection> current_direction_;
154 rtc::Optional<std::string> mid_;
Steve Antonf9381f02017-12-14 10:23:57 -0800155 bool created_by_addtrack_ = false;
156 // TODO(steveanton): Implement this once there is a mechanism to set the
157 // current direction.
158 bool has_ever_been_used_to_send_ = false;
Steve Anton6e634bf2017-11-13 10:44:53 -0800159
160 cricket::BaseChannel* channel_ = nullptr;
161};
162
163BEGIN_SIGNALING_PROXY_MAP(RtpTransceiver)
164PROXY_SIGNALING_THREAD_DESTRUCTOR()
165PROXY_CONSTMETHOD0(rtc::Optional<std::string>, mid);
166PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpSenderInterface>, sender);
167PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpReceiverInterface>, receiver);
168PROXY_CONSTMETHOD0(bool, stopped);
169PROXY_CONSTMETHOD0(RtpTransceiverDirection, direction);
170PROXY_METHOD1(void, SetDirection, RtpTransceiverDirection);
171PROXY_CONSTMETHOD0(rtc::Optional<RtpTransceiverDirection>, current_direction);
172PROXY_METHOD0(void, Stop);
173PROXY_METHOD1(void, SetCodecPreferences, rtc::ArrayView<RtpCodecCapability>);
174END_PROXY_MAP();
175
176} // namespace webrtc
177
178#endif // PC_RTPTRANSCEIVER_H_