| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/extensions/cast_streaming_native_handler.h" | 5 #include "chrome/renderer/extensions/cast_streaming_native_handler.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 namespace extensions { | 32 namespace extensions { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 const char kRtpStreamNotFound[] = "The RTP stream cannot be found"; | 35 const char kRtpStreamNotFound[] = "The RTP stream cannot be found"; |
| 36 const char kUdpTransportNotFound[] = "The UDP transport cannot be found"; | 36 const char kUdpTransportNotFound[] = "The UDP transport cannot be found"; |
| 37 const char kInvalidDestination[] = "Invalid destination"; | 37 const char kInvalidDestination[] = "Invalid destination"; |
| 38 const char kInvalidRtpParams[] = "Invalid value for RTP params"; | 38 const char kInvalidRtpParams[] = "Invalid value for RTP params"; |
| 39 const char kInvalidAesKey[] = "Invalid value for AES key"; | 39 const char kInvalidAesKey[] = "Invalid value for AES key"; |
| 40 const char kInvalidAesIvMask[] = "Invalid value for AES IV mask"; | 40 const char kInvalidAesIvMask[] = "Invalid value for AES IV mask"; |
| 41 const char kInvalidStreamArgs[] = "Invalid stream arguments"; |
| 41 const char kUnableToConvertArgs[] = "Unable to convert arguments"; | 42 const char kUnableToConvertArgs[] = "Unable to convert arguments"; |
| 42 const char kUnableToConvertParams[] = "Unable to convert params"; | 43 const char kUnableToConvertParams[] = "Unable to convert params"; |
| 43 | 44 |
| 44 // These helper methods are used to convert between Extension API | 45 // These helper methods are used to convert between Extension API |
| 45 // types and Cast types. | 46 // types and Cast types. |
| 46 void ToCastCodecSpecificParams(const CodecSpecificParams& ext_params, | 47 void ToCastCodecSpecificParams(const CodecSpecificParams& ext_params, |
| 47 CastCodecSpecificParams* cast_params) { | 48 CastCodecSpecificParams* cast_params) { |
| 48 cast_params->key = ext_params.key; | 49 cast_params->key = ext_params.key; |
| 49 cast_params->value = ext_params.value; | 50 cast_params->value = ext_params.value; |
| 50 } | 51 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 base::Bind(&CastStreamingNativeHandler::GetStats, | 190 base::Bind(&CastStreamingNativeHandler::GetStats, |
| 190 base::Unretained(this))); | 191 base::Unretained(this))); |
| 191 } | 192 } |
| 192 | 193 |
| 193 CastStreamingNativeHandler::~CastStreamingNativeHandler() { | 194 CastStreamingNativeHandler::~CastStreamingNativeHandler() { |
| 194 } | 195 } |
| 195 | 196 |
| 196 void CastStreamingNativeHandler::CreateCastSession( | 197 void CastStreamingNativeHandler::CreateCastSession( |
| 197 const v8::FunctionCallbackInfo<v8::Value>& args) { | 198 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 198 CHECK_EQ(3, args.Length()); | 199 CHECK_EQ(3, args.Length()); |
| 199 CHECK(args[0]->IsObject()); | |
| 200 CHECK(args[1]->IsObject()); | |
| 201 CHECK(args[2]->IsFunction()); | 200 CHECK(args[2]->IsFunction()); |
| 202 | 201 |
| 203 blink::WebDOMMediaStreamTrack track1 = | 202 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 204 blink::WebDOMMediaStreamTrack::fromV8Value(args[0]); | 203 if ((args[0]->IsNull() || args[0]->IsUndefined()) && |
| 205 if (track1.isNull()) | 204 (args[1]->IsNull() || args[1]->IsUndefined())) { |
| 205 isolate->ThrowException(v8::Exception::Error( |
| 206 v8::String::NewFromUtf8(isolate, kInvalidStreamArgs))); |
| 206 return; | 207 return; |
| 207 blink::WebDOMMediaStreamTrack track2 = | 208 } |
| 208 blink::WebDOMMediaStreamTrack::fromV8Value(args[1]); | |
| 209 if (track2.isNull()) | |
| 210 return; | |
| 211 | 209 |
| 212 scoped_refptr<CastSession> session(new CastSession()); | 210 scoped_refptr<CastSession> session(new CastSession()); |
| 213 scoped_ptr<CastRtpStream> stream1( | 211 scoped_ptr<CastRtpStream> stream1, stream2; |
| 214 new CastRtpStream(track1.component(), session)); | 212 if (!args[0]->IsNull() && !args[0]->IsUndefined()) { |
| 215 scoped_ptr<CastRtpStream> stream2( | 213 CHECK(args[0]->IsObject()); |
| 216 new CastRtpStream(track2.component(), session)); | 214 blink::WebDOMMediaStreamTrack track = |
| 215 blink::WebDOMMediaStreamTrack::fromV8Value(args[0]); |
| 216 if (track.isNull()) { |
| 217 isolate->ThrowException(v8::Exception::Error( |
| 218 v8::String::NewFromUtf8(isolate, kInvalidStreamArgs))); |
| 219 return; |
| 220 } |
| 221 stream1.reset(new CastRtpStream(track.component(), session)); |
| 222 } |
| 223 if (!args[1]->IsNull() && !args[1]->IsUndefined()) { |
| 224 CHECK(args[1]->IsObject()); |
| 225 blink::WebDOMMediaStreamTrack track = |
| 226 blink::WebDOMMediaStreamTrack::fromV8Value(args[1]); |
| 227 if (track.isNull()) { |
| 228 isolate->ThrowException(v8::Exception::Error( |
| 229 v8::String::NewFromUtf8(isolate, kInvalidStreamArgs))); |
| 230 return; |
| 231 } |
| 232 stream2.reset(new CastRtpStream(track.component(), session)); |
| 233 } |
| 217 scoped_ptr<CastUdpTransport> udp_transport( | 234 scoped_ptr<CastUdpTransport> udp_transport( |
| 218 new CastUdpTransport(session)); | 235 new CastUdpTransport(session)); |
| 219 | 236 |
| 220 // TODO(imcheng): Use a weak reference to ensure we don't call into an | 237 // TODO(imcheng): Use a weak reference to ensure we don't call into an |
| 221 // invalid context when the callback is invoked. | 238 // invalid context when the callback is invoked. |
| 222 create_callback_.reset(args[2].As<v8::Function>()); | 239 create_callback_.reset(args[2].As<v8::Function>()); |
| 223 | 240 |
| 224 base::MessageLoop::current()->PostTask( | 241 base::MessageLoop::current()->PostTask( |
| 225 FROM_HERE, | 242 FROM_HERE, |
| 226 base::Bind( | 243 base::Bind( |
| 227 &CastStreamingNativeHandler::CallCreateCallback, | 244 &CastStreamingNativeHandler::CallCreateCallback, |
| 228 weak_factory_.GetWeakPtr(), | 245 weak_factory_.GetWeakPtr(), |
| 229 base::Passed(&stream1), | 246 base::Passed(&stream1), |
| 230 base::Passed(&stream2), | 247 base::Passed(&stream2), |
| 231 base::Passed(&udp_transport))); | 248 base::Passed(&udp_transport))); |
| 232 } | 249 } |
| 233 | 250 |
| 234 void CastStreamingNativeHandler::CallCreateCallback( | 251 void CastStreamingNativeHandler::CallCreateCallback( |
| 235 scoped_ptr<CastRtpStream> stream1, | 252 scoped_ptr<CastRtpStream> stream1, |
| 236 scoped_ptr<CastRtpStream> stream2, | 253 scoped_ptr<CastRtpStream> stream2, |
| 237 scoped_ptr<CastUdpTransport> udp_transport) { | 254 scoped_ptr<CastUdpTransport> udp_transport) { |
| 238 v8::Isolate* isolate = context()->isolate(); | 255 v8::Isolate* isolate = context()->isolate(); |
| 239 v8::HandleScope handle_scope(isolate); | 256 v8::HandleScope handle_scope(isolate); |
| 240 v8::Context::Scope context_scope(context()->v8_context()); | 257 v8::Context::Scope context_scope(context()->v8_context()); |
| 241 | 258 |
| 242 const int stream1_id = last_transport_id_++; | 259 v8::Handle<v8::Value> callback_args[3]; |
| 243 rtp_stream_map_[stream1_id] = | 260 callback_args[0] = v8::Null(isolate); |
| 244 linked_ptr<CastRtpStream>(stream1.release()); | 261 callback_args[1] = v8::Null(isolate); |
| 245 const int stream2_id = last_transport_id_++; | 262 |
| 246 rtp_stream_map_[stream2_id] = | 263 if (stream1) { |
| 247 linked_ptr<CastRtpStream>(stream2.release()); | 264 const int stream1_id = last_transport_id_++; |
| 265 callback_args[0] = v8::Integer::New(isolate, stream1_id); |
| 266 rtp_stream_map_[stream1_id] = |
| 267 linked_ptr<CastRtpStream>(stream1.release()); |
| 268 } |
| 269 if (stream2) { |
| 270 const int stream2_id = last_transport_id_++; |
| 271 callback_args[1] = v8::Integer::New(isolate, stream2_id); |
| 272 rtp_stream_map_[stream2_id] = |
| 273 linked_ptr<CastRtpStream>(stream2.release()); |
| 274 } |
| 248 const int udp_id = last_transport_id_++; | 275 const int udp_id = last_transport_id_++; |
| 249 udp_transport_map_[udp_id] = | 276 udp_transport_map_[udp_id] = |
| 250 linked_ptr<CastUdpTransport>(udp_transport.release()); | 277 linked_ptr<CastUdpTransport>(udp_transport.release()); |
| 251 | |
| 252 v8::Handle<v8::Value> callback_args[3]; | |
| 253 callback_args[0] = v8::Integer::New(isolate, stream1_id); | |
| 254 callback_args[1] = v8::Integer::New(isolate, stream2_id); | |
| 255 callback_args[2] = v8::Integer::New(isolate, udp_id); | 278 callback_args[2] = v8::Integer::New(isolate, udp_id); |
| 256 context()->CallFunction(create_callback_.NewHandle(isolate), | 279 context()->CallFunction(create_callback_.NewHandle(isolate), |
| 257 3, callback_args); | 280 3, callback_args); |
| 258 create_callback_.reset(); | 281 create_callback_.reset(); |
| 259 } | 282 } |
| 260 | 283 |
| 261 void CastStreamingNativeHandler::CallStartCallback(int stream_id) { | 284 void CastStreamingNativeHandler::CallStartCallback(int stream_id) { |
| 262 v8::Isolate* isolate = context()->isolate(); | 285 v8::Isolate* isolate = context()->isolate(); |
| 263 v8::HandleScope handle_scope(isolate); | 286 v8::HandleScope handle_scope(isolate); |
| 264 v8::Context::Scope context_scope(context()->v8_context()); | 287 v8::Context::Scope context_scope(context()->v8_context()); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 transport_id); | 576 transport_id); |
| 554 if (iter != udp_transport_map_.end()) | 577 if (iter != udp_transport_map_.end()) |
| 555 return iter->second.get(); | 578 return iter->second.get(); |
| 556 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 579 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 557 isolate->ThrowException(v8::Exception::RangeError( | 580 isolate->ThrowException(v8::Exception::RangeError( |
| 558 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); | 581 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); |
| 559 return NULL; | 582 return NULL; |
| 560 } | 583 } |
| 561 | 584 |
| 562 } // namespace extensions | 585 } // namespace extensions |
| OLD | NEW |