andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 18 | #include "gflags/gflags.h" |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 19 | #include "webrtc/audio_processing/debug.pb.h" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 21 | #include "webrtc/typedefs.h" |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 22 | |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 23 | using webrtc::scoped_array; |
| 24 | |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 25 | using webrtc::audioproc::Event; |
| 26 | using webrtc::audioproc::ReverseStream; |
| 27 | using webrtc::audioproc::Stream; |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 28 | using webrtc::audioproc::Init; |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 29 | |
| 30 | // TODO(andrew): unpack more of the data. |
| 31 | DEFINE_string(input_file, "input.pcm", "The name of the input stream file."); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 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."); |
andrew@webrtc.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 39 | DEFINE_string(keypress_file, "keypress.bool", "The name of the keypress file."); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 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)."); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 43 | |
| 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. |
| 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.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 57 | const size_t usize = static_cast<size_t>(size); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 58 | |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 59 | scoped_array<char> array(new char[usize]); |
| 60 | if (fread(array.get(), sizeof(char), usize, file) != usize) { |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
| 64 | msg->Clear(); |
andrew@webrtc.org | 3119ecf | 2011-11-01 17:00:18 +0000 | [diff] [blame] | 65 | return msg->ParseFromArray(array.get(), usize); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 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); |
| 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.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 81 | if (debug_file == NULL) { |
| 82 | printf("Unable to open %s\n", argv[1]); |
| 83 | return 1; |
| 84 | } |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 85 | FILE* input_file = fopen(FLAGS_input_file.c_str(), "wb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 86 | if (input_file == NULL) { |
| 87 | printf("Unable to open %s\n", FLAGS_input_file.c_str()); |
| 88 | return 1; |
| 89 | } |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 90 | FILE* output_file = fopen(FLAGS_output_file.c_str(), "wb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 91 | if (output_file == NULL) { |
| 92 | printf("Unable to open %s\n", FLAGS_output_file.c_str()); |
| 93 | return 1; |
| 94 | } |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 95 | FILE* reverse_file = fopen(FLAGS_reverse_file.c_str(), "wb"); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 96 | 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.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 109 | FILE* keypress_file = NULL; |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 110 | 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.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 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()); |
| 129 | return 1; |
| 130 | } |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 131 | } |
| 132 | |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 133 | Event event_msg; |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 134 | int frame_count = 0; |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 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"); |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | const ReverseStream msg = event_msg.reverse_stream(); |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 143 | 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.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 149 | } |
| 150 | } else if (event_msg.type() == Event::STREAM) { |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 151 | frame_count++; |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 152 | 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.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 158 | 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.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 164 | } |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 165 | |
| 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.org | ce8e077 | 2014-02-12 15:28:30 +0000 | [diff] [blame] | 198 | |
| 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.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 206 | } |
| 207 | } else if (event_msg.type() == Event::INIT) { |
| 208 | if (!event_msg.has_init()) { |
| 209 | printf("Corrupted input file: Init missing.\n"); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 210 | return 1; |
| 211 | } |
| 212 | |
andrew@webrtc.org | cd82438 | 2011-11-11 19:13:36 +0000 | [diff] [blame] | 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()); |
| 225 | |
| 226 | fprintf(settings_file, "\n"); |
andrew@webrtc.org | cb18121 | 2011-10-26 00:27:17 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |