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

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

Issue 10905134: use lock to make sure device will not deliver any frame after it's stopped. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 3 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 | « media/video/capture/mac/video_capture_device_mac.h ('k') | 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "media/video/capture/mac/video_capture_device_mac.h" 5 #include "media/video/capture/mac/video_capture_device_mac.h"
6 6
7 #import <QTKit/QTKit.h> 7 #import <QTKit/QTKit.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/time.h" 10 #include "base/time.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 state_(kNotInitialized), 42 state_(kNotInitialized),
43 capture_device_(nil) { 43 capture_device_(nil) {
44 } 44 }
45 45
46 VideoCaptureDeviceMac::~VideoCaptureDeviceMac() { 46 VideoCaptureDeviceMac::~VideoCaptureDeviceMac() {
47 [capture_device_ release]; 47 [capture_device_ release];
48 } 48 }
49 49
50 void VideoCaptureDeviceMac::Allocate(int width, int height, int frame_rate, 50 void VideoCaptureDeviceMac::Allocate(int width, int height, int frame_rate,
51 EventHandler* observer) { 51 EventHandler* observer) {
52 base::AutoLock auto_lock(lock_);
52 if (state_ != kIdle) { 53 if (state_ != kIdle) {
53 return; 54 return;
54 } 55 }
55 observer_ = observer; 56 observer_ = observer;
56 NSString* deviceId = 57 NSString* deviceId =
57 [NSString stringWithUTF8String:device_name_.unique_id.c_str()]; 58 [NSString stringWithUTF8String:device_name_.unique_id.c_str()];
58 59
59 if (![capture_device_ setCaptureDevice:deviceId]) { 60 if (![capture_device_ setCaptureDevice:deviceId]) {
60 SetErrorState("Could not open capture device."); 61 SetErrorState("Could not open capture device.");
61 return; 62 return;
(...skipping 16 matching lines...) Expand all
78 79
79 observer_->OnFrameInfo(current_settings); 80 observer_->OnFrameInfo(current_settings);
80 } 81 }
81 82
82 void VideoCaptureDeviceMac::Start() { 83 void VideoCaptureDeviceMac::Start() {
83 DCHECK_EQ(state_, kAllocated); 84 DCHECK_EQ(state_, kAllocated);
84 if (![capture_device_ startCapture]) { 85 if (![capture_device_ startCapture]) {
85 SetErrorState("Could not start capture device."); 86 SetErrorState("Could not start capture device.");
86 return; 87 return;
87 } 88 }
89 base::AutoLock auto_lock(lock_);
88 state_ = kCapturing; 90 state_ = kCapturing;
89 } 91 }
90 92
91 void VideoCaptureDeviceMac::Stop() { 93 void VideoCaptureDeviceMac::Stop() {
92 DCHECK_EQ(state_, kCapturing); 94 DCHECK_EQ(state_, kCapturing);
93 [capture_device_ stopCapture]; 95 [capture_device_ stopCapture];
96 base::AutoLock auto_lock(lock_);
94 state_ = kAllocated; 97 state_ = kAllocated;
95 } 98 }
96 99
97 void VideoCaptureDeviceMac::DeAllocate() { 100 void VideoCaptureDeviceMac::DeAllocate() {
101 base::AutoLock auto_lock(lock_);
98 if (state_ != kAllocated && state_ != kCapturing) { 102 if (state_ != kAllocated && state_ != kCapturing) {
99 return; 103 return;
100 } 104 }
101 if (state_ == kCapturing) { 105 if (state_ == kCapturing) {
102 [capture_device_ stopCapture]; 106 [capture_device_ stopCapture];
103 } 107 }
104 [capture_device_ setCaptureDevice:nil]; 108 [capture_device_ setCaptureDevice:nil];
105 state_ = kIdle; 109 state_ = kIdle;
106 } 110 }
107 111
108 const VideoCaptureDevice::Name& VideoCaptureDeviceMac::device_name() { 112 const VideoCaptureDevice::Name& VideoCaptureDeviceMac::device_name() {
109 return device_name_; 113 return device_name_;
110 } 114 }
111 115
112 bool VideoCaptureDeviceMac::Init() { 116 bool VideoCaptureDeviceMac::Init() {
113 DCHECK_EQ(state_, kNotInitialized); 117 DCHECK_EQ(state_, kNotInitialized);
114 118
115 Names device_names; 119 Names device_names;
116 GetDeviceNames(&device_names); 120 GetDeviceNames(&device_names);
117 for (Names::iterator it = device_names.begin(); 121 for (Names::iterator it = device_names.begin();
118 it != device_names.end(); 122 it != device_names.end();
119 ++it) { 123 ++it) {
120 if (device_name_.unique_id == it->unique_id) { 124 if (device_name_.unique_id == it->unique_id) {
121 capture_device_ = 125 capture_device_ =
122 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; 126 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this];
123 if (!capture_device_) { 127 if (!capture_device_) {
124 return false; 128 return false;
125 } 129 }
126 state_ = kIdle; 130 state_ = kIdle;
Ronghua Wu (Left Chromium) 2012/09/06 20:48:38 Do we need the lock here?
wjia(left Chromium) 2012/09/06 20:59:15 Init() is called only when the instance is created
127 return true; 131 return true;
128 } 132 }
129 } 133 }
130 return false; 134 return false;
131 } 135 }
132 136
133 void VideoCaptureDeviceMac::ReceiveFrame( 137 void VideoCaptureDeviceMac::ReceiveFrame(
134 const uint8* video_frame, 138 const uint8* video_frame,
135 int video_frame_length, 139 int video_frame_length,
136 const VideoCaptureCapability& frame_info) { 140 const VideoCaptureCapability& frame_info) {
137 observer_->OnIncomingCapturedFrame(video_frame, video_frame_length, 141 base::AutoLock auto_lock(lock_);
138 base::Time::Now()); 142 if (state_ == kCapturing) {
143 observer_->OnIncomingCapturedFrame(video_frame, video_frame_length,
144 base::Time::Now());
145 }
139 } 146 }
140 147
141 void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) { 148 void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) {
142 DLOG(ERROR) << reason; 149 DLOG(ERROR) << reason;
150 base::AutoLock auto_lock(lock_);
143 state_ = kError; 151 state_ = kError;
144 observer_->OnError(); 152 observer_->OnError();
145 } 153 }
146 154
147 } // namespace media 155 } // namespace media
OLDNEW
« no previous file with comments | « media/video/capture/mac/video_capture_device_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698