| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_CAPTURE_VIDEO_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_ | |
| 6 #define MEDIA_CAPTURE_VIDEO_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_ | |
| 7 | |
| 8 #include <jni.h> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/android/scoped_java_ref.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/synchronization/lock.h" | |
| 14 #include "base/threading/thread.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "media/capture/capture_export.h" | |
| 17 #include "media/capture/video/video_capture_device.h" | |
| 18 | |
| 19 namespace tracked_objects { | |
| 20 class Location; | |
| 21 } // namespace tracked_ | |
| 22 | |
| 23 namespace media { | |
| 24 | |
| 25 // VideoCaptureDevice on Android. The VideoCaptureDevice API's are called | |
| 26 // by VideoCaptureManager on its own thread, while OnFrameAvailable is called | |
| 27 // on JAVA thread (i.e., UI thread). Both will access |state_| and |client_|, | |
| 28 // but only VideoCaptureManager would change their value. | |
| 29 class CAPTURE_EXPORT VideoCaptureDeviceAndroid : public VideoCaptureDevice { | |
| 30 public: | |
| 31 // Automatically generated enum to interface with Java world. | |
| 32 // | |
| 33 // A Java counterpart will be generated for this enum. | |
| 34 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.media | |
| 35 enum AndroidImageFormat { | |
| 36 // Android graphics ImageFormat mapping, see reference in: | |
| 37 // http://developer.android.com/reference/android/graphics/ImageFormat.html | |
| 38 ANDROID_IMAGE_FORMAT_NV21 = 17, | |
| 39 ANDROID_IMAGE_FORMAT_YUV_420_888 = 35, | |
| 40 ANDROID_IMAGE_FORMAT_YV12 = 842094169, | |
| 41 ANDROID_IMAGE_FORMAT_UNKNOWN = 0, | |
| 42 }; | |
| 43 | |
| 44 explicit VideoCaptureDeviceAndroid(const Name& device_name); | |
| 45 ~VideoCaptureDeviceAndroid() override; | |
| 46 | |
| 47 static VideoCaptureDevice* Create(const Name& device_name); | |
| 48 static bool RegisterVideoCaptureDevice(JNIEnv* env); | |
| 49 | |
| 50 // Registers the Java VideoCaptureDevice pointer, used by the rest of the | |
| 51 // methods of the class to operate the Java capture code. This method must be | |
| 52 // called after the class constructor and before AllocateAndStart(). | |
| 53 bool Init(); | |
| 54 | |
| 55 // VideoCaptureDevice implementation. | |
| 56 void AllocateAndStart(const VideoCaptureParams& params, | |
| 57 std::unique_ptr<Client> client) override; | |
| 58 void StopAndDeAllocate() override; | |
| 59 void GetPhotoCapabilities(GetPhotoCapabilitiesCallback callback) override; | |
| 60 void SetPhotoOptions(mojom::PhotoSettingsPtr settings, | |
| 61 SetPhotoOptionsCallback callback) override; | |
| 62 void TakePhoto(TakePhotoCallback callback) override; | |
| 63 | |
| 64 // Implement org.chromium.media.VideoCapture.nativeOnFrameAvailable. | |
| 65 void OnFrameAvailable(JNIEnv* env, | |
| 66 const base::android::JavaParamRef<jobject>& obj, | |
| 67 const base::android::JavaParamRef<jbyteArray>& data, | |
| 68 jint length, | |
| 69 jint rotation); | |
| 70 | |
| 71 // Implement org.chromium.media.VideoCapture.nativeOnError. | |
| 72 void OnError(JNIEnv* env, | |
| 73 const base::android::JavaParamRef<jobject>& obj, | |
| 74 const base::android::JavaParamRef<jstring>& message); | |
| 75 | |
| 76 // Implement org.chromium.media.VideoCapture.nativeOnPhotoTaken. | |
| 77 void OnPhotoTaken(JNIEnv* env, | |
| 78 const base::android::JavaParamRef<jobject>& obj, | |
| 79 jlong callback_id, | |
| 80 const base::android::JavaParamRef<jbyteArray>& data); | |
| 81 | |
| 82 private: | |
| 83 enum InternalState { | |
| 84 kIdle, // The device is opened but not in use. | |
| 85 kCapturing, // Video is being captured. | |
| 86 kError // Hit error. User needs to recover by destroying the object. | |
| 87 }; | |
| 88 | |
| 89 VideoPixelFormat GetColorspace(); | |
| 90 void SetErrorState(const tracked_objects::Location& from_here, | |
| 91 const std::string& reason); | |
| 92 | |
| 93 // Prevent racing on accessing |state_| and |client_| since both could be | |
| 94 // accessed from different threads. | |
| 95 base::Lock lock_; | |
| 96 InternalState state_; | |
| 97 bool got_first_frame_; | |
| 98 base::TimeTicks expected_next_frame_time_; | |
| 99 base::TimeTicks first_ref_time_; | |
| 100 base::TimeDelta frame_interval_; | |
| 101 std::unique_ptr<VideoCaptureDevice::Client> client_; | |
| 102 | |
| 103 // List of |photo_callbacks_| in flight, being served in Java side. | |
| 104 base::Lock photo_callbacks_lock_; | |
| 105 std::list<std::unique_ptr<TakePhotoCallback>> photo_callbacks_; | |
| 106 | |
| 107 Name device_name_; | |
| 108 VideoCaptureFormat capture_format_; | |
| 109 | |
| 110 // Java VideoCaptureAndroid instance. | |
| 111 base::android::ScopedJavaLocalRef<jobject> j_capture_; | |
| 112 | |
| 113 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceAndroid); | |
| 114 }; | |
| 115 | |
| 116 } // namespace media | |
| 117 | |
| 118 #endif // MEDIA_CAPTURE_VIDEO_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_ | |
| OLD | NEW |