1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 TEST_TESTSUPPORT_FRAME_WRITER_H_
12 #define TEST_TESTSUPPORT_FRAME_WRITER_H_
13 
14 #include <stdio.h>
15 
16 #include <string>
17 
18 #include "api/video/video_frame.h"
19 #include "typedefs.h"  // NOLINT(build/include)
20 
21 namespace webrtc {
22 namespace test {
23 
24 // Handles writing of video files.
25 class FrameWriter {
26  public:
~FrameWriter()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.
36   virtual bool WriteFrame(uint8_t* frame_buffer) = 0;
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.
43   virtual size_t FrameLength() = 0;
44 };
45 
46 // Writes raw I420 frames in sequence.
47 class YuvFrameWriterImpl : public FrameWriter {
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.
54   //   width, height           Size of each frame to read.
55   YuvFrameWriterImpl(std::string output_filename, int width, int height);
56   ~YuvFrameWriterImpl() override;
57   bool Init() override;
58   bool WriteFrame(uint8_t* frame_buffer) override;
59   void Close() override;
60   size_t FrameLength() override;
61 
62  protected:
63   const std::string output_filename_;
64   size_t frame_length_in_bytes_;
65   const int width_;
66   const int height_;
67   FILE* output_file_;
68 };
69 
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 
86 // LibJpeg is not available on iOS. This class will do nothing on iOS.
87 class JpegFrameWriter {
88  public:
89   JpegFrameWriter(const std::string &output_filename);
90   // Quality can be from 0 (worst) to 100 (best). Best quality is still lossy.
91   // WriteFrame can be called only once. Subsequent calls will fail.
92   bool WriteFrame(const VideoFrame& input_frame, int quality);
93 
94 #if !defined(WEBRTC_IOS)
95  private:
96   bool frame_written_;
97   const std::string output_filename_;
98   FILE* output_file_;
99 #endif
100 };
101 
102 }  // namespace test
103 }  // namespace webrtc
104 
105 #endif  // TEST_TESTSUPPORT_FRAME_WRITER_H_
106