- add sources.
[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   virtual ~VideoCaptureDeviceAndroid();
27
28   static VideoCaptureDevice* Create(const Name& device_name);
29   static bool RegisterVideoCaptureDevice(JNIEnv* env);
30
31   // VideoCaptureDevice implementation.
32   virtual void AllocateAndStart(
33       const VideoCaptureCapability& capture_format,
34       scoped_ptr<Client> client) OVERRIDE;
35   virtual void StopAndDeAllocate() OVERRIDE;
36
37   // Implement org.chromium.media.VideoCapture.nativeOnFrameAvailable.
38   void OnFrameAvailable(
39       JNIEnv* env,
40       jobject obj,
41       jbyteArray data,
42       jint length,
43       jint rotation,
44       jboolean flip_vert,
45       jboolean flip_horiz);
46
47  private:
48   enum InternalState {
49     kIdle,  // The device is opened but not in use.
50     kCapturing,  // Video is being captured.
51     kError  // Hit error. User needs to recover by destroying the object.
52   };
53
54   // Automatically generated enum to interface with Java world.
55   enum AndroidImageFormat {
56 #define DEFINE_ANDROID_IMAGEFORMAT(name, value) name = value,
57 #include "media/video/capture/android/imageformat_list.h"
58 #undef DEFINE_ANDROID_IMAGEFORMAT
59   };
60
61   explicit VideoCaptureDeviceAndroid(const Name& device_name);
62   bool Init();
63   VideoPixelFormat GetColorspace();
64   void SetErrorState(const std::string& reason);
65
66   // Prevent racing on accessing |state_| and |client_| since both could be
67   // accessed from different threads.
68   base::Lock lock_;
69   InternalState state_;
70   bool got_first_frame_;
71   base::TimeTicks expected_next_frame_time_;
72   base::TimeDelta frame_interval_;
73   scoped_ptr<VideoCaptureDevice::Client> client_;
74
75   Name device_name_;
76   VideoCaptureCapability current_settings_;
77
78   // Java VideoCaptureAndroid instance.
79   base::android::ScopedJavaGlobalRef<jobject> j_capture_;
80
81   DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceAndroid);
82 };
83
84 }  // namespace media
85
86 #endif  // MEDIA_VIDEO_CAPTURE_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_