Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / capture / video / file_video_capture_device.h
blob76edf8d793f05ee2e785819e780eb3b66b0ea98c
1 // Copyright 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.
5 #ifndef MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_
6 #define MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_
8 #include <string>
10 #include "base/files/file.h"
11 #include "base/files/memory_mapped_file.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/thread.h"
14 #include "base/threading/thread_checker.h"
15 #include "media/capture/video/video_capture_device.h"
17 namespace media {
19 class VideoFileParser;
21 // Implementation of a VideoCaptureDevice class that reads from a file. Used for
22 // testing the video capture pipeline when no real hardware is available. The
23 // supported file formats are YUV4MPEG2 (a.k.a. Y4M) and MJPEG/JPEG. YUV4MPEG2
24 // is a minimal container with a series of uncompressed video only frames, see
25 // the link http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 for more
26 // information on the file format. Several restrictions and notes apply, see the
27 // implementation file.
28 // Example Y4M videos can be found in http://media.xiph.org/video/derf.
29 // Example MJPEG videos can be found in media/data/test/bear.mjpeg.
30 // Restrictions: Y4M videos should have .y4m file extension and MJPEG videos
31 // should have .mjpeg file extension.
32 class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice {
33 public:
34 // Reads and parses the header of a |file_path|, returning the collected
35 // pixel format in |video_format|. Returns true on file parsed successfully,
36 // or false.
37 // Restrictions: Only trivial Y4M per-frame headers and MJPEG are supported.
38 static bool GetVideoCaptureFormat(const base::FilePath& file_path,
39 media::VideoCaptureFormat* video_format);
41 // Constructor of the class, with a fully qualified file path as input, which
42 // represents the Y4M or MJPEG file to stream repeatedly.
43 explicit FileVideoCaptureDevice(const base::FilePath& file_path);
45 // VideoCaptureDevice implementation, class methods.
46 ~FileVideoCaptureDevice() override;
47 void AllocateAndStart(const VideoCaptureParams& params,
48 scoped_ptr<VideoCaptureDevice::Client> client) override;
49 void StopAndDeAllocate() override;
51 private:
52 // Opens a given file |file_path| for reading, and stores collected format
53 // information in |video_format|. Returns the parsed file to the
54 // caller, who is responsible for closing it.
55 static scoped_ptr<VideoFileParser> GetVideoFileParser(
56 const base::FilePath& file_path,
57 media::VideoCaptureFormat* video_format);
59 // Called on the |capture_thread_|.
60 void OnAllocateAndStart(const VideoCaptureParams& params,
61 scoped_ptr<Client> client);
62 void OnStopAndDeAllocate();
63 const uint8_t* GetNextFrame();
64 void OnCaptureTask();
66 // |thread_checker_| is used to check that destructor, AllocateAndStart() and
67 // StopAndDeAllocate() are called in the correct thread that owns the object.
68 base::ThreadChecker thread_checker_;
70 // |capture_thread_| is used for internal operations via posting tasks to it.
71 // It is active between OnAllocateAndStart() and OnStopAndDeAllocate().
72 base::Thread capture_thread_;
73 // The following members belong to |capture_thread_|.
74 scoped_ptr<VideoCaptureDevice::Client> client_;
75 const base::FilePath file_path_;
76 scoped_ptr<VideoFileParser> file_parser_;
77 VideoCaptureFormat capture_format_;
78 // Target time for the next frame.
79 base::TimeTicks next_frame_time_;
81 DISALLOW_COPY_AND_ASSIGN(FileVideoCaptureDevice);
84 } // namespace media
86 #endif // MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_