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 <functional> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "chrome/common/extensions/api/webrtc_cast_send_transport.h" | 10 #include "chrome/common/extensions/api/webrtc_cast_send_transport.h" |
| 9 #include "chrome/common/extensions/api/webrtc_cast_udp_transport.h" | 11 #include "chrome/common/extensions/api/webrtc_cast_udp_transport.h" |
| 10 #include "chrome/renderer/extensions/chrome_v8_context.h" | 12 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 11 #include "chrome/renderer/media/cast_send_transport.h" | 13 #include "chrome/renderer/media/cast_send_transport.h" |
| 12 #include "chrome/renderer/media/cast_session.h" | 14 #include "chrome/renderer/media/cast_session.h" |
| 13 #include "chrome/renderer/media/cast_udp_transport.h" | 15 #include "chrome/renderer/media/cast_udp_transport.h" |
| 14 #include "content/public/renderer/v8_value_converter.h" | 16 #include "content/public/renderer/v8_value_converter.h" |
| 15 #include "net/base/host_port_pair.h" | 17 #include "net/base/host_port_pair.h" |
| 16 | 18 |
| 17 using content::V8ValueConverter; | 19 using content::V8ValueConverter; |
| 18 | 20 |
| 19 // Extension types. | 21 // Extension types. |
| 22 using extensions::api::webrtc_cast_send_transport::CodecSpecificParams; | |
| 20 using extensions::api::webrtc_cast_send_transport::RtpCaps; | 23 using extensions::api::webrtc_cast_send_transport::RtpCaps; |
| 21 using extensions::api::webrtc_cast_send_transport::RtpParams; | 24 using extensions::api::webrtc_cast_send_transport::RtpParams; |
| 22 using extensions::api::webrtc_cast_send_transport::RtpPayloadParams; | 25 using extensions::api::webrtc_cast_send_transport::RtpPayloadParams; |
| 23 using extensions::api::webrtc_cast_udp_transport::CreateInfo; | 26 using extensions::api::webrtc_cast_udp_transport::CreateInfo; |
| 24 using extensions::api::webrtc_cast_udp_transport::UdpParams; | 27 using extensions::api::webrtc_cast_udp_transport::UdpParams; |
| 25 | 28 |
| 26 namespace extensions { | 29 namespace extensions { |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 const char kSendTransportNotFound[] = "The send transport cannot be found"; | 32 const char kSendTransportNotFound[] = "The send transport cannot be found"; |
| 30 const char kUdpTransportNotFound[] = "The UDP transport cannot be found"; | 33 const char kUdpTransportNotFound[] = "The UDP transport cannot be found"; |
| 31 const char kInvalidUdpParams[] = "Invalid UDP params"; | 34 const char kInvalidUdpParams[] = "Invalid UDP params"; |
| 32 const char kInvalidRtpCaps[] = "Invalid value for RTP caps"; | 35 const char kInvalidRtpCaps[] = "Invalid value for RTP caps"; |
| 33 const char kInvalidRtpParams[] = "Invalid value for RTP params"; | 36 const char kInvalidRtpParams[] = "Invalid value for RTP params"; |
| 34 const char kUnableToConvertArgs[] = "Unable to convert arguments"; | 37 const char kUnableToConvertArgs[] = "Unable to convert arguments"; |
| 35 const char kUnableToConvertCaps[] = "Unable to convert caps"; | |
| 36 const char kUnableToConvertParams[] = "Unable to convert params"; | 38 const char kUnableToConvertParams[] = "Unable to convert params"; |
| 37 | 39 |
| 38 // These helper methods are used to convert between Extension API | 40 // These helper methods are used to convert between Extension API |
| 39 // types and Cast types. | 41 // types and Cast types. |
| 40 bool ToCastRtpCaps(const RtpCaps& ext_caps, CastRtpCaps* cast_caps) { | 42 void ToCastCodecSpecificParams(const CodecSpecificParams& ext_params, |
| 41 NOTIMPLEMENTED(); | 43 CastCodecSpecificParams* cast_params) { |
| 42 return true; | 44 cast_params->key = ext_params.key; |
| 45 cast_params->value = ext_params.value; | |
| 43 } | 46 } |
| 44 | 47 |
| 45 bool FromCastRtpCaps(const CastRtpCaps& cast_caps, RtpCaps* ext_caps) { | 48 void FromCastCodecSpecificParams(const CastCodecSpecificParams& cast_params, |
| 46 NOTIMPLEMENTED(); | 49 CodecSpecificParams* ext_params) { |
| 47 return true; | 50 ext_params->key = cast_params.key; |
| 51 ext_params->value = cast_params.value; | |
| 48 } | 52 } |
| 49 | 53 |
| 50 bool ToCastRtpParams(const RtpParams& ext_params, CastRtpParams* cast_params) { | 54 void ToCastRtpPayloadParams(const RtpPayloadParams& ext_params, |
| 51 NOTIMPLEMENTED(); | 55 CastRtpPayloadParams* cast_params) { |
| 52 return true; | 56 cast_params->payload_type = ext_params.payload_type; |
| 57 cast_params->codec_name = ext_params.codec_name; | |
| 58 cast_params->ssrc = ext_params.ssrc ? *ext_params.ssrc : 0; | |
| 59 cast_params->clock_rate = ext_params.clock_rate ? *ext_params.clock_rate : 0; | |
| 60 cast_params->min_bitrate = | |
| 61 ext_params.min_bitrate ? *ext_params.min_bitrate : 0; | |
| 62 cast_params->max_bitrate = | |
| 63 ext_params.max_bitrate ? *ext_params.max_bitrate : 0; | |
| 64 cast_params->channels = ext_params.channels ? *ext_params.channels : 0; | |
| 65 cast_params->width = ext_params.width ? *ext_params.width : 0; | |
| 66 cast_params->height = ext_params.height ? *ext_params.height : 0; | |
| 67 for (size_t i = 0; i < ext_params.codec_specific_params.size(); ++i) { | |
| 68 CastCodecSpecificParams cast_codec_params; | |
| 69 ToCastCodecSpecificParams(*ext_params.codec_specific_params[i], | |
| 70 &cast_codec_params); | |
| 71 cast_params->codec_specific_params.push_back(cast_codec_params); | |
|
not at google - send to devlin
2013/11/01 22:45:34
I'm not confident in C++ to know whether this is a
Alpha Left Google
2013/11/06 00:15:56
Those are fairly small data structures so copying
| |
| 72 } | |
| 53 } | 73 } |
| 54 | 74 |
| 55 bool FromCastRtpParams(const CastRtpParams& cast_params, | 75 void FromCastRtpPayloadParams(const CastRtpPayloadParams& cast_params, |
| 76 RtpPayloadParams* ext_params) { | |
| 77 ext_params->payload_type = cast_params.payload_type; | |
| 78 ext_params->codec_name = cast_params.codec_name; | |
| 79 if (cast_params.ssrc) { | |
| 80 ext_params->ssrc.reset(new int); | |
| 81 *ext_params->ssrc = cast_params.ssrc; | |
|
not at google - send to devlin
2013/11/01 22:45:34
why not just ext_params->ssrc.reset(new int(cast_p
Alpha Left Google
2013/11/06 00:15:56
Done.
| |
| 82 } | |
| 83 if (cast_params.clock_rate) { | |
| 84 ext_params->clock_rate.reset(new int); | |
| 85 *ext_params->clock_rate = cast_params.clock_rate; | |
| 86 } | |
| 87 if (cast_params.min_bitrate) { | |
| 88 ext_params->min_bitrate.reset(new int); | |
| 89 *ext_params->min_bitrate = cast_params.min_bitrate; | |
| 90 } | |
| 91 if (cast_params.max_bitrate) { | |
| 92 ext_params->max_bitrate.reset(new int); | |
| 93 *ext_params->max_bitrate = cast_params.max_bitrate; | |
| 94 } | |
| 95 if (cast_params.channels) { | |
| 96 ext_params->channels.reset(new int); | |
| 97 *ext_params->channels = cast_params.channels; | |
| 98 } | |
| 99 if (cast_params.width) { | |
| 100 ext_params->width.reset(new int); | |
| 101 *ext_params->width = cast_params.width; | |
| 102 } | |
| 103 if (cast_params.height) { | |
| 104 ext_params->height.reset(new int); | |
| 105 *ext_params->height = cast_params.height; | |
| 106 } | |
| 107 for (size_t i = 0; i < cast_params.codec_specific_params.size(); ++i) { | |
| 108 linked_ptr<CodecSpecificParams> ext_codec_params( | |
| 109 new CodecSpecificParams()); | |
| 110 FromCastCodecSpecificParams(cast_params.codec_specific_params[i], | |
| 111 ext_codec_params.get()); | |
| 112 ext_params->codec_specific_params.push_back(ext_codec_params); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void ToCastRtpCaps(const RtpCaps& ext_caps, CastRtpCaps* cast_caps) { | |
| 117 std::copy(ext_caps.rtcp_features.begin(), ext_caps.rtcp_features.end(), | |
| 118 cast_caps->rtcp_features.begin()); | |
| 119 std::copy(ext_caps.fec_mechanisms.begin(), ext_caps.fec_mechanisms.end(), | |
| 120 cast_caps->fec_mechanisms.begin()); | |
| 121 for (size_t i = 0; i < ext_caps.payloads.size(); ++i) { | |
| 122 CastRtpPayloadParams cast_payload_params; | |
| 123 ToCastRtpPayloadParams(*ext_caps.payloads[i], &cast_payload_params); | |
| 124 cast_caps->payloads.push_back(cast_payload_params); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void FromCastRtpCaps(const CastRtpCaps& cast_caps, RtpCaps* ext_caps) { | |
| 129 std::copy(cast_caps.rtcp_features.begin(), cast_caps.rtcp_features.end(), | |
| 130 ext_caps->rtcp_features.begin()); | |
| 131 std::copy(cast_caps.fec_mechanisms.begin(), cast_caps.fec_mechanisms.end(), | |
| 132 ext_caps->fec_mechanisms.begin()); | |
| 133 for (size_t i = 0; i < cast_caps.payloads.size(); ++i) { | |
| 134 linked_ptr<RtpPayloadParams> ext_payload_params(new RtpPayloadParams()); | |
| 135 FromCastRtpPayloadParams(cast_caps.payloads[i], ext_payload_params.get()); | |
| 136 ext_caps->payloads.push_back(ext_payload_params); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void ToCastRtpParams(const RtpParams& ext_params, CastRtpParams* cast_params) { | |
| 141 std::copy(ext_params.rtcp_features.begin(), ext_params.rtcp_features.end(), | |
| 142 cast_params->rtcp_features.begin()); | |
| 143 std::copy(ext_params.fec_mechanisms.begin(), ext_params.fec_mechanisms.end(), | |
| 144 cast_params->fec_mechanisms.begin()); | |
| 145 for (size_t i = 0; i < ext_params.payloads.size(); ++i) { | |
| 146 CastRtpPayloadParams cast_payload_params; | |
| 147 ToCastRtpPayloadParams(*ext_params.payloads[i], &cast_payload_params); | |
| 148 cast_params->payloads.push_back(cast_payload_params); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void FromCastRtpParams(const CastRtpParams& cast_params, | |
| 56 RtpParams* ext_params) { | 153 RtpParams* ext_params) { |
| 57 NOTIMPLEMENTED(); | 154 std::copy(cast_params.rtcp_features.begin(), cast_params.rtcp_features.end(), |
| 58 return true; | 155 ext_params->rtcp_features.begin()); |
| 156 std::copy(cast_params.fec_mechanisms.begin(), | |
| 157 cast_params.fec_mechanisms.end(), | |
| 158 ext_params->fec_mechanisms.begin()); | |
| 159 for (size_t i = 0; i < cast_params.payloads.size(); ++i) { | |
| 160 linked_ptr<RtpPayloadParams> ext_payload_params(new RtpPayloadParams()); | |
| 161 FromCastRtpPayloadParams(cast_params.payloads[i], | |
| 162 ext_payload_params.get()); | |
| 163 ext_params->payloads.push_back(ext_payload_params); | |
| 164 } | |
| 59 } | 165 } |
| 60 | 166 |
| 61 } // namespace | 167 } // namespace |
| 62 | 168 |
| 63 WebRtcNativeHandler::WebRtcNativeHandler(ChromeV8Context* context) | 169 WebRtcNativeHandler::WebRtcNativeHandler(ChromeV8Context* context) |
| 64 : ObjectBackedNativeHandler(context), | 170 : ObjectBackedNativeHandler(context), |
| 65 last_transport_id_(0) { | 171 last_transport_id_(0) { |
| 66 RouteFunction("CreateCastSendTransport", | 172 RouteFunction("CreateCastSendTransport", |
| 67 base::Bind(&WebRtcNativeHandler::CreateCastSendTransport, | 173 base::Bind(&WebRtcNativeHandler::CreateCastSendTransport, |
| 68 base::Unretained(this))); | 174 base::Unretained(this))); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 261 } |
| 156 scoped_ptr<RtpCaps> remote_caps = | 262 scoped_ptr<RtpCaps> remote_caps = |
| 157 RtpCaps::FromValue(*remote_caps_value); | 263 RtpCaps::FromValue(*remote_caps_value); |
| 158 if (!remote_caps) { | 264 if (!remote_caps) { |
| 159 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | 265 v8::ThrowException(v8::Exception::TypeError(v8::String::New( |
| 160 kInvalidRtpCaps))); | 266 kInvalidRtpCaps))); |
| 161 return; | 267 return; |
| 162 } | 268 } |
| 163 | 269 |
| 164 CastRtpCaps cast_remote_caps; | 270 CastRtpCaps cast_remote_caps; |
| 165 if (!ToCastRtpCaps(*remote_caps, &cast_remote_caps)) { | 271 ToCastRtpCaps(*remote_caps, &cast_remote_caps); |
| 166 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 167 kUnableToConvertCaps))); | |
| 168 return; | |
| 169 } | |
| 170 CastRtpParams cast_params = transport->CreateParams(cast_remote_caps); | 272 CastRtpParams cast_params = transport->CreateParams(cast_remote_caps); |
| 171 RtpParams params; | 273 RtpParams params; |
| 172 if (!FromCastRtpParams(cast_params, ¶ms)) { | 274 FromCastRtpParams(cast_params, ¶ms); |
| 173 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 174 kUnableToConvertParams))); | |
| 175 return; | |
| 176 } | |
| 177 | 275 |
| 178 scoped_ptr<base::DictionaryValue> params_value = params.ToValue(); | 276 scoped_ptr<base::DictionaryValue> params_value = params.ToValue(); |
| 179 v8::Handle<v8::Value> params_v8 = converter->ToV8Value( | 277 v8::Handle<v8::Value> params_v8 = converter->ToV8Value( |
| 180 params_value.get(), context()->v8_context()); | 278 params_value.get(), context()->v8_context()); |
| 181 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[2]), 1, | 279 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[2]), 1, |
| 182 ¶ms_v8); | 280 ¶ms_v8); |
| 183 } | 281 } |
| 184 | 282 |
| 185 void WebRtcNativeHandler::GetCapsCastSendTransport( | 283 void WebRtcNativeHandler::GetCapsCastSendTransport( |
| 186 const v8::FunctionCallbackInfo<v8::Value>& args) { | 284 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 187 CHECK_EQ(2, args.Length()); | 285 CHECK_EQ(2, args.Length()); |
| 188 CHECK(args[0]->IsInt32()); | 286 CHECK(args[0]->IsInt32()); |
| 189 CHECK(args[1]->IsFunction()); | 287 CHECK(args[1]->IsFunction()); |
| 190 | 288 |
| 191 const int transport_id = args[0]->ToInt32()->Value(); | 289 const int transport_id = args[0]->ToInt32()->Value(); |
| 192 CastSendTransport* transport = GetSendTransportOrThrow(transport_id); | 290 CastSendTransport* transport = GetSendTransportOrThrow(transport_id); |
| 193 if (!transport) | 291 if (!transport) |
| 194 return; | 292 return; |
| 195 | 293 |
| 196 CastRtpCaps cast_caps = transport->GetCaps(); | 294 CastRtpCaps cast_caps = transport->GetCaps(); |
| 197 RtpCaps caps; | 295 RtpCaps caps; |
| 198 if (!FromCastRtpCaps(cast_caps, &caps)) { | 296 FromCastRtpCaps(cast_caps, &caps); |
| 199 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 200 kUnableToConvertCaps))); | |
| 201 return; | |
| 202 } | |
| 203 | 297 |
| 204 scoped_ptr<base::DictionaryValue> caps_value = caps.ToValue(); | 298 scoped_ptr<base::DictionaryValue> caps_value = caps.ToValue(); |
| 205 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 299 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 206 v8::Handle<v8::Value> caps_v8 = converter->ToV8Value( | 300 v8::Handle<v8::Value> caps_v8 = converter->ToV8Value( |
| 207 caps_value.get(), context()->v8_context()); | 301 caps_value.get(), context()->v8_context()); |
| 208 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[1]), 1, | 302 context()->CallFunction(v8::Handle<v8::Function>::Cast(args[1]), 1, |
| 209 &caps_v8); | 303 &caps_v8); |
| 210 } | 304 } |
| 211 | 305 |
| 212 void WebRtcNativeHandler::StartCastSendTransport( | 306 void WebRtcNativeHandler::StartCastSendTransport( |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 229 return; | 323 return; |
| 230 } | 324 } |
| 231 scoped_ptr<RtpParams> params = RtpParams::FromValue(*params_value); | 325 scoped_ptr<RtpParams> params = RtpParams::FromValue(*params_value); |
| 232 if (!params) { | 326 if (!params) { |
| 233 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | 327 v8::ThrowException(v8::Exception::TypeError(v8::String::New( |
| 234 kInvalidRtpParams))); | 328 kInvalidRtpParams))); |
| 235 return; | 329 return; |
| 236 } | 330 } |
| 237 | 331 |
| 238 CastRtpCaps cast_params; | 332 CastRtpCaps cast_params; |
| 239 if (!ToCastRtpParams(*params, &cast_params)) { | 333 ToCastRtpParams(*params, &cast_params); |
| 240 v8::ThrowException(v8::Exception::TypeError(v8::String::New( | |
| 241 kUnableToConvertParams))); | |
| 242 return; | |
| 243 } | |
| 244 transport->Start(cast_params); | 334 transport->Start(cast_params); |
| 245 } | 335 } |
| 246 | 336 |
| 247 void WebRtcNativeHandler::StopCastSendTransport( | 337 void WebRtcNativeHandler::StopCastSendTransport( |
| 248 const v8::FunctionCallbackInfo<v8::Value>& args) { | 338 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 249 CHECK_EQ(1, args.Length()); | 339 CHECK_EQ(1, args.Length()); |
| 250 CHECK(args[0]->IsInt32()); | 340 CHECK(args[0]->IsInt32()); |
| 251 | 341 |
| 252 const int transport_id = args[0]->ToInt32()->Value(); | 342 const int transport_id = args[0]->ToInt32()->Value(); |
| 253 CastSendTransport* transport = GetSendTransportOrThrow(transport_id); | 343 CastSendTransport* transport = GetSendTransportOrThrow(transport_id); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 UdpTransportMap::const_iterator iter = udp_transport_map_.find( | 432 UdpTransportMap::const_iterator iter = udp_transport_map_.find( |
| 343 transport_id); | 433 transport_id); |
| 344 if (iter != udp_transport_map_.end()) | 434 if (iter != udp_transport_map_.end()) |
| 345 return iter->second.get(); | 435 return iter->second.get(); |
| 346 v8::ThrowException(v8::Exception::RangeError(v8::String::New( | 436 v8::ThrowException(v8::Exception::RangeError(v8::String::New( |
| 347 kUdpTransportNotFound))); | 437 kUdpTransportNotFound))); |
| 348 return NULL; | 438 return NULL; |
| 349 } | 439 } |
| 350 | 440 |
| 351 } // namespace extensions | 441 } // namespace extensions |
| OLD | NEW |