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