Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/video_capture/video_capture_device_client_impl.h" | |
| 6 | |
| 7 #include "base/time/time.h" | |
| 8 #include "components/video_capture/mojo_video_frame.h" | |
| 9 #include "media/base/bind_to_current_loop.h" | |
| 10 #include "third_party/libyuv/include/libyuv.h" | |
| 11 #include "ui/gfx/geometry/rect.h" | |
| 12 #include "ui/gfx/geometry/size.h" | |
| 13 | |
| 14 namespace video_capture { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // This implements a VideoCaptureDevice::Client::Buffer as a lightweight wrapper | |
| 19 // around a MojoVideoFrame. This Buffer is not meant to be used beyond the | |
| 20 // VideoCaptureDevice and its Client implementation. | |
| 21 class BufferImpl : public media::VideoCaptureDevice::Client::Buffer { | |
| 22 public: | |
| 23 BufferImpl(scoped_refptr<MojoVideoFrame> video_frame) | |
| 24 : video_frame_(video_frame) {} | |
| 25 | |
| 26 ~BufferImpl() override {} | |
| 27 | |
| 28 // media::VideoCaptureDevice::Client::Buffer: | |
| 29 int id() const override { // Not needed. | |
| 30 NOTREACHED(); | |
| 31 return 0; | |
| 32 } | |
| 33 gfx::Size dimensions() const override { return video_frame_->coded_size(); } | |
| 34 size_t mapped_size() const override { return video_frame_->mapped_size(); } | |
| 35 void* data(int plane) override { return video_frame_->data(plane); } | |
| 36 ClientBuffer AsClientBuffer(int plane) override { | |
| 37 NOTREACHED(); | |
| 38 return nullptr; | |
| 39 } | |
| 40 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 41 base::FileDescriptor AsPlatformFile() override { | |
| 42 NOTREACHED(); | |
| 43 return base::FileDescriptor(); | |
| 44 } | |
| 45 #endif | |
| 46 | |
| 47 const scoped_refptr<MojoVideoFrame> video_frame() { return video_frame_; } | |
| 48 | |
| 49 private: | |
| 50 const scoped_refptr<MojoVideoFrame> video_frame_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(BufferImpl); | |
| 53 }; | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 // A pool of -possibly heterogenous- MojoVideoFrames, provides MojoVideoFrames | |
| 58 // if available and of the appropriate format, otherwise allocates it. The MVF | |
| 59 // are returned via registration of DestructionObserver. This class is used from | |
| 60 // the mojo component main thread and from a number of device threads. | |
| 61 class VideoCaptureDeviceClientImpl::VideoFramePool | |
| 62 : public base::RefCountedThreadSafe< | |
| 63 VideoCaptureDeviceClientImpl::VideoFramePool> { | |
| 64 public: | |
| 65 VideoFramePool(); | |
| 66 | |
| 67 // Produces a frame matching the specified |dimensions|, either by grabbing it | |
| 68 // from the pool or creating a new frame if no suitable one is found. | |
| 69 scoped_refptr<MojoVideoFrame> CreateFrame(const gfx::Size& dimensions, | |
| 70 base::TimeDelta timestamp); | |
| 71 | |
| 72 // Once this is called frames will no longer be inserted into |frames_|. | |
| 73 void Shutdown(); | |
| 74 | |
| 75 private: | |
| 76 // Can't use a WeakPtr since this class is used by multiple threads. | |
| 77 friend class base::RefCountedThreadSafe< | |
| 78 VideoCaptureDeviceClientImpl::VideoFramePool>; | |
| 79 ~VideoFramePool(); | |
| 80 | |
| 81 // Called when the frame wrapper gets destroyed. |frame| is the actual frame | |
| 82 // that was wrapped and is re inserted in |frames_| by this function. | |
| 83 void FrameReleased(const scoped_refptr<MojoVideoFrame>& frame); | |
| 84 | |
| 85 // For mutual exclusion when accessing from multiple threads. | |
| 86 base::Lock lock_; | |
| 87 bool is_shutdown_; | |
| 88 | |
| 89 std::list<scoped_refptr<MojoVideoFrame>> frames_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(VideoFramePool); | |
| 92 }; | |
| 93 | |
| 94 | |
| 95 VideoCaptureDeviceClientImpl::VideoCaptureDeviceClientImpl(Delegate* delegate) | |
| 96 : delegate_(delegate), | |
| 97 video_frame_pool_(new VideoFramePool()), | |
| 98 weak_factory_(this) { | |
| 99 DCHECK(delegate_); | |
| 100 // |this| is used exclusively by internal video capture device code which may, | |
| 101 // and ususally will, operate on a different thread from the constructor. | |
| 102 thread_checker_.DetachFromThread(); | |
| 103 } | |
| 104 | |
| 105 VideoCaptureDeviceClientImpl::~VideoCaptureDeviceClientImpl() { | |
| 106 // This happens on an OS thread. | |
| 107 video_frame_pool_->Shutdown(); | |
| 108 } | |
| 109 | |
| 110 void VideoCaptureDeviceClientImpl::OnIncomingCapturedData( | |
| 111 const uint8_t* data, | |
| 112 int length, | |
| 113 const media::VideoCaptureFormat& frame_format, | |
| 114 int clockwise_rotation, | |
| 115 const base::TimeTicks& timestamp) { | |
| 116 DVLOG(1) << __FUNCTION__; | |
| 117 // N.B.: All of the logic throughout this method is lifted directly from | |
| 118 // content::VideoCaptureDeviceClient and should generally continue to mirror | |
| 119 // that implementation until this one becomes the source of truth. | |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 121 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage); | |
| 122 | |
| 123 if (!frame_format.IsValid()) | |
| 124 return; | |
| 125 | |
| 126 const int width_parity = frame_format.frame_size.width() & 1; | |
| 127 const int height_parity = frame_format.frame_size.height() & 1; | |
| 128 const int even_width = frame_format.frame_size.width() & ~1; | |
| 129 const int even_height = frame_format.frame_size.height() & ~1; | |
| 130 | |
| 131 int destination_width = even_width; | |
| 132 int destination_height = even_height; | |
| 133 | |
| 134 if (clockwise_rotation == 90 || clockwise_rotation == 270) | |
| 135 std::swap(destination_width, destination_height); | |
| 136 | |
| 137 DCHECK_EQ(clockwise_rotation % 90, 0) | |
| 138 << "Rotation must be a multiple of 90, now: " << clockwise_rotation; | |
| 139 libyuv::RotationMode rotation_mode = libyuv::kRotate0; | |
| 140 if (clockwise_rotation == 90) | |
| 141 rotation_mode = libyuv::kRotate90; | |
| 142 else if (clockwise_rotation == 180) | |
| 143 rotation_mode = libyuv::kRotate180; | |
| 144 else if (clockwise_rotation == 270) | |
| 145 rotation_mode = libyuv::kRotate270; | |
| 146 | |
| 147 libyuv::FourCC colorspace = libyuv::FOURCC_ANY; | |
| 148 bool flip = false; | |
| 149 switch (frame_format.pixel_format) { | |
| 150 case media::PIXEL_FORMAT_UNKNOWN: | |
| 151 break; | |
| 152 case media::PIXEL_FORMAT_I420: | |
| 153 DCHECK(!width_parity && !height_parity); | |
| 154 colorspace = libyuv::FOURCC_I420; | |
| 155 break; | |
| 156 case media::PIXEL_FORMAT_YV12: | |
| 157 DCHECK(!width_parity && !height_parity); | |
| 158 colorspace = libyuv::FOURCC_YV12; | |
| 159 break; | |
| 160 case media::PIXEL_FORMAT_NV12: | |
| 161 DCHECK(!width_parity && !height_parity); | |
| 162 colorspace = libyuv::FOURCC_NV12; | |
| 163 break; | |
| 164 case media::PIXEL_FORMAT_NV21: | |
| 165 DCHECK(!width_parity && !height_parity); | |
| 166 colorspace = libyuv::FOURCC_NV21; | |
| 167 break; | |
| 168 case media::PIXEL_FORMAT_YUY2: | |
| 169 DCHECK(!width_parity && !height_parity); | |
| 170 colorspace = libyuv::FOURCC_YUY2; | |
| 171 break; | |
| 172 case media::PIXEL_FORMAT_UYVY: | |
| 173 DCHECK(!width_parity && !height_parity); | |
| 174 colorspace = libyuv::FOURCC_UYVY; | |
| 175 break; | |
| 176 case media::PIXEL_FORMAT_RGB24: | |
| 177 // Linux RGB24 defines red at lowest byte address, | |
| 178 // see http://linuxtv.org/downloads/v4l-dvb-apis/packed-rgb.html. | |
| 179 // Windows RGB24 defines blue at lowest byte, | |
| 180 // see https://msdn.microsoft.com/en-us/library/windows/desktop/dd407253 | |
| 181 #if defined(OS_LINUX) | |
| 182 colorspace = libyuv::FOURCC_RAW; | |
| 183 #elif defined(OS_WIN) | |
| 184 colorspace = libyuv::FOURCC_24BG; | |
| 185 #else | |
| 186 NOTREACHED() << "RGB24 is only available in Linux and Windows platforms"; | |
| 187 #endif | |
| 188 #if defined(OS_WIN) | |
| 189 // TODO(wjia): Currently, for RGB24 on WIN, capture device always | |
| 190 // passes in positive src_width and src_height. Remove this hardcoded | |
| 191 // value when nagative src_height is supported. The negative src_height | |
| 192 // indicates that vertical flipping is needed. | |
| 193 flip = true; | |
| 194 #endif | |
| 195 break; | |
| 196 case media::PIXEL_FORMAT_RGB32: | |
| 197 // Fallback to PIXEL_FORMAT_ARGB setting |flip| in Windows | |
| 198 // platforms. | |
| 199 #if defined(OS_WIN) | |
| 200 flip = true; | |
| 201 #endif | |
| 202 case media::PIXEL_FORMAT_ARGB: | |
| 203 colorspace = libyuv::FOURCC_ARGB; | |
| 204 break; | |
| 205 case media::PIXEL_FORMAT_MJPEG: | |
| 206 colorspace = libyuv::FOURCC_MJPG; | |
| 207 break; | |
| 208 default: | |
| 209 NOTREACHED() << media::VideoPixelFormatToString( | |
| 210 frame_format.pixel_format); | |
| 211 } | |
| 212 | |
| 213 const gfx::Size dimensions(destination_width, destination_height); | |
| 214 scoped_ptr<Buffer> buffer = ReserveOutputBuffer( | |
| 215 dimensions, media::PIXEL_FORMAT_I420, media::PIXEL_STORAGE_CPU); | |
| 216 if (!buffer) | |
| 217 return; | |
| 218 | |
| 219 int y_stride = dimensions.width(); | |
| 220 int uv_stride = y_stride / 2; | |
| 221 if (libyuv::ConvertToI420( | |
| 222 data, length, | |
| 223 static_cast<uint8_t*>(buffer->data(media::VideoFrame::kYPlane)), | |
| 224 y_stride, | |
| 225 static_cast<uint8_t*>(buffer->data(media::VideoFrame::kUPlane)), | |
| 226 uv_stride, | |
| 227 static_cast<uint8_t*>(buffer->data(media::VideoFrame::kVPlane)), | |
| 228 uv_stride, 0 /* crop_x */, 0 /* crop_y */, | |
| 229 frame_format.frame_size.width(), | |
| 230 (flip ? -1 : 1) * frame_format.frame_size.height(), even_width, | |
| 231 even_height, rotation_mode, colorspace)) { | |
| 232 DLOG(ERROR) << "Failed to convert frame pixels to I420"; | |
| 233 return; | |
| 234 } | |
| 235 | |
| 236 OnIncomingCapturedBuffer(std::move(buffer), | |
| 237 media::VideoCaptureFormat(frame_format.frame_size, | |
| 238 0.0f, | |
| 239 media::PIXEL_FORMAT_I420), | |
| 240 timestamp); | |
| 241 } | |
| 242 | |
| 243 void VideoCaptureDeviceClientImpl::OnIncomingCapturedYuvData( | |
| 244 const uint8_t* y_data, | |
| 245 const uint8_t* u_data, | |
| 246 const uint8_t* v_data, | |
| 247 size_t y_stride, | |
| 248 size_t u_stride, | |
| 249 size_t v_stride, | |
| 250 const media::VideoCaptureFormat& frame_format, | |
| 251 int clockwise_rotation, | |
| 252 const base::TimeTicks& timestamp) { | |
| 253 NOTIMPLEMENTED(); | |
| 254 } | |
| 255 | |
| 256 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> | |
| 257 VideoCaptureDeviceClientImpl::ReserveOutputBuffer( | |
| 258 const gfx::Size& dimensions, | |
| 259 media::VideoPixelFormat pixel_format, | |
| 260 media::VideoPixelStorage /* pixel_storage */) { | |
| 261 DVLOG(1) << __FUNCTION__; | |
| 262 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 263 DCHECK_EQ(media::PIXEL_FORMAT_I420, pixel_format); | |
| 264 DCHECK(!dimensions.IsEmpty()); | |
| 265 | |
| 266 return make_scoped_ptr<Buffer>(new BufferImpl(video_frame_pool_->CreateFrame( | |
| 267 dimensions, base::TimeTicks::Now() - base::TimeTicks()))); | |
| 268 } | |
| 269 | |
| 270 void VideoCaptureDeviceClientImpl::OnIncomingCapturedBuffer( | |
| 271 scoped_ptr<Buffer> buffer, | |
| 272 const media::VideoCaptureFormat& frame_format, | |
| 273 const base::TimeTicks& timestamp) { | |
| 274 DVLOG(1) << __FUNCTION__; | |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 276 DCHECK(buffer); | |
| 277 DCHECK_EQ(frame_format.pixel_format, media::PIXEL_FORMAT_I420); | |
| 278 DCHECK_EQ(frame_format.pixel_storage, media::PIXEL_STORAGE_CPU); | |
| 279 | |
| 280 // |buffer| can get out of scope since we have the important frame. | |
| 281 delegate_->OnFrame(static_cast<BufferImpl*>(buffer.get())->video_frame(), | |
| 282 timestamp); | |
| 283 } | |
| 284 | |
| 285 void VideoCaptureDeviceClientImpl::OnIncomingCapturedVideoFrame( | |
| 286 scoped_ptr<Buffer> buffer, | |
| 287 const scoped_refptr<media::VideoFrame>& frame, | |
| 288 const base::TimeTicks& timestamp) { | |
| 289 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 290 DCHECK(buffer); | |
| 291 | |
| 292 // TODO(mcasas): Contents capture calls here. Wire this path by wrapping | |
| 293 // |frame| in a MojoVideoFrame. | |
| 294 NOTIMPLEMENTED(); | |
| 295 } | |
| 296 | |
| 297 void VideoCaptureDeviceClientImpl::OnError( | |
| 298 const tracked_objects::Location& from_here, | |
| 299 const std::string& reason) { | |
| 300 DVLOG(1) << __FUNCTION__ << " " << reason; | |
| 301 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 302 delegate_->OnError(reason); | |
| 303 } | |
| 304 | |
| 305 double VideoCaptureDeviceClientImpl::GetBufferPoolUtilization() const { | |
| 306 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 307 NOTIMPLEMENTED(); | |
| 308 // TODO(mcasas): In content/, the frame pool did have a maximum. Consider | |
| 309 // doing the same in this VideoFramePool, and return here the amount in use | |
| 310 // there. | |
| 311 return 0.0; | |
| 312 } | |
| 313 | |
| 314 VideoCaptureDeviceClientImpl::VideoFramePool::VideoFramePool() | |
| 315 : is_shutdown_(false) {} | |
| 316 | |
| 317 scoped_refptr<MojoVideoFrame> | |
| 318 VideoCaptureDeviceClientImpl::VideoFramePool::CreateFrame( | |
| 319 const gfx::Size& dimensions, | |
| 320 base::TimeDelta timestamp) { | |
| 321 DVLOG(1) << __FUNCTION__ << " " << dimensions.ToString(); | |
| 322 base::AutoLock auto_lock(lock_); | |
| 323 DCHECK(!is_shutdown_); | |
| 324 | |
| 325 scoped_refptr<MojoVideoFrame> frame; | |
| 326 for(const auto& pool_frame: frames_) { | |
| 327 if (pool_frame->coded_size() == dimensions) { | |
| 328 DCHECK(pool_frame->handle().is_valid()); | |
| 329 frame = pool_frame; | |
| 330 frame->set_timestamp(timestamp); | |
| 331 frame->metadata()->Clear(); | |
| 332 break; | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 // Found no suitable frame available: Create a new MojoVideoFrame. | |
| 337 if (!frame.get()) { | |
| 338 frame = MojoVideoFrame::CreateMojoVideoFrame(dimensions, timestamp); | |
| 339 LOG_IF(ERROR, !frame.get()) << "Failed to create a video frame"; | |
| 340 | |
| 341 frames_.push_back(frame); | |
| 342 } | |
| 343 | |
| 344 // Make a copy of |frame| to register a Destruction Observer. | |
| 345 scoped_refptr<MojoVideoFrame> wrapped_frame = frame; | |
| 346 wrapped_frame->AddDestructionObserver( | |
| 347 base::Bind(&VideoFramePool::FrameReleased, this, frame)); | |
|
emircan
2016/02/16 17:45:48
Since these are refptr objects, they can be in use
mcasas
2016/02/16 23:17:21
When Shutdown() is called, |is_shutdown_| is flipp
| |
| 348 return wrapped_frame; | |
| 349 } | |
| 350 | |
| 351 void VideoCaptureDeviceClientImpl::VideoFramePool::Shutdown() { | |
| 352 base::AutoLock auto_lock(lock_); | |
| 353 is_shutdown_ = true; | |
| 354 frames_.clear(); | |
| 355 } | |
| 356 | |
| 357 VideoCaptureDeviceClientImpl::VideoFramePool::~VideoFramePool() { | |
| 358 DCHECK(is_shutdown_); | |
| 359 } | |
| 360 | |
| 361 void VideoCaptureDeviceClientImpl::VideoFramePool::FrameReleased( | |
| 362 const scoped_refptr<MojoVideoFrame>& frame) { | |
| 363 DVLOG(1) << __FUNCTION__; | |
| 364 if (is_shutdown_) | |
| 365 return; | |
| 366 frames_.push_back(frame); | |
| 367 } | |
| 368 | |
| 369 } // namespace video_capture | |
| OLD | NEW |