1/*
2 *  Copyright (c) 2012 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#include <stdio.h>
12
13#include <algorithm>
14#include <vector>
15
16#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.h"
17#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
18
19#define FIRSTLINELEN 40
20//#define WEBRTC_DUMMY_RTP
21
22static bool pktCmp(NETEQTEST_RTPpacket *a, NETEQTEST_RTPpacket *b) {
23  return (a->time() < b->time());
24}
25
26int main(int argc, char* argv[]) {
27  FILE* in_file = fopen(argv[1], "rb");
28  if (!in_file) {
29    printf("Cannot open input file %s\n", argv[1]);
30    return -1;
31  }
32  printf("Input RTP file: %s\n", argv[1]);
33
34  FILE* stat_file = fopen(argv[2], "rt");
35  if (!stat_file) {
36    printf("Cannot open timing file %s\n", argv[2]);
37    return -1;
38  }
39  printf("Timing file: %s\n", argv[2]);
40
41  FILE* out_file = fopen(argv[3], "wb");
42  if (!out_file) {
43    printf("Cannot open output file %s\n", argv[3]);
44    return -1;
45  }
46  printf("Output RTP file: %s\n\n", argv[3]);
47
48  // Read all statistics and insert into map.
49  // Read first line.
50  char temp_str[100];
51  if (fgets(temp_str, 100, stat_file) == NULL) {
52    printf("Failed to read timing file %s\n", argv[2]);
53    return -1;
54  }
55  // Define map.
56  std::map<std::pair<uint16_t, uint32_t>, uint32_t> packet_stats;
57  uint16_t seq_no;
58  uint32_t ts;
59  uint32_t send_time;
60
61  while (fscanf(stat_file,
62                "%hu %u %u %*i %*i\n", &seq_no, &ts, &send_time) == 3) {
63    std::pair<uint16_t, uint32_t>
64        temp_pair = std::pair<uint16_t, uint32_t>(seq_no, ts);
65
66    packet_stats[temp_pair] = send_time;
67  }
68
69  fclose(stat_file);
70
71  // Read file header and write directly to output file.
72  char first_line[FIRSTLINELEN];
73  if (fgets(first_line, FIRSTLINELEN, in_file) == NULL) {
74    printf("Failed to read first line of input file %s\n", argv[1]);
75    return -1;
76  }
77  fputs(first_line, out_file);
78  // start_sec + start_usec + source + port + padding
79  const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
80  if (fread(first_line, 1, kRtpDumpHeaderSize, in_file)
81      != kRtpDumpHeaderSize) {
82    printf("Failed to read RTP dump header from input file %s\n", argv[1]);
83    return -1;
84  }
85  if (fwrite(first_line, 1, kRtpDumpHeaderSize, out_file)
86      != kRtpDumpHeaderSize) {
87    printf("Failed to write RTP dump header to output file %s\n", argv[3]);
88    return -1;
89  }
90
91  std::vector<NETEQTEST_RTPpacket *> packet_vec;
92
93  while (1) {
94    // Insert in vector.
95#ifdef WEBRTC_DUMMY_RTP
96    NETEQTEST_RTPpacket *new_packet = new NETEQTEST_DummyRTPpacket();
97#else
98    NETEQTEST_RTPpacket *new_packet = new NETEQTEST_RTPpacket();
99#endif
100    if (new_packet->readFromFile(in_file) < 0) {
101      // End of file.
102      break;
103    }
104
105    // Look for new send time in statistics vector.
106    std::pair<uint16_t, uint32_t> temp_pair =
107        std::pair<uint16_t, uint32_t>(new_packet->sequenceNumber(),
108                                      new_packet->timeStamp());
109
110    uint32_t new_send_time = packet_stats[temp_pair];
111    new_packet->setTime(new_send_time);  // Set new send time.
112    packet_vec.push_back(new_packet);  // Insert in vector.
113  }
114
115  // Sort the vector according to send times.
116  std::sort(packet_vec.begin(), packet_vec.end(), pktCmp);
117
118  std::vector<NETEQTEST_RTPpacket *>::iterator it;
119  for (it = packet_vec.begin(); it != packet_vec.end(); it++) {
120    // Write to out file.
121    if ((*it)->writeToFile(out_file) < 0) {
122      printf("Error writing to file\n");
123      return -1;
124    }
125    // Delete packet.
126    delete *it;
127  }
128
129  fclose(in_file);
130  fclose(out_file);
131
132  return 0;
133}
134