You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
2.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #import "AVCaptureSession+DevicePosition.h"
  11. BOOL CFStringContainsString(CFStringRef theString, CFStringRef stringToFind) {
  12. return CFStringFindWithOptions(theString,
  13. stringToFind,
  14. CFRangeMake(0, CFStringGetLength(theString)),
  15. kCFCompareCaseInsensitive,
  16. nil);
  17. }
  18. @implementation AVCaptureSession (DevicePosition)
  19. + (AVCaptureDevicePosition)devicePositionForSampleBuffer:(CMSampleBufferRef)sampleBuffer {
  20. // Check the image's EXIF for the camera the image came from.
  21. AVCaptureDevicePosition cameraPosition = AVCaptureDevicePositionUnspecified;
  22. CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(
  23. kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
  24. if (attachments) {
  25. int size = CFDictionaryGetCount(attachments);
  26. if (size > 0) {
  27. CFDictionaryRef cfExifDictVal = nil;
  28. if (CFDictionaryGetValueIfPresent(
  29. attachments, (const void *)CFSTR("{Exif}"), (const void **)&cfExifDictVal)) {
  30. CFStringRef cfLensModelStrVal;
  31. if (CFDictionaryGetValueIfPresent(cfExifDictVal,
  32. (const void *)CFSTR("LensModel"),
  33. (const void **)&cfLensModelStrVal)) {
  34. if (CFStringContainsString(cfLensModelStrVal, CFSTR("front"))) {
  35. cameraPosition = AVCaptureDevicePositionFront;
  36. } else if (CFStringContainsString(cfLensModelStrVal, CFSTR("back"))) {
  37. cameraPosition = AVCaptureDevicePositionBack;
  38. }
  39. }
  40. }
  41. }
  42. CFRelease(attachments);
  43. }
  44. return cameraPosition;
  45. }
  46. @end