video_capture_device_linux.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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// Linux specific implementation of VideoCaptureDevice.
6// V4L2 is used for capturing. V4L2 does not provide its own thread for
7// capturing so this implementation uses a Chromium thread for fetching frames
8// from V4L2.
9
10#ifndef MEDIA_VIDEO_CAPTURE_LINUX_VIDEO_CAPTURE_DEVICE_LINUX_H_
11#define MEDIA_VIDEO_CAPTURE_LINUX_VIDEO_CAPTURE_DEVICE_LINUX_H_
12
13#include <string>
14
15#include "base/threading/thread.h"
16#include "media/video/capture/video_capture_device.h"
17#include "media/video/capture/video_capture_types.h"
18
19namespace media {
20
21class VideoCaptureDeviceLinux : public VideoCaptureDevice {
22 public:
23  explicit VideoCaptureDeviceLinux(const Name& device_name);
24  virtual ~VideoCaptureDeviceLinux();
25
26  // VideoCaptureDevice implementation.
27  virtual void Allocate(int width,
28                        int height,
29                        int frame_rate,
30                        EventHandler* observer) OVERRIDE;
31  virtual void Start() OVERRIDE;
32  virtual void Stop() OVERRIDE;
33  virtual void DeAllocate() OVERRIDE;
34  virtual const Name& device_name() OVERRIDE;
35
36 private:
37  enum InternalState {
38    kIdle,  // The device driver is opened but camera is not in use.
39    kAllocated,  // The camera has been allocated and can be started.
40    kCapturing,  // Video is being captured.
41    kError  // Error accessing HW functions.
42            // User needs to recover by destroying the object.
43  };
44
45  // Buffers used to receive video frames from with v4l2.
46  struct Buffer {
47    Buffer() : start(0), length(0) {}
48    void* start;
49    size_t length;
50  };
51
52  // Called on the v4l2_thread_.
53  void OnAllocate(int width,
54                  int height,
55                  int frame_rate,
56                  EventHandler* observer);
57  void OnStart();
58  void OnStop();
59  void OnDeAllocate();
60  void OnCaptureTask();
61
62  bool AllocateVideoBuffers();
63  void DeAllocateVideoBuffers();
64  void SetErrorState(const std::string& reason);
65
66  InternalState state_;
67  VideoCaptureDevice::EventHandler* observer_;
68  Name device_name_;
69  int device_fd_;  // File descriptor for the opened camera device.
70  base::Thread v4l2_thread_;  // Thread used for reading data from the device.
71  Buffer* buffer_pool_;
72  int buffer_pool_size_;  // Number of allocated buffers.
73
74  DISALLOW_IMPLICIT_CONSTRUCTORS(VideoCaptureDeviceLinux);
75};
76
77}  // namespace media
78
79#endif  // MEDIA_VIDEO_CAPTURE_LINUX_VIDEO_CAPTURE_DEVICE_LINUX_H_
80