OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/media/peer_connection_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "content/renderer/media/media_stream_dependency_factory.h" | |
11 #include "content/renderer/media/media_stream_impl.h" | |
12 #include "third_party/libjingle/source/talk/base/thread.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamDescrip tor.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamSource. h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPeerConnectionHand lerClient.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" | |
17 | |
18 PeerConnectionHandler::PeerConnectionHandler( | |
19 WebKit::WebPeerConnectionHandlerClient* client, | |
20 MediaStreamImpl* msi, | |
21 MediaStreamDependencyFactory* dependency_factory, | |
22 talk_base::Thread* signaling_thread) | |
23 : client_(client), | |
24 media_stream_impl_(msi), | |
25 dependency_factory_(dependency_factory), | |
26 message_loop_proxy_(base::MessageLoopProxy::current()), | |
27 signaling_thread_(signaling_thread), | |
28 call_state_(NOT_STARTED) { | |
29 } | |
30 | |
31 PeerConnectionHandler::~PeerConnectionHandler() { | |
32 if (native_peer_connection_.get()) { | |
33 native_peer_connection_->RegisterObserver(NULL); | |
34 native_peer_connection_->Close(); | |
35 } | |
36 } | |
37 | |
38 bool PeerConnectionHandler::SetVideoRenderer( | |
39 const std::string& stream_label, | |
40 cricket::VideoRenderer* renderer) { | |
41 return native_peer_connection_->SetVideoRenderer(stream_label, renderer); | |
42 } | |
43 | |
44 void PeerConnectionHandler::initialize( | |
45 const WebKit::WebString& server_configuration, | |
46 const WebKit::WebSecurityOrigin& security_origin) { | |
47 native_peer_connection_.reset(dependency_factory_->CreatePeerConnection( | |
48 signaling_thread_)); | |
49 native_peer_connection_->RegisterObserver(this); | |
50 } | |
51 | |
52 void PeerConnectionHandler::produceInitialOffer( | |
53 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | |
54 pending_add_streams) { | |
tommi (sloooow) - chröme
2011/11/09 10:45:48
indent 4 more spaces
Henrik Grunell
2011/11/09 20:36:04
Done.
| |
55 // We currently don't support creating an initial offer without a stream. | |
56 // Native PeerConnection will anyway create the initial offer when the first | |
57 // (and only) stream is added, so it will be done when processPendingStreams | |
58 // is called if not here. | |
59 if (pending_add_streams.isEmpty()) { | |
60 DVLOG(1) << "Can't produce initial offer with no stream."; | |
61 return; | |
62 } | |
63 // TODO(grunell): Support several streams. | |
64 DCHECK_EQ(pending_add_streams.size(), static_cast<size_t>(1)); | |
65 AddStream(UTF16ToUTF8(pending_add_streams[0].label())); | |
66 } | |
67 | |
68 void PeerConnectionHandler::handleInitialOffer(const WebKit::WebString& sdp) { | |
69 native_peer_connection_->SignalingMessage(UTF16ToUTF8(sdp)); | |
70 } | |
71 | |
72 void PeerConnectionHandler::processSDP(const WebKit::WebString& sdp) { | |
73 native_peer_connection_->SignalingMessage(UTF16ToUTF8(sdp)); | |
74 } | |
75 | |
76 void PeerConnectionHandler::processPendingStreams( | |
77 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | |
78 pending_add_streams, | |
79 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& | |
80 pending_remove_streams) { | |
81 // TODO(grunell): Support several streams. | |
82 if (!pending_add_streams.isEmpty()) { | |
83 DCHECK_EQ(pending_add_streams.size(), static_cast<size_t>(1)); | |
84 AddStream(UTF16ToUTF8(pending_add_streams[0].label())); | |
85 } | |
86 // Currently we ignore remove stream, no support in native PeerConnection. | |
87 } | |
88 | |
89 void PeerConnectionHandler::sendDataStreamMessage( | |
90 const char* data, | |
91 size_t length) { | |
92 // TODO(grunell): Implement. Not supported in native PeerConnection. | |
93 NOTIMPLEMENTED(); | |
94 } | |
95 | |
96 void PeerConnectionHandler::stop() { | |
97 if (native_peer_connection_.get()) | |
98 native_peer_connection_->RegisterObserver(NULL); | |
99 native_peer_connection_.reset(); | |
100 // The close function will delete us. | |
101 media_stream_impl_->ClosePeerConnection(); | |
102 } | |
103 | |
104 // TODO(grunell): Check usage of | |
105 // WebPeerConnectionHandlerClient::didCompleteICEProcessing. Do we need to call | |
106 // it? | |
107 | |
108 void PeerConnectionHandler::OnSignalingMessage(const std::string& msg) { | |
109 if (!message_loop_proxy_->BelongsToCurrentThread()) { | |
110 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
111 &PeerConnectionHandler::OnSignalingMessage, | |
112 base::Unretained(this), | |
113 msg)); | |
114 return; | |
115 } | |
116 client_->didGenerateSDP(UTF8ToUTF16(msg)); | |
117 } | |
118 | |
119 void PeerConnectionHandler::OnAddStream(const std::string& stream_id, | |
120 bool video) { | |
121 if (!video) | |
122 return; | |
123 | |
124 if (!message_loop_proxy_->BelongsToCurrentThread()) { | |
125 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
126 &PeerConnectionHandler::OnAddStreamCallback, | |
127 base::Unretained(this), | |
128 stream_id)); | |
129 } else { | |
130 OnAddStreamCallback(stream_id); | |
131 } | |
132 } | |
133 | |
134 void PeerConnectionHandler::OnRemoveStream( | |
135 const std::string& stream_id, | |
136 bool video) { | |
137 if (!video) | |
138 return; | |
139 | |
140 if (!message_loop_proxy_->BelongsToCurrentThread()) { | |
141 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
142 &PeerConnectionHandler::OnRemoveStreamCallback, | |
143 base::Unretained(this), | |
144 remote_label_)); | |
145 } else { | |
146 OnRemoveStreamCallback(remote_label_); | |
147 } | |
148 } | |
149 | |
150 void PeerConnectionHandler::AddStream(const std::string label) { | |
151 // TODO(grunell): Fix code in this function after a new native PeerConnection | |
152 // version has been rolled out. | |
153 if (call_state_ == NOT_STARTED) { | |
154 // TODO(grunell): Add audio and/or video depending on what's enabled | |
155 // in the stream. | |
156 std::string audio_label = label; | |
157 audio_label.append("-audio"); | |
158 native_peer_connection_->AddStream(audio_label, false); // Audio | |
159 native_peer_connection_->AddStream(label, true); // Video | |
160 call_state_ = INITIATING; | |
161 } | |
162 if (call_state_ == INITIATING || call_state_ == RECEIVING) { | |
163 local_label_ = label; | |
164 if (media_stream_impl_->SetVideoCaptureModule(label)) | |
165 native_peer_connection_->SetVideoCapture(""); | |
166 if (call_state_ == INITIATING) | |
167 native_peer_connection_->Connect(); | |
168 else if (call_state_ == RECEIVING) | |
169 call_state_ = SENDING_AND_RECEIVING; | |
170 } else { | |
171 DLOG(ERROR) << "Multiple streams not supported"; | |
172 } | |
173 } | |
174 | |
175 void PeerConnectionHandler::OnAddStreamCallback( | |
176 const std::string& stream_label) { | |
177 // TODO(grunell): Fix code in this function after a new native PeerConnection | |
178 // version has been rolled out. | |
179 if (call_state_ == NOT_STARTED) { | |
180 remote_label_ = stream_label; | |
181 call_state_ = RECEIVING; | |
182 } else if (call_state_ == INITIATING) { | |
183 remote_label_ = local_label_; | |
184 remote_label_ += "-remote"; | |
185 call_state_ = SENDING_AND_RECEIVING; | |
186 } | |
187 | |
188 // TODO(grunell): Support several tracks. | |
189 // TODO(grunell): One for audio and one for video? | |
190 WebKit::WebVector<WebKit::WebMediaStreamSource> | |
191 source_vector(static_cast<size_t>(1)); | |
192 source_vector[0].initialize(WebKit::WebString::fromUTF8(""), | |
193 WebKit::WebMediaStreamSource::TypeVideo, | |
194 WebKit::WebString::fromUTF8(remote_label_)); | |
195 WebKit::WebMediaStreamDescriptor descriptor; | |
196 descriptor.initialize(UTF8ToUTF16(remote_label_), source_vector); | |
197 client_->didAddRemoteStream(descriptor); | |
198 } | |
199 | |
200 void PeerConnectionHandler::OnRemoveStreamCallback( | |
201 const std::string& stream_label) { | |
202 // TODO(grunell): Support several tracks. | |
203 // TODO(grunell): One for audio and one for video? | |
204 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( | |
205 static_cast<size_t>(1)); | |
206 source_vector[0].initialize(WebKit::WebString::fromUTF8(""), | |
207 WebKit::WebMediaStreamSource::TypeVideo, | |
208 WebKit::WebString::fromUTF8(stream_label)); | |
209 WebKit::WebMediaStreamDescriptor descriptor; | |
210 descriptor.initialize(UTF8ToUTF16(stream_label), source_vector); | |
211 client_->didRemoveRemoteStream(descriptor); | |
212 } | |
OLD | NEW |