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/webrtc_native_handler.h" | 5 #include "chrome/renderer/extensions/webrtc_native_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/common/extensions/api/webrtc_cast_send_transport.h" | |
| 9 #include "chrome/common/extensions/api/webrtc_cast_udp_transport.h" | |
|
not at google - send to devlin
2013/10/30 20:41:57
you can't use the generated objects on the rendere
Alpha Left Google
2013/10/30 23:58:26
I observed that chrome_common.gypi depends on api.
| |
| 10 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
| 11 #include "chrome/renderer/media/cast_send_transport.h" | |
| 12 #include "chrome/renderer/media/cast_session.h" | |
| 13 #include "chrome/renderer/media/cast_udp_transport.h" | |
| 14 #include "content/public/renderer/v8_value_converter.h" | |
| 15 #include "net/base/host_port_pair.h" | |
| 16 | |
| 17 using content::V8ValueConverter; | |
| 18 | |
| 19 // Extension types. | |
| 20 using extensions::api::webrtc_cast_send_transport::RtpCaps; | |
| 21 using extensions::api::webrtc_cast_send_transport::RtpParams; | |
| 22 using extensions::api::webrtc_cast_send_transport::RtpPayloadParams; | |
| 23 using extensions::api::webrtc_cast_udp_transport::CreateInfo; | |
| 24 using extensions::api::webrtc_cast_udp_transport::UdpParams; | |
| 8 | 25 |
| 9 namespace extensions { | 26 namespace extensions { |
| 10 | 27 |
| 28 namespace { | |
| 29 const char kSendTransportNotFound[] = "The send transport cannot be found"; | |
| 30 const char kUdpTransportNotFound[] = "The UDP transport cannot be found"; | |
| 31 const char kInvalidUdpParams[] = "Invalid UDP params"; | |
| 32 const char kInvalidRtpCaps[] = "Invalid value for RTP caps"; | |
| 33 const char kInvalidRtpParams[] = "Invalid value for RTP params"; | |
| 34 const char kUnableToConvertArgs[] = "Unable to convert arguments"; | |
| 35 const char kUnableToConvertCaps[] = "Unable to convert caps"; | |
| 36 const char kUnableToConvertParams[] = "Unable to convert params"; | |
| 37 | |
| 38 // These helper methods are used to convert between Extension API | |
| 39 // types and Cast types. | |
| 40 bool ToCastRtpCaps(const RtpCaps& ext_caps, CastRtpCaps* cast_caps) { | |
| 41 NOTIMPLEMENTED(); | |
| 42 return true; | |
| 43 } | |
| 44 | |
| 45 bool FromCastRtpCaps(const CastRtpCaps& cast_caps, RtpCaps* ext_caps) { | |
| 46 NOTIMPLEMENTED(); | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 bool ToCastRtpParams(const RtpParams& ext_params, CastRtpParams* cast_params) { | |
| 51 NOTIMPLEMENTED(); | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 bool FromCastRtpParams(const CastRtpParams& cast_params, | |
| 56 RtpParams* ext_params) { | |
| 57 NOTIMPLEMENTED(); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 11 WebRtcNativeHandler::WebRtcNativeHandler(ChromeV8Context* context) | 63 WebRtcNativeHandler::WebRtcNativeHandler(ChromeV8Context* context) |
| 12 : ObjectBackedNativeHandler(context) { | 64 : ObjectBackedNativeHandler(context), |
| 65 last_transport_id_(0) { | |
| 13 RouteFunction("CreateCastSendTransport", | 66 RouteFunction("CreateCastSendTransport", |
| 14 base::Bind(&WebRtcNativeHandler::CreateCastSendTransport, | 67 base::Bind(&WebRtcNativeHandler::CreateCastSendTransport, |
| 15 base::Unretained(this))); | 68 base::Unretained(this))); |
| 16 RouteFunction("DestroyCastSendTransport", | 69 RouteFunction("DestroyCastSendTransport", |
| 17 base::Bind(&WebRtcNativeHandler::DestroyCastSendTransport, | 70 base::Bind(&WebRtcNativeHandler::DestroyCastSendTransport, |
| 18 base::Unretained(this))); | 71 base::Unretained(this))); |
| 19 RouteFunction("CreateParamsCastSendTransport", | 72 RouteFunction("CreateParamsCastSendTransport", |
| 20 base::Bind(&WebRtcNativeHandler::CreateParamsCastSendTransport, | 73 base::Bind(&WebRtcNativeHandler::CreateParamsCastSendTransport, |
| 21 base::Unretained(this))); | 74 base::Unretained(this))); |
| 22 RouteFunction("GetCapsCastSendTransport", | 75 RouteFunction("GetCapsCastSendTransport", |
| 23 base::Bind(&WebRtcNativeHandler::GetCapsCastSendTransport, | 76 base::Bind(&WebRtcNativeHandler::GetCapsCastSendTransport, |
| 24 base::Unretained(this))); | 77 base::Unretained(this))); |
| 25 RouteFunction("StartCastSendTransport", | 78 RouteFunction("StartCastSendTransport", |
| 26 base::Bind(&WebRtcNativeHandler::StartCastSendTransport, | 79 base::Bind(&WebRtcNativeHandler::StartCastSendTransport, |
| 27 base::Unretained(this))); | 80 base::Unretained(this))); |
| 28 RouteFunction("StopCastSendTransport", | 81 RouteFunction("StopCastSendTransport", |
| 29 base::Bind(&WebRtcNativeHandler::StopCastSendTransport, | 82 base::Bind(&WebRtcNativeHandler::StopCastSendTransport, |
| 30 base::Unretained(this))); | 83 base::Unretained(this))); |
| 31 RouteFunction("CreateCastUdpTransport", | 84 RouteFunction("CreateCastUdpTransport", |
| 32 base::Bind(&WebRtcNativeHandler::CreateCastUdpTransport, | 85 base::Bind(&WebRtcNativeHandler::CreateCastUdpTransport, |
| 33 base::Unretained(this))); | 86 base::Unretained(this))); |
| 87 RouteFunction("DestroyCastUdpTransport", | |
| 88 base::Bind(&WebRtcNativeHandler::DestroyCastUdpTransport, | |
| 89 base::Unretained(this))); | |
| 34 RouteFunction("StartCastUdpTransport", | 90 RouteFunction("StartCastUdpTransport", |
| 35 base::Bind(&WebRtcNativeHandler::StartCastUdpTransport, | 91 base::Bind(&WebRtcNativeHandler::StartCastUdpTransport, |
| 36 base::Unretained(this))); | 92 base::Unretained(this))); |
| 37 RouteFunction("StopCastUdpTransport", | 93 RouteFunction("StopCastUdpTransport", |
| 38 base::Bind(&WebRtcNativeHandler::StopCastUdpTransport, | 94 base::Bind(&WebRtcNativeHandler::StopCastUdpTransport, |
| 39 base::Unretained(this))); | 95 base::Unretained(this))); |
| 40 } | 96 } |
| 41 | 97 |
| 42 WebRtcNativeHandler::~WebRtcNativeHandler() { | 98 WebRtcNativeHandler::~WebRtcNativeHandler() { |
| 43 } | 99 } |
| 44 | 100 |
| 45 void WebRtcNativeHandler::CreateCastSendTransport( | 101 void WebRtcNativeHandler::CreateCastSendTransport( |
| 46 const v8::FunctionCallbackInfo<v8::Value>& args) { | 102 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 47 NOTIMPLEMENTED(); | 103 CHECK_EQ(3, args.Length()); |
| 104 CHECK(args[0]->IsInt32()); | |
| 105 CHECK(args[1]->IsObject()); | |
| 106 CHECK(args[2]->IsFunction()); | |
| 107 | |
| 108 const int inner_transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
|
not at google - send to devlin
2013/10/30 20:41:57
use args[0]->Cast<v8::Int32> here, more efficient
not at google - send to devlin
2013/10/30 20:44:15
actually you should probably use args[0]->IsIntege
Alpha Left Google
2013/10/30 23:58:26
There's only IsInt32() but not IsInteger(). I drop
| |
| 109 CastUdpTransport* inner_transport = GetUdpTransport(inner_transport_id); | |
| 110 if (!inner_transport) { | |
| 111 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 112 kUdpTransportNotFound))); | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 // TODO(hclam): Convert second argument from v8::Value to | |
| 117 // WebMediaStreamTrack. | |
| 118 int transport_id = last_transport_id_++; | |
| 119 send_transport_map_[transport_id] = | |
| 120 linked_ptr<CastSendTransport>(new CastSendTransport(inner_transport)); | |
| 121 | |
| 122 v8::Handle<v8::Value> transport_id_v8 = v8::Integer::New(transport_id); | |
| 123 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[2]), 1, | |
| 124 &transport_id_v8); | |
| 48 } | 125 } |
| 49 | 126 |
| 50 void WebRtcNativeHandler::DestroyCastSendTransport( | 127 void WebRtcNativeHandler::DestroyCastSendTransport( |
| 51 const v8::FunctionCallbackInfo<v8::Value>& args) { | 128 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 52 NOTIMPLEMENTED(); | 129 CHECK_EQ(1, args.Length()); |
| 130 CHECK(args[0]->IsInt32()); | |
| 131 | |
| 132 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 133 if (!GetSendTransport(transport_id)) { | |
| 134 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 135 kSendTransportNotFound))); | |
| 136 return; | |
| 137 } | |
| 138 send_transport_map_.erase(transport_id); | |
| 53 } | 139 } |
| 54 | 140 |
| 55 void WebRtcNativeHandler::CreateParamsCastSendTransport( | 141 void WebRtcNativeHandler::CreateParamsCastSendTransport( |
| 56 const v8::FunctionCallbackInfo<v8::Value>& args) { | 142 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 57 NOTIMPLEMENTED(); | 143 CHECK_EQ(3, args.Length()); |
| 144 CHECK(args[0]->IsInt32()); | |
| 145 CHECK(args[1]->IsObject()); | |
| 146 CHECK(args[2]->IsFunction()); | |
| 147 | |
| 148 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 149 CastSendTransport* transport = GetSendTransport(transport_id); | |
| 150 if (!transport) { | |
| 151 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 152 kSendTransportNotFound))); | |
| 153 return; | |
| 154 } | |
| 155 | |
| 156 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 157 scoped_ptr<Value> remote_caps_value( | |
| 158 converter->FromV8Value(args[1], context()->v8_context())); | |
| 159 if (!remote_caps_value) { | |
| 160 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 161 kUnableToConvertArgs))); | |
| 162 return; | |
| 163 } | |
| 164 scoped_ptr<RtpCaps> remote_caps = | |
| 165 RtpCaps::FromValue(*remote_caps_value); | |
| 166 if (!remote_caps) { | |
| 167 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 168 kInvalidRtpCaps))); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 CastRtpCaps cast_remote_caps; | |
| 173 if (!ToCastRtpCaps(*remote_caps, &cast_remote_caps)) { | |
| 174 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 175 kUnableToConvertCaps))); | |
| 176 return; | |
| 177 } | |
| 178 CastRtpParams cast_params = transport->CreateParams(cast_remote_caps); | |
| 179 RtpParams params; | |
| 180 if (!FromCastRtpParams(cast_params, ¶ms)) { | |
| 181 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 182 kUnableToConvertParams))); | |
| 183 return; | |
| 184 } | |
| 185 | |
| 186 scoped_ptr<base::DictionaryValue> params_value = params.ToValue(); | |
| 187 v8::Handle<v8::Value> params_v8 = converter->ToV8Value( | |
| 188 params_value.get(), context()->v8_context()); | |
| 189 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[2]), 1, | |
| 190 ¶ms_v8); | |
| 58 } | 191 } |
| 59 | 192 |
| 60 void WebRtcNativeHandler::GetCapsCastSendTransport( | 193 void WebRtcNativeHandler::GetCapsCastSendTransport( |
| 61 const v8::FunctionCallbackInfo<v8::Value>& args) { | 194 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 62 NOTIMPLEMENTED(); | 195 CHECK_EQ(2, args.Length()); |
| 196 CHECK(args[0]->IsInt32()); | |
| 197 CHECK(args[1]->IsFunction()); | |
| 198 | |
| 199 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 200 CastSendTransport* transport = GetSendTransport(transport_id); | |
| 201 if (!transport) { | |
| 202 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 203 kSendTransportNotFound))); | |
| 204 return; | |
| 205 } | |
| 206 | |
| 207 CastRtpCaps cast_caps = transport->GetCaps(); | |
| 208 RtpCaps caps; | |
| 209 if (!FromCastRtpCaps(cast_caps, &caps)) { | |
| 210 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 211 kUnableToConvertCaps))); | |
| 212 return; | |
| 213 } | |
| 214 | |
| 215 scoped_ptr<base::DictionaryValue> caps_value = caps.ToValue(); | |
| 216 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 217 v8::Handle<v8::Value> caps_v8 = converter->ToV8Value( | |
| 218 caps_value.get(), context()->v8_context()); | |
| 219 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[1]), 1, | |
| 220 &caps_v8); | |
| 63 } | 221 } |
| 64 | 222 |
| 65 void WebRtcNativeHandler::StartCastSendTransport( | 223 void WebRtcNativeHandler::StartCastSendTransport( |
| 66 const v8::FunctionCallbackInfo<v8::Value>& args) { | 224 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 67 NOTIMPLEMENTED(); | 225 CHECK_EQ(2, args.Length()); |
| 226 CHECK(args[0]->IsInt32()); | |
| 227 CHECK(args[1]->IsObject()); | |
| 228 | |
| 229 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 230 CastSendTransport* transport = GetSendTransport(transport_id); | |
| 231 if (!transport) { | |
| 232 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 233 kSendTransportNotFound))); | |
| 234 return; | |
| 235 } | |
| 236 | |
| 237 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 238 scoped_ptr<Value> params_value( | |
| 239 converter->FromV8Value(args[1], context()->v8_context())); | |
| 240 if (!params_value) { | |
| 241 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 242 kUnableToConvertParams))); | |
| 243 return; | |
| 244 } | |
| 245 scoped_ptr<RtpParams> params = RtpParams::FromValue(*params_value); | |
| 246 if (!params) { | |
| 247 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 248 kInvalidRtpParams))); | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 CastRtpCaps cast_params; | |
| 253 if (!ToCastRtpParams(*params, &cast_params)) { | |
| 254 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 255 kUnableToConvertParams))); | |
| 256 return; | |
| 257 } | |
| 258 transport->Start(cast_params); | |
| 68 } | 259 } |
| 69 | 260 |
| 70 void WebRtcNativeHandler::StopCastSendTransport( | 261 void WebRtcNativeHandler::StopCastSendTransport( |
| 71 const v8::FunctionCallbackInfo<v8::Value>& args) { | 262 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 72 NOTIMPLEMENTED(); | 263 CHECK_EQ(1, args.Length()); |
| 264 CHECK(args[0]->IsInt32()); | |
| 265 | |
| 266 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 267 CastSendTransport* transport = GetSendTransport(transport_id); | |
| 268 if (!transport) { | |
| 269 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 270 kSendTransportNotFound))); | |
| 271 return; | |
| 272 } | |
| 273 transport->Stop(); | |
| 73 } | 274 } |
| 74 | 275 |
| 75 void WebRtcNativeHandler::CreateCastUdpTransport( | 276 void WebRtcNativeHandler::CreateCastUdpTransport( |
| 76 const v8::FunctionCallbackInfo<v8::Value>& args) { | 277 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 77 NOTIMPLEMENTED(); | 278 CHECK_EQ(1, args.Length()); |
| 279 CHECK(args[0]->IsFunction()); | |
| 280 | |
| 281 // TODO(hclam): Fill in |create_info| properly. | |
| 282 CreateInfo create_info; | |
| 283 create_info.transport_id = last_transport_id_++; | |
| 284 udp_transport_map_[create_info.transport_id] = | |
| 285 linked_ptr<CastUdpTransport>(new CastUdpTransport()); | |
| 286 | |
| 287 scoped_ptr<base::DictionaryValue> create_info_value = create_info.ToValue(); | |
| 288 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 289 v8::Handle<v8::Value> create_info_v8 = converter->ToV8Value( | |
| 290 create_info_value.get(), context()->v8_context()); | |
| 291 context()->CallFunction(v8::Handle<v8::Function>::Cast( | |
| 292 args[0]), 1, &create_info_v8); | |
| 293 } | |
| 294 | |
| 295 void WebRtcNativeHandler::DestroyCastUdpTransport( | |
| 296 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
| 297 CHECK_EQ(1, args.Length()); | |
| 298 CHECK(args[0]->IsInt32()); | |
| 299 | |
| 300 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 301 if (!GetUdpTransport(transport_id)) { | |
| 302 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 303 kUdpTransportNotFound))); | |
| 304 return; | |
| 305 } | |
| 306 udp_transport_map_.erase(transport_id); | |
| 78 } | 307 } |
| 79 | 308 |
| 80 void WebRtcNativeHandler::StartCastUdpTransport( | 309 void WebRtcNativeHandler::StartCastUdpTransport( |
| 81 const v8::FunctionCallbackInfo<v8::Value>& args) { | 310 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 82 NOTIMPLEMENTED(); | 311 CHECK_EQ(2, args.Length()); |
| 312 CHECK(args[0]->IsInt32()); | |
| 313 CHECK(args[1]->IsObject()); | |
| 314 | |
| 315 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 316 CastUdpTransport* transport = GetUdpTransport(transport_id); | |
| 317 if (!transport) { | |
| 318 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 319 kUdpTransportNotFound))); | |
| 320 return; | |
| 321 } | |
| 322 | |
| 323 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 324 scoped_ptr<Value> udp_params_value( | |
| 325 converter->FromV8Value(args[1], context()->v8_context())); | |
| 326 if (!udp_params_value) { | |
| 327 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 328 kUnableToConvertArgs))); | |
| 329 return; | |
| 330 } | |
| 331 scoped_ptr<UdpParams> udp_params = UdpParams::FromValue(*udp_params_value); | |
| 332 if (!udp_params) { | |
| 333 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 334 kInvalidUdpParams))); | |
| 335 return; | |
| 336 } | |
| 337 transport->Start(net::HostPortPair(udp_params->address, udp_params->port)); | |
| 83 } | 338 } |
| 84 | 339 |
| 85 void WebRtcNativeHandler::StopCastUdpTransport( | 340 void WebRtcNativeHandler::StopCastUdpTransport( |
| 86 const v8::FunctionCallbackInfo<v8::Value>& args) { | 341 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 87 NOTIMPLEMENTED(); | 342 CHECK_EQ(1, args.Length()); |
| 343 CHECK(args[0]->IsInt32()); | |
| 344 | |
| 345 const int transport_id = static_cast<int>(args[0]->ToInt32()->Value()); | |
| 346 CastUdpTransport* transport = GetUdpTransport(transport_id); | |
| 347 if (!transport) { | |
| 348 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | |
| 349 kUdpTransportNotFound))); | |
| 350 return; | |
| 351 } | |
| 352 transport->Stop(); | |
| 353 } | |
| 354 | |
| 355 CastSendTransport* WebRtcNativeHandler::GetSendTransport( | |
|
not at google - send to devlin
2013/10/30 20:41:57
maybe this method should actually throw the v8::ex
Alpha Left Google
2013/10/30 23:58:26
I think this is a difference in style. scherkus@ p
not at google - send to devlin
2013/10/31 02:44:44
I understand your point, but I think it would be b
| |
| 356 int transport_id) const { | |
| 357 SendTransportMap::const_iterator iter = send_transport_map_.find( | |
| 358 transport_id); | |
| 359 if (iter != send_transport_map_.end()) | |
| 360 return iter->second.get(); | |
| 361 return NULL; | |
| 362 } | |
| 363 | |
| 364 CastUdpTransport* WebRtcNativeHandler::GetUdpTransport( | |
|
not at google - send to devlin
2013/10/30 20:41:57
ditto
| |
| 365 int transport_id) const { | |
| 366 UdpTransportMap::const_iterator iter = udp_transport_map_.find( | |
| 367 transport_id); | |
| 368 if (iter != udp_transport_map_.end()) | |
| 369 return iter->second.get(); | |
| 370 return NULL; | |
| 88 } | 371 } |
| 89 | 372 |
| 90 } // namespace extensions | 373 } // namespace extensions |
| OLD | NEW |