camera.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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 CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/timer.h"
13
14class SkBitmap;
15
16namespace base {
17class TimeDelta;
18}  // namespace base
19
20namespace chromeos {
21
22// Class that wraps interaction with video capturing device. Returns
23// frames captured with specified intervals of time via delegate interface.
24class Camera {
25 public:
26  class Delegate {
27   public:
28    virtual ~Delegate() {}
29
30    // Called with specified intervals with decoded frame as a parameter.
31    virtual void OnVideoFrameCaptured(const SkBitmap& frame) = 0;
32  };
33
34  explicit Camera(Delegate* delegate);
35  ~Camera();
36
37  // Initializes camera device. Returns true if succeeded, false otherwise.
38  // Does nothing on subsequent calls until Uninitialize is called.
39  // Sets the desired width and height of the frame to receive from camera.
40  bool Initialize(int desired_width, int desired_height);
41
42  // Uninitializes the camera. Can be called anytime, any number of times.
43  void Uninitialize();
44
45  // Starts capturing video frames with specified interval.
46  // Does nothing on subsequent calls until StopCapturing is called.
47  // Returns true if succeeded, false otherwise.
48  bool StartCapturing(const base::TimeDelta& interval);
49
50  // Stops capturing video frames. Can be called anytime, any number of
51  // times.
52  void StopCapturing();
53
54  void set_mirrored(bool mirrored) { mirrored_ = mirrored; }
55
56 private:
57  // Tries to open the device with specified name. Returns opened device
58  // descriptor if succeeds, -1 otherwise.
59  int OpenDevice(const char* device_name) const;
60
61  // Initializes reading mode for the device. Returns true on success, false
62  // otherwise.
63  bool InitializeReadingMode(int fd);
64
65  // Unmaps video buffers stored in |buffers_|.
66  void UnmapVideoBuffers();
67
68  // Called by |timer_| to get the frame from video device and send it to
69  // |delegate_| via its method.
70  void OnCapture();
71
72  // Reads a frame from the video device. If retry is needed, returns false.
73  // Otherwise, returns true despite of success status.
74  bool ReadFrame();
75
76  // Transforms raw data received from camera into SkBitmap with desired
77  // size and notifies the delegate that the image is ready.
78  void ProcessImage(void* data);
79
80  // Defines a buffer in memory where one frame from the camera is stored.
81  struct VideoBuffer {
82    void* start;
83    size_t length;
84  };
85
86  // Delegate that receives the frames from the camera.
87  Delegate* delegate_;
88
89  // Name of the device file, i.e. "/dev/video0".
90  std::string device_name_;
91
92  // File descriptor of the opened device.
93  int device_descriptor_;
94
95  // Vector of buffers where to store video frames from camera.
96  std::vector<VideoBuffer> buffers_;
97
98  // Timer for getting frames.
99  base::RepeatingTimer<Camera> timer_;
100
101  // Desired size of the frame to get from camera. If it doesn't match
102  // camera's supported resolution, higher resolution is selected (if
103  // available) and frame is cropped. If higher resolution is not available,
104  // the highest is selected and resized.
105  int desired_width_;
106  int desired_height_;
107
108  // Size of the frame that camera will give to us. It may not match the
109  // desired size.
110  int frame_width_;
111  int frame_height_;
112
113  // If set to true, the returned image will be reflected from the Y axis to
114  // mimic mirror behavior.
115  bool mirrored_;
116
117  DISALLOW_COPY_AND_ASSIGN(Camera);
118};
119
120}  // namespace chromeos
121
122#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_
123