• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 namespace webrtc {
21 namespace test {
22 
23 // Handles writing of video files.
24 class FrameWriter {
25  public:
~FrameWriter()26   virtual ~FrameWriter() {}
27 
28   // Initializes the file handler, i.e. opens the input and output files etc.
29   // This must be called before reading or writing frames has started.
30   // Returns false if an error has occurred, in addition to printing to stderr.
31   virtual bool Init() = 0;
32 
33   // Writes a frame of the configured frame length to the output file.
34   // Returns true if the write was successful, false otherwise.
35   virtual bool WriteFrame(uint8_t* frame_buffer) = 0;
36 
37   // Closes the output file if open. Essentially makes this class impossible
38   // to use anymore. Will also be invoked by the destructor.
39   virtual void Close() = 0;
40 
41   // Frame length in bytes of a single frame image.
42   virtual size_t FrameLength() = 0;
43 };
44 
45 // Writes raw I420 frames in sequence.
46 class YuvFrameWriterImpl : public FrameWriter {
47  public:
48   // Creates a file handler. The input file is assumed to exist and be readable
49   // and the output file must be writable.
50   // Parameters:
51   //   output_filename         The file to write. Will be overwritten if already
52   //                           existing.
53   //   width, height           Size of each frame to read.
54   YuvFrameWriterImpl(std::string output_filename, int width, int height);
55   ~YuvFrameWriterImpl() override;
56   bool Init() override;
57   bool WriteFrame(uint8_t* frame_buffer) override;
58   void Close() override;
59   size_t FrameLength() override;
60 
61  protected:
62   const std::string output_filename_;
63   size_t frame_length_in_bytes_;
64   const int width_;
65   const int height_;
66   FILE* output_file_;
67 };
68 
69 // Writes raw I420 frames in sequence, but with Y4M file and frame headers for
70 // more convenient playback in external media players.
71 class Y4mFrameWriterImpl : public YuvFrameWriterImpl {
72  public:
73   Y4mFrameWriterImpl(std::string output_filename,
74                      int width,
75                      int height,
76                      int frame_rate);
77   ~Y4mFrameWriterImpl() override;
78   bool Init() override;
79   bool WriteFrame(uint8_t* frame_buffer) override;
80 
81  private:
82   const int frame_rate_;
83 };
84 
85 // LibJpeg is not available on iOS. This class will do nothing on iOS.
86 class JpegFrameWriter {
87  public:
88   JpegFrameWriter(const std::string& output_filename);
89   // Quality can be from 0 (worst) to 100 (best). Best quality is still lossy.
90   // WriteFrame can be called only once. Subsequent calls will fail.
91   bool WriteFrame(const VideoFrame& input_frame, int quality);
92 
93 #if !defined(WEBRTC_IOS)
94  private:
95   bool frame_written_;
96   const std::string output_filename_;
97   FILE* output_file_;
98 #endif
99 };
100 
101 }  // namespace test
102 }  // namespace webrtc
103 
104 #endif  // TEST_TESTSUPPORT_FRAME_WRITER_H_
105