Chromium Code Reviews| 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 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 base::Unretained(this))); | 161 base::Unretained(this))); |
| 162 RouteFunction("StopCastRtpStream", | 162 RouteFunction("StopCastRtpStream", |
| 163 base::Bind(&CastStreamingNativeHandler::StopCastRtpStream, | 163 base::Bind(&CastStreamingNativeHandler::StopCastRtpStream, |
| 164 base::Unretained(this))); | 164 base::Unretained(this))); |
| 165 RouteFunction("DestroyCastUdpTransport", | 165 RouteFunction("DestroyCastUdpTransport", |
| 166 base::Bind(&CastStreamingNativeHandler::DestroyCastUdpTransport, | 166 base::Bind(&CastStreamingNativeHandler::DestroyCastUdpTransport, |
| 167 base::Unretained(this))); | 167 base::Unretained(this))); |
| 168 RouteFunction("SetDestinationCastUdpTransport", | 168 RouteFunction("SetDestinationCastUdpTransport", |
| 169 base::Bind(&CastStreamingNativeHandler::SetDestinationCastUdpTransport, | 169 base::Bind(&CastStreamingNativeHandler::SetDestinationCastUdpTransport, |
| 170 base::Unretained(this))); | 170 base::Unretained(this))); |
| 171 RouteFunction("ToggleLogging", | |
| 172 base::Bind(&CastStreamingNativeHandler::ToggleLogging, | |
| 173 base::Unretained(this))); | |
| 174 RouteFunction("GetRawEvents", | |
| 175 base::Bind(&CastStreamingNativeHandler::GetRawEvents, | |
| 176 base::Unretained(this))); | |
| 177 RouteFunction("GetStats", | |
| 178 base::Bind(&CastStreamingNativeHandler::GetStats, | |
| 179 base::Unretained(this))); | |
| 171 } | 180 } |
| 172 | 181 |
| 173 CastStreamingNativeHandler::~CastStreamingNativeHandler() { | 182 CastStreamingNativeHandler::~CastStreamingNativeHandler() { |
| 174 } | 183 } |
| 175 | 184 |
| 176 void CastStreamingNativeHandler::CreateCastSession( | 185 void CastStreamingNativeHandler::CreateCastSession( |
| 177 const v8::FunctionCallbackInfo<v8::Value>& args) { | 186 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 178 CHECK_EQ(3, args.Length()); | 187 CHECK_EQ(3, args.Length()); |
| 179 CHECK(args[0]->IsObject()); | 188 CHECK(args[0]->IsObject()); |
| 180 CHECK(args[1]->IsObject()); | 189 CHECK(args[1]->IsObject()); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 211 | 220 |
| 212 void CastStreamingNativeHandler::CallCreateCallback( | 221 void CastStreamingNativeHandler::CallCreateCallback( |
| 213 scoped_ptr<CastRtpStream> stream1, | 222 scoped_ptr<CastRtpStream> stream1, |
| 214 scoped_ptr<CastRtpStream> stream2, | 223 scoped_ptr<CastRtpStream> stream2, |
| 215 scoped_ptr<CastUdpTransport> udp_transport) { | 224 scoped_ptr<CastUdpTransport> udp_transport) { |
| 216 v8::Isolate* isolate = context()->isolate(); | 225 v8::Isolate* isolate = context()->isolate(); |
| 217 v8::HandleScope handle_scope(isolate); | 226 v8::HandleScope handle_scope(isolate); |
| 218 v8::Context::Scope context_scope(context()->v8_context()); | 227 v8::Context::Scope context_scope(context()->v8_context()); |
| 219 | 228 |
| 220 const int stream1_id = last_transport_id_++; | 229 const int stream1_id = last_transport_id_++; |
| 230 stream1->SetStreamId(stream1_id); | |
|
Alpha Left Google
2014/02/24 23:25:51
stream id is a extensions API thing. It shouldn't
imcheng
2014/02/25 08:10:18
Per offline discussion, I am removing stream_id fr
| |
| 221 rtp_stream_map_[stream1_id] = | 231 rtp_stream_map_[stream1_id] = |
| 222 linked_ptr<CastRtpStream>(stream1.release()); | 232 linked_ptr<CastRtpStream>(stream1.release()); |
| 223 const int stream2_id = last_transport_id_++; | 233 const int stream2_id = last_transport_id_++; |
| 234 stream2->SetStreamId(stream2_id); | |
| 224 rtp_stream_map_[stream2_id] = | 235 rtp_stream_map_[stream2_id] = |
| 225 linked_ptr<CastRtpStream>(stream2.release()); | 236 linked_ptr<CastRtpStream>(stream2.release()); |
| 226 const int udp_id = last_transport_id_++; | 237 const int udp_id = last_transport_id_++; |
| 227 udp_transport_map_[udp_id] = | 238 udp_transport_map_[udp_id] = |
| 228 linked_ptr<CastUdpTransport>(udp_transport.release()); | 239 linked_ptr<CastUdpTransport>(udp_transport.release()); |
| 229 | 240 |
| 230 v8::Handle<v8::Value> callback_args[3]; | 241 v8::Handle<v8::Value> callback_args[3]; |
| 231 callback_args[0] = v8::Integer::New(isolate, stream1_id); | 242 callback_args[0] = v8::Integer::New(isolate, stream1_id); |
| 232 callback_args[1] = v8::Integer::New(isolate, stream2_id); | 243 callback_args[1] = v8::Integer::New(isolate, stream2_id); |
| 233 callback_args[2] = v8::Integer::New(isolate, udp_id); | 244 callback_args[2] = v8::Integer::New(isolate, udp_id); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 } | 413 } |
| 403 net::IPAddressNumber ip; | 414 net::IPAddressNumber ip; |
| 404 if (!net::ParseIPLiteralToNumber(destination->address, &ip)) { | 415 if (!net::ParseIPLiteralToNumber(destination->address, &ip)) { |
| 405 args.GetIsolate()->ThrowException(v8::Exception::TypeError( | 416 args.GetIsolate()->ThrowException(v8::Exception::TypeError( |
| 406 v8::String::NewFromUtf8(args.GetIsolate(), kInvalidDestination))); | 417 v8::String::NewFromUtf8(args.GetIsolate(), kInvalidDestination))); |
| 407 return; | 418 return; |
| 408 } | 419 } |
| 409 transport->SetDestination(net::IPEndPoint(ip, destination->port)); | 420 transport->SetDestination(net::IPEndPoint(ip, destination->port)); |
| 410 } | 421 } |
| 411 | 422 |
| 423 void CastStreamingNativeHandler::ToggleLogging( | |
| 424 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 425 CHECK_EQ(2, args.Length()); | |
| 426 CHECK(args[0]->IsInt32()); | |
| 427 CHECK(args[1]->IsBoolean()); | |
| 428 | |
| 429 const int stream_id = args[0]->ToInt32()->Value(); | |
| 430 CastRtpStream* stream = GetRtpStreamOrThrow(stream_id); | |
| 431 if (!stream) | |
| 432 return; | |
| 433 | |
| 434 const bool enable = args[1]->ToBoolean()->Value(); | |
| 435 stream->ToggleLogging(enable); | |
| 436 } | |
| 437 | |
| 438 void CastStreamingNativeHandler::GetRawEvents( | |
| 439 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 440 CHECK_EQ(2, args.Length()); | |
| 441 CHECK(args[0]->IsInt32()); | |
| 442 CHECK(args[1]->IsFunction()); | |
| 443 const int transport_id = args[0]->ToInt32()->Value(); | |
| 444 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); | |
| 445 if (!transport) | |
| 446 return; | |
| 447 | |
| 448 linked_ptr<extensions::ScopedPersistent<v8::Function> > callback( | |
| 449 new extensions::ScopedPersistent<v8::Function>); | |
| 450 callback->reset(args[1].As<v8::Function>()); | |
| 451 get_raw_events_callbacks_.insert(std::make_pair(transport_id, callback)); | |
| 452 | |
| 453 transport->GetRawEvents( | |
| 454 base::Bind(&CastStreamingNativeHandler::CallGetRawEventsCallback, | |
| 455 weak_factory_.GetWeakPtr(), | |
| 456 transport_id)); | |
| 457 } | |
| 458 | |
| 459 void CastStreamingNativeHandler::GetStats( | |
| 460 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 461 CHECK_EQ(2, args.Length()); | |
| 462 CHECK(args[0]->IsInt32()); | |
| 463 CHECK(args[1]->IsFunction()); | |
| 464 | |
| 465 // TODO(imcheng): Implement this. | |
| 466 } | |
| 467 | |
| 468 void CastStreamingNativeHandler::CallGetRawEventsCallback( | |
| 469 int transport_id, | |
| 470 scoped_ptr<std::string> raw_events) { | |
| 471 v8::Isolate* isolate = context()->isolate(); | |
| 472 v8::HandleScope handle_scope(isolate); | |
| 473 v8::Context::Scope context_scope(context()->v8_context()); | |
| 474 | |
| 475 RtpStreamCallbackMap::iterator it = | |
| 476 get_raw_events_callbacks_.find(transport_id); | |
| 477 if (it != get_raw_events_callbacks_.end()) { | |
| 478 v8::Handle<v8::Value> callback_args[1]; | |
| 479 callback_args[0] = v8::String::NewFromUtf8(isolate, | |
| 480 raw_events->data(), | |
| 481 v8::String::kNormalString, | |
| 482 raw_events->size()); | |
| 483 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); | |
| 484 get_raw_events_callbacks_.erase(it); | |
| 485 } | |
| 486 } | |
| 487 | |
| 412 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( | 488 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( |
| 413 int transport_id) const { | 489 int transport_id) const { |
| 414 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( | 490 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( |
| 415 transport_id); | 491 transport_id); |
| 416 if (iter != rtp_stream_map_.end()) | 492 if (iter != rtp_stream_map_.end()) |
| 417 return iter->second.get(); | 493 return iter->second.get(); |
| 418 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 494 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 419 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( | 495 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( |
| 420 isolate, kRtpStreamNotFound))); | 496 isolate, kRtpStreamNotFound))); |
| 421 return NULL; | 497 return NULL; |
| 422 } | 498 } |
| 423 | 499 |
| 424 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( | 500 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( |
| 425 int transport_id) const { | 501 int transport_id) const { |
| 426 UdpTransportMap::const_iterator iter = udp_transport_map_.find( | 502 UdpTransportMap::const_iterator iter = udp_transport_map_.find( |
| 427 transport_id); | 503 transport_id); |
| 428 if (iter != udp_transport_map_.end()) | 504 if (iter != udp_transport_map_.end()) |
| 429 return iter->second.get(); | 505 return iter->second.get(); |
| 430 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 506 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 431 isolate->ThrowException(v8::Exception::RangeError( | 507 isolate->ThrowException(v8::Exception::RangeError( |
| 432 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); | 508 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); |
| 433 return NULL; | 509 return NULL; |
| 434 } | 510 } |
| 435 | 511 |
| 436 } // namespace extensions | 512 } // namespace extensions |
| OLD | NEW |