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 | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 68 | rtc::scoped_refptr<StreamCollectionInterface> local_streams() override; |
| 69 | rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override; |
| 70 | bool AddStream(MediaStreamInterface* local_stream) override; |
| 71 | void RemoveStream(MediaStreamInterface* local_stream) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 73 | rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender( |
| 74 | AudioTrackInterface* track) override; |
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 | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 81 | rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | const std::string& label, |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 83 | const DataChannelInit* config) override; |
| 84 | bool GetStats(StatsObserver* observer, |
| 85 | webrtc::MediaStreamTrackInterface* track, |
| 86 | StatsOutputLevel level) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 88 | SignalingState signaling_state() override; |
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 | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 91 | IceState ice_state() override; |
| 92 | IceConnectionState ice_connection_state() override; |
| 93 | IceGatheringState ice_gathering_state() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 95 | const SessionDescriptionInterface* local_description() const override; |
| 96 | const SessionDescriptionInterface* remote_description() const override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | |
| 98 | // JSEP01 |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 99 | void CreateOffer(CreateSessionDescriptionObserver* observer, |
| 100 | const MediaConstraintsInterface* constraints) override; |
| 101 | void CreateOffer(CreateSessionDescriptionObserver* observer, |
| 102 | const RTCOfferAnswerOptions& options) override; |
| 103 | void CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 104 | const MediaConstraintsInterface* constraints) override; |
| 105 | void SetLocalDescription(SetSessionDescriptionObserver* observer, |
| 106 | SessionDescriptionInterface* desc) override; |
| 107 | void SetRemoteDescription(SetSessionDescriptionObserver* observer, |
| 108 | SessionDescriptionInterface* desc) override; |
| 109 | bool SetConfiguration( |
| 110 | const PeerConnectionInterface::RTCConfiguration& config) override; |
| 111 | bool AddIceCandidate(const IceCandidateInterface* candidate) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 113 | void RegisterUMAObserver(UMAObserver* observer) override; |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 114 | |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 115 | void Close() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | |
| 117 | protected: |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 118 | ~PeerConnection() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | // Implements MessageHandler. |
deadbeef | a67696b | 2015-09-29 11:56:26 -0700 | [diff] [blame^] | 122 | void OnMessage(rtc::Message* msg) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | |
| 124 | // Implements MediaStreamSignalingObserver. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 125 | void OnAddRemoteStream(MediaStreamInterface* stream) override; |
| 126 | void OnRemoveRemoteStream(MediaStreamInterface* stream) override; |
| 127 | void OnAddDataChannel(DataChannelInterface* data_channel) override; |
| 128 | void OnAddRemoteAudioTrack(MediaStreamInterface* stream, |
| 129 | AudioTrackInterface* audio_track, |
| 130 | uint32 ssrc) override; |
| 131 | void OnAddRemoteVideoTrack(MediaStreamInterface* stream, |
| 132 | VideoTrackInterface* video_track, |
| 133 | uint32 ssrc) override; |
| 134 | void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream, |
| 135 | AudioTrackInterface* audio_track) override; |
| 136 | void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream, |
| 137 | VideoTrackInterface* video_track) override; |
| 138 | void OnAddLocalAudioTrack(MediaStreamInterface* stream, |
| 139 | AudioTrackInterface* audio_track, |
| 140 | uint32 ssrc) override; |
| 141 | void OnAddLocalVideoTrack(MediaStreamInterface* stream, |
| 142 | VideoTrackInterface* video_track, |
| 143 | uint32 ssrc) override; |
| 144 | void OnRemoveLocalAudioTrack(MediaStreamInterface* stream, |
| 145 | AudioTrackInterface* audio_track, |
| 146 | uint32 ssrc) override; |
| 147 | void OnRemoveLocalVideoTrack(MediaStreamInterface* stream, |
| 148 | VideoTrackInterface* video_track) override; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 149 | void OnRemoveLocalStream(MediaStreamInterface* stream) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | |
| 151 | // Implements IceObserver |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 152 | void OnIceConnectionChange(IceConnectionState new_state) override; |
| 153 | void OnIceGatheringChange(IceGatheringState new_state) override; |
| 154 | void OnIceCandidate(const IceCandidateInterface* candidate) override; |
| 155 | void OnIceComplete() override; |
| 156 | void OnIceConnectionReceivingChange(bool receiving) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | |
| 158 | // Signals from WebRtcSession. |
| 159 | void OnSessionStateChange(cricket::BaseSession* session, |
| 160 | cricket::BaseSession::State state); |
| 161 | void ChangeSignalingState(SignalingState signaling_state); |
| 162 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 163 | rtc::Thread* signaling_thread() const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | return factory_->signaling_thread(); |
| 165 | } |
| 166 | |
| 167 | void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer, |
| 168 | const std::string& error); |
| 169 | |
| 170 | bool IsClosed() const { |
| 171 | return signaling_state_ == PeerConnectionInterface::kClosed; |
| 172 | } |
| 173 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 174 | std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator |
| 175 | FindSenderForTrack(MediaStreamTrackInterface* track); |
| 176 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator |
| 177 | FindReceiverForTrack(MediaStreamTrackInterface* track); |
| 178 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | // Storing the factory as a scoped reference pointer ensures that the memory |
| 180 | // in the PeerConnectionFactoryImpl remains available as long as the |
| 181 | // PeerConnection is running. It is passed to PeerConnection as a raw pointer. |
| 182 | // However, since the reference counting is done in the |
| 183 | // PeerConnectionFactoryInteface all instances created using the raw pointer |
| 184 | // will refer to the same reference count. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 185 | rtc::scoped_refptr<PeerConnectionFactory> factory_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 186 | PeerConnectionObserver* observer_; |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 187 | UMAObserver* uma_observer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 188 | SignalingState signaling_state_; |
| 189 | // TODO(bemasc): Remove ice_state_. |
| 190 | IceState ice_state_; |
| 191 | IceConnectionState ice_connection_state_; |
| 192 | IceGatheringState ice_gathering_state_; |
| 193 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 194 | rtc::scoped_ptr<cricket::PortAllocator> port_allocator_; |
| 195 | rtc::scoped_ptr<WebRtcSession> session_; |
| 196 | rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 197 | rtc::scoped_ptr<StatsCollector> stats_; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 198 | |
| 199 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_; |
| 200 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } // namespace webrtc |
| 204 | |
| 205 | #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_ |