19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2011 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
1649d62206ededc5905d6121d42fdcce8ed665b2c0kjellander@webrtc.org#include "testing/gtest/include/gtest/gtest.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
219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgint main(int argc, char* argv[]) {
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (argc < 3) {
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Usage: RTPcat in1.rtp int2.rtp [...] out.rtp\n");
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    exit(1);
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
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
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  FILE* out_file = fopen(argv[argc - 1], "wb");  // Last parameter is out file.
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  if (!out_file) {
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Cannot open output file %s\n", argv[argc - 1]);
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return -1;
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  printf("Output RTP file: %s\n\n", argv[argc - 1]);
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Read file header and write directly to output file.
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  char firstline[FIRSTLINELEN];
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_TRUE(fgets(firstline, FIRSTLINELEN, in_file) != NULL);
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_GT(fputs(firstline, out_file), 0);
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kRtpDumpHeaderSize, fread(firstline, 1, kRtpDumpHeaderSize,
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                      in_file));
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  EXPECT_EQ(kRtpDumpHeaderSize, fwrite(firstline, 1, kRtpDumpHeaderSize,
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                                       out_file));
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Close input file and re-open it later (easier to write the loop below).
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  fclose(in_file);
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  for (int i = 1; i < argc - 1; i++) {
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    in_file = fopen(argv[i], "rb");
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (!in_file) {
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      printf("Cannot open input file %s\n", argv[i]);
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      return -1;
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Input RTP file: %s\n", argv[i]);
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    NETEQTEST_RTPpacket::skipFileHeader(in_file);
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    NETEQTEST_RTPpacket packet;
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    int pack_len = packet.readFromFile(in_file);
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (pack_len < 0) {
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      exit(1);
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    while (pack_len >= 0) {
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      packet.writeToFile(out_file);
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org      pack_len = packet.readFromFile(in_file);
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    fclose(in_file);
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  }
739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  fclose(out_file);
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  return 0;
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
76