Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / media / video / capture / android / video_capture_device_android.h
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_VIDEO_CAPTURE_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_
6 #define MEDIA_VIDEO_CAPTURE_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/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/video/capture/video_capture_device.h"
17
18 namespace media {
19
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.
27   //
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_YV12 = 842094169,
35     ANDROID_IMAGE_FORMAT_UNKNOWN = 0,
36   };
37
38   explicit VideoCaptureDeviceAndroid(const Name& device_name);
39   virtual ~VideoCaptureDeviceAndroid();
40
41   static VideoCaptureDevice* Create(const Name& device_name);
42   static bool RegisterVideoCaptureDevice(JNIEnv* env);
43
44   // Registers the Java VideoCaptureDevice pointer, used by the rest of the
45   // methods of the class to operate the Java capture code. This method must be
46   // called after the class constructor and before AllocateAndStart().
47   bool Init();
48
49   // VideoCaptureDevice implementation.
50   virtual void AllocateAndStart(const VideoCaptureParams& params,
51                                 scoped_ptr<Client> client) override;
52   virtual void StopAndDeAllocate() override;
53
54   // Implement org.chromium.media.VideoCapture.nativeOnFrameAvailable.
55   void OnFrameAvailable(
56       JNIEnv* env,
57       jobject obj,
58       jbyteArray data,
59       jint length,
60       jint rotation);
61
62  private:
63   enum InternalState {
64     kIdle,  // The device is opened but not in use.
65     kCapturing,  // Video is being captured.
66     kError  // Hit error. User needs to recover by destroying the object.
67   };
68
69   VideoPixelFormat GetColorspace();
70   void SetErrorState(const std::string& reason);
71
72   // Prevent racing on accessing |state_| and |client_| since both could be
73   // accessed from different threads.
74   base::Lock lock_;
75   InternalState state_;
76   bool got_first_frame_;
77   base::TimeTicks expected_next_frame_time_;
78   base::TimeDelta frame_interval_;
79   scoped_ptr<VideoCaptureDevice::Client> client_;
80
81   Name device_name_;
82   VideoCaptureFormat capture_format_;
83
84   // Java VideoCaptureAndroid instance.
85   base::android::ScopedJavaLocalRef<jobject> j_capture_;
86
87   DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceAndroid);
88 };
89
90 }  // namespace media
91
92 #endif  // MEDIA_VIDEO_CAPTURE_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_