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 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" | 5 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" |
| 6 | 6 |
| 7 #import <CoreVideo/CoreVideo.h> | 7 #import <CoreVideo/CoreVideo.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "media/video/capture/mac/video_capture_device_mac.h" | 11 #include "media/video/capture/mac/video_capture_device_mac.h" |
| 12 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 13 | 13 |
| 14 // Prefer MJPEG if frame width or height is larger than this. | 14 // Prefer MJPEG if frame width or height is larger than this. |
| 15 static const int kMjpegWidthThreshold = 640; | 15 static const int kMjpegWidthThreshold = 640; |
| 16 static const int kMjpegHeightThreshold = 480; | 16 static const int kMjpegHeightThreshold = 480; |
| 17 | 17 |
| 18 // This function translates Mac Core Video pixel formats to Chromium pixel | 18 // This function translates Mac Core Video pixel formats to Chromium pixel |
| 19 // formats. Chromium pixel formats are sorted in order of preference. | 19 // formats. Chromium pixel formats are sorted in order of preference. |
| 20 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { | 20 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { |
|
mcasas
2015/01/28 20:17:01
This shouldn't be FourCharCode, that's too generic
magjed_chromium
2015/01/29 13:42:17
Acknowledged.
| |
| 21 switch (code) { | 21 switch (code) { |
| 22 case kCVPixelFormatType_422YpCbCr8: | 22 case kCVPixelFormatType_422YpCbCr8: |
| 23 return media::PIXEL_FORMAT_UYVY; | 23 return media::PIXEL_FORMAT_UYVY; |
| 24 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: | 24 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: |
| 25 return media::PIXEL_FORMAT_YUY2; | 25 return media::PIXEL_FORMAT_YUY2; |
| 26 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: | 26 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: |
| 27 return media::PIXEL_FORMAT_MJPEG; | 27 return media::PIXEL_FORMAT_MJPEG; |
| 28 case CoreMediaGlue::kCMPixelFormat_420YpCbCr8BiPlanarVideoRange: | |
| 29 case CoreMediaGlue::kCMPixelFormat_420YpCbCr8BiPlanarFullRange: | |
|
mcasas
2015/01/28 20:17:01
Correct naming as per earlier comments.
magjed_chromium
2015/01/29 13:42:17
Done.
| |
| 30 return media::PIXEL_FORMAT_NV12; | |
| 28 default: | 31 default: |
| 29 return media::PIXEL_FORMAT_UNKNOWN; | 32 return media::PIXEL_FORMAT_UNKNOWN; |
| 30 } | 33 } |
| 31 } | 34 } |
| 32 | 35 |
| 33 @implementation VideoCaptureDeviceAVFoundation | 36 @implementation VideoCaptureDeviceAVFoundation |
| 34 | 37 |
| 35 #pragma mark Class methods | 38 #pragma mark Class methods |
| 36 | 39 |
| 37 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { | 40 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 // VideoCaptureDeviceMac::ReceiveFrame() is calling here, depending on the | 187 // VideoCaptureDeviceMac::ReceiveFrame() is calling here, depending on the |
| 185 // running state. VCDM::ReceiveFrame() calls here to change aspect ratio. | 188 // running state. VCDM::ReceiveFrame() calls here to change aspect ratio. |
| 186 DCHECK((![captureSession_ isRunning] && | 189 DCHECK((![captureSession_ isRunning] && |
| 187 main_thread_checker_.CalledOnValidThread()) || | 190 main_thread_checker_.CalledOnValidThread()) || |
| 188 callback_thread_checker_.CalledOnValidThread()); | 191 callback_thread_checker_.CalledOnValidThread()); |
| 189 | 192 |
| 190 frameWidth_ = width; | 193 frameWidth_ = width; |
| 191 frameHeight_ = height; | 194 frameHeight_ = height; |
| 192 frameRate_ = frameRate; | 195 frameRate_ = frameRate; |
| 193 | 196 |
| 194 FourCharCode best_fourcc = kCVPixelFormatType_422YpCbCr8; | 197 FourCharCode best_fourcc = kCVPixelFormatType_422YpCbCr8; |
|
mcasas
2015/01/28 20:17:01
See comments above for FourCharCode.
magjed_chromium
2015/01/29 13:42:17
Acknowledged.
| |
| 195 const bool prefer_mjpeg = | 198 const bool prefer_mjpeg = |
| 196 width > kMjpegWidthThreshold || height > kMjpegHeightThreshold; | 199 width > kMjpegWidthThreshold || height > kMjpegHeightThreshold; |
| 197 for (CrAVCaptureDeviceFormat* format in captureDevice_.formats) { | 200 for (CrAVCaptureDeviceFormat* format in captureDevice_.formats) { |
| 198 const FourCharCode fourcc = | 201 const FourCharCode fourcc = |
| 199 CoreMediaGlue::CMFormatDescriptionGetMediaSubType( | 202 CoreMediaGlue::CMFormatDescriptionGetMediaSubType( |
| 200 [format formatDescription]); | 203 [format formatDescription]); |
| 201 if (prefer_mjpeg && | 204 if (prefer_mjpeg && |
| 202 fourcc == CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML) { | 205 fourcc == CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML) { |
| 203 best_fourcc = fourcc; | 206 best_fourcc = fourcc; |
| 204 break; | 207 break; |
| 205 } | 208 } |
| 206 // Compare according to Chromium preference. | 209 // Compare according to Chromium preference. |
| 207 if (FourCCToChromiumPixelFormat(fourcc) < | 210 if (FourCCToChromiumPixelFormat(fourcc) < |
| 208 FourCCToChromiumPixelFormat(best_fourcc)) { | 211 FourCCToChromiumPixelFormat(best_fourcc)) { |
|
mcasas
2015/01/28 20:17:01
Hmm here the semantics are somehow cumbersome beca
magjed_chromium
2015/01/29 13:42:17
I will certainly consider a lambda :), but in a di
| |
| 209 best_fourcc = fourcc; | 212 best_fourcc = fourcc; |
| 210 } | 213 } |
| 211 } | 214 } |
| 212 | 215 |
| 213 // The capture output has to be configured, despite Mac documentation | 216 // The capture output has to be configured, despite Mac documentation |
| 214 // detailing that setting the sessionPreset would be enough. The reason for | 217 // detailing that setting the sessionPreset would be enough. The reason for |
| 215 // this mismatch is probably because most of the AVFoundation docs are written | 218 // this mismatch is probably because most of the AVFoundation docs are written |
| 216 // for iOS and not for MacOsX. AVVideoScalingModeKey() refers to letterboxing | 219 // for iOS and not for MacOsX. AVVideoScalingModeKey() refers to letterboxing |
| 217 // yes/no and preserve aspect ratio yes/no when scaling. Currently we set | 220 // yes/no and preserve aspect ratio yes/no when scaling. Currently we set |
| 218 // cropping and preservation. | 221 // cropping and preservation. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); | 308 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); |
| 306 // Expect the MJPEG data to be available as a contiguous reference, i.e. | 309 // Expect the MJPEG data to be available as a contiguous reference, i.e. |
| 307 // not covered by multiple memory blocks. | 310 // not covered by multiple memory blocks. |
| 308 CHECK_EQ(lengthAtOffset, frameSize); | 311 CHECK_EQ(lengthAtOffset, frameSize); |
| 309 } | 312 } |
| 310 } else { | 313 } else { |
| 311 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); | 314 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); |
| 312 // Lock the frame and calculate frame size. | 315 // Lock the frame and calculate frame size. |
| 313 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == | 316 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == |
| 314 kCVReturnSuccess) { | 317 kCVReturnSuccess) { |
| 315 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame)); | 318 baseAddress = static_cast<char*>( |
| 316 frameSize = CVPixelBufferGetHeight(videoFrame) * | 319 CVPixelBufferGetPlaneCount(videoFrame) == 0 |
| 317 CVPixelBufferGetBytesPerRow(videoFrame); | 320 ? CVPixelBufferGetBaseAddress(videoFrame) |
| 321 : CVPixelBufferGetBaseAddressOfPlane(videoFrame, 0)); | |
| 322 // Chromium expects the data to be tightly packed, i.e. no row padding | |
| 323 // and the planes should follow consecutively. | |
| 324 switch (captureFormat.pixel_format) { | |
| 325 case media::PIXEL_FORMAT_YUY2: | |
| 326 case media::PIXEL_FORMAT_UYVY: | |
| 327 CHECK_EQ(CVPixelBufferGetBytesPerRow(videoFrame), | |
| 328 static_cast<UInt>(2 * captureFormat.frame_size.width())); | |
| 329 frameSize = captureFormat.frame_size.GetArea() * 16 / 8; // 16bpp. | |
| 330 break; | |
| 331 case media::PIXEL_FORMAT_NV12: | |
| 332 CHECK_EQ(CVPixelBufferGetBytesPerRowOfPlane(videoFrame, 0), | |
| 333 static_cast<UInt>(captureFormat.frame_size.width())); | |
|
mcasas
2015/01/28 20:17:01
|UInt| looks funny. What about |uint32| or even be
magjed_chromium
2015/01/29 13:42:17
Done.
| |
| 334 CHECK_EQ(CVPixelBufferGetBytesPerRowOfPlane(videoFrame, 1), | |
| 335 static_cast<UInt>(captureFormat.frame_size.width())); | |
| 336 CHECK_EQ(CVPixelBufferGetBaseAddressOfPlane(videoFrame, 1), | |
| 337 baseAddress + captureFormat.frame_size.GetArea()); | |
| 338 frameSize = captureFormat.frame_size.GetArea() * 12 / 8; // 12bpp. | |
| 339 break; | |
| 340 default: | |
| 341 NOTREACHED(); | |
| 342 } | |
| 318 } else { | 343 } else { |
| 319 videoFrame = nil; | 344 videoFrame = nil; |
| 320 } | 345 } |
| 321 } | 346 } |
| 322 | 347 |
| 323 { | 348 { |
| 324 base::AutoLock lock(lock_); | 349 base::AutoLock lock(lock_); |
| 325 if (frameReceiver_ && baseAddress) { | 350 if (frameReceiver_ && baseAddress) { |
| 326 frameReceiver_->ReceiveFrame(reinterpret_cast<uint8_t*>(baseAddress), | 351 frameReceiver_->ReceiveFrame(reinterpret_cast<uint8_t*>(baseAddress), |
| 327 frameSize, captureFormat, 0, 0); | 352 frameSize, captureFormat, 0, 0); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 342 } | 367 } |
| 343 | 368 |
| 344 - (void)sendErrorString:(NSString*)error { | 369 - (void)sendErrorString:(NSString*)error { |
| 345 DLOG(ERROR) << [error UTF8String]; | 370 DLOG(ERROR) << [error UTF8String]; |
| 346 base::AutoLock lock(lock_); | 371 base::AutoLock lock(lock_); |
| 347 if (frameReceiver_) | 372 if (frameReceiver_) |
| 348 frameReceiver_->ReceiveError([error UTF8String]); | 373 frameReceiver_->ReceiveError([error UTF8String]); |
| 349 } | 374 } |
| 350 | 375 |
| 351 @end | 376 @end |
| OLD | NEW |