kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | d7a71d0 | 2012-08-01 01:40:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_TEST_TESTSUPPORT_FRAME_WRITER_H_ |
| 12 | #define WEBRTC_TEST_TESTSUPPORT_FRAME_WRITER_H_ |
| 13 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | |
ilnik | ee42d19 | 2017-08-22 07:16:20 -0700 | [diff] [blame] | 18 | #include "webrtc/api/video/video_frame.h" |
kjellander@webrtc.org | 9c4e662 | 2013-02-13 09:35:12 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
| 24 | // Handles writing of video files. |
| 25 | class FrameWriter { |
| 26 | public: |
| 27 | virtual ~FrameWriter() {} |
| 28 | |
| 29 | // Initializes the file handler, i.e. opens the input and output files etc. |
| 30 | // This must be called before reading or writing frames has started. |
| 31 | // Returns false if an error has occurred, in addition to printing to stderr. |
| 32 | virtual bool Init() = 0; |
| 33 | |
| 34 | // Writes a frame of the configured frame length to the output file. |
| 35 | // Returns true if the write was successful, false otherwise. |
pbos@webrtc.org | a5f1787 | 2013-04-09 11:10:21 +0000 | [diff] [blame] | 36 | virtual bool WriteFrame(uint8_t* frame_buffer) = 0; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 37 | |
| 38 | // Closes the output file if open. Essentially makes this class impossible |
| 39 | // to use anymore. Will also be invoked by the destructor. |
| 40 | virtual void Close() = 0; |
| 41 | |
| 42 | // Frame length in bytes of a single frame image. |
kjellander@webrtc.org | fa53d87 | 2013-02-04 10:07:17 +0000 | [diff] [blame] | 43 | virtual size_t FrameLength() = 0; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 46 | // Writes raw I420 frames in sequence. |
| 47 | class YuvFrameWriterImpl : public FrameWriter { |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 48 | public: |
| 49 | // Creates a file handler. The input file is assumed to exist and be readable |
| 50 | // and the output file must be writable. |
| 51 | // Parameters: |
| 52 | // output_filename The file to write. Will be overwritten if already |
| 53 | // existing. |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 54 | // width, height Size of each frame to read. |
| 55 | YuvFrameWriterImpl(std::string output_filename, int width, int height); |
| 56 | ~YuvFrameWriterImpl() override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 57 | bool Init() override; |
| 58 | bool WriteFrame(uint8_t* frame_buffer) override; |
| 59 | void Close() override; |
| 60 | size_t FrameLength() override; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 61 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 62 | protected: |
| 63 | const std::string output_filename_; |
brandtr | 2a8135a | 2017-02-21 05:24:03 -0800 | [diff] [blame] | 64 | size_t frame_length_in_bytes_; |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 65 | const int width_; |
| 66 | const int height_; |
brandtr | 2a8135a | 2017-02-21 05:24:03 -0800 | [diff] [blame] | 67 | FILE* output_file_; |
brandtr | 872104a | 2017-02-21 03:59:15 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 70 | // Writes raw I420 frames in sequence, but with Y4M file and frame headers for |
| 71 | // more convenient playback in external media players. |
| 72 | class Y4mFrameWriterImpl : public YuvFrameWriterImpl { |
| 73 | public: |
| 74 | Y4mFrameWriterImpl(std::string output_filename, |
| 75 | int width, |
| 76 | int height, |
| 77 | int frame_rate); |
| 78 | ~Y4mFrameWriterImpl() override; |
| 79 | bool Init() override; |
| 80 | bool WriteFrame(uint8_t* frame_buffer) override; |
| 81 | |
| 82 | private: |
| 83 | const int frame_rate_; |
| 84 | }; |
| 85 | |
ilnik | ee42d19 | 2017-08-22 07:16:20 -0700 | [diff] [blame] | 86 | // LibJpeg is not available on iOS |
| 87 | #if !defined(is_ios) |
| 88 | class JpegFrameWriter { |
| 89 | public: |
| 90 | JpegFrameWriter(const std::string &output_filename); |
| 91 | bool WriteFrame(const VideoFrame& input_frame, int quality); |
| 92 | |
| 93 | private: |
| 94 | bool frame_written_; |
| 95 | const std::string output_filename_; |
| 96 | FILE* output_file_; |
| 97 | }; |
| 98 | #endif |
| 99 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 100 | } // namespace test |
| 101 | } // namespace webrtc |
| 102 | |
| 103 | #endif // WEBRTC_TEST_TESTSUPPORT_FRAME_WRITER_H_ |