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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 208 |
218 void VideoCaptureDeviceAndroid::OnPhotoTaken( | 209 void VideoCaptureDeviceAndroid::OnPhotoTaken( |
219 JNIEnv* env, | 210 JNIEnv* env, |
220 const base::android::JavaParamRef<jobject>& obj, | 211 const base::android::JavaParamRef<jobject>& obj, |
221 jlong callback_id, | 212 jlong callback_id, |
222 const base::android::JavaParamRef<jbyteArray>& data) { | 213 const base::android::JavaParamRef<jbyteArray>& data) { |
223 DCHECK(callback_id); | 214 DCHECK(callback_id); |
224 | 215 |
225 base::AutoLock lock(photo_callbacks_lock_); | 216 base::AutoLock lock(photo_callbacks_lock_); |
226 | 217 |
227 TakePhotoCallback* const cb = | 218 ScopedCallback<TakePhotoCallback>* const cb = |
228 reinterpret_cast<TakePhotoCallback*>(callback_id); | 219 reinterpret_cast<ScopedCallback<TakePhotoCallback>*>(callback_id); |
229 // Search for the pointer |cb| in the list of |photo_callbacks_|. | 220 // Search for the pointer |cb| in the list of |photo_callbacks_|. |
230 const auto reference_it = | 221 const auto reference_it = std::find_if( |
231 std::find_if(photo_callbacks_.begin(), photo_callbacks_.end(), | 222 photo_callbacks_.begin(), photo_callbacks_.end(), |
232 [cb](const std::unique_ptr<TakePhotoCallback>& callback) { | 223 [cb](const std::unique_ptr<ScopedCallback<TakePhotoCallback>>& callback) { |
233 return callback.get() == cb; | 224 return callback.get() == cb; |
234 }); | 225 }); |
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->PassCallback().Run(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 Loading... |
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 |
OLD | NEW |