blob: f8a4bb17ad539409af6349b66b4a228cf518cbe8 [file] [log] [blame]
andrew@webrtc.orgcb181212011-10-26 00:27:17 +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// Commandline tool to unpack audioproc debug files.
12//
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.
15
16#include <stdio.h>
17
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000018#include "gflags/gflags.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000019#include "webrtc/audio_processing/debug.pb.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000020#include "webrtc/system_wrappers/interface/scoped_ptr.h"
21#include "webrtc/typedefs.h"
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000022
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +000023using webrtc::scoped_array;
24
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000025using webrtc::audioproc::Event;
26using webrtc::audioproc::ReverseStream;
27using webrtc::audioproc::Stream;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000028using webrtc::audioproc::Init;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000029
30// TODO(andrew): unpack more of the data.
31DEFINE_string(input_file, "input.pcm", "The name of the input stream file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000032DEFINE_string(output_file, "ref_out.pcm",
33 "The name of the reference output stream file.");
34DEFINE_string(reverse_file, "reverse.pcm",
35 "The name of the reverse input stream file.");
36DEFINE_string(delay_file, "delay.int32", "The name of the delay file.");
37DEFINE_string(drift_file, "drift.int32", "The name of the drift file.");
38DEFINE_string(level_file, "level.int32", "The name of the level file.");
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +000039DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file.");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000040DEFINE_string(settings_file, "settings.txt", "The name of the settings file.");
41DEFINE_bool(full, false,
42 "Unpack the full set of files (normally not needed).");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000043
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.
46bool 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.
50 int32_t size = 0;
51 if (fread(&size, sizeof(int32_t), 1, file) != 1) {
52 return false;
53 }
54 if (size <= 0) {
55 return false;
56 }
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +000057 const size_t usize = static_cast<size_t>(size);
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000058
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +000059 scoped_array<char> array(new char[usize]);
60 if (fread(array.get(), sizeof(char), usize, file) != usize) {
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000061 return false;
62 }
63
64 msg->Clear();
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +000065 return msg->ParseFromArray(array.get(), usize);
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000066}
67
68int 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);
74
75 if (argc < 2) {
76 printf("%s", google::ProgramUsage());
77 return 1;
78 }
79
80 FILE* debug_file = fopen(argv[1], "rb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000081 if (debug_file == NULL) {
82 printf("Unable to open %s\n", argv[1]);
83 return 1;
84 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000085 FILE* input_file = fopen(FLAGS_input_file.c_str(), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000086 if (input_file == NULL) {
87 printf("Unable to open %s\n", FLAGS_input_file.c_str());
88 return 1;
89 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000090 FILE* output_file = fopen(FLAGS_output_file.c_str(), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000091 if (output_file == NULL) {
92 printf("Unable to open %s\n", FLAGS_output_file.c_str());
93 return 1;
94 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +000095 FILE* reverse_file = fopen(FLAGS_reverse_file.c_str(), "wb");
andrew@webrtc.orgcd824382011-11-11 19:13:36 +000096 if (reverse_file == NULL) {
97 printf("Unable to open %s\n", FLAGS_reverse_file.c_str());
98 return 1;
99 }
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());
103 return 1;
104 }
105
106 FILE* delay_file = NULL;
107 FILE* drift_file = NULL;
108 FILE* level_file = NULL;
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000109 FILE* keypress_file = NULL;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000110 if (FLAGS_full) {
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());
114 return 1;
115 }
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());
119 return 1;
120 }
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());
124 return 1;
125 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000126 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());
129 return 1;
130 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000131 }
132
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000133 Event event_msg;
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000134 int frame_count = 0;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000135 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");
139 return 1;
140 }
141
142 const ReverseStream msg = event_msg.reverse_stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000143 if (msg.has_data()) {
144 if (fwrite(msg.data().data(), msg.data().size(), 1, reverse_file) !=
145 1) {
146 printf("Error when writing to %s\n", FLAGS_reverse_file.c_str());
147 return 1;
148 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000149 }
150 } else if (event_msg.type() == Event::STREAM) {
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000151 frame_count++;
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000152 if (!event_msg.has_stream()) {
153 printf("Corrupted input file: Stream missing.\n");
154 return 1;
155 }
156
157 const Stream msg = event_msg.stream();
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000158 if (msg.has_input_data()) {
159 if (fwrite(msg.input_data().data(), msg.input_data().size(), 1,
160 input_file) != 1) {
161 printf("Error when writing to %s\n", FLAGS_input_file.c_str());
162 return 1;
163 }
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000164 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000165
166 if (msg.has_output_data()) {
167 if (fwrite(msg.output_data().data(), msg.output_data().size(), 1,
168 output_file) != 1) {
169 printf("Error when writing to %s\n", FLAGS_output_file.c_str());
170 return 1;
171 }
172 }
173
174 if (FLAGS_full) {
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());
179 return 1;
180 }
181 }
182
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());
187 return 1;
188 }
189 }
190
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());
195 return 1;
196 }
197 }
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000198
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());
203 return 1;
204 }
205 }
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000206 }
207 } else if (event_msg.type() == Event::INIT) {
208 if (!event_msg.has_init()) {
209 printf("Corrupted input file: Init missing.\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000210 return 1;
211 }
212
andrew@webrtc.orgcd824382011-11-11 19:13:36 +0000213 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());
225
226 fprintf(settings_file, "\n");
andrew@webrtc.orgcb181212011-10-26 00:27:17 +0000227 }
228 }
229
230 return 0;
231}