19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <stdio.h>
129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <algorithm>
149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <vector>
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
16e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_DummyRTPpacket.h"
17e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#define FIRSTLINELEN 40
209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org//#define WEBRTC_DUMMY_RTP
219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgstatic bool pktCmp(NETEQTEST_RTPpacket *a, NETEQTEST_RTPpacket *b) {
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return (a->time() < b->time());
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgint main(int argc, char* argv[]) {
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  FILE* in_file = fopen(argv[1], "rb");
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (!in_file) {
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Cannot open input file %s\n", argv[1]);
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  printf("Input RTP file: %s\n", argv[1]);
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  FILE* stat_file = fopen(argv[2], "rt");
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (!stat_file) {
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Cannot open timing file %s\n", argv[2]);
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  printf("Timing file: %s\n", argv[2]);
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  FILE* out_file = fopen(argv[3], "wb");
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (!out_file) {
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Cannot open output file %s\n", argv[3]);
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  printf("Output RTP file: %s\n\n", argv[3]);
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Read all statistics and insert into map.
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Read first line.
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  char temp_str[100];
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (fgets(temp_str, 100, stat_file) == NULL) {
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Failed to read timing file %s\n", argv[2]);
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Define map.
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  std::map<std::pair<uint16_t, uint32_t>, uint32_t> packet_stats;
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint16_t seq_no;
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t ts;
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t send_time;
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (fscanf(stat_file,
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                "%hu %u %u %*i %*i\n", &seq_no, &ts, &send_time) == 3) {
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    std::pair<uint16_t, uint32_t>
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        temp_pair = std::pair<uint16_t, uint32_t>(seq_no, ts);
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet_stats[temp_pair] = send_time;
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  fclose(stat_file);
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Read file header and write directly to output file.
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  char first_line[FIRSTLINELEN];
739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (fgets(first_line, FIRSTLINELEN, in_file) == NULL) {
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Failed to read first line of input file %s\n", argv[1]);
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  fputs(first_line, out_file);
789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // start_sec + start_usec + source + port + padding
799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (fread(first_line, 1, kRtpDumpHeaderSize, in_file)
819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      != kRtpDumpHeaderSize) {
829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Failed to read RTP dump header from input file %s\n", argv[1]);
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (fwrite(first_line, 1, kRtpDumpHeaderSize, out_file)
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      != kRtpDumpHeaderSize) {
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Failed to write RTP dump header to output file %s\n", argv[3]);
889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  std::vector<NETEQTEST_RTPpacket *> packet_vec;
929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  while (1) {
949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Insert in vector.
959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#ifdef WEBRTC_DUMMY_RTP
969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    NETEQTEST_RTPpacket *new_packet = new NETEQTEST_DummyRTPpacket();
979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#else
989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    NETEQTEST_RTPpacket *new_packet = new NETEQTEST_RTPpacket();
999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#endif
1009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (new_packet->readFromFile(in_file) < 0) {
1019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      // End of file.
1029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      break;
1039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
1049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Look for new send time in statistics vector.
1069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    std::pair<uint16_t, uint32_t> temp_pair =
1079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        std::pair<uint16_t, uint32_t>(new_packet->sequenceNumber(),
1089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                      new_packet->timeStamp());
1099a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    uint32_t new_send_time = packet_stats[temp_pair];
1119a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    new_packet->setTime(new_send_time);  // Set new send time.
1129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    packet_vec.push_back(new_packet);  // Insert in vector.
1139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Sort the vector according to send times.
1169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  std::sort(packet_vec.begin(), packet_vec.end(), pktCmp);
1179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  std::vector<NETEQTEST_RTPpacket *>::iterator it;
1199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (it = packet_vec.begin(); it != packet_vec.end(); it++) {
1209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Write to out file.
1219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if ((*it)->writeToFile(out_file) < 0) {
1229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      printf("Error writing to file\n");
1239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      return -1;
1249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
1259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // Delete packet.
1269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    delete *it;
1279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
1289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  fclose(in_file);
1309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  fclose(out_file);
1319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return 0;
1339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
134