| 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 |
| 19 class VideoFileParser; |
| 20 |
| 18 // Implementation of a VideoCaptureDevice class that reads from a file. Used for | 21 // 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 | 22 // 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 | 23 // 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 | 24 // 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 | 25 // the link http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 for more |
| 23 // on the file format. Several restrictions and notes apply, see the | 26 // information on the file format. Several restrictions and notes apply, see the |
| 24 // implementation file. | 27 // implementation file. |
| 25 // Example videos can be found in http://media.xiph.org/video/derf. | 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. |
| 26 class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice { | 32 class MEDIA_EXPORT FileVideoCaptureDevice : public VideoCaptureDevice { |
| 27 public: | 33 public: |
| 28 static int64 ParseFileAndExtractVideoFormat( | 34 // Reads and parses the header of a |file_path|, returning the collected |
| 29 base::File* file, | 35 // pixel format in |video_format|. Returns true on file parsed successfully, |
| 30 media::VideoCaptureFormat* video_format); | 36 // or false. |
| 31 static base::File OpenFileForRead(const base::FilePath& file_path); | 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); |
| 32 | 40 |
| 33 // Constructor of the class, with a fully qualified file path as input, which | 41 // Constructor of the class, with a fully qualified file path as input, which |
| 34 // represents the Y4M video file to stream repeatedly. | 42 // represents the Y4M or MJPEG file to stream repeatedly. |
| 35 explicit FileVideoCaptureDevice(const base::FilePath& file_path); | 43 explicit FileVideoCaptureDevice(const base::FilePath& file_path); |
| 36 | 44 |
| 37 // VideoCaptureDevice implementation, class methods. | 45 // VideoCaptureDevice implementation, class methods. |
| 38 ~FileVideoCaptureDevice() override; | 46 ~FileVideoCaptureDevice() override; |
| 39 void AllocateAndStart(const VideoCaptureParams& params, | 47 void AllocateAndStart(const VideoCaptureParams& params, |
| 40 scoped_ptr<VideoCaptureDevice::Client> client) override; | 48 scoped_ptr<VideoCaptureDevice::Client> client) override; |
| 41 void StopAndDeAllocate() override; | 49 void StopAndDeAllocate() override; |
| 42 | 50 |
| 43 private: | 51 private: |
| 44 // Returns size in bytes of an I420 frame, not including possible paddings, | 52 // Opens a given file |file_path| for reading, and stores collected format |
| 45 // defined by |capture_format_|. | 53 // information in |video_format|. Returns the parsed file to the |
| 46 int CalculateFrameSize() const; | 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); |
| 47 | 58 |
| 48 // Called on the |capture_thread_|. | 59 // Called on the |capture_thread_|. |
| 49 void OnAllocateAndStart(const VideoCaptureParams& params, | 60 void OnAllocateAndStart(const VideoCaptureParams& params, |
| 50 scoped_ptr<Client> client); | 61 scoped_ptr<Client> client); |
| 51 void OnStopAndDeAllocate(); | 62 void OnStopAndDeAllocate(); |
| 63 const uint8_t* GetNextFrame(); |
| 52 void OnCaptureTask(); | 64 void OnCaptureTask(); |
| 53 | 65 |
| 54 // |thread_checker_| is used to check that destructor, AllocateAndStart() and | 66 // |thread_checker_| is used to check that destructor, AllocateAndStart() and |
| 55 // StopAndDeAllocate() are called in the correct thread that owns the object. | 67 // StopAndDeAllocate() are called in the correct thread that owns the object. |
| 56 base::ThreadChecker thread_checker_; | 68 base::ThreadChecker thread_checker_; |
| 57 | 69 |
| 58 // |capture_thread_| is used for internal operations via posting tasks to it. | 70 // |capture_thread_| is used for internal operations via posting tasks to it. |
| 59 // It is active between OnAllocateAndStart() and OnStopAndDeAllocate(). | 71 // It is active between OnAllocateAndStart() and OnStopAndDeAllocate(). |
| 60 base::Thread capture_thread_; | 72 base::Thread capture_thread_; |
| 61 // The following members belong to |capture_thread_|. | 73 // The following members belong to |capture_thread_|. |
| 62 scoped_ptr<VideoCaptureDevice::Client> client_; | 74 scoped_ptr<VideoCaptureDevice::Client> client_; |
| 63 const base::FilePath file_path_; | 75 const base::FilePath file_path_; |
| 64 base::File file_; | 76 scoped_ptr<VideoFileParser> file_parser_; |
| 65 scoped_ptr<uint8[]> video_frame_; | |
| 66 VideoCaptureFormat capture_format_; | 77 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. | 78 // Target time for the next frame. |
| 71 base::TimeTicks next_frame_time_; | 79 base::TimeTicks next_frame_time_; |
| 72 | 80 |
| 73 DISALLOW_COPY_AND_ASSIGN(FileVideoCaptureDevice); | 81 DISALLOW_COPY_AND_ASSIGN(FileVideoCaptureDevice); |
| 74 }; | 82 }; |
| 75 | 83 |
| 76 } // namespace media | 84 } // namespace media |
| 77 | 85 |
| 78 #endif // MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ | 86 #endif // MEDIA_VIDEO_CAPTURE_FILE_VIDEO_CAPTURE_DEVICE_H_ |
| OLD | NEW |