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 msi_(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 dependency_factory_->DeletePeerConnectionFactory(); |
| 37 } |
| 38 |
| 39 void PeerConnectionHandler::initialize( |
| 40 const WebKit::WebString& serverConfiguration, |
| 41 const WebKit::WebSecurityOrigin&) { |
| 42 native_peer_connection_.reset(dependency_factory_->CreatePeerConnection( |
| 43 signaling_thread_)); |
| 44 } |
| 45 |
| 46 void PeerConnectionHandler::produceInitialOffer( |
| 47 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
| 48 pendingAddStreams) { |
| 49 // We currently don't support creating an initial offer without a stream. |
| 50 // Native PeerConnection will anyway create the initial offer when the first |
| 51 // (and only) stream is added, so it will be done when processPendingStreams |
| 52 // is called if not here. |
| 53 if (pendingAddStreams.isEmpty()) { |
| 54 DVLOG(1) << "Can't produce initial offer with no stream."; |
| 55 return; |
| 56 } |
| 57 // TODO(grunell): Support several streams. |
| 58 DCHECK_EQ(pendingAddStreams.size(), static_cast<size_t>(1)); |
| 59 AddStream(UTF16ToUTF8(pendingAddStreams[0].label())); |
| 60 } |
| 61 |
| 62 void PeerConnectionHandler::handleInitialOffer(const WebKit::WebString& sdp) { |
| 63 native_peer_connection_->SignalingMessage(UTF16ToUTF8(sdp)); |
| 64 } |
| 65 |
| 66 void PeerConnectionHandler::processSDP(const WebKit::WebString& sdp) { |
| 67 native_peer_connection_->SignalingMessage(UTF16ToUTF8(sdp)); |
| 68 } |
| 69 |
| 70 void PeerConnectionHandler::processPendingStreams( |
| 71 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
| 72 pendingAddStreams, |
| 73 const WebKit::WebVector<WebKit::WebMediaStreamDescriptor>& |
| 74 pendingRemoveStreams) { |
| 75 // TODO(grunell): Support several streams. |
| 76 if (!pendingAddStreams.isEmpty()) { |
| 77 DCHECK_EQ(pendingAddStreams.size(), static_cast<size_t>(1)); |
| 78 AddStream(UTF16ToUTF8(pendingAddStreams[0].label())); |
| 79 } |
| 80 // Currently we ignore remove stream, no support in native PeerConnection. |
| 81 } |
| 82 |
| 83 void PeerConnectionHandler::sendDataStreamMessage(const char* data, |
| 84 size_t length) { |
| 85 // TODO(grunell): Implement. Not supported in native PeerConnection. |
| 86 NOTIMPLEMENTED(); |
| 87 } |
| 88 |
| 89 void PeerConnectionHandler::stop() { |
| 90 if (native_peer_connection_.get()) |
| 91 native_peer_connection_->RegisterObserver(NULL); |
| 92 native_peer_connection_.reset(); |
| 93 // The close function will delete us. |
| 94 msi_->ClosePeerConnection(); |
| 95 } |
| 96 |
| 97 // TODO(grunell): Check usage of |
| 98 // WebPeerConnectionHandlerClient::didCompleteICEProcessing. Do we need to call |
| 99 // it? |
| 100 |
| 101 void PeerConnectionHandler::OnSignalingMessage(const std::string& msg) { |
| 102 if (!message_loop_proxy_->BelongsToCurrentThread()) { |
| 103 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( |
| 104 &PeerConnectionHandler::OnSignalingMessage, |
| 105 base::Unretained(this), |
| 106 msg)); |
| 107 return; |
| 108 } |
| 109 client_->didGenerateSDP(UTF8ToUTF16(msg)); |
| 110 } |
| 111 |
| 112 void PeerConnectionHandler::OnAddStream(const std::string& stream_id, |
| 113 bool video) { |
| 114 if (!video) |
| 115 return; |
| 116 |
| 117 if (!message_loop_proxy_->BelongsToCurrentThread()) { |
| 118 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( |
| 119 &PeerConnectionHandler::OnAddStreamCallback, |
| 120 base::Unretained(this), |
| 121 stream_id)); |
| 122 } else { |
| 123 OnAddStreamCallback(stream_id); |
| 124 } |
| 125 } |
| 126 |
| 127 void PeerConnectionHandler::OnRemoveStream( |
| 128 const std::string& stream_id, |
| 129 bool video) { |
| 130 if (!video) |
| 131 return; |
| 132 |
| 133 if (!message_loop_proxy_->BelongsToCurrentThread()) { |
| 134 message_loop_proxy_->PostTask(FROM_HERE, base::Bind( |
| 135 &PeerConnectionHandler::OnRemoveStreamCallback, |
| 136 base::Unretained(this), |
| 137 remote_label_)); |
| 138 } else { |
| 139 OnRemoveStreamCallback(remote_label_); |
| 140 } |
| 141 } |
| 142 |
| 143 void PeerConnectionHandler::AddStream(const std::string label) { |
| 144 // TODO(grunell): Fix code in this function after a new native PeerConnection |
| 145 // version has been rolled out. |
| 146 if (call_state_ == NOT_STARTED) { |
| 147 // TODO(grunell): Add audio and/or video depending on what's enabled |
| 148 // in the stream. |
| 149 std::string audio_label = label; |
| 150 audio_label.append("-audio"); |
| 151 native_peer_connection_->AddStream(audio_label, false); // Audio |
| 152 native_peer_connection_->AddStream(label, true); // Video |
| 153 call_state_ = INITIATING; |
| 154 } |
| 155 if (call_state_ == INITIATING || call_state_ == RECEIVING) { |
| 156 local_label_ = label; |
| 157 if (msi_->SetVideoCaptureModule(label)) |
| 158 native_peer_connection_->SetVideoCapture(""); |
| 159 if (call_state_ == INITIATING) |
| 160 native_peer_connection_->Connect(); |
| 161 else if (call_state_ == RECEIVING) |
| 162 call_state_ = SENDING_AND_RECEIVING; |
| 163 } else { |
| 164 DLOG(ERROR) << "Multiple streams not supported"; |
| 165 } |
| 166 } |
| 167 |
| 168 void PeerConnectionHandler::OnAddStreamCallback( |
| 169 const std::string& stream_label) { |
| 170 // TODO(grunell): Fix code in this function after a new native PeerConnection |
| 171 // version has been rolled out. |
| 172 if (call_state_ == NOT_STARTED) { |
| 173 remote_label_ = stream_label; |
| 174 call_state_ = RECEIVING; |
| 175 } else if (call_state_ == INITIATING) { |
| 176 remote_label_ = local_label_; |
| 177 remote_label_ += "-remote"; |
| 178 call_state_ = SENDING_AND_RECEIVING; |
| 179 } |
| 180 |
| 181 // TODO(grunell): Support several tracks. |
| 182 // TODO(grunell): One for audio and one for video? |
| 183 WebKit::WebVector<WebKit::WebMediaStreamSource> |
| 184 source_vector(static_cast<size_t>(1)); |
| 185 source_vector[0].initialize(WebKit::WebString::fromUTF8(""), |
| 186 WebKit::WebMediaStreamSource::TypeVideo, |
| 187 WebKit::WebString::fromUTF8(remote_label_)); |
| 188 WebKit::WebMediaStreamDescriptor descriptor; |
| 189 descriptor.initialize(UTF8ToUTF16(remote_label_), source_vector); |
| 190 client_->didAddRemoteStream(descriptor); |
| 191 } |
| 192 |
| 193 void PeerConnectionHandler::OnRemoveStreamCallback( |
| 194 const std::string& stream_label) { |
| 195 // TODO(grunell): Support several tracks. |
| 196 // TODO(grunell): One for audio and one for video? |
| 197 WebKit::WebVector<WebKit::WebMediaStreamSource> source_vector( |
| 198 static_cast<size_t>(1)); |
| 199 source_vector[0].initialize(WebKit::WebString::fromUTF8(""), |
| 200 WebKit::WebMediaStreamSource::TypeVideo, |
| 201 WebKit::WebString::fromUTF8(stream_label)); |
| 202 WebKit::WebMediaStreamDescriptor descriptor; |
| 203 descriptor.initialize(UTF8ToUTF16(stream_label), source_vector); |
| 204 client_->didRemoveRemoteStream(descriptor); |
| 205 } |
OLD | NEW |