linux_aura: Disable the plugin install infobar.
[chromium-blink-merge.git] / media / video / capture / mac / avfoundation_glue.mm
blobafc92d159bfad89c2ed5c1bee2a5ff1b0de5eda3
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
3 // found in the LICENSE file.
5 #import "media/video/capture/mac/avfoundation_glue.h"
7 #include <dlfcn.h>
9 #include "base/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "base/mac/mac_util.h"
12 #include "base/metrics/field_trial.h"
13 #include "media/base/media_switches.h"
15 namespace {
17 // This class is used to retrieve AVFoundation NSBundle and library handle. It
18 // must be used as a LazyInstance so that it is initialised once and in a
19 // thread-safe way. Normally no work is done in constructors: LazyInstance is
20 // an exception.
21 class AVFoundationInternal {
22  public:
23   AVFoundationInternal() {
24     bundle_ = [NSBundle
25         bundleWithPath:@"/System/Library/Frameworks/AVFoundation.framework"];
27     const char* path = [[bundle_ executablePath] fileSystemRepresentation];
28     CHECK(path);
29     library_handle_ = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
30     CHECK(library_handle_) << dlerror();
32     struct {
33       NSString** loaded_string;
34       const char* symbol;
35     } av_strings[] = {
36         {&AVCaptureDeviceWasConnectedNotification_,
37          "AVCaptureDeviceWasConnectedNotification"},
38         {&AVCaptureDeviceWasDisconnectedNotification_,
39          "AVCaptureDeviceWasDisconnectedNotification"},
40         {&AVMediaTypeVideo_, "AVMediaTypeVideo"},
41         {&AVMediaTypeAudio_, "AVMediaTypeAudio"},
42         {&AVMediaTypeMuxed_, "AVMediaTypeMuxed"},
43         {&AVCaptureSessionRuntimeErrorNotification_,
44          "AVCaptureSessionRuntimeErrorNotification"},
45         {&AVCaptureSessionDidStopRunningNotification_,
46          "AVCaptureSessionDidStopRunningNotification"},
47         {&AVCaptureSessionErrorKey_, "AVCaptureSessionErrorKey"},
48         {&AVVideoScalingModeKey_, "AVVideoScalingModeKey"},
49         {&AVVideoScalingModeResizeAspectFill_,
50          "AVVideoScalingModeResizeAspectFill"},
51     };
52     for (size_t i = 0; i < arraysize(av_strings); ++i) {
53       *av_strings[i].loaded_string = *reinterpret_cast<NSString**>(
54           dlsym(library_handle_, av_strings[i].symbol));
55       DCHECK(*av_strings[i].loaded_string) << dlerror();
56     }
57   }
59   NSBundle* bundle() const { return bundle_; }
60   void* library_handle() const { return library_handle_; }
62   NSString* AVCaptureDeviceWasConnectedNotification() const {
63     return AVCaptureDeviceWasConnectedNotification_;
64   }
65   NSString* AVCaptureDeviceWasDisconnectedNotification() const {
66     return AVCaptureDeviceWasDisconnectedNotification_;
67   }
68   NSString* AVMediaTypeVideo() const { return AVMediaTypeVideo_; }
69   NSString* AVMediaTypeAudio() const { return AVMediaTypeAudio_; }
70   NSString* AVMediaTypeMuxed() const { return AVMediaTypeMuxed_; }
71   NSString* AVCaptureSessionRuntimeErrorNotification() const {
72     return AVCaptureSessionRuntimeErrorNotification_;
73   }
74   NSString* AVCaptureSessionDidStopRunningNotification() const {
75     return AVCaptureSessionDidStopRunningNotification_;
76   }
77   NSString* AVCaptureSessionErrorKey() const {
78     return AVCaptureSessionErrorKey_;
79   }
80   NSString* AVVideoScalingModeKey() const { return AVVideoScalingModeKey_; }
81   NSString* AVVideoScalingModeResizeAspectFill() const {
82     return AVVideoScalingModeResizeAspectFill_;
83   }
85  private:
86   NSBundle* bundle_;
87   void* library_handle_;
88   // The following members are replicas of the respectives in AVFoundation.
89   NSString* AVCaptureDeviceWasConnectedNotification_;
90   NSString* AVCaptureDeviceWasDisconnectedNotification_;
91   NSString* AVMediaTypeVideo_;
92   NSString* AVMediaTypeAudio_;
93   NSString* AVMediaTypeMuxed_;
94   NSString* AVCaptureSessionRuntimeErrorNotification_;
95   NSString* AVCaptureSessionDidStopRunningNotification_;
96   NSString* AVCaptureSessionErrorKey_;
97   NSString* AVVideoScalingModeKey_;
98   NSString* AVVideoScalingModeResizeAspectFill_;
100   DISALLOW_COPY_AND_ASSIGN(AVFoundationInternal);
103 }  // namespace
105 static base::LazyInstance<AVFoundationInternal> g_avfoundation_handle =
106     LAZY_INSTANCE_INITIALIZER;
108 bool AVFoundationGlue::IsAVFoundationSupported() {
109   // DeviceMonitorMac will initialize this static bool from the main UI thread
110   // once, during Chrome startup so this construction is thread safe.
111   // Use AVFoundation if possible, enabled, and QTKit is not explicitly forced.
112   static CommandLine* command_line = CommandLine::ForCurrentProcess();
113   static bool is_av_foundation_supported = base::mac::IsOSLionOrLater() &&
114       ((command_line->HasSwitch(switches::kEnableAVFoundation) &&
115       !command_line->HasSwitch(switches::kForceQTKit)) ||
116       base::FieldTrialList::FindFullName("AVFoundationMacVideoCapture")
117           == "Enabled") && [AVFoundationBundle() load];
118   return is_av_foundation_supported;
121 NSBundle const* AVFoundationGlue::AVFoundationBundle() {
122   return g_avfoundation_handle.Get().bundle();
125 void* AVFoundationGlue::AVFoundationLibraryHandle() {
126   return g_avfoundation_handle.Get().library_handle();
129 NSString* AVFoundationGlue::AVCaptureDeviceWasConnectedNotification() {
130   return g_avfoundation_handle.Get().AVCaptureDeviceWasConnectedNotification();
133 NSString* AVFoundationGlue::AVCaptureDeviceWasDisconnectedNotification() {
134   return
135       g_avfoundation_handle.Get().AVCaptureDeviceWasDisconnectedNotification();
138 NSString* AVFoundationGlue::AVMediaTypeVideo() {
139   return g_avfoundation_handle.Get().AVMediaTypeVideo();
142 NSString* AVFoundationGlue::AVMediaTypeAudio() {
143   return g_avfoundation_handle.Get().AVMediaTypeAudio();
146 NSString* AVFoundationGlue::AVMediaTypeMuxed() {
147   return g_avfoundation_handle.Get().AVMediaTypeMuxed();
150 NSString* AVFoundationGlue::AVCaptureSessionRuntimeErrorNotification() {
151   return g_avfoundation_handle.Get().AVCaptureSessionRuntimeErrorNotification();
154 NSString* AVFoundationGlue::AVCaptureSessionDidStopRunningNotification() {
155   return
156       g_avfoundation_handle.Get().AVCaptureSessionDidStopRunningNotification();
159 NSString* AVFoundationGlue::AVCaptureSessionErrorKey() {
160   return g_avfoundation_handle.Get().AVCaptureSessionErrorKey();
163 NSString* AVFoundationGlue::AVVideoScalingModeKey() {
164   return g_avfoundation_handle.Get().AVVideoScalingModeKey();
167 NSString* AVFoundationGlue::AVVideoScalingModeResizeAspectFill() {
168   return g_avfoundation_handle.Get().AVVideoScalingModeResizeAspectFill();
171 Class AVFoundationGlue::AVCaptureSessionClass() {
172   return [AVFoundationBundle() classNamed:@"AVCaptureSession"];
175 Class AVFoundationGlue::AVCaptureVideoDataOutputClass() {
176   return [AVFoundationBundle() classNamed:@"AVCaptureVideoDataOutput"];
179 @implementation AVCaptureDeviceGlue
181 + (NSArray*)devices {
182   Class avcClass =
183       [AVFoundationGlue::AVFoundationBundle() classNamed:@"AVCaptureDevice"];
184   if ([avcClass respondsToSelector:@selector(devices)]) {
185     return [avcClass performSelector:@selector(devices)];
186   }
187   return nil;
190 + (CrAVCaptureDevice*)deviceWithUniqueID:(NSString*)deviceUniqueID {
191   Class avcClass =
192       [AVFoundationGlue::AVFoundationBundle() classNamed:@"AVCaptureDevice"];
193   return [avcClass performSelector:@selector(deviceWithUniqueID:)
194                         withObject:deviceUniqueID];
197 @end  // @implementation AVCaptureDeviceGlue
199 @implementation AVCaptureDeviceInputGlue
201 + (CrAVCaptureDeviceInput*)deviceInputWithDevice:(CrAVCaptureDevice*)device
202                                            error:(NSError**)outError {
203   return [[AVFoundationGlue::AVFoundationBundle()
204       classNamed:@"AVCaptureDeviceInput"] deviceInputWithDevice:device
205                                                           error:outError];
208 @end  // @implementation AVCaptureDeviceInputGlue