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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 v8::String::NewFromUtf8(isolate, kInvalidStreamArgs))); | 256 v8::String::NewFromUtf8(isolate, kInvalidStreamArgs))); |
257 return; | 257 return; |
258 } | 258 } |
259 stream2.reset(new CastRtpStream(track.component(), session)); | 259 stream2.reset(new CastRtpStream(track.component(), session)); |
260 } | 260 } |
261 scoped_ptr<CastUdpTransport> udp_transport( | 261 scoped_ptr<CastUdpTransport> udp_transport( |
262 new CastUdpTransport(session)); | 262 new CastUdpTransport(session)); |
263 | 263 |
264 // TODO(imcheng): Use a weak reference to ensure we don't call into an | 264 // TODO(imcheng): Use a weak reference to ensure we don't call into an |
265 // invalid context when the callback is invoked. | 265 // invalid context when the callback is invoked. |
266 create_callback_.reset(args[2].As<v8::Function>()); | 266 create_callback_.Reset(isolate, args[2].As<v8::Function>()); |
267 | 267 |
268 base::MessageLoop::current()->PostTask( | 268 base::MessageLoop::current()->PostTask( |
269 FROM_HERE, | 269 FROM_HERE, |
270 base::Bind( | 270 base::Bind( |
271 &CastStreamingNativeHandler::CallCreateCallback, | 271 &CastStreamingNativeHandler::CallCreateCallback, |
272 weak_factory_.GetWeakPtr(), | 272 weak_factory_.GetWeakPtr(), |
273 base::Passed(&stream1), | 273 base::Passed(&stream1), |
274 base::Passed(&stream2), | 274 base::Passed(&stream2), |
275 base::Passed(&udp_transport))); | 275 base::Passed(&udp_transport))); |
276 } | 276 } |
(...skipping 19 matching lines...) Expand all Loading... |
296 if (stream2) { | 296 if (stream2) { |
297 const int stream2_id = last_transport_id_++; | 297 const int stream2_id = last_transport_id_++; |
298 callback_args[1] = v8::Integer::New(isolate, stream2_id); | 298 callback_args[1] = v8::Integer::New(isolate, stream2_id); |
299 rtp_stream_map_[stream2_id] = | 299 rtp_stream_map_[stream2_id] = |
300 linked_ptr<CastRtpStream>(stream2.release()); | 300 linked_ptr<CastRtpStream>(stream2.release()); |
301 } | 301 } |
302 const int udp_id = last_transport_id_++; | 302 const int udp_id = last_transport_id_++; |
303 udp_transport_map_[udp_id] = | 303 udp_transport_map_[udp_id] = |
304 linked_ptr<CastUdpTransport>(udp_transport.release()); | 304 linked_ptr<CastUdpTransport>(udp_transport.release()); |
305 callback_args[2] = v8::Integer::New(isolate, udp_id); | 305 callback_args[2] = v8::Integer::New(isolate, udp_id); |
306 context()->CallFunction(create_callback_.NewHandle(isolate), | 306 context()->CallFunction( |
307 3, callback_args); | 307 v8::Local<v8::Function>::New(isolate, create_callback_), 3, |
308 create_callback_.reset(); | 308 callback_args); |
| 309 create_callback_.Reset(); |
309 } | 310 } |
310 | 311 |
311 void CastStreamingNativeHandler::CallStartCallback(int stream_id) { | 312 void CastStreamingNativeHandler::CallStartCallback(int stream_id) { |
312 v8::Isolate* isolate = context()->isolate(); | 313 v8::Isolate* isolate = context()->isolate(); |
313 v8::HandleScope handle_scope(isolate); | 314 v8::HandleScope handle_scope(isolate); |
314 v8::Context::Scope context_scope(context()->v8_context()); | 315 v8::Context::Scope context_scope(context()->v8_context()); |
315 v8::Handle<v8::Array> event_args = v8::Array::New(isolate, 1); | 316 v8::Handle<v8::Array> event_args = v8::Array::New(isolate, 1); |
316 event_args->Set(0, v8::Integer::New(isolate, stream_id)); | 317 event_args->Set(0, v8::Integer::New(isolate, stream_id)); |
317 context()->DispatchEvent("cast.streaming.rtpStream.onStarted", event_args); | 318 context()->DispatchEvent("cast.streaming.rtpStream.onStarted", event_args); |
318 } | 319 } |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 void CastStreamingNativeHandler::GetRawEvents( | 513 void CastStreamingNativeHandler::GetRawEvents( |
513 const v8::FunctionCallbackInfo<v8::Value>& args) { | 514 const v8::FunctionCallbackInfo<v8::Value>& args) { |
514 CHECK_EQ(3, args.Length()); | 515 CHECK_EQ(3, args.Length()); |
515 CHECK(args[0]->IsInt32()); | 516 CHECK(args[0]->IsInt32()); |
516 CHECK(args[1]->IsNull() || args[1]->IsString()); | 517 CHECK(args[1]->IsNull() || args[1]->IsString()); |
517 CHECK(args[2]->IsFunction()); | 518 CHECK(args[2]->IsFunction()); |
518 | 519 |
519 const int transport_id = args[0]->ToInt32(args.GetIsolate())->Value(); | 520 const int transport_id = args[0]->ToInt32(args.GetIsolate())->Value(); |
520 // TODO(imcheng): Use a weak reference to ensure we don't call into an | 521 // TODO(imcheng): Use a weak reference to ensure we don't call into an |
521 // invalid context when the callback is invoked. | 522 // invalid context when the callback is invoked. |
522 linked_ptr<ScopedPersistent<v8::Function> > callback( | 523 linked_ptr<v8::UniquePersistent<v8::Function>> callback( |
523 new ScopedPersistent<v8::Function>(args[2].As<v8::Function>())); | 524 new v8::UniquePersistent<v8::Function>(args.GetIsolate(), |
| 525 args[2].As<v8::Function>())); |
524 std::string extra_data; | 526 std::string extra_data; |
525 if (!args[1]->IsNull()) { | 527 if (!args[1]->IsNull()) { |
526 extra_data = *v8::String::Utf8Value(args[1]); | 528 extra_data = *v8::String::Utf8Value(args[1]); |
527 } | 529 } |
528 | 530 |
529 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); | 531 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); |
530 if (!transport) | 532 if (!transport) |
531 return; | 533 return; |
532 | 534 |
533 get_raw_events_callbacks_.insert(std::make_pair(transport_id, callback)); | 535 get_raw_events_callbacks_.insert(std::make_pair(transport_id, callback)); |
(...skipping 10 matching lines...) Expand all Loading... |
544 CHECK_EQ(2, args.Length()); | 546 CHECK_EQ(2, args.Length()); |
545 CHECK(args[0]->IsInt32()); | 547 CHECK(args[0]->IsInt32()); |
546 CHECK(args[1]->IsFunction()); | 548 CHECK(args[1]->IsFunction()); |
547 const int transport_id = args[0]->ToInt32(args.GetIsolate())->Value(); | 549 const int transport_id = args[0]->ToInt32(args.GetIsolate())->Value(); |
548 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); | 550 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); |
549 if (!transport) | 551 if (!transport) |
550 return; | 552 return; |
551 | 553 |
552 // TODO(imcheng): Use a weak reference to ensure we don't call into an | 554 // TODO(imcheng): Use a weak reference to ensure we don't call into an |
553 // invalid context when the callback is invoked. | 555 // invalid context when the callback is invoked. |
554 linked_ptr<ScopedPersistent<v8::Function> > callback( | 556 linked_ptr<v8::UniquePersistent<v8::Function>> callback( |
555 new ScopedPersistent<v8::Function>(args[1].As<v8::Function>())); | 557 new v8::UniquePersistent<v8::Function>(args.GetIsolate(), |
| 558 args[1].As<v8::Function>())); |
556 get_stats_callbacks_.insert(std::make_pair(transport_id, callback)); | 559 get_stats_callbacks_.insert(std::make_pair(transport_id, callback)); |
557 | 560 |
558 transport->GetStats( | 561 transport->GetStats( |
559 base::Bind(&CastStreamingNativeHandler::CallGetStatsCallback, | 562 base::Bind(&CastStreamingNativeHandler::CallGetStatsCallback, |
560 weak_factory_.GetWeakPtr(), | 563 weak_factory_.GetWeakPtr(), |
561 transport_id)); | 564 transport_id)); |
562 } | 565 } |
563 | 566 |
564 void CastStreamingNativeHandler::CallGetRawEventsCallback( | 567 void CastStreamingNativeHandler::CallGetRawEventsCallback( |
565 int transport_id, | 568 int transport_id, |
566 scoped_ptr<base::BinaryValue> raw_events) { | 569 scoped_ptr<base::BinaryValue> raw_events) { |
567 v8::Isolate* isolate = context()->isolate(); | 570 v8::Isolate* isolate = context()->isolate(); |
568 v8::HandleScope handle_scope(isolate); | 571 v8::HandleScope handle_scope(isolate); |
569 v8::Context::Scope context_scope(context()->v8_context()); | 572 v8::Context::Scope context_scope(context()->v8_context()); |
570 | 573 |
571 RtpStreamCallbackMap::iterator it = | 574 RtpStreamCallbackMap::iterator it = |
572 get_raw_events_callbacks_.find(transport_id); | 575 get_raw_events_callbacks_.find(transport_id); |
573 if (it == get_raw_events_callbacks_.end()) | 576 if (it == get_raw_events_callbacks_.end()) |
574 return; | 577 return; |
575 v8::Handle<v8::Value> callback_args[1]; | |
576 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 578 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
577 callback_args[0] = | 579 v8::Handle<v8::Value> callback_args[] = { |
578 converter->ToV8Value(raw_events.get(), context()->v8_context()); | 580 converter->ToV8Value(raw_events.get(), context()->v8_context())}; |
579 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); | 581 context()->CallFunction(v8::Local<v8::Function>::New(isolate, *it->second), |
| 582 arraysize(callback_args), callback_args); |
580 get_raw_events_callbacks_.erase(it); | 583 get_raw_events_callbacks_.erase(it); |
581 } | 584 } |
582 | 585 |
583 void CastStreamingNativeHandler::CallGetStatsCallback( | 586 void CastStreamingNativeHandler::CallGetStatsCallback( |
584 int transport_id, | 587 int transport_id, |
585 scoped_ptr<base::DictionaryValue> stats) { | 588 scoped_ptr<base::DictionaryValue> stats) { |
586 v8::Isolate* isolate = context()->isolate(); | 589 v8::Isolate* isolate = context()->isolate(); |
587 v8::HandleScope handle_scope(isolate); | 590 v8::HandleScope handle_scope(isolate); |
588 v8::Context::Scope context_scope(context()->v8_context()); | 591 v8::Context::Scope context_scope(context()->v8_context()); |
589 | 592 |
590 RtpStreamCallbackMap::iterator it = get_stats_callbacks_.find(transport_id); | 593 RtpStreamCallbackMap::iterator it = get_stats_callbacks_.find(transport_id); |
591 if (it == get_stats_callbacks_.end()) | 594 if (it == get_stats_callbacks_.end()) |
592 return; | 595 return; |
593 | 596 |
594 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 597 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
595 v8::Handle<v8::Value> callback_args[1]; | 598 v8::Handle<v8::Value> callback_args[] = { |
596 callback_args[0] = converter->ToV8Value(stats.get(), context()->v8_context()); | 599 converter->ToV8Value(stats.get(), context()->v8_context())}; |
597 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); | 600 context()->CallFunction(v8::Local<v8::Function>::New(isolate, *it->second), |
| 601 arraysize(callback_args), callback_args); |
598 get_stats_callbacks_.erase(it); | 602 get_stats_callbacks_.erase(it); |
599 } | 603 } |
600 | 604 |
601 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( | 605 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( |
602 int transport_id) const { | 606 int transport_id) const { |
603 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( | 607 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( |
604 transport_id); | 608 transport_id); |
605 if (iter != rtp_stream_map_.end()) | 609 if (iter != rtp_stream_map_.end()) |
606 return iter->second.get(); | 610 return iter->second.get(); |
607 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 611 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 return false; | 656 return false; |
653 } | 657 } |
654 config->channels = 2; | 658 config->channels = 2; |
655 if (params->codec_name == "OPUS") { | 659 if (params->codec_name == "OPUS") { |
656 config->codec = media::cast::CODEC_AUDIO_OPUS; | 660 config->codec = media::cast::CODEC_AUDIO_OPUS; |
657 config->rtp_timebase = 48000; | 661 config->rtp_timebase = 48000; |
658 config->rtp_payload_type = 127; | 662 config->rtp_payload_type = 127; |
659 } else if (params->codec_name == "PCM16") { | 663 } else if (params->codec_name == "PCM16") { |
660 config->codec = media::cast::CODEC_AUDIO_PCM16; | 664 config->codec = media::cast::CODEC_AUDIO_PCM16; |
661 config->rtp_timebase = 48000; | 665 config->rtp_timebase = 48000; |
662 config->rtp_payload_type =127; | 666 config->rtp_payload_type = 127; |
663 } else if (params->codec_name == "AAC") { | 667 } else if (params->codec_name == "AAC") { |
664 config->codec = media::cast::CODEC_AUDIO_AAC; | 668 config->codec = media::cast::CODEC_AUDIO_AAC; |
665 config->rtp_timebase = 48000; | 669 config->rtp_timebase = 48000; |
666 config->rtp_payload_type = 127; | 670 config->rtp_payload_type = 127; |
667 } else if (params->codec_name == "VP8") { | 671 } else if (params->codec_name == "VP8") { |
668 config->codec = media::cast::CODEC_VIDEO_VP8; | 672 config->codec = media::cast::CODEC_VIDEO_VP8; |
669 config->rtp_timebase = 90000; | 673 config->rtp_timebase = 90000; |
670 config->rtp_payload_type = 96; | 674 config->rtp_payload_type = 96; |
671 } else if (params->codec_name == "H264") { | 675 } else if (params->codec_name == "H264") { |
672 config->codec = media::cast::CODEC_VIDEO_H264; | 676 config->codec = media::cast::CODEC_VIDEO_H264; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 void CastStreamingNativeHandler::AddTracksToMediaStream( | 855 void CastStreamingNativeHandler::AddTracksToMediaStream( |
852 const std::string& url, | 856 const std::string& url, |
853 const media::AudioParameters& params, | 857 const media::AudioParameters& params, |
854 scoped_refptr<media::AudioCapturerSource> audio, | 858 scoped_refptr<media::AudioCapturerSource> audio, |
855 scoped_ptr<media::VideoCapturerSource> video) { | 859 scoped_ptr<media::VideoCapturerSource> video) { |
856 content::AddAudioTrackToMediaStream(audio, params, true, true, url); | 860 content::AddAudioTrackToMediaStream(audio, params, true, true, url); |
857 content::AddVideoTrackToMediaStream(video.Pass(), true, true, url); | 861 content::AddVideoTrackToMediaStream(video.Pass(), true, true, url); |
858 } | 862 } |
859 | 863 |
860 } // namespace extensions | 864 } // namespace extensions |
OLD | NEW |