peerconnection.h revision 0a6c4ca942f3a25c15c7af64a9515d381c34cd9c
1/* 2 * libjingle 3 * Copyright 2012 Google Inc. 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/dtlsidentitystore.h" 34#include "talk/app/webrtc/mediastreamsignaling.h" 35#include "talk/app/webrtc/peerconnectionfactory.h" 36#include "talk/app/webrtc/peerconnectioninterface.h" 37#include "talk/app/webrtc/rtpreceiverinterface.h" 38#include "talk/app/webrtc/rtpsenderinterface.h" 39#include "talk/app/webrtc/statscollector.h" 40#include "talk/app/webrtc/streamcollection.h" 41#include "talk/app/webrtc/webrtcsession.h" 42#include "webrtc/base/scoped_ptr.h" 43 44namespace webrtc { 45 46typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration> 47 StunConfigurations; 48typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration> 49 TurnConfigurations; 50 51// Parses the URLs for each server in |servers| to build |stun_config| and 52// |turn_config|. 53bool ParseIceServers(const PeerConnectionInterface::IceServers& servers, 54 StunConfigurations* stun_config, 55 TurnConfigurations* turn_config); 56 57// PeerConnection implements the PeerConnectionInterface interface. 58// It uses MediaStreamSignaling and WebRtcSession to implement 59// the PeerConnection functionality. 60class PeerConnection : public PeerConnectionInterface, 61 public MediaStreamSignalingObserver, 62 public IceObserver, 63 public rtc::MessageHandler, 64 public sigslot::has_slots<> { 65 public: 66 explicit PeerConnection(PeerConnectionFactory* factory); 67 68 bool Initialize( 69 const PeerConnectionInterface::RTCConfiguration& configuration, 70 const MediaConstraintsInterface* constraints, 71 PortAllocatorFactoryInterface* allocator_factory, 72 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store, 73 PeerConnectionObserver* observer); 74 rtc::scoped_refptr<StreamCollectionInterface> local_streams() override; 75 rtc::scoped_refptr<StreamCollectionInterface> remote_streams() override; 76 bool AddStream(MediaStreamInterface* local_stream) override; 77 void RemoveStream(MediaStreamInterface* local_stream) override; 78 79 rtc::scoped_refptr<DtmfSenderInterface> CreateDtmfSender( 80 AudioTrackInterface* track) override; 81 82 std::vector<rtc::scoped_refptr<RtpSenderInterface>> GetSenders() 83 const override; 84 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceivers() 85 const override; 86 87 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( 88 const std::string& label, 89 const DataChannelInit* config) override; 90 bool GetStats(StatsObserver* observer, 91 webrtc::MediaStreamTrackInterface* track, 92 StatsOutputLevel level) override; 93 94 SignalingState signaling_state() override; 95 96 // TODO(bemasc): Remove ice_state() when callers are removed. 97 IceState ice_state() override; 98 IceConnectionState ice_connection_state() override; 99 IceGatheringState ice_gathering_state() override; 100 101 const SessionDescriptionInterface* local_description() const override; 102 const SessionDescriptionInterface* remote_description() const override; 103 104 // JSEP01 105 void CreateOffer(CreateSessionDescriptionObserver* observer, 106 const MediaConstraintsInterface* constraints) override; 107 void CreateOffer(CreateSessionDescriptionObserver* observer, 108 const RTCOfferAnswerOptions& options) override; 109 void CreateAnswer(CreateSessionDescriptionObserver* observer, 110 const MediaConstraintsInterface* constraints) override; 111 void SetLocalDescription(SetSessionDescriptionObserver* observer, 112 SessionDescriptionInterface* desc) override; 113 void SetRemoteDescription(SetSessionDescriptionObserver* observer, 114 SessionDescriptionInterface* desc) override; 115 bool SetConfiguration( 116 const PeerConnectionInterface::RTCConfiguration& config) override; 117 bool AddIceCandidate(const IceCandidateInterface* candidate) override; 118 119 void RegisterUMAObserver(UMAObserver* observer) override; 120 121 void Close() override; 122 123 protected: 124 ~PeerConnection() override; 125 126 private: 127 // Implements MessageHandler. 128 void OnMessage(rtc::Message* msg) override; 129 130 // Implements MediaStreamSignalingObserver. 131 void OnAddRemoteStream(MediaStreamInterface* stream) override; 132 void OnRemoveRemoteStream(MediaStreamInterface* stream) override; 133 void OnAddDataChannel(DataChannelInterface* data_channel) override; 134 void OnAddRemoteAudioTrack(MediaStreamInterface* stream, 135 AudioTrackInterface* audio_track, 136 uint32 ssrc) override; 137 void OnAddRemoteVideoTrack(MediaStreamInterface* stream, 138 VideoTrackInterface* video_track, 139 uint32 ssrc) override; 140 void OnRemoveRemoteAudioTrack(MediaStreamInterface* stream, 141 AudioTrackInterface* audio_track) override; 142 void OnRemoveRemoteVideoTrack(MediaStreamInterface* stream, 143 VideoTrackInterface* video_track) override; 144 void OnAddLocalAudioTrack(MediaStreamInterface* stream, 145 AudioTrackInterface* audio_track, 146 uint32 ssrc) override; 147 void OnAddLocalVideoTrack(MediaStreamInterface* stream, 148 VideoTrackInterface* video_track, 149 uint32 ssrc) override; 150 void OnRemoveLocalAudioTrack(MediaStreamInterface* stream, 151 AudioTrackInterface* audio_track, 152 uint32 ssrc) override; 153 void OnRemoveLocalVideoTrack(MediaStreamInterface* stream, 154 VideoTrackInterface* video_track) override; 155 void OnRemoveLocalStream(MediaStreamInterface* stream) override; 156 157 // Implements IceObserver 158 void OnIceConnectionChange(IceConnectionState new_state) override; 159 void OnIceGatheringChange(IceGatheringState new_state) override; 160 void OnIceCandidate(const IceCandidateInterface* candidate) override; 161 void OnIceComplete() override; 162 void OnIceConnectionReceivingChange(bool receiving) override; 163 164 // Signals from WebRtcSession. 165 void OnSessionStateChange(cricket::BaseSession* session, 166 cricket::BaseSession::State state); 167 void ChangeSignalingState(SignalingState signaling_state); 168 169 rtc::Thread* signaling_thread() const { 170 return factory_->signaling_thread(); 171 } 172 173 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer, 174 const std::string& error); 175 176 bool IsClosed() const { 177 return signaling_state_ == PeerConnectionInterface::kClosed; 178 } 179 180 std::vector<rtc::scoped_refptr<RtpSenderInterface>>::iterator 181 FindSenderForTrack(MediaStreamTrackInterface* track); 182 std::vector<rtc::scoped_refptr<RtpReceiverInterface>>::iterator 183 FindReceiverForTrack(MediaStreamTrackInterface* track); 184 185 // Storing the factory as a scoped reference pointer ensures that the memory 186 // in the PeerConnectionFactoryImpl remains available as long as the 187 // PeerConnection is running. It is passed to PeerConnection as a raw pointer. 188 // However, since the reference counting is done in the 189 // PeerConnectionFactoryInteface all instances created using the raw pointer 190 // will refer to the same reference count. 191 rtc::scoped_refptr<PeerConnectionFactory> factory_; 192 PeerConnectionObserver* observer_; 193 UMAObserver* uma_observer_; 194 SignalingState signaling_state_; 195 // TODO(bemasc): Remove ice_state_. 196 IceState ice_state_; 197 IceConnectionState ice_connection_state_; 198 IceGatheringState ice_gathering_state_; 199 200 rtc::scoped_ptr<cricket::PortAllocator> port_allocator_; 201 rtc::scoped_ptr<WebRtcSession> session_; 202 rtc::scoped_ptr<MediaStreamSignaling> mediastream_signaling_; 203 rtc::scoped_ptr<StatsCollector> stats_; 204 205 std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_; 206 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_; 207}; 208 209} // namespace webrtc 210 211#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_ 212