blob: 1b9e8a82efda2f1dd41e78a7ed0a8059e5847a3d [file] [log] [blame]
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00001/*
2 * Copyright (c) 2011 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
kjellander@webrtc.org9c4e6622013-02-13 09:35:12 +000011#include "webrtc/test/testsupport/frame_writer.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000014
15namespace webrtc {
16namespace test {
17
18FrameWriterImpl::FrameWriterImpl(std::string output_filename,
kjellander@webrtc.org9c4e6622013-02-13 09:35:12 +000019 size_t frame_length_in_bytes)
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000020 : output_filename_(output_filename),
21 frame_length_in_bytes_(frame_length_in_bytes),
22 output_file_(NULL) {
23}
24
25FrameWriterImpl::~FrameWriterImpl() {
26 Close();
27}
28
29bool FrameWriterImpl::Init() {
30 if (frame_length_in_bytes_ <= 0) {
kjellander@webrtc.orgfa53d872013-02-04 10:07:17 +000031 fprintf(stderr, "Frame length must be >0, was %zu\n",
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000032 frame_length_in_bytes_);
33 return false;
34 }
35 output_file_ = fopen(output_filename_.c_str(), "wb");
36 if (output_file_ == NULL) {
37 fprintf(stderr, "Couldn't open output file for writing: %s\n",
38 output_filename_.c_str());
39 return false;
40 }
41 return true;
42}
43
44void FrameWriterImpl::Close() {
45 if (output_file_ != NULL) {
46 fclose(output_file_);
47 output_file_ = NULL;
48 }
49}
50
pbos@webrtc.orge6c39662013-07-30 13:08:38 +000051size_t FrameWriterImpl::FrameLength() { return frame_length_in_bytes_; }
52
pbos@webrtc.orga5f17872013-04-09 11:10:21 +000053bool FrameWriterImpl::WriteFrame(uint8_t* frame_buffer) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000054 assert(frame_buffer);
55 if (output_file_ == NULL) {
kjellander@webrtc.org173b7bb2011-12-21 16:11:25 +000056 fprintf(stderr, "FrameWriter is not initialized (output file is NULL)\n");
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000057 return false;
58 }
kjellander@webrtc.orgfa53d872013-02-04 10:07:17 +000059 size_t bytes_written = fwrite(frame_buffer, 1, frame_length_in_bytes_,
60 output_file_);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000061 if (bytes_written != frame_length_in_bytes_) {
kjellander@webrtc.orgfa53d872013-02-04 10:07:17 +000062 fprintf(stderr, "Failed to write %zu bytes to file %s\n",
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000063 frame_length_in_bytes_, output_filename_.c_str());
64 return false;
65 }
66 return true;
67}
68
69} // namespace test
70} // namespace webrtc