| 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" |
| 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/photo_capabilities.h" | 15 #include "media/capture/video/android/photo_capabilities.h" |
| 16 #include "media/capture/video/android/video_capture_device_factory_android.h" | 16 #include "media/capture/video/android/video_capture_device_factory_android.h" |
| 17 #include "third_party/libyuv/include/libyuv.h" |
| 17 | 18 |
| 18 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
| 19 using base::android::CheckException; | 20 using base::android::CheckException; |
| 20 using base::android::GetClass; | 21 using base::android::GetClass; |
| 21 using base::android::MethodID; | 22 using base::android::MethodID; |
| 22 using base::android::JavaRef; | 23 using base::android::JavaRef; |
| 23 using base::android::ScopedJavaLocalRef; | 24 using base::android::ScopedJavaLocalRef; |
| 24 | 25 |
| 25 namespace media { | 26 namespace media { |
| 26 | 27 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 if (state_ != kCapturing || !client_.get()) | 202 if (state_ != kCapturing || !client_.get()) |
| 202 return; | 203 return; |
| 203 | 204 |
| 204 jbyte* buffer = env->GetByteArrayElements(data, NULL); | 205 jbyte* buffer = env->GetByteArrayElements(data, NULL); |
| 205 if (!buffer) { | 206 if (!buffer) { |
| 206 LOG(ERROR) << "VideoCaptureDeviceAndroid::OnFrameAvailable: " | 207 LOG(ERROR) << "VideoCaptureDeviceAndroid::OnFrameAvailable: " |
| 207 "failed to GetByteArrayElements"; | 208 "failed to GetByteArrayElements"; |
| 208 return; | 209 return; |
| 209 } | 210 } |
| 210 | 211 |
| 211 base::TimeTicks current_time = base::TimeTicks::Now(); | 212 const base::TimeTicks current_time = base::TimeTicks::Now(); |
| 212 if (!got_first_frame_) { | 213 if (!got_first_frame_) { |
| 213 // Set aside one frame allowance for fluctuation. | 214 // Set aside one frame allowance for fluctuation. |
| 214 expected_next_frame_time_ = current_time - frame_interval_; | 215 expected_next_frame_time_ = current_time - frame_interval_; |
| 215 first_ref_time_ = current_time; | 216 first_ref_time_ = current_time; |
| 216 got_first_frame_ = true; | 217 got_first_frame_ = true; |
| 217 } | 218 } |
| 218 | 219 |
| 219 // Deliver the frame when it doesn't arrive too early. | 220 // Deliver the frame when it doesn't arrive too early. |
| 220 if (expected_next_frame_time_ <= current_time) { | 221 if (expected_next_frame_time_ <= current_time) { |
| 221 expected_next_frame_time_ += frame_interval_; | 222 expected_next_frame_time_ += frame_interval_; |
| 222 | 223 |
| 223 // TODO(qiangchen): Investigate how to get raw timestamp for Android, | 224 // TODO(qiangchen): Investigate how to get raw timestamp for Android, |
| 224 // rather than using reference time to calculate timestamp. | 225 // rather than using reference time to calculate timestamp. |
| 225 client_->OnIncomingCapturedData(reinterpret_cast<uint8_t*>(buffer), length, | 226 client_->OnIncomingCapturedData(reinterpret_cast<uint8_t*>(buffer), length, |
| 226 capture_format_, rotation, current_time, | 227 capture_format_, rotation, current_time, |
| 227 current_time - first_ref_time_); | 228 current_time - first_ref_time_); |
| 228 } | 229 } |
| 229 | 230 |
| 230 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); | 231 env->ReleaseByteArrayElements(data, buffer, JNI_ABORT); |
| 231 } | 232 } |
| 232 | 233 |
| 234 void VideoCaptureDeviceAndroid::OnI420FrameAvailable(JNIEnv* env, |
| 235 jobject obj, |
| 236 jobject y_buffer, |
| 237 jint y_stride, |
| 238 jobject u_buffer, |
| 239 jobject v_buffer, |
| 240 jint uv_row_stride, |
| 241 jint uv_pixel_stride, |
| 242 jint width, |
| 243 jint height, |
| 244 jint rotation) { |
| 245 const base::TimeTicks current_time = base::TimeTicks::Now(); |
| 246 if (!got_first_frame_) { |
| 247 // Set aside one frame allowance for fluctuation. |
| 248 expected_next_frame_time_ = current_time - frame_interval_; |
| 249 first_ref_time_ = current_time; |
| 250 got_first_frame_ = true; |
| 251 } |
| 252 |
| 253 uint8_t* const y_src = |
| 254 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(y_buffer)); |
| 255 CHECK(y_src); |
| 256 uint8_t* const u_src = |
| 257 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(u_buffer)); |
| 258 CHECK(u_src); |
| 259 uint8_t* const v_src = |
| 260 reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(v_buffer)); |
| 261 CHECK(v_src); |
| 262 |
| 263 const int y_plane_length = width * height; |
| 264 const int uv_plane_length = y_plane_length / 4; |
| 265 const int buffer_length = y_plane_length + uv_plane_length * 2; |
| 266 std::unique_ptr<uint8_t> buffer(new uint8_t[buffer_length]); |
| 267 |
| 268 libyuv::Android420ToI420(y_src, y_stride, u_src, uv_row_stride, v_src, |
| 269 uv_row_stride, uv_pixel_stride, buffer.get(), width, |
| 270 buffer.get() + y_plane_length, width / 2, |
| 271 buffer.get() + y_plane_length + uv_plane_length, |
| 272 width / 2, width, height); |
| 273 |
| 274 // Deliver the frame when it doesn't arrive too early. |
| 275 if (expected_next_frame_time_ <= current_time) { |
| 276 expected_next_frame_time_ += frame_interval_; |
| 277 |
| 278 // TODO(qiangchen): Investigate how to get raw timestamp for Android, |
| 279 // rather than using reference time to calculate timestamp. |
| 280 client_->OnIncomingCapturedData(buffer.get(), buffer_length, |
| 281 capture_format_, rotation, current_time, |
| 282 current_time - first_ref_time_); |
| 283 } |
| 284 } |
| 285 |
| 233 void VideoCaptureDeviceAndroid::OnError(JNIEnv* env, | 286 void VideoCaptureDeviceAndroid::OnError(JNIEnv* env, |
| 234 const JavaParamRef<jobject>& obj, | 287 const JavaParamRef<jobject>& obj, |
| 235 const JavaParamRef<jstring>& message) { | 288 const JavaParamRef<jstring>& message) { |
| 236 SetErrorState(FROM_HERE, | 289 SetErrorState(FROM_HERE, |
| 237 base::android::ConvertJavaStringToUTF8(env, message)); | 290 base::android::ConvertJavaStringToUTF8(env, message)); |
| 238 } | 291 } |
| 239 | 292 |
| 240 void VideoCaptureDeviceAndroid::OnPhotoTaken( | 293 void VideoCaptureDeviceAndroid::OnPhotoTaken( |
| 241 JNIEnv* env, | 294 JNIEnv* env, |
| 242 const base::android::JavaParamRef<jobject>& obj, | 295 const base::android::JavaParamRef<jobject>& obj, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 const tracked_objects::Location& from_here, | 341 const tracked_objects::Location& from_here, |
| 289 const std::string& reason) { | 342 const std::string& reason) { |
| 290 { | 343 { |
| 291 base::AutoLock lock(lock_); | 344 base::AutoLock lock(lock_); |
| 292 state_ = kError; | 345 state_ = kError; |
| 293 } | 346 } |
| 294 client_->OnError(from_here, reason); | 347 client_->OnError(from_here, reason); |
| 295 } | 348 } |
| 296 | 349 |
| 297 } // namespace media | 350 } // namespace media |
| OLD | NEW |