2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
11 // Commandline tool to unpack audioproc debug files.
13 // The debug files are dumped as protobuf blobs. For analysis, it's necessary
14 // to unpack the file into its component parts: audio and other data.
18 #include "gflags/gflags.h"
19 #include "webrtc/audio_processing/debug.pb.h"
20 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
21 #include "webrtc/typedefs.h"
23 using webrtc::scoped_array;
25 using webrtc::audioproc::Event;
26 using webrtc::audioproc::ReverseStream;
27 using webrtc::audioproc::Stream;
28 using webrtc::audioproc::Init;
30 // TODO(andrew): unpack more of the data.
31 DEFINE_string(input_file, "input.pcm", "The name of the input stream file.");
32 DEFINE_string(output_file, "ref_out.pcm",
33 "The name of the reference output stream file.");
34 DEFINE_string(reverse_file, "reverse.pcm",
35 "The name of the reverse input stream file.");
36 DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
37 DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
38 DEFINE_string(level_file, "level.int32", "The name of the level file.");
39 DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
40 DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
41 DEFINE_bool(full, false,
42 "Unpack the full set of files (normally not needed).");
44 // TODO(andrew): move this to a helper class to share with process_test.cc?
45 // Returns true on success, false on error or end-of-file.
46 bool ReadMessageFromFile(FILE* file,
47 ::google::protobuf::MessageLite* msg) {
48 // The "wire format" for the size is little-endian.
49 // Assume process_test is running on a little-endian machine.
51 if (fread(&size, sizeof(int32_t), 1, file) != 1) {
57 const size_t usize = static_cast<size_t>(size);
59 scoped_array<char> array(new char[usize]);
60 if (fread(array.get(), sizeof(char), usize, file) != usize) {
65 return msg->ParseFromArray(array.get(), usize);
68 int main(int argc, char* argv[]) {
69 std::string program_name = argv[0];
70 std::string usage = "Commandline tool to unpack audioproc debug files.\n"
71 "Example usage:\n" + program_name + " debug_dump.pb\n";
72 google::SetUsageMessage(usage);
73 google::ParseCommandLineFlags(&argc, &argv, true);
76 printf("%s", google::ProgramUsage());
80 FILE* debug_file = fopen(argv[1], "rb");
81 if (debug_file == NULL) {
82 printf("Unable to open %s\n", argv[1]);
85 FILE* input_file = fopen(FLAGS_input_file.c_str(), "wb");
86 if (input_file == NULL) {
87 printf("Unable to open %s\n", FLAGS_input_file.c_str());
90 FILE* output_file = fopen(FLAGS_output_file.c_str(), "wb");
91 if (output_file == NULL) {
92 printf("Unable to open %s\n", FLAGS_output_file.c_str());
95 FILE* reverse_file = fopen(FLAGS_reverse_file.c_str(), "wb");
96 if (reverse_file == NULL) {
97 printf("Unable to open %s\n", FLAGS_reverse_file.c_str());
100 FILE* settings_file = fopen(FLAGS_settings_file.c_str(), "wb");
101 if (settings_file == NULL) {
102 printf("Unable to open %s\n", FLAGS_settings_file.c_str());
106 FILE* delay_file = NULL;
107 FILE* drift_file = NULL;
108 FILE* level_file = NULL;
109 FILE* keypress_file = NULL;
111 delay_file = fopen(FLAGS_delay_file.c_str(), "wb");
112 if (delay_file == NULL) {
113 printf("Unable to open %s\n", FLAGS_delay_file.c_str());
116 drift_file = fopen(FLAGS_drift_file.c_str(), "wb");
117 if (drift_file == NULL) {
118 printf("Unable to open %s\n", FLAGS_drift_file.c_str());
121 level_file = fopen(FLAGS_level_file.c_str(), "wb");
122 if (level_file == NULL) {
123 printf("Unable to open %s\n", FLAGS_level_file.c_str());
126 keypress_file = fopen(FLAGS_keypress_file.c_str(), "wb");
127 if (keypress_file == NULL) {
128 printf("Unable to open %s\n", FLAGS_keypress_file.c_str());
135 while (ReadMessageFromFile(debug_file, &event_msg)) {
136 if (event_msg.type() == Event::REVERSE_STREAM) {
137 if (!event_msg.has_reverse_stream()) {
138 printf("Corrupted input file: ReverseStream missing.\n");
142 const ReverseStream msg = event_msg.reverse_stream();
143 if (msg.has_data()) {
144 if (fwrite(msg.data().data(), msg.data().size(), 1, reverse_file) !=
146 printf("Error when writing to %s\n", FLAGS_reverse_file.c_str());
150 } else if (event_msg.type() == Event::STREAM) {
152 if (!event_msg.has_stream()) {
153 printf("Corrupted input file: Stream missing.\n");
157 const Stream msg = event_msg.stream();
158 if (msg.has_input_data()) {
159 if (fwrite(msg.input_data().data(), msg.input_data().size(), 1,
161 printf("Error when writing to %s\n", FLAGS_input_file.c_str());
166 if (msg.has_output_data()) {
167 if (fwrite(msg.output_data().data(), msg.output_data().size(), 1,
169 printf("Error when writing to %s\n", FLAGS_output_file.c_str());
175 if (msg.has_delay()) {
176 int32_t delay = msg.delay();
177 if (fwrite(&delay, sizeof(int32_t), 1, delay_file) != 1) {
178 printf("Error when writing to %s\n", FLAGS_delay_file.c_str());
183 if (msg.has_drift()) {
184 int32_t drift = msg.drift();
185 if (fwrite(&drift, sizeof(int32_t), 1, drift_file) != 1) {
186 printf("Error when writing to %s\n", FLAGS_drift_file.c_str());
191 if (msg.has_level()) {
192 int32_t level = msg.level();
193 if (fwrite(&level, sizeof(int32_t), 1, level_file) != 1) {
194 printf("Error when writing to %s\n", FLAGS_level_file.c_str());
199 if (msg.has_keypress()) {
200 bool keypress = msg.keypress();
201 if (fwrite(&keypress, sizeof(bool), 1, keypress_file) != 1) {
202 printf("Error when writing to %s\n", FLAGS_keypress_file.c_str());
207 } else if (event_msg.type() == Event::INIT) {
208 if (!event_msg.has_init()) {
209 printf("Corrupted input file: Init missing.\n");
213 const Init msg = event_msg.init();
214 // These should print out zeros if they're missing.
215 fprintf(settings_file, "Init at frame: %d\n", frame_count);
216 fprintf(settings_file, " Sample rate: %d\n", msg.sample_rate());
217 fprintf(settings_file, " Device sample rate: %d\n",
218 msg.device_sample_rate());
219 fprintf(settings_file, " Input channels: %d\n",
220 msg.num_input_channels());
221 fprintf(settings_file, " Output channels: %d\n",
222 msg.num_output_channels());
223 fprintf(settings_file, " Reverse channels: %d\n",
224 msg.num_reverse_channels());
226 fprintf(settings_file, "\n");