OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "media/capture/video/android/video_capture_device_android.h" | 5 #include "media/capture/video/android/video_capture_device_android.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 // an unknown device model. | 34 // an unknown device model. |
35 return ""; | 35 return ""; |
36 } | 36 } |
37 | 37 |
38 VideoCaptureDeviceAndroid::VideoCaptureDeviceAndroid(const Name& device_name) | 38 VideoCaptureDeviceAndroid::VideoCaptureDeviceAndroid(const Name& device_name) |
39 : state_(kIdle), got_first_frame_(false), device_name_(device_name) { | 39 : state_(kIdle), got_first_frame_(false), device_name_(device_name) { |
40 } | 40 } |
41 | 41 |
42 VideoCaptureDeviceAndroid::~VideoCaptureDeviceAndroid() { | 42 VideoCaptureDeviceAndroid::~VideoCaptureDeviceAndroid() { |
43 StopAndDeAllocate(); | 43 StopAndDeAllocate(); |
44 // If there are still |photo_callbacks_|, resolve them with empty datas. | |
45 { | |
46 base::AutoLock lock(photo_callbacks_lock_); | |
47 std::for_each(photo_callbacks_.begin(), photo_callbacks_.end(), | |
48 [](const std::unique_ptr<TakePhotoCallback>& callback) { | |
49 std::unique_ptr<std::vector<uint8_t>> empty_data( | |
50 new std::vector<uint8_t>()); | |
51 callback->Run("", std::move(empty_data)); | |
52 }); | |
53 } | |
54 } | 44 } |
55 | 45 |
56 bool VideoCaptureDeviceAndroid::Init() { | 46 bool VideoCaptureDeviceAndroid::Init() { |
57 int id; | 47 int id; |
58 if (!base::StringToInt(device_name_.id(), &id)) | 48 if (!base::StringToInt(device_name_.id(), &id)) |
59 return false; | 49 return false; |
60 | 50 |
61 j_capture_.Reset(VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid( | 51 j_capture_.Reset(VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid( |
62 id, reinterpret_cast<intptr_t>(this))); | 52 id, reinterpret_cast<intptr_t>(this))); |
63 return true; | 53 return true; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 128 |
139 { | 129 { |
140 base::AutoLock lock(lock_); | 130 base::AutoLock lock(lock_); |
141 state_ = kIdle; | 131 state_ = kIdle; |
142 client_.reset(); | 132 client_.reset(); |
143 } | 133 } |
144 | 134 |
145 Java_VideoCapture_deallocate(env, j_capture_.obj()); | 135 Java_VideoCapture_deallocate(env, j_capture_.obj()); |
146 } | 136 } |
147 | 137 |
148 bool VideoCaptureDeviceAndroid::TakePhoto(const TakePhotoCallback& callback) { | 138 void VideoCaptureDeviceAndroid::TakePhoto( |
| 139 ScopedCallback<TakePhotoCallback> callback) { |
149 { | 140 { |
150 base::AutoLock lock(lock_); | 141 base::AutoLock lock(lock_); |
151 if (state_ != kCapturing) | 142 if (state_ != kCapturing) |
152 return false; | 143 return; |
153 } | 144 } |
154 | 145 |
155 JNIEnv* env = AttachCurrentThread(); | 146 JNIEnv* env = AttachCurrentThread(); |
156 | 147 |
157 // Make copy on the heap so we can pass the pointer through JNI. | 148 // Make copy on the heap so we can pass the pointer through JNI. |
158 std::unique_ptr<TakePhotoCallback> cb(new TakePhotoCallback(callback)); | 149 std::unique_ptr<ScopedCallback<TakePhotoCallback>> heap_callback( |
159 const intptr_t callback_id = reinterpret_cast<intptr_t>(cb.get()); | 150 new ScopedCallback<TakePhotoCallback>(std::move(callback))); |
| 151 const intptr_t callback_id = reinterpret_cast<intptr_t>(heap_callback.get()); |
160 if (!Java_VideoCapture_takePhoto(env, j_capture_.obj(), callback_id)) | 152 if (!Java_VideoCapture_takePhoto(env, j_capture_.obj(), callback_id)) |
161 return false; | 153 return; |
162 | 154 |
163 { | 155 { |
164 base::AutoLock lock(photo_callbacks_lock_); | 156 base::AutoLock lock(photo_callbacks_lock_); |
165 photo_callbacks_.push_back(std::move(cb)); | 157 photo_callbacks_.push_back(std::move(heap_callback)); |
166 } | 158 } |
167 return true; | |
168 } | 159 } |
169 | 160 |
170 void VideoCaptureDeviceAndroid::OnFrameAvailable( | 161 void VideoCaptureDeviceAndroid::OnFrameAvailable( |
171 JNIEnv* env, | 162 JNIEnv* env, |
172 const JavaParamRef<jobject>& obj, | 163 const JavaParamRef<jobject>& obj, |
173 const JavaParamRef<jbyteArray>& data, | 164 const JavaParamRef<jbyteArray>& data, |
174 jint length, | 165 jint length, |
175 jint rotation) { | 166 jint rotation) { |
176 DVLOG(3) << "VideoCaptureDeviceAndroid::OnFrameAvailable: length =" << length; | 167 DVLOG(3) << "VideoCaptureDeviceAndroid::OnFrameAvailable: length =" << length; |
177 | 168 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 205 |
215 void VideoCaptureDeviceAndroid::OnPhotoTaken( | 206 void VideoCaptureDeviceAndroid::OnPhotoTaken( |
216 JNIEnv* env, | 207 JNIEnv* env, |
217 const base::android::JavaParamRef<jobject>& obj, | 208 const base::android::JavaParamRef<jobject>& obj, |
218 jlong callback_id, | 209 jlong callback_id, |
219 const base::android::JavaParamRef<jbyteArray>& data) { | 210 const base::android::JavaParamRef<jbyteArray>& data) { |
220 DCHECK(callback_id); | 211 DCHECK(callback_id); |
221 | 212 |
222 base::AutoLock lock(photo_callbacks_lock_); | 213 base::AutoLock lock(photo_callbacks_lock_); |
223 | 214 |
224 TakePhotoCallback* const cb = | 215 ScopedCallback<TakePhotoCallback>* const cb = |
225 reinterpret_cast<TakePhotoCallback*>(callback_id); | 216 reinterpret_cast<ScopedCallback<TakePhotoCallback>*>(callback_id); |
226 // Search for the pointer |cb| in the list of |photo_callbacks_|. | 217 // Search for the pointer |cb| in the list of |photo_callbacks_|. |
227 const auto reference_it = | 218 const auto reference_it = std::find_if( |
228 std::find_if(photo_callbacks_.begin(), photo_callbacks_.end(), | 219 photo_callbacks_.begin(), photo_callbacks_.end(), |
229 [cb](const std::unique_ptr<TakePhotoCallback>& callback) { | 220 [cb](const std::unique_ptr<ScopedCallback<TakePhotoCallback>>& callback) { |
230 return callback.get() == cb; | 221 return callback.get() == cb; |
231 }); | 222 }); |
232 if (reference_it == photo_callbacks_.end()) { | 223 if (reference_it == photo_callbacks_.end()) { |
233 NOTREACHED() << "|callback_id| not found."; | 224 NOTREACHED() << "|callback_id| not found."; |
234 return; | 225 return; |
235 } | 226 } |
236 | 227 |
237 std::unique_ptr<std::vector<uint8_t>> native_data(new std::vector<uint8_t>()); | 228 std::vector<uint8_t> native_data; |
238 base::android::JavaByteArrayToByteVector(env, data.obj(), native_data.get()); | 229 base::android::JavaByteArrayToByteVector(env, data.obj(), &native_data); |
239 | 230 |
240 cb->Run(native_data->size() ? "image/jpeg" : "", std::move(native_data)); | 231 cb->PassCallback().Run(native_data.empty() ? "" : "image/jpeg", |
| 232 mojo::Array<uint8_t>::From(native_data)); |
241 | 233 |
242 photo_callbacks_.erase(reference_it); | 234 photo_callbacks_.erase(reference_it); |
243 } | 235 } |
244 | 236 |
245 VideoPixelFormat VideoCaptureDeviceAndroid::GetColorspace() { | 237 VideoPixelFormat VideoCaptureDeviceAndroid::GetColorspace() { |
246 JNIEnv* env = AttachCurrentThread(); | 238 JNIEnv* env = AttachCurrentThread(); |
247 const int current_capture_colorspace = | 239 const int current_capture_colorspace = |
248 Java_VideoCapture_getColorspace(env, j_capture_.obj()); | 240 Java_VideoCapture_getColorspace(env, j_capture_.obj()); |
249 switch (current_capture_colorspace) { | 241 switch (current_capture_colorspace) { |
250 case ANDROID_IMAGE_FORMAT_YV12: | 242 case ANDROID_IMAGE_FORMAT_YV12: |
(...skipping 12 matching lines...) Expand all Loading... |
263 const tracked_objects::Location& from_here, | 255 const tracked_objects::Location& from_here, |
264 const std::string& reason) { | 256 const std::string& reason) { |
265 { | 257 { |
266 base::AutoLock lock(lock_); | 258 base::AutoLock lock(lock_); |
267 state_ = kError; | 259 state_ = kError; |
268 } | 260 } |
269 client_->OnError(from_here, reason); | 261 client_->OnError(from_here, reason); |
270 } | 262 } |
271 | 263 |
272 } // namespace media | 264 } // namespace media |
OLD | NEW |