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 |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
11 #include "media/video/capture/mac/video_capture_device_mac.h" | 13 #include "media/video/capture/mac/video_capture_device_mac.h" |
12 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
13 | 15 |
14 // Prefer MJPEG if frame width or height is larger than this. | 16 // Prefer MJPEG if frame width or height is larger than this. |
15 static const int kMjpegWidthThreshold = 640; | 17 static const int kMjpegWidthThreshold = 640; |
16 static const int kMjpegHeightThreshold = 480; | 18 static const int kMjpegHeightThreshold = 480; |
17 | 19 |
18 // This function translates Mac Core Video pixel formats to Chromium pixel | 20 // This function translates Mac Core Video pixel formats to Chromium pixel |
19 // formats. Chromium pixel formats are sorted in order of preference. | 21 // formats. Chromium pixel formats are sorted in order of preference. |
20 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { | 22 media::VideoPixelFormat FourCCToChromiumPixelFormat(FourCharCode code) { |
21 switch (code) { | 23 switch (code) { |
22 case kCVPixelFormatType_422YpCbCr8: | 24 case kCVPixelFormatType_422YpCbCr8: |
23 return media::PIXEL_FORMAT_UYVY; | 25 return media::PIXEL_FORMAT_UYVY; |
24 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: | 26 case CoreMediaGlue::kCMPixelFormat_422YpCbCr8_yuvs: |
25 return media::PIXEL_FORMAT_YUY2; | 27 return media::PIXEL_FORMAT_YUY2; |
26 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: | 28 case CoreMediaGlue::kCMVideoCodecType_JPEG_OpenDML: |
27 return media::PIXEL_FORMAT_MJPEG; | 29 return media::PIXEL_FORMAT_MJPEG; |
28 default: | 30 default: |
29 return media::PIXEL_FORMAT_UNKNOWN; | 31 return media::PIXEL_FORMAT_UNKNOWN; |
30 } | 32 } |
31 } | 33 } |
32 | 34 |
| 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 |
33 @implementation VideoCaptureDeviceAVFoundation | 50 @implementation VideoCaptureDeviceAVFoundation |
34 | 51 |
35 #pragma mark Class methods | 52 #pragma mark Class methods |
36 | 53 |
37 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { | 54 + (void)getDeviceNames:(NSMutableDictionary*)deviceNames { |
38 // At this stage we already know that AVFoundation is supported and the whole | 55 // At this stage we already know that AVFoundation is supported and the whole |
39 // library is loaded and initialised, by the device monitoring. | 56 // library is loaded and initialised, by the device monitoring. |
40 NSArray* devices = [AVCaptureDeviceGlue devices]; | 57 NSArray* devices = [AVCaptureDeviceGlue devices]; |
41 for (CrAVCaptureDevice* device in devices) { | 58 for (CrAVCaptureDevice* device in devices) { |
42 if (([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()] || | 59 if (([device hasMediaType:AVFoundationGlue::AVMediaTypeVideo()] || |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 // If MJPEG, use block buffer instead of pixel buffer. | 316 // If MJPEG, use block buffer instead of pixel buffer. |
300 CoreMediaGlue::CMBlockBufferRef blockBuffer = | 317 CoreMediaGlue::CMBlockBufferRef blockBuffer = |
301 CoreMediaGlue::CMSampleBufferGetDataBuffer(sampleBuffer); | 318 CoreMediaGlue::CMSampleBufferGetDataBuffer(sampleBuffer); |
302 if (blockBuffer) { | 319 if (blockBuffer) { |
303 size_t lengthAtOffset; | 320 size_t lengthAtOffset; |
304 CoreMediaGlue::CMBlockBufferGetDataPointer( | 321 CoreMediaGlue::CMBlockBufferGetDataPointer( |
305 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); | 322 blockBuffer, 0, &lengthAtOffset, &frameSize, &baseAddress); |
306 // Expect the MJPEG data to be available as a contiguous reference, i.e. | 323 // Expect the MJPEG data to be available as a contiguous reference, i.e. |
307 // not covered by multiple memory blocks. | 324 // not covered by multiple memory blocks. |
308 CHECK_EQ(lengthAtOffset, frameSize); | 325 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; |
309 } | 336 } |
310 } else { | 337 } else { |
311 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); | 338 videoFrame = CoreMediaGlue::CMSampleBufferGetImageBuffer(sampleBuffer); |
312 // Lock the frame and calculate frame size. | 339 // Lock the frame and calculate frame size. |
313 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == | 340 if (CVPixelBufferLockBaseAddress(videoFrame, kCVPixelBufferLock_ReadOnly) == |
314 kCVReturnSuccess) { | 341 kCVReturnSuccess) { |
315 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame)); | 342 baseAddress = static_cast<char*>(CVPixelBufferGetBaseAddress(videoFrame)); |
316 frameSize = CVPixelBufferGetHeight(videoFrame) * | 343 frameSize = CVPixelBufferGetHeight(videoFrame) * |
317 CVPixelBufferGetBytesPerRow(videoFrame); | 344 CVPixelBufferGetBytesPerRow(videoFrame); |
318 } else { | 345 } else { |
(...skipping 23 matching lines...) Expand all Loading... |
342 } | 369 } |
343 | 370 |
344 - (void)sendErrorString:(NSString*)error { | 371 - (void)sendErrorString:(NSString*)error { |
345 DLOG(ERROR) << [error UTF8String]; | 372 DLOG(ERROR) << [error UTF8String]; |
346 base::AutoLock lock(lock_); | 373 base::AutoLock lock(lock_); |
347 if (frameReceiver_) | 374 if (frameReceiver_) |
348 frameReceiver_->ReceiveError([error UTF8String]); | 375 frameReceiver_->ReceiveError([error UTF8String]); |
349 } | 376 } |
350 | 377 |
351 @end | 378 @end |
OLD | NEW |