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 <cstring> // For memchr. | |
10 | |
11 #include "base/logging.h" | 9 #include "base/logging.h" |
12 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
13 #include "media/video/capture/mac/video_capture_device_mac.h" | 11 #include "media/video/capture/mac/video_capture_device_mac.h" |
14 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
15 | 13 |
16 // Prefer MJPEG if frame width or height is larger than this. | 14 // Prefer MJPEG if frame width or height is larger than this. |
17 static const int kMjpegWidthThreshold = 640; | 15 static const int kMjpegWidthThreshold = 640; |
18 static const int kMjpegHeightThreshold = 480; | 16 static const int kMjpegHeightThreshold = 480; |
19 | 17 |
20 // This function translates Mac Core Video pixel formats to Chromium pixel | 18 // This function translates Mac Core Video pixel formats to Chromium pixel |
21 // formats. Chromium pixel formats are sorted in order of preference. | 19 // formats. Chromium pixel formats are sorted in order of preference. |
22 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { | 20 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { |
23 switch (code) { | 21 switch (code) { |
24 case kCVPixelFormatType_422YpCbCr8: | 22 case kCVPixelFormatType_422YpCbCr8: |
25 return media::PIXEL_FORMAT_UYVY; | 23 return media::PIXEL_FORMAT_UYVY; |
26 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: | 24 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: |
27 return media::PIXEL_FORMAT_YUY2; | 25 return media::PIXEL_FORMAT_YUY2; |
28 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: | 26 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: |
29 return media::PIXEL_FORMAT_MJPEG; | 27 return media::PIXEL_FORMAT_MJPEG; |
30 default: | 28 default: |
31 return media::PIXEL_FORMAT_UNKNOWN; | 29 return media::PIXEL_FORMAT_UNKNOWN; |
32 } | 30 } |
33 } | 31 } |
34 | 32 |
35 // TODO(magjed): Remove this when Chromium has the latest libyuv version. | |
36 // Returns frame size by finding the End Of Image marker, or 0 if not found. | |
37 size_t JpegFrameSize(const char* sample, size_t sampleSize) { | |
38 // Jump to next marker (0xff), check for End Of Image (0xd9), repeat. | |
39 const char* end = sample + sampleSize - 1; | |
40 for (const char* it = sample; | |
41 (it = static_cast<const char*>(memchr(it, 0xff, end - it))); | |
42 ++it) { | |
43 if (it[1] == static_cast<char>(0xd9)) | |
44 return 2 + (it - sample); | |
45 } | |
46 DLOG(WARNING) << "JPEG End Of Image (EOI) marker not found."; | |
47 return 0; | |
48 } | |
49 | |
50 @implementation VideoCaptureDeviceAVFoundation | 33 @implementation VideoCaptureDeviceAVFoundation |
51 | 34 |
52 #pragma mark Class methods | 35 #pragma mark Class methods |
53 | 36 |
54 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { | 37 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { |
55 // At this stage we already know that AVFoundation is supported and the whole | 38 // At this stage we already know that AVFoundation is supported and the whole |
56 // library is loaded and initialised, by the device monitoring. | 39 // library is loaded and initialised, by the device monitoring. |
57 NSArray* devices = [AVCaptureDeviceGlue devices]; | 40 NSArray* devices = [AVCaptureDeviceGlue devices]; |
58 for (CrAVCaptureDevice* device in devices) { | 41 for (CrAVCaptureDevice* device in devices) { |
59 if (([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()] || | 42 if (([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()] || |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 // If MJPEG, use block buffer instead of pixel buffer. | 299 // If MJPEG, use block buffer instead of pixel buffer. |
317 CoreMediaGlue::CMBlockBufferRef blockBuffer = | 300 CoreMediaGlue::CMBlockBufferRef blockBuffer = |
318 CoreMediaGlue::CMSampleBufferGetDataBuffer(sampleBuffer); | 301 CoreMediaGlue::CMSampleBufferGetDataBuffer(sampleBuffer); |
319 if (blockBuffer) { | 302 if (blockBuffer) { |
320 size_t lengthAtOffset; | 303 size_t lengthAtOffset; |
321 CoreMediaGlue::CMBlockBufferGetDataPointer( | 304 CoreMediaGlue::CMBlockBufferGetDataPointer( |
322 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); | 305 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); |
323 // 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. |
324 // not covered by multiple memory blocks. | 307 // not covered by multiple memory blocks. |
325 CHECK_EQ(lengthAtOffset, frameSize); | 308 CHECK_EQ(lengthAtOffset, frameSize); |
326 | |
327 // TODO(magjed): Remove this when Chromium has the latest libyuv version. | |
328 // If |frameSize| is suspiciously high (>= 8 bpp), calculate the actual | |
329 // size by finding the end of image marker. The purpose is to speed up the | |
330 // jpeg decoding in the browser. | |
331 if (static_cast<int>(frameSize) >= dimensions.width * dimensions.height) | |
332 frameSize = JpegFrameSize(baseAddress, frameSize); | |
333 | |
334 if (frameSize == 0) | |
335 return; | |
336 } | 309 } |
337 } else { | 310 } else { |
338 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); | 311 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); |
339 // Lock the frame and calculate frame size. | 312 // Lock the frame and calculate frame size. |
340 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == | 313 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == |
341 kCVReturnSuccess) { | 314 kCVReturnSuccess) { |
342 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame)); | 315 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame)); |
343 frameSize = CVPixelBufferGetHeight(videoFrame) * | 316 frameSize = CVPixelBufferGetHeight(videoFrame) * |
344 CVPixelBufferGetBytesPerRow(videoFrame); | 317 CVPixelBufferGetBytesPerRow(videoFrame); |
345 } else { | 318 } else { |
(...skipping 23 matching lines...) Expand all Loading... |
369 } | 342 } |
370 | 343 |
371 - (void)sendErrorString:(NSString*)error { | 344 - (void)sendErrorString:(NSString*)error { |
372 DLOG(ERROR) << [error UTF8String]; | 345 DLOG(ERROR) << [error UTF8String]; |
373 base::AutoLock lock(lock_); | 346 base::AutoLock lock(lock_); |
374 if (frameReceiver_) | 347 if (frameReceiver_) |
375 frameReceiver_->ReceiveError([error UTF8String]); | 348 frameReceiver_->ReceiveError([error UTF8String]); |
376 } | 349 } |
377 | 350 |
378 @end | 351 @end |
OLD | NEW |