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

Side by Side Diff: media/capture/video/android/video_capture_device_android.cc

Issue 2005753006: ImageCapture: ScopedResultCallback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rockot@s nit Created 4 years, 6 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
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"
11 #include "base/android/jni_array.h" 11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "jni/VideoCapture_jni.h" 14 #include "jni/VideoCapture_jni.h"
15 #include "media/capture/video/android/video_capture_device_factory_android.h" 15 #include "media/capture/video/android/video_capture_device_factory_android.h"
16 #include "mojo/public/cpp/bindings/string.h"
16 17
17 using base::android::AttachCurrentThread; 18 using base::android::AttachCurrentThread;
18 using base::android::CheckException; 19 using base::android::CheckException;
19 using base::android::GetClass; 20 using base::android::GetClass;
20 using base::android::MethodID; 21 using base::android::MethodID;
21 using base::android::JavaRef; 22 using base::android::JavaRef;
22 using base::android::ScopedJavaLocalRef; 23 using base::android::ScopedJavaLocalRef;
23 24
24 namespace media { 25 namespace media {
25 26
26 // static 27 // static
27 bool VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(JNIEnv* env) { 28 bool VideoCaptureDeviceAndroid::RegisterVideoCaptureDevice(JNIEnv* env) {
28 return RegisterNativesImpl(env); 29 return RegisterNativesImpl(env);
29 } 30 }
30 31
31 const std::string VideoCaptureDevice::Name::GetModel() const { 32 const std::string VideoCaptureDevice::Name::GetModel() const {
32 // Android cameras are not typically USB devices, and this method is currently 33 // Android cameras are not typically USB devices, and this method is currently
33 // only used for USB model identifiers, so this implementation just indicates 34 // only used for USB model identifiers, so this implementation just indicates
34 // an unknown device model. 35 // an unknown device model.
35 return ""; 36 return "";
36 } 37 }
37 38
38 VideoCaptureDeviceAndroid::VideoCaptureDeviceAndroid(const Name& device_name) 39 VideoCaptureDeviceAndroid::VideoCaptureDeviceAndroid(const Name& device_name)
39 : state_(kIdle), got_first_frame_(false), device_name_(device_name) { 40 : state_(kIdle), got_first_frame_(false), device_name_(device_name) {
40 } 41 }
41 42
42 VideoCaptureDeviceAndroid::~VideoCaptureDeviceAndroid() { 43 VideoCaptureDeviceAndroid::~VideoCaptureDeviceAndroid() {
43 StopAndDeAllocate(); 44 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 } 45 }
55 46
56 bool VideoCaptureDeviceAndroid::Init() { 47 bool VideoCaptureDeviceAndroid::Init() {
57 int id; 48 int id;
58 if (!base::StringToInt(device_name_.id(), &id)) 49 if (!base::StringToInt(device_name_.id(), &id))
59 return false; 50 return false;
60 51
61 j_capture_.Reset(VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid( 52 j_capture_.Reset(VideoCaptureDeviceFactoryAndroid::createVideoCaptureAndroid(
62 id, reinterpret_cast<intptr_t>(this))); 53 id, reinterpret_cast<intptr_t>(this)));
63 return true; 54 return true;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 129
139 { 130 {
140 base::AutoLock lock(lock_); 131 base::AutoLock lock(lock_);
141 state_ = kIdle; 132 state_ = kIdle;
142 client_.reset(); 133 client_.reset();
143 } 134 }
144 135
145 Java_VideoCapture_deallocate(env, j_capture_.obj()); 136 Java_VideoCapture_deallocate(env, j_capture_.obj());
146 } 137 }
147 138
148 bool VideoCaptureDeviceAndroid::TakePhoto(const TakePhotoCallback& callback) { 139 void VideoCaptureDeviceAndroid::TakePhoto(
140 ScopedResultCallback<TakePhotoCallback> callback) {
149 { 141 {
150 base::AutoLock lock(lock_); 142 base::AutoLock lock(lock_);
151 if (state_ != kCapturing) 143 if (state_ != kCapturing)
152 return false; 144 return;
153 } 145 }
154 146
155 JNIEnv* env = AttachCurrentThread(); 147 JNIEnv* env = AttachCurrentThread();
156 148
157 // Make copy on the heap so we can pass the pointer through JNI. 149 // Make copy on the heap so we can pass the pointer through JNI.
158 std::unique_ptr<TakePhotoCallback> cb(new TakePhotoCallback(callback)); 150 std::unique_ptr<ScopedResultCallback<TakePhotoCallback>> heap_callback(
159 const intptr_t callback_id = reinterpret_cast<intptr_t>(cb.get()); 151 new ScopedResultCallback<TakePhotoCallback>(std::move(callback)));
152 const intptr_t callback_id = reinterpret_cast<intptr_t>(heap_callback.get());
160 if (!Java_VideoCapture_takePhoto(env, j_capture_.obj(), callback_id)) 153 if (!Java_VideoCapture_takePhoto(env, j_capture_.obj(), callback_id))
161 return false; 154 return;
162 155
163 { 156 {
164 base::AutoLock lock(photo_callbacks_lock_); 157 base::AutoLock lock(photo_callbacks_lock_);
165 photo_callbacks_.push_back(std::move(cb)); 158 photo_callbacks_.push_back(std::move(heap_callback));
166 } 159 }
167 return true;
168 } 160 }
169 161
170 void VideoCaptureDeviceAndroid::OnFrameAvailable( 162 void VideoCaptureDeviceAndroid::OnFrameAvailable(
171 JNIEnv* env, 163 JNIEnv* env,
172 const JavaParamRef<jobject>& obj, 164 const JavaParamRef<jobject>& obj,
173 const JavaParamRef<jbyteArray>& data, 165 const JavaParamRef<jbyteArray>& data,
174 jint length, 166 jint length,
175 jint rotation) { 167 jint rotation) {
176 DVLOG(3) << "VideoCaptureDeviceAndroid::OnFrameAvailable: length =" << length; 168 DVLOG(3) << "VideoCaptureDeviceAndroid::OnFrameAvailable: length =" << length;
177 169
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 209
218 void VideoCaptureDeviceAndroid::OnPhotoTaken( 210 void VideoCaptureDeviceAndroid::OnPhotoTaken(
219 JNIEnv* env, 211 JNIEnv* env,
220 const base::android::JavaParamRef<jobject>& obj, 212 const base::android::JavaParamRef<jobject>& obj,
221 jlong callback_id, 213 jlong callback_id,
222 const base::android::JavaParamRef<jbyteArray>& data) { 214 const base::android::JavaParamRef<jbyteArray>& data) {
223 DCHECK(callback_id); 215 DCHECK(callback_id);
224 216
225 base::AutoLock lock(photo_callbacks_lock_); 217 base::AutoLock lock(photo_callbacks_lock_);
226 218
227 TakePhotoCallback* const cb = 219 ScopedResultCallback<TakePhotoCallback>* const cb =
228 reinterpret_cast<TakePhotoCallback*>(callback_id); 220 reinterpret_cast<ScopedResultCallback<TakePhotoCallback>*>(callback_id);
229 // Search for the pointer |cb| in the list of |photo_callbacks_|. 221 // Search for the pointer |cb| in the list of |photo_callbacks_|.
230 const auto reference_it = 222 const auto reference_it = std::find_if(
231 std::find_if(photo_callbacks_.begin(), photo_callbacks_.end(), 223 photo_callbacks_.begin(), photo_callbacks_.end(),
232 [cb](const std::unique_ptr<TakePhotoCallback>& callback) { 224 [cb](const std::unique_ptr<ScopedResultCallback<TakePhotoCallback>>&
233 return callback.get() == cb; 225 callback) { return callback.get() == cb; });
234 });
235 if (reference_it == photo_callbacks_.end()) { 226 if (reference_it == photo_callbacks_.end()) {
236 NOTREACHED() << "|callback_id| not found."; 227 NOTREACHED() << "|callback_id| not found.";
237 return; 228 return;
238 } 229 }
239 230
240 std::unique_ptr<std::vector<uint8_t>> native_data(new std::vector<uint8_t>()); 231 std::vector<uint8_t> native_data;
241 base::android::JavaByteArrayToByteVector(env, data.obj(), native_data.get()); 232 base::android::JavaByteArrayToByteVector(env, data.obj(), &native_data);
242 233
243 cb->Run(native_data->size() ? "image/jpeg" : "", std::move(native_data)); 234 cb->Run(mojo::String::From(native_data.empty() ? "" : "image/jpeg"),
235 mojo::Array<uint8_t>::From(native_data));
244 236
245 photo_callbacks_.erase(reference_it); 237 photo_callbacks_.erase(reference_it);
246 } 238 }
247 239
248 VideoPixelFormat VideoCaptureDeviceAndroid::GetColorspace() { 240 VideoPixelFormat VideoCaptureDeviceAndroid::GetColorspace() {
249 JNIEnv* env = AttachCurrentThread(); 241 JNIEnv* env = AttachCurrentThread();
250 const int current_capture_colorspace = 242 const int current_capture_colorspace =
251 Java_VideoCapture_getColorspace(env, j_capture_.obj()); 243 Java_VideoCapture_getColorspace(env, j_capture_.obj());
252 switch (current_capture_colorspace) { 244 switch (current_capture_colorspace) {
253 case ANDROID_IMAGE_FORMAT_YV12: 245 case ANDROID_IMAGE_FORMAT_YV12:
(...skipping 12 matching lines...) Expand all
266 const tracked_objects::Location& from_here, 258 const tracked_objects::Location& from_here,
267 const std::string& reason) { 259 const std::string& reason) {
268 { 260 {
269 base::AutoLock lock(lock_); 261 base::AutoLock lock(lock_);
270 state_ = kError; 262 state_ = kError;
271 } 263 }
272 client_->OnError(from_here, reason); 264 client_->OnError(from_here, reason);
273 } 265 }
274 266
275 } // namespace media 267 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/android/video_capture_device_android.h ('k') | media/capture/video/fake_video_capture_device.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698