1c55a96383497a772a307b346368133960b02ad03Eric Laurent/*
2c55a96383497a772a307b346368133960b02ad03Eric Laurent *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3c55a96383497a772a307b346368133960b02ad03Eric Laurent *
4c55a96383497a772a307b346368133960b02ad03Eric Laurent *  Use of this source code is governed by a BSD-style license
5c55a96383497a772a307b346368133960b02ad03Eric Laurent *  that can be found in the LICENSE file in the root of the source
6c55a96383497a772a307b346368133960b02ad03Eric Laurent *  tree. An additional intellectual property rights grant can be found
7c55a96383497a772a307b346368133960b02ad03Eric Laurent *  in the file PATENTS.  All contributing project authors may
8c55a96383497a772a307b346368133960b02ad03Eric Laurent *  be found in the AUTHORS file in the root of the source tree.
9c55a96383497a772a307b346368133960b02ad03Eric Laurent */
10c55a96383497a772a307b346368133960b02ad03Eric Laurent
11c55a96383497a772a307b346368133960b02ad03Eric Laurent#ifndef WEBRTC_TEST_TESTSUPPORT_FRAME_WRITER_H_
12c55a96383497a772a307b346368133960b02ad03Eric Laurent#define WEBRTC_TEST_TESTSUPPORT_FRAME_WRITER_H_
13c55a96383497a772a307b346368133960b02ad03Eric Laurent
14c55a96383497a772a307b346368133960b02ad03Eric Laurent#include <cstdio>
15c55a96383497a772a307b346368133960b02ad03Eric Laurent#include <string>
16c55a96383497a772a307b346368133960b02ad03Eric Laurent
17c55a96383497a772a307b346368133960b02ad03Eric Laurent#include "typedefs.h"
18c55a96383497a772a307b346368133960b02ad03Eric Laurent
19c55a96383497a772a307b346368133960b02ad03Eric Laurentnamespace webrtc {
20c55a96383497a772a307b346368133960b02ad03Eric Laurentnamespace test {
21c55a96383497a772a307b346368133960b02ad03Eric Laurent
22c55a96383497a772a307b346368133960b02ad03Eric Laurent// Handles writing of video files.
23c55a96383497a772a307b346368133960b02ad03Eric Laurentclass FrameWriter {
24c55a96383497a772a307b346368133960b02ad03Eric Laurent public:
25c55a96383497a772a307b346368133960b02ad03Eric Laurent  virtual ~FrameWriter() {}
26c55a96383497a772a307b346368133960b02ad03Eric Laurent
27c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Initializes the file handler, i.e. opens the input and output files etc.
28c55a96383497a772a307b346368133960b02ad03Eric Laurent  // This must be called before reading or writing frames has started.
29c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Returns false if an error has occurred, in addition to printing to stderr.
30c55a96383497a772a307b346368133960b02ad03Eric Laurent  virtual bool Init() = 0;
31c55a96383497a772a307b346368133960b02ad03Eric Laurent
32c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Writes a frame of the configured frame length to the output file.
33c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Returns true if the write was successful, false otherwise.
34c55a96383497a772a307b346368133960b02ad03Eric Laurent  virtual bool WriteFrame(WebRtc_UWord8* frame_buffer) = 0;
35c55a96383497a772a307b346368133960b02ad03Eric Laurent
36c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Closes the output file if open. Essentially makes this class impossible
37c55a96383497a772a307b346368133960b02ad03Eric Laurent  // to use anymore. Will also be invoked by the destructor.
38c55a96383497a772a307b346368133960b02ad03Eric Laurent  virtual void Close() = 0;
39c55a96383497a772a307b346368133960b02ad03Eric Laurent
40c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Frame length in bytes of a single frame image.
41c55a96383497a772a307b346368133960b02ad03Eric Laurent  virtual int FrameLength() = 0;
42c55a96383497a772a307b346368133960b02ad03Eric Laurent};
43c55a96383497a772a307b346368133960b02ad03Eric Laurent
44c55a96383497a772a307b346368133960b02ad03Eric Laurentclass FrameWriterImpl : public FrameWriter {
45c55a96383497a772a307b346368133960b02ad03Eric Laurent public:
46c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Creates a file handler. The input file is assumed to exist and be readable
47c55a96383497a772a307b346368133960b02ad03Eric Laurent  // and the output file must be writable.
48c55a96383497a772a307b346368133960b02ad03Eric Laurent  // Parameters:
49c55a96383497a772a307b346368133960b02ad03Eric Laurent  //   output_filename         The file to write. Will be overwritten if already
50c55a96383497a772a307b346368133960b02ad03Eric Laurent  //                           existing.
51c55a96383497a772a307b346368133960b02ad03Eric Laurent  //   frame_length_in_bytes   The size of each frame.
52c55a96383497a772a307b346368133960b02ad03Eric Laurent  //                           For YUV: 3*width*height/2
53c55a96383497a772a307b346368133960b02ad03Eric Laurent  FrameWriterImpl(std::string output_filename, int frame_length_in_bytes);
54c55a96383497a772a307b346368133960b02ad03Eric Laurent  virtual ~FrameWriterImpl();
55c55a96383497a772a307b346368133960b02ad03Eric Laurent  bool Init();
56c55a96383497a772a307b346368133960b02ad03Eric Laurent  bool WriteFrame(WebRtc_UWord8* frame_buffer);
57c55a96383497a772a307b346368133960b02ad03Eric Laurent  void Close();
58c55a96383497a772a307b346368133960b02ad03Eric Laurent  int FrameLength() { return frame_length_in_bytes_; }
59c55a96383497a772a307b346368133960b02ad03Eric Laurent
60c55a96383497a772a307b346368133960b02ad03Eric Laurent private:
61c55a96383497a772a307b346368133960b02ad03Eric Laurent  std::string output_filename_;
62c55a96383497a772a307b346368133960b02ad03Eric Laurent  int frame_length_in_bytes_;
63c55a96383497a772a307b346368133960b02ad03Eric Laurent  int number_of_frames_;
64c55a96383497a772a307b346368133960b02ad03Eric Laurent  FILE* output_file_;
65c55a96383497a772a307b346368133960b02ad03Eric Laurent};
66c55a96383497a772a307b346368133960b02ad03Eric Laurent
67c55a96383497a772a307b346368133960b02ad03Eric Laurent}  // namespace test
68c55a96383497a772a307b346368133960b02ad03Eric Laurent}  // namespace webrtc
69c55a96383497a772a307b346368133960b02ad03Eric Laurent
70c55a96383497a772a307b346368133960b02ad03Eric Laurent#endif  // WEBRTC_TEST_TESTSUPPORT_FRAME_WRITER_H_
71