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 | |
| 18 | #include "google/gflags.h" |
| 19 | #include "webrtc/audio_processing/debug.pb.h" |
| 20 | |
| 21 | using webrtc::audioproc::Event; |
| 22 | using webrtc::audioproc::ReverseStream; |
| 23 | using webrtc::audioproc::Stream; |
| 24 | |
| 25 | // TODO(andrew): unpack more of the data. |
| 26 | DEFINE_string(input_file, "input.pcm", "The name of the input stream file."); |
| 27 | DEFINE_string(output_file, "output.pcm", "The name of the output stream file."); |
| 28 | DEFINE_string(reverse_file, "reverse.pcm", "The name of the reverse input " |
| 29 | "file."); |
| 30 | |
| 31 | // TODO(andrew): move this to a helper class to share with process_test.cc? |
| 32 | // Returns true on success, false on error or end-of-file. |
| 33 | bool ReadMessageFromFile(FILE* file, |
| 34 | ::google::protobuf::MessageLite* msg) { |
| 35 | // The "wire format" for the size is little-endian. |
| 36 | // Assume process_test is running on a little-endian machine. |
| 37 | int32_t size = 0; |
| 38 | if (fread(&size, sizeof(int32_t), 1, file) != 1) { |
| 39 | return false; |
| 40 | } |
| 41 | if (size <= 0) { |
| 42 | return false; |
| 43 | } |
| 44 | size_t usize = static_cast<size_t>(size); |
| 45 | |
| 46 | char array[usize]; |
| 47 | if (fread(array, sizeof(char), usize, file) != usize) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | msg->Clear(); |
| 52 | return msg->ParseFromArray(array, usize); |
| 53 | } |
| 54 | |
| 55 | int main(int argc, char* argv[]) { |
| 56 | std::string program_name = argv[0]; |
| 57 | std::string usage = "Commandline tool to unpack audioproc debug files.\n" |
| 58 | "Example usage:\n" + program_name + " debug_dump.pb\n"; |
| 59 | google::SetUsageMessage(usage); |
| 60 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 61 | |
| 62 | if (argc < 2) { |
| 63 | printf("%s", google::ProgramUsage()); |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | FILE* debug_file = fopen(argv[1], "rb"); |
| 68 | FILE* input_file = fopen(FLAGS_input_file.c_str(), "wb"); |
| 69 | FILE* output_file = fopen(FLAGS_output_file.c_str(), "wb"); |
| 70 | FILE* reverse_file = fopen(FLAGS_reverse_file.c_str(), "wb"); |
| 71 | Event event_msg; |
| 72 | while (ReadMessageFromFile(debug_file, &event_msg)) { |
| 73 | if (event_msg.type() == Event::REVERSE_STREAM) { |
| 74 | if (!event_msg.has_reverse_stream()) { |
| 75 | printf("Corrupted input file: ReverseStream missing.\n"); |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | const ReverseStream msg = event_msg.reverse_stream(); |
| 80 | if (!msg.has_data()) { |
| 81 | printf("Corrupted input file: ReverseStream::data missing.\n"); |
| 82 | return 1; |
| 83 | } |
| 84 | if (fwrite(msg.data().data(), msg.data().size(), 1, reverse_file) != 1) { |
| 85 | printf("Error when writing to %s\n", FLAGS_reverse_file.c_str()); |
| 86 | return 1; |
| 87 | } |
| 88 | } else if (event_msg.type() == Event::STREAM) { |
| 89 | if (!event_msg.has_stream()) { |
| 90 | printf("Corrupted input file: Stream missing.\n"); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | const Stream msg = event_msg.stream(); |
| 95 | if (!msg.has_input_data()) { |
| 96 | printf("Corrupted input file: Stream::input_data missing.\n"); |
| 97 | return 1; |
| 98 | } |
| 99 | if (fwrite(msg.input_data().data(), msg.input_data().size(), 1, |
| 100 | input_file) != 1) { |
| 101 | printf("Error when writing to %s\n", FLAGS_input_file.c_str()); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | if (!msg.has_output_data()) { |
| 106 | printf("Corrupted input file: Stream::output_data missing.\n"); |
| 107 | return 1; |
| 108 | } |
| 109 | if (fwrite(msg.output_data().data(), msg.output_data().size(), 1, |
| 110 | output_file) != 1) { |
| 111 | printf("Error when writing to %s\n", FLAGS_output_file.c_str()); |
| 112 | return 1; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |