blob: 259a773739469954ab9be4d10624c28e188b34c0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.orgf589dfe2012-03-27 17:05:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
niklase@google.com470e71d2011-07-07 08:21:25 +000011#include <stdio.h>
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000012
13#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <vector>
15
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000016#include "modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
andrew@webrtc.org61b1b4b2012-03-27 17:33:29 +000017#include "modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19#define FIRSTLINELEN 40
andrew@webrtc.orgf589dfe2012-03-27 17:05:44 +000020//#define WEBRTC_DUMMY_RTP
niklase@google.com470e71d2011-07-07 08:21:25 +000021
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000022static bool pktCmp(NETEQTEST_RTPpacket *a, NETEQTEST_RTPpacket *b) {
23 return (a->time() < b->time());
niklase@google.com470e71d2011-07-07 08:21:25 +000024}
25
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000026int 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]);
niklase@google.com470e71d2011-07-07 08:21:25 +000033
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000034 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]);
niklase@google.com470e71d2011-07-07 08:21:25 +000040
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000041 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]);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000048 // 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;
niklase@google.com470e71d2011-07-07 08:21:25 +000060
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000061 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);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000066 packet_stats[temp_pair] = send_time;
67 }
niklase@google.com470e71d2011-07-07 08:21:25 +000068
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000069 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.
andrew@webrtc.orgf589dfe2012-03-27 17:05:44 +000095#ifdef WEBRTC_DUMMY_RTP
96 NETEQTEST_RTPpacket *new_packet = new NETEQTEST_DummyRTPpacket();
97#else
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +000098 NETEQTEST_RTPpacket *new_packet = new NETEQTEST_RTPpacket();
andrew@webrtc.orgf589dfe2012-03-27 17:05:44 +000099#endif
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +0000100 if (new_packet->readFromFile(in_file) < 0) {
101 // End of file.
102 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 }
104
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +0000105 // 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());
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +0000110 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;
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +0000124 }
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +0000125 // Delete packet.
126 delete *it;
127 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +0000129 fclose(in_file);
130 fclose(out_file);
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
henrik.lundin@webrtc.org6c877362012-01-03 10:03:19 +0000132 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000133}