Home | History | Annotate | Download | only in android
      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 "media/base/media_export.h"
     15 #include "media/video/capture/video_capture_device.h"
     16 
     17 namespace media {
     18 
     19 // VideoCaptureDevice on Android. The VideoCaptureDevice API's are called
     20 // by VideoCaptureManager on its own thread, while OnFrameAvailable is called
     21 // on JAVA thread (i.e., UI thread). Both will access |state_| and |observer_|,
     22 // but only VideoCaptureManager would change their value.
     23 class MEDIA_EXPORT VideoCaptureDeviceAndroid : public VideoCaptureDevice {
     24  public:
     25   virtual ~VideoCaptureDeviceAndroid();
     26 
     27   static VideoCaptureDevice* Create(const Name& device_name);
     28   static bool RegisterVideoCaptureDevice(JNIEnv* env);
     29 
     30   // VideoCaptureDevice implementation.
     31   virtual void Allocate(const VideoCaptureCapability& capture_format,
     32                          EventHandler* observer) OVERRIDE;
     33   virtual void Start() OVERRIDE;
     34   virtual void Stop() OVERRIDE;
     35   virtual void DeAllocate() OVERRIDE;
     36   virtual const Name& device_name() OVERRIDE;
     37 
     38   // Implement org.chromium.media.VideoCapture.nativeOnFrameAvailable.
     39   void OnFrameAvailable(
     40       JNIEnv* env,
     41       jobject obj,
     42       jbyteArray data,
     43       jint length,
     44       jint rotation,
     45       jboolean flip_vert,
     46       jboolean flip_horiz);
     47 
     48  private:
     49   enum InternalState {
     50     kIdle,  // The device is opened but not in use.
     51     kAllocated,  // All resouces have been allocated and camera can be started.
     52     kCapturing,  // Video is being captured.
     53     kError  // Hit error. User needs to recover by destroying the object.
     54   };
     55 
     56   explicit VideoCaptureDeviceAndroid(const Name& device_name);
     57   bool Init();
     58   void SetErrorState(const std::string& reason);
     59 
     60   // Prevent racing on accessing |state_| and |observer_| since both could be
     61   // accessed from different threads.
     62   base::Lock lock_;
     63   InternalState state_;
     64   VideoCaptureDevice::EventHandler* observer_;
     65 
     66   Name device_name_;
     67   VideoCaptureCapability current_settings_;
     68 
     69   // Java VideoCaptureAndroid instance.
     70   base::android::ScopedJavaGlobalRef<jobject> j_capture_;
     71 
     72   DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceAndroid);
     73 };
     74 
     75 }  // namespace media
     76 
     77 #endif  // MEDIA_VIDEO_CAPTURE_ANDROID_VIDEO_CAPTURE_DEVICE_ANDROID_H_
     78