henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_APP_WEBRTC_PEERCONNECTION_H_ |
| 29 | #define TALK_APP_WEBRTC_PEERCONNECTION_H_ |
| 30 | |
| 31 | #include <string> |
| 32 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 33 | #include "talk/app/webrtc/dtlsidentitystore.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #include "talk/app/webrtc/mediastreamsignaling.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | #include "talk/app/webrtc/peerconnectionfactory.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 36 | #include "talk/app/webrtc/peerconnectioninterface.h" |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame^] | 37 | #include "talk/app/webrtc/rtpreceiverinterface.h" |
| 38 | #include "talk/app/webrtc/rtpsenderinterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | #include "talk/app/webrtc/statscollector.h" |
| 40 | #include "talk/app/webrtc/streamcollection.h" |
| 41 | #include "talk/app/webrtc/webrtcsession.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 42 | #include "webrtc/base/scoped_ptr.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | |
| 44 | namespace webrtc { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | |
| 46 | typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration> |
| 47 | StunConfigurations; |
| 48 | typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration> |
| 49 | TurnConfigurations; |
| 50 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame^] | 51 | // PeerConnection implements the PeerConnectionInterface interface. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | // It uses MediaStreamSignaling and WebRtcSession to implement |
| 53 | // the PeerConnection functionality. |
| 54 | class PeerConnection : public PeerConnectionInterface, |
| 55 | public MediaStreamSignalingObserver, |
| 56 | public IceObserver, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 57 | public rtc::MessageHandler, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | public sigslot::has_slots<> { |
| 59 | public: |
| 60 | explicit PeerConnection(PeerConnectionFactory* factory); |
| 61 | |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 62 | bool Initialize( |
| 63 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 64 | const MediaConstraintsInterface* constraints, |
| 65 | PortAllocatorFactoryInterface* allocator_factory, |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 66 | rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 67 | PeerConnectionObserver* observer); |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 68 | virtual rtc::scoped_refptr<StreamCollectionInterface> local_streams(); |
| 69 | virtual rtc::scoped_refptr<StreamCollectionInterface> remote_streams(); |
| 70 | virtual bool AddStream(MediaStreamInterface* local_stream); |
| 71 | virtual void RemoveStream(MediaStreamInterface* local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 73 | virtual rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender( |
| 74 | AudioTrackInterface* track); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame^] | 76 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders() |
| 77 | const override; |
| 78 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers() |
| 79 | const override; |
| 80 | |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 81 | virtual rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | const std::string& label, |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 83 | const DataChannelInit* config); |
| 84 | virtual bool GetStats(StatsObserver* observer, |
| 85 | webrtc::MediaStreamTrackInterface* track, |
| 86 | StatsOutputLevel level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 88 | virtual SignalingState signaling_state(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | |
| 90 | // TODO(bemasc): Remove ice_state() when callers are removed. |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 91 | virtual IceState ice_state(); |
| 92 | virtual IceConnectionState ice_connection_state(); |
| 93 | virtual IceGatheringState ice_gathering_state(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 95 | virtual const SessionDescriptionInterface* local_description() const; |
| 96 | virtual const SessionDescriptionInterface* remote_description() const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | |
| 98 | // JSEP01 |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 99 | virtual void CreateOffer(CreateSessionDescriptionObserver* observer, |
| 100 | const MediaConstraintsInterface* constraints); |
| 101 | virtual void CreateOffer(CreateSessionDescriptionObserver* observer, |
| 102 | const RTCOfferAnswerOptions& options); |
| 103 | virtual void CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 104 | const MediaConstraintsInterface* constraints); |
| 105 | virtual void SetLocalDescription(SetSessionDescriptionObserver* observer, |
| 106 | SessionDescriptionInterface* desc); |
| 107 | virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer, |
| 108 | SessionDescriptionInterface* desc); |
| 109 | // TODO(mallinath) : Deprecated version, remove after all clients are updated. |
| 110 | virtual bool UpdateIce(const IceServers& configuration, |
| 111 | const MediaConstraintsInterface* constraints); |
| 112 | virtual bool UpdateIce( |
| 113 | const PeerConnectionInterface::RTCConfiguration& config); |
| 114 | virtual bool AddIceCandidate(const IceCandidateInterface* candidate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 116 | virtual void RegisterUMAObserver(UMAObserver* observer); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 117 | |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 118 | virtual void Close(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | |
| 120 | protected: |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 121 | virtual ~PeerConnection(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | // Implements MessageHandler. |
deadbeef | 7603c76 | 2015-09-23 17:37:11 -0700 | [diff] [blame] | 125 | virtual void OnMessage(rtc::Message* msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | |
| 127 | // Implements MediaStreamSignalingObserver. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 128 | void OnAddRemoteStream(MediaStreamInterface* stream) override; |
| 129 | void OnRemoveRemoteStream(MediaStreamInterface* stream) override; |
| 130 | void OnAddDataChannel(DataChannelInterface* data_channel) override; |
| 131 | void OnAddRemoteAudioTrack(MediaStreamInterface* stream, |
| 132 | AudioTrackInterface* audio_track, |
| 133 | uint32 ssrc) override; |
| 134 | void OnAddRemoteVideoTrack(MediaStreamInterface* stream, |
| 135 | VideoTrackInterface* video_track, |
| 136 | uint32 ssrc) override; |
| 137 | void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream, |
| 138 | AudioTrackInterface* audio_track) override; |
| 139 | void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream, |
| 140 | VideoTrackInterface* video_track) override; |
| 141 | void OnAddLocalAudioTrack(MediaStreamInterface* stream, |
| 142 | AudioTrackInterface* audio_track, |
| 143 | uint32 ssrc) override; |
| 144 | void OnAddLocalVideoTrack(MediaStreamInterface* stream, |
| 145 | VideoTrackInterface* video_track, |
| 146 | uint32 ssrc) override; |
| 147 | void OnRemoveLocalAudioTrack(MediaStreamInterface* stream, |
| 148 | AudioTrackInterface* audio_track, |
| 149 | uint32 ssrc) override; |
| 150 | void OnRemoveLocalVideoTrack(MediaStreamInterface* stream, |
| 151 | VideoTrackInterface* video_track) override; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 152 | void OnRemoveLocalStream(MediaStreamInterface* stream) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | |
| 154 | // Implements IceObserver |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 155 | void OnIceConnectionChange(IceConnectionState new_state) override; |
| 156 | void OnIceGatheringChange(IceGatheringState new_state) override; |
| 157 | void OnIceCandidate(const IceCandidateInterface* candidate) override; |
| 158 | void OnIceComplete() override; |
| 159 | void OnIceConnectionReceivingChange(bool receiving) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | |
| 161 | // Signals from WebRtcSession. |
| 162 | void OnSessionStateChange(cricket::BaseSession* session, |
| 163 | cricket::BaseSession::State state); |
| 164 | void ChangeSignalingState(SignalingState signaling_state); |
| 165 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 166 | rtc::Thread* signaling_thread() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | return factory_->signaling_thread(); |
| 168 | } |
| 169 | |
| 170 | void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer, |
| 171 | const std::string& error); |
| 172 | |
| 173 | bool IsClosed() const { |
| 174 | return signaling_state_ == PeerConnectionInterface::kClosed; |
| 175 | } |
| 176 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame^] | 177 | std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator |
| 178 | FindSenderForTrack(MediaStreamTrackInterface* track); |
| 179 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator |
| 180 | FindReceiverForTrack(MediaStreamTrackInterface* track); |
| 181 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | // Storing the factory as a scoped reference pointer ensures that the memory |
| 183 | // in the PeerConnectionFactoryImpl remains available as long as the |
| 184 | // PeerConnection is running. It is passed to PeerConnection as a raw pointer. |
| 185 | // However, since the reference counting is done in the |
| 186 | // PeerConnectionFactoryInteface all instances created using the raw pointer |
| 187 | // will refer to the same reference count. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 188 | rtc::scoped_refptr<PeerConnectionFactory> factory_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | PeerConnectionObserver* observer_; |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 190 | UMAObserver* uma_observer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 191 | SignalingState signaling_state_; |
| 192 | // TODO(bemasc): Remove ice_state_. |
| 193 | IceState ice_state_; |
| 194 | IceConnectionState ice_connection_state_; |
| 195 | IceGatheringState ice_gathering_state_; |
| 196 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 197 | rtc::scoped_ptr<cricket::PortAllocator> port_allocator_; |
| 198 | rtc::scoped_ptr<WebRtcSession> session_; |
| 199 | rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 200 | rtc::scoped_ptr<StatsCollector> stats_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame^] | 201 | |
| 202 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_; |
| 203 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | } // namespace webrtc |
| 207 | |
| 208 | #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_ |