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/base/mac/corevideo_glue.h" | |
12 #include "media/video/capture/mac/video_capture_device_mac.h" | 11 #include "media/video/capture/mac/video_capture_device_mac.h" |
13 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
14 | 13 |
15 // Prefer MJPEG if frame width or height is larger than this. | 14 // Prefer MJPEG if frame width or height is larger than this. |
16 static const int kMjpegWidthThreshold = 640; | 15 static const int kMjpegWidthThreshold = 640; |
17 static const int kMjpegHeightThreshold = 480; | 16 static const int kMjpegHeightThreshold = 480; |
18 | 17 |
19 // This function translates Mac Core Video pixel formats to Chromium pixel | 18 // This function translates Mac Core Video pixel formats to Chromium pixel |
20 // formats. Chromium pixel formats are sorted in order of preference. | 19 // formats. Chromium pixel formats are sorted in order of preference. |
21 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { | 20 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { |
22 switch (code) { | 21 switch (code) { |
23 case kCVPixelFormatType_422YpCbCr8: | 22 case kCVPixelFormatType_422YpCbCr8: |
24 return media::PIXEL_FORMAT_UYVY; | 23 return media::PIXEL_FORMAT_UYVY; |
25 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: | 24 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: |
26 return media::PIXEL_FORMAT_YUY2; | 25 return media::PIXEL_FORMAT_YUY2; |
27 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: | 26 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: |
28 return media::PIXEL_FORMAT_MJPEG; | 27 return media::PIXEL_FORMAT_MJPEG; |
29 case CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: | |
30 return media::PIXEL_FORMAT_NV12; | |
31 default: | 28 default: |
32 return media::PIXEL_FORMAT_UNKNOWN; | 29 return media::PIXEL_FORMAT_UNKNOWN; |
33 } | 30 } |
34 } | 31 } |
35 | 32 |
36 @implementation VideoCaptureDeviceAVFoundation | 33 @implementation VideoCaptureDeviceAVFoundation |
37 | 34 |
38 #pragma mark Class methods | 35 #pragma mark Class methods |
39 | 36 |
40 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { | 37 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); | 305 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); |
309 // Expect the MJPEG data to be available as a contiguous reference, i.e. | 306 // Expect the MJPEG data to be available as a contiguous reference, i.e. |
310 // not covered by multiple memory blocks. | 307 // not covered by multiple memory blocks. |
311 CHECK_EQ(lengthAtOffset, frameSize); | 308 CHECK_EQ(lengthAtOffset, frameSize); |
312 } | 309 } |
313 } else { | 310 } else { |
314 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); | 311 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); |
315 // Lock the frame and calculate frame size. | 312 // Lock the frame and calculate frame size. |
316 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == | 313 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == |
317 kCVReturnSuccess) { | 314 kCVReturnSuccess) { |
318 baseAddress = static_cast<char*>( | 315 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame)); |
319 CVPixelBufferGetPlaneCount(videoFrame) == 0 | 316 frameSize = CVPixelBufferGetHeight(videoFrame) * |
320 ? CVPixelBufferGetBaseAddress(videoFrame) | 317 CVPixelBufferGetBytesPerRow(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<size_t>(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<size_t>(captureFormat.frame_size.width())); | |
334 CHECK_EQ(CVPixelBufferGetBytesPerRowOfPlane(videoFrame, 1), | |
335 static_cast<size_t>(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 } | |
343 } else { | 318 } else { |
344 videoFrame = nil; | 319 videoFrame = nil; |
345 } | 320 } |
346 } | 321 } |
347 | 322 |
348 { | 323 { |
349 base::AutoLock lock(lock_); | 324 base::AutoLock lock(lock_); |
350 if (frameReceiver_ && baseAddress) { | 325 if (frameReceiver_ && baseAddress) { |
351 frameReceiver_->ReceiveFrame(reinterpret_cast<uint8_t*>(baseAddress), | 326 frameReceiver_->ReceiveFrame(reinterpret_cast<uint8_t*>(baseAddress), |
352 frameSize, captureFormat, 0, 0); | 327 frameSize, captureFormat, 0, 0); |
(...skipping 14 matching lines...) Expand all Loading... |
367 } | 342 } |
368 | 343 |
369 - (void)sendErrorString:(NSString*)error { | 344 - (void)sendErrorString:(NSString*)error { |
370 DLOG(ERROR) << [error UTF8String]; | 345 DLOG(ERROR) << [error UTF8String]; |
371 base::AutoLock lock(lock_); | 346 base::AutoLock lock(lock_); |
372 if (frameReceiver_) | 347 if (frameReceiver_) |
373 frameReceiver_->ReceiveError([error UTF8String]); | 348 frameReceiver_->ReceiveError([error UTF8String]); |
374 } | 349 } |
375 | 350 |
376 @end | 351 @end |
OLD | NEW |