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("StartLogging", | |
172 base::Bind(&CastStreamingNativeHandler::StartLogging, | |
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); | |
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::StartLogging( | |
424 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
425 CHECK_EQ(1, args.Length()); | |
426 CHECK(args[0]->IsInt32()); | |
427 const int stream_id = args[0]->ToInt32()->Value(); | |
428 CastRtpStream* stream = GetRtpStreamOrThrow(stream_id); | |
429 if (!stream) | |
430 return; | |
431 | |
432 stream->StartLogging(); | |
433 } | |
434 | |
435 void CastStreamingNativeHandler::GetRawEvents( | |
436 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
437 CHECK_EQ(2, args.Length()); | |
438 CHECK(args[0]->IsInt32()); | |
439 CHECK(args[1]->IsFunction()); | |
440 const int transport_id = args[0]->ToInt32()->Value(); | |
441 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); | |
442 if (!transport) | |
443 return; | |
444 | |
445 linked_ptr<extensions::ScopedPersistent<v8::Function> > callback( | |
446 new extensions::ScopedPersistent<v8::Function>); | |
447 callback->reset(args[1].As<v8::Function>()); | |
448 get_raw_events_callbacks_.insert(std::make_pair(transport_id, callback)); | |
Alpha Left Google
2014/02/24 20:18:56
This is odd. Why not just pass the callback and v8
imcheng
2014/02/24 21:28:26
This is similar to CreateCastSession / CallCreateC
| |
449 | |
450 transport->GetRawEvents( | |
451 base::Bind(&CastStreamingNativeHandler::CallGetRawEventsCallback, | |
452 weak_factory_.GetWeakPtr(), | |
453 transport_id)); | |
454 } | |
455 | |
456 void CastStreamingNativeHandler::GetStats( | |
457 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
458 CHECK_EQ(2, args.Length()); | |
459 CHECK(args[0]->IsInt32()); | |
460 CHECK(args[1]->IsFunction()); | |
461 | |
462 // TODO(imcheng): Implement this. | |
463 } | |
464 | |
465 void CastStreamingNativeHandler::CallGetRawEventsCallback( | |
466 int transport_id, | |
467 scoped_ptr<std::string> raw_events) { | |
468 v8::Isolate* isolate = context()->isolate(); | |
469 v8::HandleScope handle_scope(isolate); | |
470 v8::Context::Scope context_scope(context()->v8_context()); | |
471 | |
472 RtpStreamCallbackMap::iterator it = | |
473 get_raw_events_callbacks_.find(transport_id); | |
474 if (it != get_raw_events_callbacks_.end()) { | |
475 v8::Handle<v8::Value> callback_args[1]; | |
476 callback_args[0] = v8::String::NewFromUtf8(isolate, | |
477 raw_events->data(), | |
478 v8::String::kNormalString, | |
479 raw_events->size()); | |
480 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); | |
481 get_raw_events_callbacks_.erase(it); | |
482 } | |
483 } | |
484 | |
412 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( | 485 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( |
413 int transport_id) const { | 486 int transport_id) const { |
414 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( | 487 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( |
415 transport_id); | 488 transport_id); |
416 if (iter != rtp_stream_map_.end()) | 489 if (iter != rtp_stream_map_.end()) |
417 return iter->second.get(); | 490 return iter->second.get(); |
418 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 491 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
419 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( | 492 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( |
420 isolate, kRtpStreamNotFound))); | 493 isolate, kRtpStreamNotFound))); |
421 return NULL; | 494 return NULL; |
422 } | 495 } |
423 | 496 |
424 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( | 497 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( |
425 int transport_id) const { | 498 int transport_id) const { |
426 UdpTransportMap::const_iterator iter = udp_transport_map_.find( | 499 UdpTransportMap::const_iterator iter = udp_transport_map_.find( |
427 transport_id); | 500 transport_id); |
428 if (iter != udp_transport_map_.end()) | 501 if (iter != udp_transport_map_.end()) |
429 return iter->second.get(); | 502 return iter->second.get(); |
430 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 503 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
431 isolate->ThrowException(v8::Exception::RangeError( | 504 isolate->ThrowException(v8::Exception::RangeError( |
432 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); | 505 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); |
433 return NULL; | 506 return NULL; |
434 } | 507 } |
435 | 508 |
436 } // namespace extensions | 509 } // namespace extensions |
OLD | NEW |