Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / capture / video / android / video_capture_device_android.h
blobccab5e2a35259552d85c16d543c6dee79e54de82
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.
5 #ifndef MEDIA_VIDEO_CAPTURE_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_
6 #define MEDIA_VIDEO_CAPTURE_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_
8 #include <jni.h>
9 #include <string>
11 #include "base/android/scoped_java_ref.h"
12 #include "base/synchronization/lock.h"
13 #include "base/threading/thread.h"
14 #include "base/time/time.h"
15 #include "media/base/media_export.h"
16 #include "media/capture/video/video_capture_device.h"
18 namespace media {
20 // VideoCaptureDevice on Android. The VideoCaptureDevice API's are called
21 // by VideoCaptureManager on its own thread, while OnFrameAvailable is called
22 // on JAVA thread (i.e., UI thread). Both will access |state_| and |client_|,
23 // but only VideoCaptureManager would change their value.
24 class MEDIA_EXPORT VideoCaptureDeviceAndroid : public VideoCaptureDevice {
25 public:
26 // Automatically generated enum to interface with Java world.
28 // A Java counterpart will be generated for this enum.
29 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.media
30 enum AndroidImageFormat {
31 // Android graphics ImageFormat mapping, see reference in:
32 // http://developer.android.com/reference/android/graphics/ImageFormat.html
33 ANDROID_IMAGE_FORMAT_NV21 = 17,
34 ANDROID_IMAGE_FORMAT_YUV_420_888 = 35,
35 ANDROID_IMAGE_FORMAT_YV12 = 842094169,
36 ANDROID_IMAGE_FORMAT_UNKNOWN = 0,
39 explicit VideoCaptureDeviceAndroid(const Name& device_name);
40 ~VideoCaptureDeviceAndroid() override;
42 static VideoCaptureDevice* Create(const Name& device_name);
43 static bool RegisterVideoCaptureDevice(JNIEnv* env);
45 // Registers the Java VideoCaptureDevice pointer, used by the rest of the
46 // methods of the class to operate the Java capture code. This method must be
47 // called after the class constructor and before AllocateAndStart().
48 bool Init();
50 // VideoCaptureDevice implementation.
51 void AllocateAndStart(const VideoCaptureParams& params,
52 scoped_ptr<Client> client) override;
53 void StopAndDeAllocate() override;
55 // Implement org.chromium.media.VideoCapture.nativeOnFrameAvailable.
56 void OnFrameAvailable(JNIEnv* env,
57 jobject obj,
58 jbyteArray data,
59 jint length,
60 jint rotation);
62 // Implement org.chromium.media.VideoCapture.nativeOnError.
63 void OnError(JNIEnv* env, jobject obj, jstring message);
65 private:
66 enum InternalState {
67 kIdle, // The device is opened but not in use.
68 kCapturing, // Video is being captured.
69 kError // Hit error. User needs to recover by destroying the object.
72 VideoPixelFormat GetColorspace();
73 void SetErrorState(const std::string& reason);
75 // Prevent racing on accessing |state_| and |client_| since both could be
76 // accessed from different threads.
77 base::Lock lock_;
78 InternalState state_;
79 bool got_first_frame_;
80 base::TimeTicks expected_next_frame_time_;
81 base::TimeDelta frame_interval_;
82 scoped_ptr<VideoCaptureDevice::Client> client_;
84 Name device_name_;
85 VideoCaptureFormat capture_format_;
87 // Java VideoCaptureAndroid instance.
88 base::android::ScopedJavaLocalRef<jobject> j_capture_;
90 DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceAndroid);
93 } // namespace media
95 #endif // MEDIA_VIDEO_CAPTURE_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_