Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_processing / test / unpack.cc
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 "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"
22
23 using webrtc::scoped_array;
24
25 using webrtc::audioproc::Event;
26 using webrtc::audioproc::ReverseStream;
27 using webrtc::audioproc::Stream;
28 using webrtc::audioproc::Init;
29
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).");
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   }
57   const size_t usize = static_cast<size_t>(size);
58
59   scoped_array<char> array(new char[usize]);
60   if (fread(array.get(), sizeof(char), usize, file) != usize) {
61     return false;
62   }
63
64   msg->Clear();
65   return msg->ParseFromArray(array.get(), usize);
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");
81   if (debug_file == NULL) {
82     printf("Unable to open %s\n", argv[1]);
83     return 1;
84   }
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());
88     return 1;
89   }
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());
93     return 1;
94   }
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());
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;
109   FILE* keypress_file = NULL;
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     }
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     }
131   }
132
133   Event event_msg;
134   int frame_count = 0;
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();
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         }
149       }
150     } else if (event_msg.type() == Event::STREAM) {
151       frame_count++;
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();
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         }
164       }
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         }
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         }
206       }
207     } else if (event_msg.type() == Event::INIT) {
208       if (!event_msg.has_init()) {
209         printf("Corrupted input file: Init missing.\n");
210         return 1;
211       }
212
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");
227     }
228   }
229
230   return 0;
231 }