blob: 28aa9d82ccf05084dd486c8ef2f8272a5c2e3fcf [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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/mediastreamsignaling.h"
34#include "talk/app/webrtc/peerconnectioninterface.h"
35#include "talk/app/webrtc/peerconnectionfactory.h"
36#include "talk/app/webrtc/statscollector.h"
37#include "talk/app/webrtc/streamcollection.h"
38#include "talk/app/webrtc/webrtcsession.h"
39#include "talk/base/scoped_ptr.h"
40
41namespace webrtc {
42class MediaStreamHandlerContainer;
43
44typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
45 StunConfigurations;
46typedef 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.
52class PeerConnection : public PeerConnectionInterface,
53 public MediaStreamSignalingObserver,
54 public IceObserver,
55 public talk_base::MessageHandler,
56 public sigslot::has_slots<> {
57 public:
58 explicit PeerConnection(PeerConnectionFactory* factory);
59
60 bool Initialize(const PeerConnectionInterface::IceServers& configuration,
61 const MediaConstraintsInterface* constraints,
62 webrtc::PortAllocatorFactoryInterface* allocator_factory,
63 PeerConnectionObserver* observer);
64 virtual talk_base::scoped_refptr<StreamCollectionInterface> local_streams();
65 virtual talk_base::scoped_refptr<StreamCollectionInterface> remote_streams();
66 virtual bool AddStream(MediaStreamInterface* local_stream,
67 const MediaConstraintsInterface* constraints);
68 virtual void RemoveStream(MediaStreamInterface* local_stream);
69
70 virtual talk_base::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
71 AudioTrackInterface* track);
72
73 virtual talk_base::scoped_refptr<DataChannelInterface> CreateDataChannel(
74 const std::string& label,
75 const DataChannelInit* config);
76 virtual bool GetStats(StatsObserver* observer,
77 webrtc::MediaStreamTrackInterface* track);
78
79 virtual SignalingState signaling_state();
80
81 // TODO(bemasc): Remove ice_state() when callers are removed.
82 virtual IceState ice_state();
83 virtual IceConnectionState ice_connection_state();
84 virtual IceGatheringState ice_gathering_state();
85
86 virtual const SessionDescriptionInterface* local_description() const;
87 virtual const SessionDescriptionInterface* remote_description() const;
88
89 // JSEP01
90 virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
91 const MediaConstraintsInterface* constraints);
92 virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
93 const MediaConstraintsInterface* constraints);
94 virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
95 SessionDescriptionInterface* desc);
96 virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
97 SessionDescriptionInterface* desc);
98 virtual bool UpdateIce(const IceServers& configuration,
99 const MediaConstraintsInterface* constraints);
100 virtual bool AddIceCandidate(const IceCandidateInterface* candidate);
101
102 virtual void Close();
103
104 protected:
105 virtual ~PeerConnection();
106
107 private:
108 // Implements MessageHandler.
109 virtual void OnMessage(talk_base::Message* msg);
110
111 // Implements MediaStreamSignalingObserver.
112 virtual void OnAddRemoteStream(MediaStreamInterface* stream) OVERRIDE;
113 virtual void OnRemoveRemoteStream(MediaStreamInterface* stream) OVERRIDE;
114 virtual void OnAddDataChannel(DataChannelInterface* data_channel) OVERRIDE;
115 virtual void OnAddRemoteAudioTrack(MediaStreamInterface* stream,
116 AudioTrackInterface* audio_track,
117 uint32 ssrc) OVERRIDE;
118 virtual void OnAddRemoteVideoTrack(MediaStreamInterface* stream,
119 VideoTrackInterface* video_track,
120 uint32 ssrc) OVERRIDE;
121 virtual void OnRemoveRemoteAudioTrack(
122 MediaStreamInterface* stream,
123 AudioTrackInterface* audio_track) OVERRIDE;
124 virtual void OnRemoveRemoteVideoTrack(
125 MediaStreamInterface* stream,
126 VideoTrackInterface* video_track) OVERRIDE;
127 virtual void OnAddLocalAudioTrack(MediaStreamInterface* stream,
128 AudioTrackInterface* audio_track,
129 uint32 ssrc) OVERRIDE;
130 virtual void OnAddLocalVideoTrack(MediaStreamInterface* stream,
131 VideoTrackInterface* video_track,
132 uint32 ssrc) OVERRIDE;
133 virtual void OnRemoveLocalAudioTrack(
134 MediaStreamInterface* stream,
135 AudioTrackInterface* audio_track) OVERRIDE;
136 virtual void OnRemoveLocalVideoTrack(
137 MediaStreamInterface* stream,
138 VideoTrackInterface* video_track) OVERRIDE;
139 virtual void OnRemoveLocalStream(MediaStreamInterface* stream);
140
141 // Implements IceObserver
142 virtual void OnIceConnectionChange(IceConnectionState new_state);
143 virtual void OnIceGatheringChange(IceGatheringState new_state);
144 virtual void OnIceCandidate(const IceCandidateInterface* candidate);
145 virtual void OnIceComplete();
146
147 // Signals from WebRtcSession.
148 void OnSessionStateChange(cricket::BaseSession* session,
149 cricket::BaseSession::State state);
150 void ChangeSignalingState(SignalingState signaling_state);
151
152 bool DoInitialize(const StunConfigurations& stun_config,
153 const TurnConfigurations& turn_config,
154 const MediaConstraintsInterface* constraints,
155 webrtc::PortAllocatorFactoryInterface* allocator_factory,
156 PeerConnectionObserver* observer);
157
158 talk_base::Thread* signaling_thread() const {
159 return factory_->signaling_thread();
160 }
161
162 void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
163 const std::string& error);
164
165 bool IsClosed() const {
166 return signaling_state_ == PeerConnectionInterface::kClosed;
167 }
168
169 // Storing the factory as a scoped reference pointer ensures that the memory
170 // in the PeerConnectionFactoryImpl remains available as long as the
171 // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
172 // However, since the reference counting is done in the
173 // PeerConnectionFactoryInteface all instances created using the raw pointer
174 // will refer to the same reference count.
175 talk_base::scoped_refptr<PeerConnectionFactory> factory_;
176 PeerConnectionObserver* observer_;
177 SignalingState signaling_state_;
178 // TODO(bemasc): Remove ice_state_.
179 IceState ice_state_;
180 IceConnectionState ice_connection_state_;
181 IceGatheringState ice_gathering_state_;
182
183 talk_base::scoped_ptr<cricket::PortAllocator> port_allocator_;
184 talk_base::scoped_ptr<WebRtcSession> session_;
185 talk_base::scoped_ptr<MediaStreamSignaling> mediastream_signaling_;
186 talk_base::scoped_ptr<MediaStreamHandlerContainer> stream_handler_container_;
187 StatsCollector stats_;
188};
189
190} // namespace webrtc
191
192#endif // TALK_APP_WEBRTC_PEERCONNECTION_H_