Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Side by Side Diff: media/video/capture/mac/avfoundation_glue.mm

Issue 137903003: AVFoundation NSString*, cache them in the AVFoundationInternal LazyInstance. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rsesek@ comments Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/avfoundation_glue.h" 5 #import "media/video/capture/mac/avfoundation_glue.h"
6 6
7 #include <dlfcn.h> 7 #include <dlfcn.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 10 matching lines...) Expand all
21 class AVFoundationInternal { 21 class AVFoundationInternal {
22 public: 22 public:
23 AVFoundationInternal() { 23 AVFoundationInternal() {
24 bundle_ = [NSBundle 24 bundle_ = [NSBundle
25 bundleWithPath:@"/System/Library/Frameworks/AVFoundation.framework"]; 25 bundleWithPath:@"/System/Library/Frameworks/AVFoundation.framework"];
26 26
27 const char* path = [[bundle_ executablePath] fileSystemRepresentation]; 27 const char* path = [[bundle_ executablePath] fileSystemRepresentation];
28 CHECK(path); 28 CHECK(path);
29 library_handle_ = dlopen(path, RTLD_LAZY | RTLD_LOCAL); 29 library_handle_ = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
30 CHECK(library_handle_) << dlerror(); 30 CHECK(library_handle_) << dlerror();
31
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 {&AVCaptureSessionPreset320x240_, "AVCaptureSessionPreset320x240"},
49 {&AVCaptureSessionPreset640x480_, "AVCaptureSessionPreset640x480"},
50 {&AVCaptureSessionPreset1280x720_, "AVCaptureSessionPreset1280x720"},
51 {&AVVideoScalingModeKey_, "AVVideoScalingModeKey"},
52 {&AVVideoScalingModeResizeAspect_, "AVVideoScalingModeResizeAspect"},
53 };
54 for (size_t i = 0; i < arraysize(av_strings); ++i) {
55 *av_strings[i].loaded_string = *reinterpret_cast<NSString**>(
56 dlsym(library_handle_, av_strings[i].symbol));
57 DCHECK(*av_strings[i].loaded_string) << dlerror();
58 }
31 } 59 }
60
32 NSBundle* bundle() const { return bundle_; } 61 NSBundle* bundle() const { return bundle_; }
33 void* library_handle() const { return library_handle_; } 62 void* library_handle() const { return library_handle_; }
34 63
64 NSString* AVCaptureDeviceWasConnectedNotification() const {
65 return AVCaptureDeviceWasConnectedNotification_;
66 }
67 NSString* AVCaptureDeviceWasDisconnectedNotification() const {
68 return AVCaptureDeviceWasDisconnectedNotification_;
69 }
70 NSString* AVMediaTypeVideo() const { return AVMediaTypeVideo_; }
71 NSString* AVMediaTypeAudio() const { return AVMediaTypeAudio_; }
72 NSString* AVMediaTypeMuxed() const { return AVMediaTypeMuxed_; }
73 NSString* AVCaptureSessionRuntimeErrorNotification() const {
74 return AVCaptureSessionRuntimeErrorNotification_;
75 }
76 NSString* AVCaptureSessionDidStopRunningNotification() const {
77 return AVCaptureSessionDidStopRunningNotification_;
78 }
79 NSString* AVCaptureSessionErrorKey() const {
80 return AVCaptureSessionErrorKey_;
81 }
82 NSString* AVCaptureSessionPreset320x240() const {
83 return AVCaptureSessionPreset320x240_;
84 }
85 NSString* AVCaptureSessionPreset640x480() const {
86 return AVCaptureSessionPreset640x480_;
87 }
88 NSString* AVCaptureSessionPreset1280x720() const {
89 return AVCaptureSessionPreset1280x720_;
90 }
91 NSString* AVVideoScalingModeKey() const { return AVVideoScalingModeKey_; }
92 NSString* AVVideoScalingModeResizeAspect() const {
93 return AVVideoScalingModeResizeAspect_;
94 }
95
35 private: 96 private:
36 NSBundle* bundle_; 97 NSBundle* bundle_;
37 void* library_handle_; 98 void* library_handle_;
99 // The following members are replicas of the respectives in AVFoundation.
100 NSString* AVCaptureDeviceWasConnectedNotification_;
101 NSString* AVCaptureDeviceWasDisconnectedNotification_;
102 NSString* AVMediaTypeVideo_;
103 NSString* AVMediaTypeAudio_;
104 NSString* AVMediaTypeMuxed_;
105 NSString* AVCaptureSessionRuntimeErrorNotification_;
106 NSString* AVCaptureSessionDidStopRunningNotification_;
107 NSString* AVCaptureSessionErrorKey_;
108 NSString* AVCaptureSessionPreset320x240_;
109 NSString* AVCaptureSessionPreset640x480_;
110 NSString* AVCaptureSessionPreset1280x720_;
111 NSString* AVVideoScalingModeKey_;
112 NSString* AVVideoScalingModeResizeAspect_;
38 113
39 DISALLOW_COPY_AND_ASSIGN(AVFoundationInternal); 114 DISALLOW_COPY_AND_ASSIGN(AVFoundationInternal);
40 }; 115 };
41 116
42 } // namespace 117 } // namespace
43 118
44 static base::LazyInstance<AVFoundationInternal> g_avfoundation_handle = 119 static base::LazyInstance<AVFoundationInternal> g_avfoundation_handle =
45 LAZY_INSTANCE_INITIALIZER; 120 LAZY_INSTANCE_INITIALIZER;
46 121
47 namespace media {
48
49 // TODO(mcasas):http://crbug.com/323536 cache the string pointers.
50 static NSString* ReadNSStringPtr(const char* symbol) {
51 NSString** string_pointer = reinterpret_cast<NSString**>(
52 dlsym(AVFoundationGlue::AVFoundationLibraryHandle(), symbol));
53 DCHECK(string_pointer) << dlerror();
54 return *string_pointer;
55 }
56
57 } // namespace media
58
59 bool AVFoundationGlue::IsAVFoundationSupported() { 122 bool AVFoundationGlue::IsAVFoundationSupported() {
60 if (!base::mac::IsOSLionOrLater()) 123 if (!base::mac::IsOSLionOrLater())
61 return false; 124 return false;
62 const std::string group_name = 125 const std::string group_name =
63 base::FieldTrialList::FindFullName("AVFoundationMacVideoCapture"); 126 base::FieldTrialList::FindFullName("AVFoundationMacVideoCapture");
64 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 127 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
65 return (cmd_line->HasSwitch(switches::kEnableAVFoundation) || 128 return (cmd_line->HasSwitch(switches::kEnableAVFoundation) ||
66 group_name == "Enabled") && [AVFoundationBundle() load]; 129 group_name == "Enabled") && [AVFoundationBundle() load];
67 } 130 }
68 131
69 NSBundle const* AVFoundationGlue::AVFoundationBundle() { 132 NSBundle const* AVFoundationGlue::AVFoundationBundle() {
70 return g_avfoundation_handle.Get().bundle(); 133 return g_avfoundation_handle.Get().bundle();
71 } 134 }
72 135
73 void* AVFoundationGlue::AVFoundationLibraryHandle() { 136 void* AVFoundationGlue::AVFoundationLibraryHandle() {
74 return g_avfoundation_handle.Get().library_handle(); 137 return g_avfoundation_handle.Get().library_handle();
75 } 138 }
76 139
77 NSString* AVFoundationGlue::AVCaptureDeviceWasConnectedNotification() { 140 NSString* AVFoundationGlue::AVCaptureDeviceWasConnectedNotification() {
78 return media::ReadNSStringPtr("AVCaptureDeviceWasConnectedNotification"); 141 return g_avfoundation_handle.Get().AVCaptureDeviceWasConnectedNotification();
79 } 142 }
80 143
81 NSString* AVFoundationGlue::AVCaptureDeviceWasDisconnectedNotification() { 144 NSString* AVFoundationGlue::AVCaptureDeviceWasDisconnectedNotification() {
82 return media::ReadNSStringPtr("AVCaptureDeviceWasDisconnectedNotification"); 145 return
146 g_avfoundation_handle.Get().AVCaptureDeviceWasDisconnectedNotification();
83 } 147 }
84 148
85 NSString* AVFoundationGlue::AVMediaTypeVideo() { 149 NSString* AVFoundationGlue::AVMediaTypeVideo() {
86 return media::ReadNSStringPtr("AVMediaTypeVideo"); 150 return g_avfoundation_handle.Get().AVMediaTypeVideo();
87 } 151 }
88 152
89 NSString* AVFoundationGlue::AVMediaTypeAudio() { 153 NSString* AVFoundationGlue::AVMediaTypeAudio() {
90 return media::ReadNSStringPtr("AVMediaTypeAudio"); 154 return g_avfoundation_handle.Get().AVMediaTypeAudio();
91 } 155 }
92 156
93 NSString* AVFoundationGlue::AVMediaTypeMuxed() { 157 NSString* AVFoundationGlue::AVMediaTypeMuxed() {
94 return media::ReadNSStringPtr("AVMediaTypeMuxed"); 158 return g_avfoundation_handle.Get().AVMediaTypeMuxed();
95 } 159 }
96 160
97 NSString* AVFoundationGlue::AVCaptureSessionRuntimeErrorNotification() { 161 NSString* AVFoundationGlue::AVCaptureSessionRuntimeErrorNotification() {
98 return media::ReadNSStringPtr("AVCaptureSessionRuntimeErrorNotification"); 162 return g_avfoundation_handle.Get().AVCaptureSessionRuntimeErrorNotification();
99 } 163 }
100 164
101 NSString* AVFoundationGlue::AVCaptureSessionDidStopRunningNotification() { 165 NSString* AVFoundationGlue::AVCaptureSessionDidStopRunningNotification() {
102 return media::ReadNSStringPtr("AVCaptureSessionDidStopRunningNotification"); 166 return
167 g_avfoundation_handle.Get().AVCaptureSessionDidStopRunningNotification();
103 } 168 }
104 169
105 NSString* AVFoundationGlue::AVCaptureSessionErrorKey() { 170 NSString* AVFoundationGlue::AVCaptureSessionErrorKey() {
106 return media::ReadNSStringPtr("AVCaptureSessionErrorKey"); 171 return g_avfoundation_handle.Get().AVCaptureSessionErrorKey();
107 } 172 }
108 173
109 NSString* AVFoundationGlue::AVCaptureSessionPreset320x240() { 174 NSString* AVFoundationGlue::AVCaptureSessionPreset320x240() {
110 return media::ReadNSStringPtr("AVCaptureSessionPreset320x240"); 175 return g_avfoundation_handle.Get().AVCaptureSessionPreset320x240();
111 } 176 }
112 177
113 NSString* AVFoundationGlue::AVCaptureSessionPreset640x480() { 178 NSString* AVFoundationGlue::AVCaptureSessionPreset640x480() {
114 return media::ReadNSStringPtr("AVCaptureSessionPreset640x480"); 179 return g_avfoundation_handle.Get().AVCaptureSessionPreset640x480();
115 } 180 }
116 181
117 NSString* AVFoundationGlue::AVCaptureSessionPreset1280x720() { 182 NSString* AVFoundationGlue::AVCaptureSessionPreset1280x720() {
118 return media::ReadNSStringPtr("AVCaptureSessionPreset1280x720"); 183 return g_avfoundation_handle.Get().AVCaptureSessionPreset1280x720();
119 } 184 }
120 185
121 NSString* AVFoundationGlue::AVVideoScalingModeKey() { 186 NSString* AVFoundationGlue::AVVideoScalingModeKey() {
122 return media::ReadNSStringPtr("AVVideoScalingModeKey"); 187 return g_avfoundation_handle.Get().AVVideoScalingModeKey();
123 } 188 }
124 189
125 NSString* AVFoundationGlue::AVVideoScalingModeResizeAspect() { 190 NSString* AVFoundationGlue::AVVideoScalingModeResizeAspect() {
126 return media::ReadNSStringPtr("AVVideoScalingModeResizeAspect"); 191 return g_avfoundation_handle.Get().AVVideoScalingModeResizeAspect();
127 } 192 }
128 193
129 Class AVFoundationGlue::AVCaptureSessionClass() { 194 Class AVFoundationGlue::AVCaptureSessionClass() {
130 return [AVFoundationBundle() classNamed:@"AVCaptureSession"]; 195 return [AVFoundationBundle() classNamed:@"AVCaptureSession"];
131 } 196 }
132 197
133 Class AVFoundationGlue::AVCaptureVideoDataOutputClass() { 198 Class AVFoundationGlue::AVCaptureVideoDataOutputClass() {
134 return [AVFoundationBundle() classNamed:@"AVCaptureVideoDataOutput"]; 199 return [AVFoundationBundle() classNamed:@"AVCaptureVideoDataOutput"];
135 } 200 }
136 201
(...skipping 20 matching lines...) Expand all
157 @implementation AVCaptureDeviceInputGlue 222 @implementation AVCaptureDeviceInputGlue
158 223
159 + (CrAVCaptureDeviceInput*)deviceInputWithDevice:(CrAVCaptureDevice*)device 224 + (CrAVCaptureDeviceInput*)deviceInputWithDevice:(CrAVCaptureDevice*)device
160 error:(NSError**)outError { 225 error:(NSError**)outError {
161 return [[AVFoundationGlue::AVFoundationBundle() 226 return [[AVFoundationGlue::AVFoundationBundle()
162 classNamed:@"AVCaptureDeviceInput"] deviceInputWithDevice:device 227 classNamed:@"AVCaptureDeviceInput"] deviceInputWithDevice:device
163 error:outError]; 228 error:outError];
164 } 229 }
165 230
166 @end // @implementation AVCaptureDeviceInputGlue 231 @end // @implementation AVCaptureDeviceInputGlue
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698