Chromium Code Reviews| Index: media/capture/video/file_video_capture_device.h |
| diff --git a/media/capture/video/file_video_capture_device.h b/media/capture/video/file_video_capture_device.h |
| index f50f04146d37094c6d3033412336858461961233..651f6d5b563c7353114b5494e48e27b65f50ab60 100644 |
| --- a/media/capture/video/file_video_capture_device.h |
| +++ b/media/capture/video/file_video_capture_device.h |
| @@ -8,6 +8,7 @@ |
| #include <string> |
| #include "base/files/file.h" |
| +#include "base/files/memory_mapped_file.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/threading/thread.h" |
| #include "base/threading/thread_checker.h" |
| @@ -17,21 +18,26 @@ namespace media { |
| // Implementation of a VideoCaptureDevice class that reads from a file. Used for |
| // testing the video capture pipeline when no real hardware is available. The |
| -// only supported file format is YUV4MPEG2 (a.k.a. Y4M), a minimal container |
| -// with a series of uncompressed video only frames, see the link |
| -// http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 for more information |
| -// on the file format. Several restrictions and notes apply, see the |
| +// supported file formats are YUV4MPEG2 (a.k.a. Y4M) and MJPEG/JPEG. YUV4MPEG2 |
| +// is a minimal container with a series of uncompressed video only frames, see |
| +// the link http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 for more |
| +// information on the file format. Several restrictions and notes apply, see the |
| // implementation file. |
| -// Example videos can be found in http://media.xiph.org/video/derf. |
| +// Example Y4M videos can be found in http://media.xiph.org/video/derf. |
| +// Example MJPEG videos can be found in media/data/test/bear.mjpeg. |
| +// Restrictions: Y4M videos should have .y4m file extension and MJPEG videos |
| +// should have .mjpeg file extension. |
| class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice { |
| public: |
| - static int64 ParseFileAndExtractVideoFormat( |
| - base::File* file, |
| - media::VideoCaptureFormat* video_format); |
| - static base::File OpenFileForRead(const base::FilePath& file_path); |
| + // Reads and parses the header of a |file_path|, returning the collected |
| + // 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.
|
| + // or false. |
| + // Restrictions: Only trivial Y4M per-frame headers and MJPEG are supported. |
| + static bool GetVideoCaptureFormat(const base::FilePath& file_path, |
| + media::VideoCaptureFormat* video_format); |
| // Constructor of the class, with a fully qualified file path as input, which |
| - // represents the Y4M video file to stream repeatedly. |
| + // represents the Y4M or MJPEG file to stream repeatedly. |
| explicit FileVideoCaptureDevice(const base::FilePath& file_path); |
| // VideoCaptureDevice implementation, class methods. |
| @@ -41,14 +47,74 @@ class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice { |
| void StopAndDeAllocate() override; |
| private: |
| - // Returns size in bytes of an I420 frame, not including possible paddings, |
| - // defined by |capture_format_|. |
| - int CalculateFrameSize() const; |
| + class VideoFileParser { |
| + public: |
| + explicit VideoFileParser(const base::FilePath& file_path); |
| + virtual ~VideoFileParser(); |
| + |
| + // Parses file header and collects format information in |capture_format|. |
| + virtual bool Initialize(media::VideoCaptureFormat* capture_format) = 0; |
| + |
| + // Gets the start pointer of next frame and stores current frame size in |
| + // |frame_size|. |
| + virtual const uint8_t* GetNextFrame(int* frame_size) = 0; |
| + |
| + protected: |
| + const base::FilePath file_path_; |
| + int frame_size_; |
| + size_t current_byte_index_; |
| + size_t first_frame_byte_index_; |
| + }; |
| + |
| + 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.
|
| + public: |
| + explicit Y4mFileParser(const base::FilePath& file_path); |
| + |
| + // VideoFileParser implementation, class methods. |
| + ~Y4mFileParser() override; |
| + bool Initialize(media::VideoCaptureFormat* capture_format) override; |
| + const uint8_t* GetNextFrame(int* frame_size) override; |
| + |
| + private: |
| + int ParseY4MInt(const base::StringPiece& token); |
| + void ParseY4MRational(const base::StringPiece& token, |
| + int* numerator, |
| + int* denominator); |
| + void ParseY4MTags(const std::string& file_header, |
| + media::VideoCaptureFormat* video_format); |
| + scoped_ptr<base::File> file_; |
| + scoped_ptr<uint8[]> video_frame_; |
|
kcwu
2015/08/19 11:29:38
uint8_t
henryhsu
2015/08/20 03:18:57
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(Y4mFileParser); |
| + }; |
| + |
| + class MjpegFileParser : public VideoFileParser { |
| + public: |
| + explicit MjpegFileParser(const base::FilePath& file_path); |
| + |
| + // VideoFileParser implementation, class methods. |
| + ~MjpegFileParser() override; |
| + bool Initialize(media::VideoCaptureFormat* capture_format) override; |
| + const uint8_t* GetNextFrame(int* frame_size) override; |
| + |
| + private: |
| + scoped_ptr<base::MemoryMappedFile> mapped_file_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MjpegFileParser); |
| + }; |
| + |
| + // Opens a given file |file_path| for reading, and stores collected format |
| + // information in |video_format|. Returns the parsed file to the |
| + // caller, who is responsible for closing it. |
| + static scoped_ptr<VideoFileParser> GetVideoFileParser( |
| + const base::FilePath& file_path, |
| + media::VideoCaptureFormat* video_format); |
| // Called on the |capture_thread_|. |
| void OnAllocateAndStart(const VideoCaptureParams& params, |
| scoped_ptr<Client> client); |
| void OnStopAndDeAllocate(); |
| + const uint8_t* GetNextFrame(); |
| void OnCaptureTask(); |
| // |thread_checker_| is used to check that destructor, AllocateAndStart() and |
| @@ -61,12 +127,8 @@ class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice { |
| // The following members belong to |capture_thread_|. |
| scoped_ptr<VideoCaptureDevice::Client> client_; |
| const base::FilePath file_path_; |
| - base::File file_; |
| - scoped_ptr<uint8[]> video_frame_; |
| + scoped_ptr<VideoFileParser> file_parser_; |
| VideoCaptureFormat capture_format_; |
| - int frame_size_; |
| - int64 current_byte_index_; |
| - int64 first_frame_byte_index_; |
| // Target time for the next frame. |
| base::TimeTicks next_frame_time_; |