Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace 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. |
| 53 | class RtpTransceiver final |
| 54 | : public rtc::RefCountedObject<RtpTransceiverInterface> { |
| 55 | public: |
| 56 | // Construct an RtpTransceiver with no senders, receivers, or channel set. |
| 57 | // |media_type| specifies the type of RtpTransceiver (and, by transitivity, |
| 58 | // the type of senders, receivers, and channel). Can either by audio or video. |
| 59 | explicit RtpTransceiver(cricket::MediaType media_type); |
| 60 | ~RtpTransceiver() override; |
| 61 | |
| 62 | cricket::MediaType media_type() const { return media_type_; } |
| 63 | |
| 64 | // Returns the Voice/VideoChannel set for this transceiver. May be null if |
| 65 | // the transceiver is not in the currently set local/remote description. |
| 66 | cricket::BaseChannel* channel() const { return channel_; } |
| 67 | |
| 68 | // Sets the Voice/VideoChannel. The caller must pass in the correct channel |
| 69 | // implementation based on the type of the transceiver. |
| 70 | void SetChannel(cricket::BaseChannel* channel); |
| 71 | |
| 72 | // Adds an RtpSender of the appropriate type to be owned by this transceiver. |
| 73 | // Must not be null. |
| 74 | void AddSender( |
| 75 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender); |
| 76 | |
| 77 | // Removes the given RtpSender. Returns false if the sender is not owned by |
| 78 | // this transceiver. |
| 79 | bool RemoveSender(RtpSenderInterface* sender); |
| 80 | |
| 81 | // Returns a vector of the senders owned by this transceiver. |
| 82 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 83 | senders() const { |
| 84 | return senders_; |
| 85 | } |
| 86 | |
| 87 | // Adds an RtpReceiver of the appropriate type to be owned by this |
| 88 | // transceiver. Must not be null. |
| 89 | void AddReceiver( |
| 90 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 91 | receiver); |
| 92 | |
| 93 | // Removes the given RtpReceiver. Returns false if the sender is not owned by |
| 94 | // this transceiver. |
| 95 | bool RemoveReceiver(RtpReceiverInterface* receiver); |
| 96 | |
| 97 | // Returns a vector of the receivers owned by this transceiver. |
| 98 | std::vector< |
| 99 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 100 | receivers() const { |
| 101 | return receivers_; |
| 102 | } |
| 103 | |
| 104 | // RtpTransceiverInterface implementation. |
| 105 | rtc::Optional<std::string> mid() const override; |
| 106 | rtc::scoped_refptr<RtpSenderInterface> sender() const override; |
| 107 | rtc::scoped_refptr<RtpReceiverInterface> receiver() const override; |
| 108 | bool stopped() const override; |
| 109 | RtpTransceiverDirection direction() const override; |
| 110 | void SetDirection(RtpTransceiverDirection new_direction) override; |
| 111 | rtc::Optional<RtpTransceiverDirection> current_direction() const override; |
| 112 | void Stop() override; |
| 113 | void SetCodecPreferences(rtc::ArrayView<RtpCodecCapability> codecs) override; |
| 114 | |
| 115 | private: |
| 116 | const bool unified_plan_; |
| 117 | const cricket::MediaType media_type_; |
| 118 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 119 | senders_; |
| 120 | std::vector< |
| 121 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 122 | receivers_; |
| 123 | |
| 124 | bool stopped_ = false; |
| 125 | RtpTransceiverDirection direction_ = RtpTransceiverDirection::kInactive; |
| 126 | rtc::Optional<RtpTransceiverDirection> current_direction_; |
| 127 | rtc::Optional<std::string> mid_; |
| 128 | |
| 129 | cricket::BaseChannel* channel_ = nullptr; |
| 130 | }; |
| 131 | |
| 132 | BEGIN_SIGNALING_PROXY_MAP(RtpTransceiver) |
| 133 | PROXY_SIGNALING_THREAD_DESTRUCTOR() |
| 134 | PROXY_CONSTMETHOD0(rtc::Optional<std::string>, mid); |
| 135 | PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpSenderInterface>, sender); |
| 136 | PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpReceiverInterface>, receiver); |
| 137 | PROXY_CONSTMETHOD0(bool, stopped); |
| 138 | PROXY_CONSTMETHOD0(RtpTransceiverDirection, direction); |
| 139 | PROXY_METHOD1(void, SetDirection, RtpTransceiverDirection); |
| 140 | PROXY_CONSTMETHOD0(rtc::Optional<RtpTransceiverDirection>, current_direction); |
| 141 | PROXY_METHOD0(void, Stop); |
| 142 | PROXY_METHOD1(void, SetCodecPreferences, rtc::ArrayView<RtpCodecCapability>); |
| 143 | END_PROXY_MAP(); |
| 144 | |
| 145 | } // namespace webrtc |
| 146 | |
| 147 | #endif // PC_RTPTRANSCEIVER_H_ |