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#include <stdio.h>
12#include <algorithm>
13#include <vector>
14
15#include "testing/gtest/include/gtest/gtest.h"
16#include "webrtc/modules/audio_coding/neteq/test/NETEQTEST_RTPpacket.h"
17
18#define FIRSTLINELEN 40
19
20int main(int argc, char* argv[]) {
21  if (argc < 4 || argc > 6) {
22    printf(
23        "Usage: RTPtimeshift in.rtp out.rtp newStartTS [newStartSN "
24        "[newStartArrTime]]\n");
25    exit(1);
26  }
27
28  FILE* inFile = fopen(argv[1], "rb");
29  if (!inFile) {
30    printf("Cannot open input file %s\n", argv[1]);
31    return (-1);
32  }
33  printf("Input RTP file: %s\n", argv[1]);
34
35  FILE* outFile = fopen(argv[2], "wb");
36  if (!outFile) {
37    printf("Cannot open output file %s\n", argv[2]);
38    return (-1);
39  }
40  printf("Output RTP file: %s\n\n", argv[2]);
41
42  // Read file header and write directly to output file.
43  const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
44  char firstline[FIRSTLINELEN];
45  EXPECT_TRUE(fgets(firstline, FIRSTLINELEN, inFile) != NULL);
46  EXPECT_GT(fputs(firstline, outFile), 0);
47  EXPECT_EQ(kRtpDumpHeaderSize,
48            fread(firstline, 1, kRtpDumpHeaderSize, inFile));
49  EXPECT_EQ(kRtpDumpHeaderSize,
50            fwrite(firstline, 1, kRtpDumpHeaderSize, outFile));
51  NETEQTEST_RTPpacket packet;
52  int packLen = packet.readFromFile(inFile);
53  if (packLen < 0) {
54    exit(1);
55  }
56
57  // Get new start TS and start SeqNo from arguments.
58  uint32_t TSdiff = atoi(argv[3]) - packet.timeStamp();
59  uint16_t SNdiff = 0;
60  uint32_t ATdiff = 0;
61  if (argc > 4) {
62    int startSN = atoi(argv[4]);
63    if (startSN >= 0)
64      SNdiff = startSN - packet.sequenceNumber();
65    if (argc > 5) {
66      int startTS = atoi(argv[5]);
67      if (startTS >= 0)
68        ATdiff = startTS - packet.time();
69    }
70  }
71
72  while (packLen >= 0) {
73    packet.setTimeStamp(packet.timeStamp() + TSdiff);
74    packet.setSequenceNumber(packet.sequenceNumber() + SNdiff);
75    packet.setTime(packet.time() + ATdiff);
76
77    packet.writeToFile(outFile);
78
79    packLen = packet.readFromFile(inFile);
80  }
81
82  fclose(inFile);
83  fclose(outFile);
84
85  return 0;
86}
87