| 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 | 9 |
| 9 #include "base/base64.h" | 10 #include "base/base64.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "chrome/common/extensions/api/cast_streaming_rtp_stream.h" | 13 #include "chrome/common/extensions/api/cast_streaming_rtp_stream.h" |
| 13 #include "chrome/common/extensions/api/cast_streaming_udp_transport.h" | 14 #include "chrome/common/extensions/api/cast_streaming_udp_transport.h" |
| 14 #include "chrome/renderer/extensions/chrome_v8_context.h" | 15 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 15 #include "chrome/renderer/media/cast_rtp_stream.h" | 16 #include "chrome/renderer/media/cast_rtp_stream.h" |
| 16 #include "chrome/renderer/media/cast_session.h" | 17 #include "chrome/renderer/media/cast_session.h" |
| 17 #include "chrome/renderer/media/cast_udp_transport.h" | 18 #include "chrome/renderer/media/cast_udp_transport.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 linked_ptr<CodecSpecificParams> ext_codec_params( | 117 linked_ptr<CodecSpecificParams> ext_codec_params( |
| 117 new CodecSpecificParams()); | 118 new CodecSpecificParams()); |
| 118 FromCastCodecSpecificParams(cast_params.codec_specific_params[i], | 119 FromCastCodecSpecificParams(cast_params.codec_specific_params[i], |
| 119 ext_codec_params.get()); | 120 ext_codec_params.get()); |
| 120 ext_params->codec_specific_params.push_back(ext_codec_params); | 121 ext_params->codec_specific_params.push_back(ext_codec_params); |
| 121 } | 122 } |
| 122 } | 123 } |
| 123 | 124 |
| 124 void FromCastRtpParams(const CastRtpParams& cast_params, | 125 void FromCastRtpParams(const CastRtpParams& cast_params, |
| 125 RtpParams* ext_params) { | 126 RtpParams* ext_params) { |
| 126 std::copy(cast_params.rtcp_features.begin(), cast_params.rtcp_features.end(), | 127 std::copy(cast_params.rtcp_features.begin(), |
| 127 ext_params->rtcp_features.begin()); | 128 cast_params.rtcp_features.end(), |
| 129 std::back_inserter(ext_params->rtcp_features)); |
| 128 FromCastRtpPayloadParams(cast_params.payload, &ext_params->payload); | 130 FromCastRtpPayloadParams(cast_params.payload, &ext_params->payload); |
| 129 } | 131 } |
| 130 | 132 |
| 131 bool ToCastRtpParamsOrThrow(v8::Isolate* isolate, | 133 bool ToCastRtpParamsOrThrow(v8::Isolate* isolate, |
| 132 const RtpParams& ext_params, | 134 const RtpParams& ext_params, |
| 133 CastRtpParams* cast_params) { | 135 CastRtpParams* cast_params) { |
| 134 std::copy(ext_params.rtcp_features.begin(), ext_params.rtcp_features.end(), | 136 std::copy(ext_params.rtcp_features.begin(), |
| 135 cast_params->rtcp_features.begin()); | 137 ext_params.rtcp_features.end(), |
| 138 std::back_inserter(cast_params->rtcp_features)); |
| 136 if (!ToCastRtpPayloadParamsOrThrow(isolate, | 139 if (!ToCastRtpPayloadParamsOrThrow(isolate, |
| 137 ext_params.payload, | 140 ext_params.payload, |
| 138 &cast_params->payload)) { | 141 &cast_params->payload)) { |
| 139 return false; | 142 return false; |
| 140 } | 143 } |
| 141 return true; | 144 return true; |
| 142 } | 145 } |
| 143 | 146 |
| 144 } // namespace | 147 } // namespace |
| 145 | 148 |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 base::Bind(&CastStreamingNativeHandler::CallGetRawEventsCallback, | 455 base::Bind(&CastStreamingNativeHandler::CallGetRawEventsCallback, |
| 453 weak_factory_.GetWeakPtr(), | 456 weak_factory_.GetWeakPtr(), |
| 454 transport_id)); | 457 transport_id)); |
| 455 } | 458 } |
| 456 | 459 |
| 457 void CastStreamingNativeHandler::GetStats( | 460 void CastStreamingNativeHandler::GetStats( |
| 458 const v8::FunctionCallbackInfo<v8::Value>& args) { | 461 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 459 CHECK_EQ(2, args.Length()); | 462 CHECK_EQ(2, args.Length()); |
| 460 CHECK(args[0]->IsInt32()); | 463 CHECK(args[0]->IsInt32()); |
| 461 CHECK(args[1]->IsFunction()); | 464 CHECK(args[1]->IsFunction()); |
| 465 const int transport_id = args[0]->ToInt32()->Value(); |
| 466 CastRtpStream* transport = GetRtpStreamOrThrow(transport_id); |
| 467 if (!transport) |
| 468 return; |
| 462 | 469 |
| 463 // TODO(imcheng): Implement this. | 470 linked_ptr<extensions::ScopedPersistent<v8::Function> > callback( |
| 471 new extensions::ScopedPersistent<v8::Function>); |
| 472 callback->reset(args[1].As<v8::Function>()); |
| 473 get_stats_callbacks_.insert(std::make_pair(transport_id, callback)); |
| 474 |
| 475 transport->GetStats( |
| 476 base::Bind(&CastStreamingNativeHandler::CallGetStatsCallback, |
| 477 weak_factory_.GetWeakPtr(), |
| 478 transport_id)); |
| 464 } | 479 } |
| 465 | 480 |
| 466 void CastStreamingNativeHandler::CallGetRawEventsCallback( | 481 void CastStreamingNativeHandler::CallGetRawEventsCallback( |
| 467 int transport_id, | 482 int transport_id, |
| 468 scoped_ptr<std::string> raw_events) { | 483 scoped_ptr<std::string> raw_events) { |
| 469 v8::Isolate* isolate = context()->isolate(); | 484 v8::Isolate* isolate = context()->isolate(); |
| 470 v8::HandleScope handle_scope(isolate); | 485 v8::HandleScope handle_scope(isolate); |
| 471 v8::Context::Scope context_scope(context()->v8_context()); | 486 v8::Context::Scope context_scope(context()->v8_context()); |
| 472 | 487 |
| 473 RtpStreamCallbackMap::iterator it = | 488 RtpStreamCallbackMap::iterator it = |
| 474 get_raw_events_callbacks_.find(transport_id); | 489 get_raw_events_callbacks_.find(transport_id); |
| 475 if (it != get_raw_events_callbacks_.end()) { | 490 if (it == get_raw_events_callbacks_.end()) |
| 476 v8::Handle<v8::Value> callback_args[1]; | 491 return; |
| 477 callback_args[0] = v8::String::NewFromUtf8(isolate, | 492 v8::Handle<v8::Value> callback_args[1]; |
| 478 raw_events->data(), | 493 callback_args[0] = v8::String::NewFromUtf8(isolate, |
| 479 v8::String::kNormalString, | 494 raw_events->data(), |
| 480 raw_events->size()); | 495 v8::String::kNormalString, |
| 481 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); | 496 raw_events->size()); |
| 482 get_raw_events_callbacks_.erase(it); | 497 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); |
| 483 } | 498 get_raw_events_callbacks_.erase(it); |
| 499 } |
| 500 |
| 501 void CastStreamingNativeHandler::CallGetStatsCallback( |
| 502 int transport_id, |
| 503 scoped_ptr<base::DictionaryValue> stats) { |
| 504 v8::Isolate* isolate = context()->isolate(); |
| 505 v8::HandleScope handle_scope(isolate); |
| 506 v8::Context::Scope context_scope(context()->v8_context()); |
| 507 |
| 508 RtpStreamCallbackMap::iterator it = get_stats_callbacks_.find(transport_id); |
| 509 if (it == get_stats_callbacks_.end()) |
| 510 return; |
| 511 |
| 512 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 513 v8::Handle<v8::Value> callback_args[1]; |
| 514 callback_args[0] = converter->ToV8Value(stats.get(), context()->v8_context()); |
| 515 context()->CallFunction(it->second->NewHandle(isolate), 1, callback_args); |
| 516 get_stats_callbacks_.erase(it); |
| 484 } | 517 } |
| 485 | 518 |
| 486 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( | 519 CastRtpStream* CastStreamingNativeHandler::GetRtpStreamOrThrow( |
| 487 int transport_id) const { | 520 int transport_id) const { |
| 488 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( | 521 RtpStreamMap::const_iterator iter = rtp_stream_map_.find( |
| 489 transport_id); | 522 transport_id); |
| 490 if (iter != rtp_stream_map_.end()) | 523 if (iter != rtp_stream_map_.end()) |
| 491 return iter->second.get(); | 524 return iter->second.get(); |
| 492 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 525 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 493 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( | 526 isolate->ThrowException(v8::Exception::RangeError(v8::String::NewFromUtf8( |
| 494 isolate, kRtpStreamNotFound))); | 527 isolate, kRtpStreamNotFound))); |
| 495 return NULL; | 528 return NULL; |
| 496 } | 529 } |
| 497 | 530 |
| 498 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( | 531 CastUdpTransport* CastStreamingNativeHandler::GetUdpTransportOrThrow( |
| 499 int transport_id) const { | 532 int transport_id) const { |
| 500 UdpTransportMap::const_iterator iter = udp_transport_map_.find( | 533 UdpTransportMap::const_iterator iter = udp_transport_map_.find( |
| 501 transport_id); | 534 transport_id); |
| 502 if (iter != udp_transport_map_.end()) | 535 if (iter != udp_transport_map_.end()) |
| 503 return iter->second.get(); | 536 return iter->second.get(); |
| 504 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); | 537 v8::Isolate* isolate = context()->v8_context()->GetIsolate(); |
| 505 isolate->ThrowException(v8::Exception::RangeError( | 538 isolate->ThrowException(v8::Exception::RangeError( |
| 506 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); | 539 v8::String::NewFromUtf8(isolate, kUdpTransportNotFound))); |
| 507 return NULL; | 540 return NULL; |
| 508 } | 541 } |
| 509 | 542 |
| 510 } // namespace extensions | 543 } // namespace extensions |
| OLD | NEW |