OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/pepper/pepper_media_stream_video_track_host.h" | 5 #include "content/renderer/pepper/pepper_media_stream_video_track_host.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "content/renderer/media/media_stream_video_source.h" |
14 #include "content/renderer/media/media_stream_video_track.h" | 15 #include "content/renderer/media/media_stream_video_track.h" |
15 #include "media/base/bind_to_current_loop.h" | 16 #include "media/base/bind_to_current_loop.h" |
16 #include "media/base/video_util.h" | 17 #include "media/base/video_util.h" |
17 #include "media/base/yuv_convert.h" | 18 #include "media/base/yuv_convert.h" |
18 #include "ppapi/c/pp_errors.h" | 19 #include "ppapi/c/pp_errors.h" |
19 #include "ppapi/c/ppb_media_stream_video_track.h" | 20 #include "ppapi/c/ppb_media_stream_video_track.h" |
20 #include "ppapi/c/ppb_video_frame.h" | 21 #include "ppapi/c/ppb_video_frame.h" |
21 #include "ppapi/host/dispatch_host_message.h" | 22 #include "ppapi/host/dispatch_host_message.h" |
22 #include "ppapi/host/host_message_context.h" | 23 #include "ppapi/host/host_message_context.h" |
23 #include "ppapi/proxy/ppapi_messages.h" | 24 #include "ppapi/proxy/ppapi_messages.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 PP_Instance instance, | 228 PP_Instance instance, |
228 PP_Resource resource, | 229 PP_Resource resource, |
229 const blink::WebMediaStreamTrack& track) | 230 const blink::WebMediaStreamTrack& track) |
230 : PepperMediaStreamTrackHostBase(host, instance, resource), | 231 : PepperMediaStreamTrackHostBase(host, instance, resource), |
231 track_(track), | 232 track_(track), |
232 number_of_buffers_(kDefaultNumberOfBuffers), | 233 number_of_buffers_(kDefaultNumberOfBuffers), |
233 source_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), | 234 source_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), |
234 plugin_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), | 235 plugin_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), |
235 frame_data_size_(0), | 236 frame_data_size_(0), |
236 type_(kRead), | 237 type_(kRead), |
237 output_started_(false), | |
238 weak_factory_(this) { | 238 weak_factory_(this) { |
239 DCHECK(!track_.isNull()); | 239 DCHECK(!track_.isNull()); |
240 } | 240 } |
241 | 241 |
242 PepperMediaStreamVideoTrackHost::PepperMediaStreamVideoTrackHost( | 242 PepperMediaStreamVideoTrackHost::PepperMediaStreamVideoTrackHost( |
243 RendererPpapiHost* host, | 243 RendererPpapiHost* host, |
244 PP_Instance instance, | 244 PP_Instance instance, |
245 PP_Resource resource) | 245 PP_Resource resource) |
246 : PepperMediaStreamTrackHostBase(host, instance, resource), | 246 : PepperMediaStreamTrackHostBase(host, instance, resource), |
247 number_of_buffers_(kDefaultNumberOfBuffers), | 247 number_of_buffers_(kDefaultNumberOfBuffers), |
248 source_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), | 248 source_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), |
249 plugin_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), | 249 plugin_frame_format_(PP_VIDEOFRAME_FORMAT_UNKNOWN), |
250 frame_data_size_(0), | 250 frame_data_size_(0), |
251 type_(kWrite), | 251 type_(kWrite), |
252 output_started_(false), | |
253 weak_factory_(this) { | 252 weak_factory_(this) { |
254 InitBlinkTrack(); | 253 InitBlinkTrack(); |
255 DCHECK(!track_.isNull()); | 254 DCHECK(!track_.isNull()); |
256 } | 255 } |
257 | 256 |
258 PepperMediaStreamVideoTrackHost::~PepperMediaStreamVideoTrackHost() { | 257 PepperMediaStreamVideoTrackHost::~PepperMediaStreamVideoTrackHost() { |
259 OnClose(); | 258 OnClose(); |
260 } | 259 } |
261 | 260 |
262 bool PepperMediaStreamVideoTrackHost::IsMediaStreamVideoTrackHost() { | 261 bool PepperMediaStreamVideoTrackHost::IsMediaStreamVideoTrackHost() { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 return PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(context, | 314 return PepperMediaStreamTrackHostBase::OnHostMsgEnqueueBuffer(context, |
316 index); | 315 index); |
317 } else { | 316 } else { |
318 return SendFrameToTrack(index); | 317 return SendFrameToTrack(index); |
319 } | 318 } |
320 } | 319 } |
321 | 320 |
322 int32_t PepperMediaStreamVideoTrackHost::SendFrameToTrack(int32_t index) { | 321 int32_t PepperMediaStreamVideoTrackHost::SendFrameToTrack(int32_t index) { |
323 DCHECK_EQ(type_, kWrite); | 322 DCHECK_EQ(type_, kWrite); |
324 | 323 |
325 if (output_started_) { | 324 if (frame_deliverer_) { |
326 // Sends the frame to blink video track. | 325 // Sends the frame to blink video track. |
327 ppapi::MediaStreamBuffer::Video* pp_frame = | 326 ppapi::MediaStreamBuffer::Video* pp_frame = |
328 &(buffer_manager()->GetBufferPointer(index)->video); | 327 &(buffer_manager()->GetBufferPointer(index)->video); |
329 | 328 |
330 int32_t y_stride = plugin_frame_size_.width(); | 329 int32_t y_stride = plugin_frame_size_.width(); |
331 int32_t uv_stride = (plugin_frame_size_.width() + 1) / 2; | 330 int32_t uv_stride = (plugin_frame_size_.width() + 1) / 2; |
332 uint8_t* y_data = static_cast<uint8_t*>(pp_frame->data); | 331 uint8_t* y_data = static_cast<uint8_t*>(pp_frame->data); |
333 // Default to I420 | 332 // Default to I420 |
334 uint8_t* u_data = y_data + plugin_frame_size_.GetArea(); | 333 uint8_t* u_data = y_data + plugin_frame_size_.GetArea(); |
335 uint8_t* v_data = y_data + (plugin_frame_size_.GetArea() * 5 / 4); | 334 uint8_t* v_data = y_data + (plugin_frame_size_.GetArea() * 5 / 4); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 buffer->timestamp = frame->timestamp().InSecondsF(); | 402 buffer->timestamp = frame->timestamp().InSecondsF(); |
404 buffer->format = ppformat; | 403 buffer->format = ppformat; |
405 buffer->size.width = size.width(); | 404 buffer->size.width = size.width(); |
406 buffer->size.height = size.height(); | 405 buffer->size.height = size.height(); |
407 buffer->data_size = frame_data_size_; | 406 buffer->data_size = frame_data_size_; |
408 ConvertFromMediaVideoFrame(frame, ppformat, size, buffer->data); | 407 ConvertFromMediaVideoFrame(frame, ppformat, size, buffer->data); |
409 | 408 |
410 SendEnqueueBufferMessageToPlugin(index); | 409 SendEnqueueBufferMessageToPlugin(index); |
411 } | 410 } |
412 | 411 |
413 void PepperMediaStreamVideoTrackHost::GetCurrentSupportedFormats( | 412 class PepperMediaStreamVideoTrackHost::VideoSource final |
414 int max_requested_width, int max_requested_height, | 413 : public MediaStreamVideoSource { |
415 double max_requested_frame_rate, | 414 public: |
416 const VideoCaptureDeviceFormatsCB& callback) { | 415 explicit VideoSource(base::WeakPtr<PepperMediaStreamVideoTrackHost> host) |
417 if (type_ != kWrite) { | 416 : host_(std::move(host)) {} |
418 DVLOG(1) << "GetCurrentSupportedFormats is only supported in output mode."; | 417 |
419 callback.Run(media::VideoCaptureFormats()); | 418 ~VideoSource() final { StopSourceImpl(); } |
420 return; | 419 |
| 420 void GetCurrentSupportedFormats( |
| 421 int max_requested_width, int max_requested_height, |
| 422 double max_requested_frame_rate, |
| 423 const VideoCaptureDeviceFormatsCB& callback) final { |
| 424 media::VideoCaptureFormats formats; |
| 425 if (host_) { |
| 426 formats.push_back(media::VideoCaptureFormat( |
| 427 host_->plugin_frame_size_, |
| 428 kDefaultOutputFrameRate, |
| 429 ToPixelFormat(host_->plugin_frame_format_))); |
| 430 } |
| 431 callback.Run(formats); |
421 } | 432 } |
422 | 433 |
423 media::VideoCaptureFormats formats; | 434 void StartSourceImpl( |
424 formats.push_back( | 435 const media::VideoCaptureFormat& format, |
425 media::VideoCaptureFormat(plugin_frame_size_, | 436 const blink::WebMediaConstraints& constraints, |
426 kDefaultOutputFrameRate, | 437 const VideoCaptureDeliverFrameCB& frame_callback) final { |
427 ToPixelFormat(plugin_frame_format_))); | 438 if (host_) { |
428 callback.Run(formats); | 439 host_->frame_deliverer_ = |
429 } | 440 new FrameDeliverer(io_task_runner(), frame_callback); |
| 441 } |
| 442 } |
430 | 443 |
431 void PepperMediaStreamVideoTrackHost::StartSourceImpl( | 444 void StopSourceImpl() final { |
432 const media::VideoCaptureFormat& format, | 445 if (host_) |
433 const blink::WebMediaConstraints& constraints, | 446 host_->frame_deliverer_ = nullptr; |
434 const VideoCaptureDeliverFrameCB& frame_callback) { | 447 } |
435 output_started_ = true; | |
436 frame_deliverer_ = new FrameDeliverer(io_task_runner(), frame_callback); | |
437 } | |
438 | 448 |
439 void PepperMediaStreamVideoTrackHost::StopSourceImpl() { | 449 private: |
440 output_started_ = false; | 450 const base::WeakPtr<PepperMediaStreamVideoTrackHost> host_; |
441 frame_deliverer_ = NULL; | 451 |
442 } | 452 DISALLOW_COPY_AND_ASSIGN(VideoSource); |
| 453 }; |
443 | 454 |
444 void PepperMediaStreamVideoTrackHost::DidConnectPendingHostToResource() { | 455 void PepperMediaStreamVideoTrackHost::DidConnectPendingHostToResource() { |
445 if (!MediaStreamVideoSink::connected_track().isNull()) | 456 if (!MediaStreamVideoSink::connected_track().isNull()) |
446 return; | 457 return; |
447 MediaStreamVideoSink::ConnectToTrack( | 458 MediaStreamVideoSink::ConnectToTrack( |
448 track_, | 459 track_, |
449 media::BindToCurrentLoop( | 460 media::BindToCurrentLoop( |
450 base::Bind( | 461 base::Bind( |
451 &PepperMediaStreamVideoTrackHost::OnVideoFrame, | 462 &PepperMediaStreamVideoTrackHost::OnVideoFrame, |
452 weak_factory_.GetWeakPtr()))); | 463 weak_factory_.GetWeakPtr()))); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 } | 515 } |
505 | 516 |
506 void PepperMediaStreamVideoTrackHost::InitBlinkTrack() { | 517 void PepperMediaStreamVideoTrackHost::InitBlinkTrack() { |
507 std::string source_id; | 518 std::string source_id; |
508 base::Base64Encode(base::RandBytesAsString(64), &source_id); | 519 base::Base64Encode(base::RandBytesAsString(64), &source_id); |
509 blink::WebMediaStreamSource webkit_source; | 520 blink::WebMediaStreamSource webkit_source; |
510 webkit_source.initialize(base::UTF8ToUTF16(source_id), | 521 webkit_source.initialize(base::UTF8ToUTF16(source_id), |
511 blink::WebMediaStreamSource::TypeVideo, | 522 blink::WebMediaStreamSource::TypeVideo, |
512 base::UTF8ToUTF16(kPepperVideoSourceName), | 523 base::UTF8ToUTF16(kPepperVideoSourceName), |
513 false /* remote */); | 524 false /* remote */); |
514 webkit_source.setExtraData(this); | 525 MediaStreamVideoSource* const source = |
| 526 new VideoSource(weak_factory_.GetWeakPtr()); |
| 527 webkit_source.setExtraData(source); // Takes ownership of |source|. |
515 | 528 |
516 const bool enabled = true; | 529 const bool enabled = true; |
517 blink::WebMediaConstraints constraints; | 530 blink::WebMediaConstraints constraints; |
518 constraints.initialize(); | 531 constraints.initialize(); |
519 track_ = MediaStreamVideoTrack::CreateVideoTrack( | 532 track_ = MediaStreamVideoTrack::CreateVideoTrack( |
520 this, constraints, | 533 source, constraints, |
521 base::Bind( | 534 base::Bind( |
522 &PepperMediaStreamVideoTrackHost::OnTrackStarted, | 535 &PepperMediaStreamVideoTrackHost::OnTrackStarted, |
523 base::Unretained(this)), | 536 base::Unretained(this)), |
524 enabled); | 537 enabled); |
| 538 // Note: The call to CreateVideoTrack() returned a track that holds a |
| 539 // ref-counted reference to |webkit_source| (and, implicitly, |source|). |
525 } | 540 } |
526 | 541 |
527 void PepperMediaStreamVideoTrackHost::OnTrackStarted( | 542 void PepperMediaStreamVideoTrackHost::OnTrackStarted( |
528 MediaStreamSource* source, | 543 MediaStreamSource* source, |
529 MediaStreamRequestResult result, | 544 MediaStreamRequestResult result, |
530 const blink::WebString& result_name) { | 545 const blink::WebString& result_name) { |
531 DVLOG(3) << "OnTrackStarted result: " << result; | 546 DVLOG(3) << "OnTrackStarted result: " << result; |
532 } | 547 } |
533 | 548 |
534 } // namespace content | 549 } // namespace content |
OLD | NEW |