blob: 84fba7ef0046173b4e58945184baa31e243f7e4d [file] [log] [blame]
brandtrb78bc752017-02-22 01:26:59 -08001/*
2 * Copyright (c) 2017 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
Edward Lemurc20978e2017-07-06 19:44:34 +020011#include "webrtc/rtc_base/checks.h"
brandtrb78bc752017-02-22 01:26:59 -080012#include "webrtc/test/testsupport/frame_writer.h"
13
14namespace webrtc {
15namespace test {
16
17YuvFrameWriterImpl::YuvFrameWriterImpl(std::string output_filename,
18 int width,
19 int height)
20 : output_filename_(output_filename),
21 frame_length_in_bytes_(0),
22 width_(width),
23 height_(height),
24 output_file_(nullptr) {}
25
26YuvFrameWriterImpl::~YuvFrameWriterImpl() {
27 Close();
28}
29
30bool YuvFrameWriterImpl::Init() {
31 if (width_ <= 0 || height_ <= 0) {
32 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", width_,
33 height_);
34 return false;
35 }
36 frame_length_in_bytes_ =
37 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2);
38
39 output_file_ = fopen(output_filename_.c_str(), "wb");
40 if (output_file_ == nullptr) {
41 fprintf(stderr, "Couldn't open output file for writing: %s\n",
42 output_filename_.c_str());
43 return false;
44 }
45 return true;
46}
47
48bool YuvFrameWriterImpl::WriteFrame(uint8_t* frame_buffer) {
49 RTC_DCHECK(frame_buffer);
50 if (output_file_ == nullptr) {
51 fprintf(stderr,
52 "YuvFrameWriterImpl is not initialized (output file is NULL)\n");
53 return false;
54 }
55 size_t bytes_written =
56 fwrite(frame_buffer, 1, frame_length_in_bytes_, output_file_);
57 if (bytes_written != frame_length_in_bytes_) {
58 fprintf(stderr, "Failed to write %zu bytes to file %s\n",
59 frame_length_in_bytes_, output_filename_.c_str());
60 return false;
61 }
62 return true;
63}
64
65void YuvFrameWriterImpl::Close() {
66 if (output_file_ != nullptr) {
67 fclose(output_file_);
68 output_file_ = nullptr;
69 }
70}
71
72size_t YuvFrameWriterImpl::FrameLength() {
73 return frame_length_in_bytes_;
74}
75
76} // namespace test
77} // namespace webrtc