OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ | 5 #ifndef MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ |
6 #define MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ | 6 #define MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
11 #include "base/files/memory_mapped_file.h" | |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
13 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
14 #include "media/capture/video/video_capture_device.h" | 15 #include "media/capture/video/video_capture_device.h" |
15 | 16 |
16 namespace media { | 17 namespace media { |
17 | 18 |
18 // Implementation of a VideoCaptureDevice class that reads from a file. Used for | 19 // Implementation of a VideoCaptureDevice class that reads from a file. Used for |
19 // testing the video capture pipeline when no real hardware is available. The | 20 // testing the video capture pipeline when no real hardware is available. The |
20 // only supported file format is YUV4MPEG2 (a.k.a. Y4M), a minimal container | 21 // supported file formats are YUV4MPEG2 (a.k.a. Y4M) and MJPEG/JPEG. YUV4MPEG2 |
21 // with a series of uncompressed video only frames, see the link | 22 // is a minimal container with a series of uncompressed video only frames, see |
22 // http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 for more information | 23 // the link http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 for more |
23 // on the file format. Several restrictions and notes apply, see the | 24 // information on the file format. Several restrictions and notes apply, see the |
24 // implementation file. | 25 // implementation file. |
25 // Example videos can be found in http://media.xiph.org/video/derf. | 26 // Example Y4M videos can be found in http://media.xiph.org/video/derf. |
27 // Example MJPEG videos can be found in media/data/test/bear.mjpeg. | |
28 // Restrictions: Y4M videos should have .y4m file extension and MJPEG videos | |
29 // should have .mjpeg file extension. | |
26 class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice { | 30 class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice { |
27 public: | 31 public: |
28 static int64 ParseFileAndExtractVideoFormat( | 32 // Reads and parses the header of a |file_path|, returning the collected |
29 base::File* file, | 33 // pixel format in |video_format|. Returns true on file parsed successfullly, |
kcwu
2015/08/19 11:29:38
typo: successfully
henryhsu
2015/08/20 03:18:57
Done.
| |
30 media::VideoCaptureFormat* video_format); | 34 // or false. |
31 static base::File OpenFileForRead(const base::FilePath& file_path); | 35 // Restrictions: Only trivial Y4M per-frame headers and MJPEG are supported. |
36 static bool GetVideoCaptureFormat(const base::FilePath& file_path, | |
37 media::VideoCaptureFormat* video_format); | |
32 | 38 |
33 // Constructor of the class, with a fully qualified file path as input, which | 39 // Constructor of the class, with a fully qualified file path as input, which |
34 // represents the Y4M video file to stream repeatedly. | 40 // represents the Y4M or MJPEG file to stream repeatedly. |
35 explicit FileVideoCaptureDevice(const base::FilePath& file_path); | 41 explicit FileVideoCaptureDevice(const base::FilePath& file_path); |
36 | 42 |
37 // VideoCaptureDevice implementation, class methods. | 43 // VideoCaptureDevice implementation, class methods. |
38 ~FileVideoCaptureDevice() override; | 44 ~FileVideoCaptureDevice() override; |
39 void AllocateAndStart(const VideoCaptureParams& params, | 45 void AllocateAndStart(const VideoCaptureParams& params, |
40 scoped_ptr<VideoCaptureDevice::Client> client) override; | 46 scoped_ptr<VideoCaptureDevice::Client> client) override; |
41 void StopAndDeAllocate() override; | 47 void StopAndDeAllocate() override; |
42 | 48 |
43 private: | 49 private: |
44 // Returns size in bytes of an I420 frame, not including possible paddings, | 50 class VideoFileParser { |
45 // defined by |capture_format_|. | 51 public: |
46 int CalculateFrameSize() const; | 52 explicit VideoFileParser(const base::FilePath& file_path); |
53 virtual ~VideoFileParser(); | |
54 | |
55 // Parses file header and collects format information in |capture_format|. | |
56 virtual bool Initialize(media::VideoCaptureFormat* capture_format) = 0; | |
57 | |
58 // Gets the start pointer of next frame and stores current frame size in | |
59 // |frame_size|. | |
60 virtual const uint8_t* GetNextFrame(int* frame_size) = 0; | |
61 | |
62 protected: | |
63 const base::FilePath file_path_; | |
64 int frame_size_; | |
65 size_t current_byte_index_; | |
66 size_t first_frame_byte_index_; | |
67 }; | |
68 | |
69 class Y4mFileParser : public VideoFileParser { | |
mcasas
2015/08/19 22:17:35
Move Y4MFileParser and MjpegFileParser to
the imp
henryhsu
2015/08/20 03:18:57
Done.
| |
70 public: | |
71 explicit Y4mFileParser(const base::FilePath& file_path); | |
72 | |
73 // VideoFileParser implementation, class methods. | |
74 ~Y4mFileParser() override; | |
75 bool Initialize(media::VideoCaptureFormat* capture_format) override; | |
76 const uint8_t* GetNextFrame(int* frame_size) override; | |
77 | |
78 private: | |
79 int ParseY4MInt(const base::StringPiece& token); | |
80 void ParseY4MRational(const base::StringPiece& token, | |
81 int* numerator, | |
82 int* denominator); | |
83 void ParseY4MTags(const std::string& file_header, | |
84 media::VideoCaptureFormat* video_format); | |
85 scoped_ptr<base::File> file_; | |
86 scoped_ptr<uint8[]> video_frame_; | |
kcwu
2015/08/19 11:29:38
uint8_t
henryhsu
2015/08/20 03:18:57
Done.
| |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(Y4mFileParser); | |
89 }; | |
90 | |
91 class MjpegFileParser : public VideoFileParser { | |
92 public: | |
93 explicit MjpegFileParser(const base::FilePath& file_path); | |
94 | |
95 // VideoFileParser implementation, class methods. | |
96 ~MjpegFileParser() override; | |
97 bool Initialize(media::VideoCaptureFormat* capture_format) override; | |
98 const uint8_t* GetNextFrame(int* frame_size) override; | |
99 | |
100 private: | |
101 scoped_ptr<base::MemoryMappedFile> mapped_file_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(MjpegFileParser); | |
104 }; | |
105 | |
106 // Opens a given file |file_path| for reading, and stores collected format | |
107 // information in |video_format|. Returns the parsed file to the | |
108 // caller, who is responsible for closing it. | |
109 static scoped_ptr<VideoFileParser> GetVideoFileParser( | |
110 const base::FilePath& file_path, | |
111 media::VideoCaptureFormat* video_format); | |
47 | 112 |
48 // Called on the |capture_thread_|. | 113 // Called on the |capture_thread_|. |
49 void OnAllocateAndStart(const VideoCaptureParams& params, | 114 void OnAllocateAndStart(const VideoCaptureParams& params, |
50 scoped_ptr<Client> client); | 115 scoped_ptr<Client> client); |
51 void OnStopAndDeAllocate(); | 116 void OnStopAndDeAllocate(); |
117 const uint8_t* GetNextFrame(); | |
52 void OnCaptureTask(); | 118 void OnCaptureTask(); |
53 | 119 |
54 // |thread_checker_| is used to check that destructor, AllocateAndStart() and | 120 // |thread_checker_| is used to check that destructor, AllocateAndStart() and |
55 // StopAndDeAllocate() are called in the correct thread that owns the object. | 121 // StopAndDeAllocate() are called in the correct thread that owns the object. |
56 base::ThreadChecker thread_checker_; | 122 base::ThreadChecker thread_checker_; |
57 | 123 |
58 // |capture_thread_| is used for internal operations via posting tasks to it. | 124 // |capture_thread_| is used for internal operations via posting tasks to it. |
59 // It is active between OnAllocateAndStart() and OnStopAndDeAllocate(). | 125 // It is active between OnAllocateAndStart() and OnStopAndDeAllocate(). |
60 base::Thread capture_thread_; | 126 base::Thread capture_thread_; |
61 // The following members belong to |capture_thread_|. | 127 // The following members belong to |capture_thread_|. |
62 scoped_ptr<VideoCaptureDevice::Client> client_; | 128 scoped_ptr<VideoCaptureDevice::Client> client_; |
63 const base::FilePath file_path_; | 129 const base::FilePath file_path_; |
64 base::File file_; | 130 scoped_ptr<VideoFileParser> file_parser_; |
65 scoped_ptr<uint8[]> video_frame_; | |
66 VideoCaptureFormat capture_format_; | 131 VideoCaptureFormat capture_format_; |
67 int frame_size_; | |
68 int64 current_byte_index_; | |
69 int64 first_frame_byte_index_; | |
70 // Target time for the next frame. | 132 // Target time for the next frame. |
71 base::TimeTicks next_frame_time_; | 133 base::TimeTicks next_frame_time_; |
72 | 134 |
73 DISALLOW_COPY_AND_ASSIGN(FileVideoCaptureDevice); | 135 DISALLOW_COPY_AND_ASSIGN(FileVideoCaptureDevice); |
74 }; | 136 }; |
75 | 137 |
76 } // namespace media | 138 } // namespace media |
77 | 139 |
78 #endif // MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ | 140 #endif // MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ |
OLD | NEW |