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