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 | |
| 33 | #include "talk/app/webrtc/mediastreamsignaling.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #include "talk/app/webrtc/peerconnectionfactory.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 35 | #include "talk/app/webrtc/peerconnectioninterface.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | #include "talk/app/webrtc/statscollector.h" |
| 37 | #include "talk/app/webrtc/streamcollection.h" |
| 38 | #include "talk/app/webrtc/webrtcsession.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 39 | #include "webrtc/base/scoped_ptr.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | |
| 41 | namespace webrtc { |
| 42 | class MediaStreamHandlerContainer; |
| 43 | |
| 44 | typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration> |
| 45 | StunConfigurations; |
| 46 | typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration> |
| 47 | TurnConfigurations; |
| 48 | |
| 49 | // PeerConnectionImpl implements the PeerConnection interface. |
| 50 | // It uses MediaStreamSignaling and WebRtcSession to implement |
| 51 | // the PeerConnection functionality. |
| 52 | class PeerConnection : public PeerConnectionInterface, |
| 53 | public MediaStreamSignalingObserver, |
| 54 | public IceObserver, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 55 | public rtc::MessageHandler, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | public sigslot::has_slots<> { |
| 57 | public: |
| 58 | explicit PeerConnection(PeerConnectionFactory* factory); |
| 59 | |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 60 | bool Initialize( |
| 61 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 62 | const MediaConstraintsInterface* constraints, |
| 63 | PortAllocatorFactoryInterface* allocator_factory, |
| 64 | DTLSIdentityServiceInterface* dtls_identity_service, |
| 65 | PeerConnectionObserver* observer); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 66 | virtual rtc::scoped_refptr<StreamCollectionInterface> local_streams(); |
| 67 | virtual rtc::scoped_refptr<StreamCollectionInterface> remote_streams(); |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 68 | virtual bool AddStream(MediaStreamInterface* local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | virtual void RemoveStream(MediaStreamInterface* local_stream); |
| 70 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 71 | virtual rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | AudioTrackInterface* track); |
| 73 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 74 | virtual rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | const std::string& label, |
| 76 | const DataChannelInit* config); |
| 77 | virtual bool GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 78 | webrtc::MediaStreamTrackInterface* track, |
| 79 | StatsOutputLevel level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | |
| 81 | virtual SignalingState signaling_state(); |
| 82 | |
| 83 | // TODO(bemasc): Remove ice_state() when callers are removed. |
| 84 | virtual IceState ice_state(); |
| 85 | virtual IceConnectionState ice_connection_state(); |
| 86 | virtual IceGatheringState ice_gathering_state(); |
| 87 | |
| 88 | virtual const SessionDescriptionInterface* local_description() const; |
| 89 | virtual const SessionDescriptionInterface* remote_description() const; |
| 90 | |
| 91 | // JSEP01 |
| 92 | virtual void CreateOffer(CreateSessionDescriptionObserver* observer, |
| 93 | const MediaConstraintsInterface* constraints); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 94 | virtual void CreateOffer(CreateSessionDescriptionObserver* observer, |
| 95 | const RTCOfferAnswerOptions& options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | virtual void CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 97 | const MediaConstraintsInterface* constraints); |
| 98 | virtual void SetLocalDescription(SetSessionDescriptionObserver* observer, |
| 99 | SessionDescriptionInterface* desc); |
| 100 | virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer, |
| 101 | SessionDescriptionInterface* desc); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 102 | // TODO(mallinath) : Deprecated version, remove after all clients are updated. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | virtual bool UpdateIce(const IceServers& configuration, |
| 104 | const MediaConstraintsInterface* constraints); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 105 | virtual bool UpdateIce( |
| 106 | const PeerConnectionInterface::RTCConfiguration& config); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | virtual bool AddIceCandidate(const IceCandidateInterface* candidate); |
| 108 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 109 | virtual void RegisterUMAObserver(UMAObserver* observer); |
| 110 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | virtual void Close(); |
| 112 | |
| 113 | protected: |
| 114 | virtual ~PeerConnection(); |
| 115 | |
| 116 | private: |
| 117 | // Implements MessageHandler. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 118 | virtual void OnMessage(rtc::Message* msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | |
| 120 | // Implements MediaStreamSignalingObserver. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame^] | 121 | void OnAddRemoteStream(MediaStreamInterface* stream) override; |
| 122 | void OnRemoveRemoteStream(MediaStreamInterface* stream) override; |
| 123 | void OnAddDataChannel(DataChannelInterface* data_channel) override; |
| 124 | void OnAddRemoteAudioTrack(MediaStreamInterface* stream, |
| 125 | AudioTrackInterface* audio_track, |
| 126 | uint32 ssrc) override; |
| 127 | void OnAddRemoteVideoTrack(MediaStreamInterface* stream, |
| 128 | VideoTrackInterface* video_track, |
| 129 | uint32 ssrc) override; |
| 130 | void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream, |
| 131 | AudioTrackInterface* audio_track) override; |
| 132 | void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream, |
| 133 | VideoTrackInterface* video_track) override; |
| 134 | void OnAddLocalAudioTrack(MediaStreamInterface* stream, |
| 135 | AudioTrackInterface* audio_track, |
| 136 | uint32 ssrc) override; |
| 137 | void OnAddLocalVideoTrack(MediaStreamInterface* stream, |
| 138 | VideoTrackInterface* video_track, |
| 139 | uint32 ssrc) override; |
| 140 | void OnRemoveLocalAudioTrack(MediaStreamInterface* stream, |
| 141 | AudioTrackInterface* audio_track, |
| 142 | uint32 ssrc) override; |
| 143 | void OnRemoveLocalVideoTrack(MediaStreamInterface* stream, |
| 144 | VideoTrackInterface* video_track) override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | virtual void OnRemoveLocalStream(MediaStreamInterface* stream); |
| 146 | |
| 147 | // Implements IceObserver |
| 148 | virtual void OnIceConnectionChange(IceConnectionState new_state); |
| 149 | virtual void OnIceGatheringChange(IceGatheringState new_state); |
| 150 | virtual void OnIceCandidate(const IceCandidateInterface* candidate); |
| 151 | virtual void OnIceComplete(); |
| 152 | |
| 153 | // Signals from WebRtcSession. |
| 154 | void OnSessionStateChange(cricket::BaseSession* session, |
| 155 | cricket::BaseSession::State state); |
| 156 | void ChangeSignalingState(SignalingState signaling_state); |
| 157 | |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 158 | bool DoInitialize(IceTransportsType type, |
| 159 | const StunConfigurations& stun_config, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | const TurnConfigurations& turn_config, |
| 161 | const MediaConstraintsInterface* constraints, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 162 | PortAllocatorFactoryInterface* allocator_factory, |
| 163 | DTLSIdentityServiceInterface* dtls_identity_service, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | PeerConnectionObserver* observer); |
| 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 | |
| 177 | // Storing the factory as a scoped reference pointer ensures that the memory |
| 178 | // in the PeerConnectionFactoryImpl remains available as long as the |
| 179 | // PeerConnection is running. It is passed to PeerConnection as a raw pointer. |
| 180 | // However, since the reference counting is done in the |
| 181 | // PeerConnectionFactoryInteface all instances created using the raw pointer |
| 182 | // will refer to the same reference count. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 183 | rtc::scoped_refptr<PeerConnectionFactory> factory_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 184 | PeerConnectionObserver* observer_; |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 185 | UMAObserver* uma_observer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 186 | SignalingState signaling_state_; |
| 187 | // TODO(bemasc): Remove ice_state_. |
| 188 | IceState ice_state_; |
| 189 | IceConnectionState ice_connection_state_; |
| 190 | IceGatheringState ice_gathering_state_; |
| 191 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 192 | rtc::scoped_ptr<cricket::PortAllocator> port_allocator_; |
| 193 | rtc::scoped_ptr<WebRtcSession> session_; |
| 194 | rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_; |
| 195 | rtc::scoped_ptr<MediaStreamHandlerContainer> stream_handler_container_; |
| 196 | rtc::scoped_ptr<StatsCollector> stats_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // namespace webrtc |
| 200 | |
| 201 | #endif // TALK_APP_WEBRTC_PEERCONNECTION_H_ |