blob: 148f23338cec42223b015e9c165a9aca5be293d1 [file] [log] [blame]
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00001/*
andrew@webrtc.orgd7a71d02012-08-01 01:40:02 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00003 *
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.org12dc1a32013-08-05 16:22:53 +000014#include <stdio.h>
15
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000016#include <string>
17
ilnikee42d192017-08-22 07:16:20 -070018#include "webrtc/api/video/video_frame.h"
kjellander@webrtc.org9c4e6622013-02-13 09:35:12 +000019#include "webrtc/typedefs.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000020
21namespace webrtc {
22namespace test {
23
24// Handles writing of video files.
25class 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.orga5f17872013-04-09 11:10:21 +000036 virtual bool WriteFrame(uint8_t* frame_buffer) = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000037
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.orgfa53d872013-02-04 10:07:17 +000043 virtual size_t FrameLength() = 0;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000044};
45
brandtrb78bc752017-02-22 01:26:59 -080046// Writes raw I420 frames in sequence.
47class YuvFrameWriterImpl : public FrameWriter {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000048 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.
brandtrb78bc752017-02-22 01:26:59 -080054 // width, height Size of each frame to read.
55 YuvFrameWriterImpl(std::string output_filename, int width, int height);
56 ~YuvFrameWriterImpl() override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000057 bool Init() override;
58 bool WriteFrame(uint8_t* frame_buffer) override;
59 void Close() override;
60 size_t FrameLength() override;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000061
brandtrb78bc752017-02-22 01:26:59 -080062 protected:
63 const std::string output_filename_;
brandtr2a8135a2017-02-21 05:24:03 -080064 size_t frame_length_in_bytes_;
brandtrb78bc752017-02-22 01:26:59 -080065 const int width_;
66 const int height_;
brandtr2a8135a2017-02-21 05:24:03 -080067 FILE* output_file_;
brandtr872104a2017-02-21 03:59:15 -080068};
69
brandtrb78bc752017-02-22 01:26:59 -080070// Writes raw I420 frames in sequence, but with Y4M file and frame headers for
71// more convenient playback in external media players.
72class 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
ilnikee42d192017-08-22 07:16:20 -070086// LibJpeg is not available on iOS
87#if !defined(is_ios)
88class 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.org5b97b122011-12-08 07:42:18 +0000100} // namespace test
101} // namespace webrtc
102
103#endif // WEBRTC_TEST_TESTSUPPORT_FRAME_WRITER_H_