OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/media/local_video_capture.h" |
| 6 |
| 7 #include "content/renderer/media/video_capture_impl_manager.h" |
| 8 #include "media/base/video_util.h" |
| 9 #include "media/video/capture/video_capture_proxy.h" |
| 10 |
| 11 using media::CopyYPlane; |
| 12 using media::CopyUPlane; |
| 13 using media::CopyVPlane; |
| 14 |
| 15 LocalVideoCapture::LocalVideoCapture( |
| 16 media::VideoCaptureSessionId video_stream_id, |
| 17 VideoCaptureImplManager* vc_manager, |
| 18 const media::VideoCaptureCapability& capability, |
| 19 const base::Closure& error_cb, |
| 20 const webkit_media::RepaintCB& repaint_cb) |
| 21 : video_stream_id_(video_stream_id), |
| 22 vc_manager_(vc_manager), |
| 23 capability_(capability), |
| 24 error_cb_(error_cb), |
| 25 repaint_cb_(repaint_cb), |
| 26 capture_engine_(NULL), |
| 27 message_loop_proxy_(base::MessageLoopProxy::current()), |
| 28 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 29 handler_proxy_(new media::VideoCaptureHandlerProxy( |
| 30 this, message_loop_proxy_))), |
| 31 state_(video_capture::kStopped) { |
| 32 DVLOG(3) << "LocalVideoCapture::ctor"; |
| 33 DCHECK(vc_manager); |
| 34 } |
| 35 |
| 36 LocalVideoCapture::~LocalVideoCapture() { |
| 37 DVLOG(3) << "LocalVideoCapture::dtor"; |
| 38 } |
| 39 |
| 40 void LocalVideoCapture::Start() { |
| 41 DVLOG(3) << "LocalVideoCapture::Start"; |
| 42 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 43 DCHECK_EQ(state_, video_capture::kStopped); |
| 44 |
| 45 capture_engine_ = vc_manager_->AddDevice(video_stream_id_, this); |
| 46 state_ = video_capture::kStarted; |
| 47 AddRef(); // Will be balanced in OnRemoved(). |
| 48 capture_engine_->StartCapture(handler_proxy_.get(), capability_); |
| 49 } |
| 50 |
| 51 void LocalVideoCapture::Stop() { |
| 52 DVLOG(3) << "LocalVideoCapture::Stop"; |
| 53 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 54 if (capture_engine_) { |
| 55 state_ = video_capture::kStopping; |
| 56 capture_engine_->StopCapture(handler_proxy_.get()); |
| 57 capture_engine_ = NULL; |
| 58 } |
| 59 } |
| 60 |
| 61 void LocalVideoCapture::Play() { |
| 62 DVLOG(3) << "LocalVideoCapture::Play"; |
| 63 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 64 if (capture_engine_ && state_ == video_capture::kPaused) { |
| 65 state_ = video_capture::kStarted; |
| 66 } |
| 67 } |
| 68 |
| 69 void LocalVideoCapture::Pause() { |
| 70 DVLOG(3) << "LocalVideoCapture::Pause"; |
| 71 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 72 if (capture_engine_ && state_ == video_capture::kStarted) { |
| 73 state_ = video_capture::kPaused; |
| 74 } |
| 75 } |
| 76 |
| 77 void LocalVideoCapture::OnStarted(media::VideoCapture* capture) { |
| 78 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 79 NOTIMPLEMENTED(); |
| 80 } |
| 81 |
| 82 void LocalVideoCapture::OnStopped(media::VideoCapture* capture) { |
| 83 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 84 NOTIMPLEMENTED(); |
| 85 } |
| 86 |
| 87 void LocalVideoCapture::OnPaused(media::VideoCapture* capture) { |
| 88 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 89 NOTIMPLEMENTED(); |
| 90 } |
| 91 |
| 92 void LocalVideoCapture::OnError(media::VideoCapture* capture, |
| 93 int error_code) { |
| 94 DVLOG(3) << "LocalVideoCapture::OnError, " << error_code; |
| 95 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 96 error_cb_.Run(); |
| 97 } |
| 98 |
| 99 void LocalVideoCapture::OnRemoved(media::VideoCapture* capture) { |
| 100 DVLOG(3) << "LocalVideoCapture::OnRemoved"; |
| 101 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 102 vc_manager_->RemoveDevice(video_stream_id_, this); |
| 103 Release(); // Balance the AddRef() in StartCapture(). |
| 104 } |
| 105 |
| 106 void LocalVideoCapture::OnDeviceInfoReceived( |
| 107 media::VideoCapture* capture, |
| 108 const media::VideoCaptureParams& device_info) { |
| 109 DVLOG(3) << "LocalVideoCapture::OnDeviceInfoReceived"; |
| 110 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 111 } |
| 112 |
| 113 void LocalVideoCapture::OnBufferReady( |
| 114 media::VideoCapture* capture, |
| 115 scoped_refptr<media::VideoCapture::VideoFrameBuffer> buf) { |
| 116 DVLOG(3) << "LocalVideoCapture::OnBufferReady, state:" << state_; |
| 117 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 118 DCHECK(buf); |
| 119 |
| 120 if (state_ != video_capture::kStarted) { |
| 121 capture->FeedBuffer(buf); |
| 122 return; |
| 123 } |
| 124 |
| 125 gfx::Size natural_size(buf->width, buf->height); |
| 126 scoped_refptr<media::VideoFrame> current_frame = |
| 127 media::VideoFrame::CreateFrame(media::VideoFrame::YV12, |
| 128 natural_size, natural_size, |
| 129 buf->timestamp - base::Time()); |
| 130 uint8* buffer = buf->memory_pointer; |
| 131 |
| 132 // Assume YV12 format. |
| 133 DCHECK_EQ(capability_.color, media::VideoCaptureCapability::kI420); |
| 134 int y_width = buf->width; |
| 135 int y_height = buf->height; |
| 136 int uv_width = buf->width / 2; |
| 137 int uv_height = buf->height / 2; |
| 138 CopyYPlane(buffer, y_width, y_height, current_frame); |
| 139 buffer += y_width * y_height; |
| 140 CopyUPlane(buffer, uv_width, uv_height, current_frame); |
| 141 buffer += uv_width * uv_height; |
| 142 CopyVPlane(buffer, uv_width, uv_height, current_frame); |
| 143 |
| 144 capture->FeedBuffer(buf); |
| 145 repaint_cb_.Run(current_frame); |
| 146 } |
OLD | NEW |