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

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

Issue 8177008: Adding VideoCaptureDevice for Mac. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Changes based on comments by dmac and scherkus. Created 9 years, 2 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
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "media/video/capture/mac/video_capture_device_mac.h"
6
7 #import <QTKit/QTKit.h>
8
9 #include "base/logging.h"
10 #include "base/time.h"
11 #include "media/video/capture/mac/video_capture_device_mac_qtkit.h"
12
13 namespace media {
14
15 void VideoCaptureDevice::GetDeviceNames(Names* device_names) {
16 // Loop through all available devices and add to |device_names|.
17 device_names->clear();
18 // TODO(mflodman) Return name and id as NSArray* instead of QTCaptureDevice*.
19 for (QTCaptureDevice* device in [VideoCaptureDeviceMacQTKit deviceNames]) {
20 Name name;
21 NSString* qt_device_name = [device localizedDisplayName];
22 name.device_name = [qt_device_name UTF8String];
23 NSString* qt_unique_id = [device uniqueID];
24 name.unique_id = [qt_unique_id UTF8String];
25 device_names->push_back(name);
26 }
27 }
28
29 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) {
30 VideoCaptureDeviceMac* capture_device =
31 new VideoCaptureDeviceMac(device_name);
32 if (!capture_device->Init()) {
33 LOG(ERROR) << "Could not initialize VideoCaptureDevice.";
34 delete capture_device;
35 return NULL;
36 }
37 return capture_device;
38 }
39
40 VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name)
41 : device_name_(device_name),
42 observer_(NULL),
43 state_(kNotInitialized),
44 capture_device_(NULL) {
45 }
46
47 VideoCaptureDeviceMac::~VideoCaptureDeviceMac() {
48 [capture_device_ release];
49 }
50
51 void VideoCaptureDeviceMac::Allocate(int width, int height, int frame_rate,
52 EventHandler* observer) {
53 if (state_ != kIdle) {
54 return;
55 }
56 observer_ = observer;
57 NSString* deviceId =
58 [NSString stringWithUTF8String:device_name_.unique_id.c_str()];
59
60 if (![capture_device_ setCaptureDevice:deviceId]) {
61 SetErrorState("Could not open capture device.");
62 return;
63 }
64 if ([capture_device_ setCaptureHeight:height:width:frame_rate] == NO) {
65 SetErrorState("Could not configure capture device.");
66 return;
67 }
68
69 state_ = kAllocated;
70 Capability current_settings;
71 current_settings.color = kARGB;
72 current_settings.width = width;
73 current_settings.height = height;
74 current_settings.frame_rate = frame_rate;
75
76 observer_->OnFrameInfo(current_settings);
77 }
78
79 void VideoCaptureDeviceMac::Start() {
80 DCHECK_EQ(state_, kAllocated);
81 if (![capture_device_ startCapture]) {
82 SetErrorState("Could not start capture device.");
83 return;
84 }
85 state_ = kCapturing;
86 }
87
88 void VideoCaptureDeviceMac::Stop() {
89 DCHECK_EQ(state_, kCapturing);
90 [capture_device_ stopCapture];
91 state_ = kAllocated;
92 }
93
94 void VideoCaptureDeviceMac::DeAllocate() {
95 if (state_ != kAllocated && state_ != kCapturing) {
96 return;
97 }
98 if (state_ == kCapturing) {
99 [capture_device_ stopCapture];
100 }
101 [capture_device_ setCaptureDevice:nil];
102 state_ = kIdle;
103 }
104
105 const VideoCaptureDevice::Name& VideoCaptureDeviceMac::device_name() {
106 return device_name_;
107 }
108
109 bool VideoCaptureDeviceMac::Init() {
110 DCHECK_EQ(state_, kNotInitialized);
111
112 Names device_names;
113 GetDeviceNames(&device_names);
114 for (Names::iterator it = device_names.begin();
115 it != device_names.end();
116 ++it) {
117 if (device_name_.unique_id == it->unique_id) {
118 capture_device_ = [[VideoCaptureDeviceMacQTKit alloc] init];
dmac 2011/10/11 00:25:47 ok, how about at the very least then: initWithFra
mflodman_chromium_OOO 2011/10/12 19:21:32 Sounds good, changed.
119 if (capture_device_ == NULL) {
dmac 2011/10/11 00:25:47 if (!capture_device_) or if (capture_device_ =
mflodman_chromium_OOO 2011/10/12 19:21:32 Done.
120 return false;
121 }
122 [capture_device_ setFrameReceiver:this];
123 state_ = kIdle;
124 return true;
125 }
126 }
127 return false;
128 }
129
130 void VideoCaptureDeviceMac::ReceiveFrame(const uint8* video_frame,
131 int video_frame_length,
132 const Capability& frame_info) {
133 observer_->OnIncomingCapturedFrame(video_frame, video_frame_length,
134 base::Time::Now());
135 }
136
137 void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) {
138 DLOG(ERROR) << reason;
139 state_ = kError;
140 observer_->OnError();
141 }
142
143 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698