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/base/mac/avfoundation_glue.h" | 5 #import "media/base/mac/avfoundation_glue.h" |
6 | 6 |
7 #import <AVFoundation/AVFoundation.h> | 7 #import <AVFoundation/AVFoundation.h> |
8 #include <dlfcn.h> | 8 #include <dlfcn.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... |
24 extern NSString* const AVMediaTypeVideo; | 24 extern NSString* const AVMediaTypeVideo; |
25 extern NSString* const AVMediaTypeAudio; | 25 extern NSString* const AVMediaTypeAudio; |
26 extern NSString* const AVMediaTypeMuxed; | 26 extern NSString* const AVMediaTypeMuxed; |
27 extern NSString* const AVCaptureSessionRuntimeErrorNotification; | 27 extern NSString* const AVCaptureSessionRuntimeErrorNotification; |
28 extern NSString* const AVCaptureSessionDidStopRunningNotification; | 28 extern NSString* const AVCaptureSessionDidStopRunningNotification; |
29 extern NSString* const AVCaptureSessionErrorKey; | 29 extern NSString* const AVCaptureSessionErrorKey; |
30 extern NSString* const AVVideoScalingModeKey; | 30 extern NSString* const AVVideoScalingModeKey; |
31 extern NSString* const AVVideoScalingModeResizeAspectFill; | 31 extern NSString* const AVVideoScalingModeResizeAspectFill; |
32 | 32 |
33 namespace { | 33 namespace { |
34 // Used for logging capture API usage. Classes are a partition. Elements in this | |
35 // enum should not be deleted or rearranged; the only permitted operation is to | |
36 // add new elements before CAPTURE_API_MAX, that must be equal to the last item. | |
37 enum CaptureApi { | |
38 CAPTURE_API_QTKIT_DUE_TO_OS_PREVIOUS_TO_LION = 0, | |
39 CAPTURE_API_QTKIT_FORCED_BY_FLAG = 1, | |
40 CAPTURE_API_QTKIT_DUE_TO_NO_FLAG = 2, | |
41 CAPTURE_API_QTKIT_DUE_TO_AVFOUNDATION_LOAD_ERROR = 3, | |
42 CAPTURE_API_AVFOUNDATION_LOADED_OK = 4, | |
43 CAPTURE_API_MAX = CAPTURE_API_AVFOUNDATION_LOADED_OK | |
44 }; | |
45 | |
46 void LogCaptureApi(CaptureApi api) { | |
47 UMA_HISTOGRAM_ENUMERATION("Media.VideoCaptureApi.Mac", | |
48 api, | |
49 CAPTURE_API_MAX + 1); | |
50 } | |
51 | |
52 // This class is used to retrieve AVFoundation NSBundle and library handle. It | 34 // This class is used to retrieve AVFoundation NSBundle and library handle. It |
53 // must be used as a LazyInstance so that it is initialised once and in a | 35 // must be used as a LazyInstance so that it is initialised once and in a |
54 // thread-safe way. Normally no work is done in constructors: LazyInstance is | 36 // thread-safe way. Normally no work is done in constructors: LazyInstance is |
55 // an exception. | 37 // an exception. |
56 class AVFoundationInternal { | 38 class AVFoundationInternal { |
57 public: | 39 public: |
58 AVFoundationInternal() { | 40 AVFoundationInternal() { |
59 bundle_ = [NSBundle | 41 bundle_ = [NSBundle |
60 bundleWithPath:@"/System/Library/Frameworks/AVFoundation.framework"]; | 42 bundleWithPath:@"/System/Library/Frameworks/AVFoundation.framework"]; |
61 } | 43 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 delete static_cast<AVFoundationInternal*>(value); | 97 delete static_cast<AVFoundationInternal*>(value); |
116 } | 98 } |
117 | 99 |
118 AVFoundationInternal* GetAVFoundationInternal() { | 100 AVFoundationInternal* GetAVFoundationInternal() { |
119 return static_cast<AVFoundationInternal*>(g_avfoundation_handle.Get()); | 101 return static_cast<AVFoundationInternal*>(g_avfoundation_handle.Get()); |
120 } | 102 } |
121 | 103 |
122 // This contains the logic of checking whether AVFoundation is supported. | 104 // This contains the logic of checking whether AVFoundation is supported. |
123 // It's called only once and the results are cached in a static bool. | 105 // It's called only once and the results are cached in a static bool. |
124 bool LoadAVFoundationInternal() { | 106 bool LoadAVFoundationInternal() { |
125 // AVFoundation is only available on OS Lion and above. | |
126 if (!base::mac::IsOSLionOrLater()) { | |
127 LogCaptureApi(CAPTURE_API_QTKIT_DUE_TO_OS_PREVIOUS_TO_LION); | |
128 return false; | |
129 } | |
130 | |
131 const base::CommandLine* command_line = | |
132 base::CommandLine::ForCurrentProcess(); | |
133 // The force-qtkit flag takes precedence over enable-avfoundation. | |
134 if (command_line->HasSwitch(switches::kForceQTKit)) { | |
135 LogCaptureApi(CAPTURE_API_QTKIT_FORCED_BY_FLAG); | |
136 return false; | |
137 } | |
138 | |
139 if (!command_line->HasSwitch(switches::kEnableAVFoundation)) { | |
140 LogCaptureApi(CAPTURE_API_QTKIT_DUE_TO_NO_FLAG); | |
141 return false; | |
142 } | |
143 g_avfoundation_handle.Initialize(TlsCleanup); | 107 g_avfoundation_handle.Initialize(TlsCleanup); |
144 g_avfoundation_handle.Set(new AVFoundationInternal()); | 108 g_avfoundation_handle.Set(new AVFoundationInternal()); |
145 const bool ret = [AVFoundationGlue::AVFoundationBundle() load]; | 109 const bool ret = [AVFoundationGlue::AVFoundationBundle() load]; |
146 LogCaptureApi(ret ? CAPTURE_API_AVFOUNDATION_LOADED_OK | 110 CHECK(ret); |
147 : CAPTURE_API_QTKIT_DUE_TO_AVFOUNDATION_LOAD_ERROR); | |
148 | |
149 return ret; | 111 return ret; |
150 } | 112 } |
151 | 113 |
152 enum { | 114 enum { |
153 INITIALIZE_NOT_CALLED = 0, | 115 INITIALIZE_NOT_CALLED = 0, |
154 AVFOUNDATION_IS_SUPPORTED, | 116 AVFOUNDATION_IS_SUPPORTED, |
155 AVFOUNDATION_NOT_SUPPORTED | 117 AVFOUNDATION_NOT_SUPPORTED |
156 } static g_avfoundation_initialization = INITIALIZE_NOT_CALLED; | 118 } static g_avfoundation_initialization = INITIALIZE_NOT_CALLED; |
157 | 119 |
158 } // namespace | 120 } // namespace |
159 | 121 |
160 void AVFoundationGlue::InitializeAVFoundation() { | 122 void AVFoundationGlue::InitializeAVFoundation() { |
161 TRACE_EVENT0("video", "AVFoundationGlue::InitializeAVFoundation"); | 123 TRACE_EVENT0("video", "AVFoundationGlue::InitializeAVFoundation"); |
162 CHECK([NSThread isMainThread]); | 124 CHECK([NSThread isMainThread]); |
163 if (g_avfoundation_initialization != INITIALIZE_NOT_CALLED) | 125 if (g_avfoundation_initialization != INITIALIZE_NOT_CALLED) |
164 return; | 126 return; |
165 g_avfoundation_initialization = LoadAVFoundationInternal() ? | 127 g_avfoundation_initialization = LoadAVFoundationInternal() ? |
166 AVFOUNDATION_IS_SUPPORTED : AVFOUNDATION_NOT_SUPPORTED; | 128 AVFOUNDATION_IS_SUPPORTED : AVFOUNDATION_NOT_SUPPORTED; |
167 } | 129 } |
168 | 130 |
169 bool AVFoundationGlue::IsAVFoundationSupported() { | |
170 CHECK_NE(g_avfoundation_initialization, INITIALIZE_NOT_CALLED); | |
171 return g_avfoundation_initialization == AVFOUNDATION_IS_SUPPORTED; | |
172 } | |
173 | |
174 NSBundle const* AVFoundationGlue::AVFoundationBundle() { | 131 NSBundle const* AVFoundationGlue::AVFoundationBundle() { |
175 return GetAVFoundationInternal()->bundle(); | 132 return GetAVFoundationInternal()->bundle(); |
176 } | 133 } |
177 | 134 |
178 NSString* AVFoundationGlue::AVCaptureDeviceWasConnectedNotification() { | 135 NSString* AVFoundationGlue::AVCaptureDeviceWasConnectedNotification() { |
179 return GetAVFoundationInternal()->AVCaptureDeviceWasConnectedNotification(); | 136 return GetAVFoundationInternal()->AVCaptureDeviceWasConnectedNotification(); |
180 } | 137 } |
181 | 138 |
182 NSString* AVFoundationGlue::AVCaptureDeviceWasDisconnectedNotification() { | 139 NSString* AVFoundationGlue::AVCaptureDeviceWasDisconnectedNotification() { |
183 return GetAVFoundationInternal() | 140 return GetAVFoundationInternal() |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 @implementation AVCaptureDeviceInputGlue | 205 @implementation AVCaptureDeviceInputGlue |
249 | 206 |
250 + (CrAVCaptureDeviceInput*)deviceInputWithDevice:(CrAVCaptureDevice*)device | 207 + (CrAVCaptureDeviceInput*)deviceInputWithDevice:(CrAVCaptureDevice*)device |
251 error:(NSError**)outError { | 208 error:(NSError**)outError { |
252 return [[AVFoundationGlue::AVFoundationBundle() | 209 return [[AVFoundationGlue::AVFoundationBundle() |
253 classNamed:@"AVCaptureDeviceInput"] deviceInputWithDevice:device | 210 classNamed:@"AVCaptureDeviceInput"] deviceInputWithDevice:device |
254 error:outError]; | 211 error:outError]; |
255 } | 212 } |
256 | 213 |
257 @end // @implementation AVCaptureDeviceInputGlue | 214 @end // @implementation AVCaptureDeviceInputGlue |
OLD | NEW |