blob: 7556050a21476f18e02969b03bffd58dd1c5ad11 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef TEST_TESTSUPPORT_FRAME_WRITER_H_
12#define TEST_TESTSUPPORT_FRAME_WRITER_H_
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000013
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video/video_frame.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "typedefs.h" // NOLINT(build/include)
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
ilnikd986d762017-08-28 06:08:33 -070086// LibJpeg is not available on iOS. This class will do nothing on iOS.
ilnikee42d192017-08-22 07:16:20 -070087class JpegFrameWriter {
88 public:
89 JpegFrameWriter(const std::string &output_filename);
ilnikd986d762017-08-28 06:08:33 -070090 // 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.
ilnikee42d192017-08-22 07:16:20 -070092 bool WriteFrame(const VideoFrame& input_frame, int quality);
93
ilnikd986d762017-08-28 06:08:33 -070094#if !defined(WEBRTC_IOS)
ilnikee42d192017-08-22 07:16:20 -070095 private:
96 bool frame_written_;
97 const std::string output_filename_;
98 FILE* output_file_;
ilnikee42d192017-08-22 07:16:20 -070099#endif
ilnikd986d762017-08-28 06:08:33 -0700100};
ilnikee42d192017-08-22 07:16:20 -0700101
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000102} // namespace test
103} // namespace webrtc
104
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200105#endif // TEST_TESTSUPPORT_FRAME_WRITER_H_