blob: b05ea58493d1bd5221472604f590afa0032154fb [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +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
11#include "testsupport/frame_reader.h"
12
13#include <cassert>
14
15#include "testsupport/fileutils.h"
16
17namespace webrtc {
18namespace test {
19
20FrameReaderImpl::FrameReaderImpl(std::string input_filename,
21 int frame_length_in_bytes)
22 : input_filename_(input_filename),
23 frame_length_in_bytes_(frame_length_in_bytes),
24 input_file_(NULL) {
25}
26
27FrameReaderImpl::~FrameReaderImpl() {
28 Close();
29}
30
31bool FrameReaderImpl::Init() {
32 if (frame_length_in_bytes_ <= 0) {
33 fprintf(stderr, "Frame length must be >0, was %d\n",
34 frame_length_in_bytes_);
35 return false;
36 }
37 input_file_ = fopen(input_filename_.c_str(), "rb");
38 if (input_file_ == NULL) {
39 fprintf(stderr, "Couldn't open input file for reading: %s\n",
40 input_filename_.c_str());
41 return false;
42 }
43 // Calculate total number of frames.
44 size_t source_file_size = GetFileSize(input_filename_);
45 if (source_file_size <= 0u) {
46 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str());
47 return false;
48 }
49 number_of_frames_ = source_file_size / frame_length_in_bytes_;
50 return true;
51}
52
53void FrameReaderImpl::Close() {
54 if (input_file_ != NULL) {
55 fclose(input_file_);
56 input_file_ = NULL;
57 }
58}
59
60bool FrameReaderImpl::ReadFrame(WebRtc_UWord8* source_buffer) {
61 assert(source_buffer);
62 if (input_file_ == NULL) {
63 fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n");
64 return false;
65 }
66 size_t nbr_read = fread(source_buffer, 1, frame_length_in_bytes_,
67 input_file_);
68 if (nbr_read != static_cast<unsigned int>(frame_length_in_bytes_) &&
69 ferror(input_file_)) {
70 fprintf(stderr, "Error reading from input file: %s\n",
71 input_filename_.c_str());
72 return false;
73 }
74 if (feof(input_file_) != 0) {
75 return false; // No more frames to process.
76 }
77 return true;
78}
79
80} // namespace test
81} // namespace webrtc