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 <algorithm>
129a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <stdio.h>
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <vector>
149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "NETEQTEST_RTPpacket.h"
169a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "gtest/gtest.h"
179a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*********************/
199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/* Misc. definitions */
209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*********************/
219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#define FIRSTLINELEN 40
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgint main(int argc, char* argv[])
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org{
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if(argc < 4 || argc > 6)
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    {
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        printf("Usage: RTPtimeshift in.rtp out.rtp newStartTS [newStartSN [newStartArrTime]]\n");
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        exit(1);
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	FILE *inFile=fopen(argv[1],"rb");
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	if (!inFile)
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    {
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        printf("Cannot open input file %s\n", argv[1]);
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        return(-1);
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    printf("Input RTP file: %s\n",argv[1]);
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	FILE *outFile=fopen(argv[2],"wb");
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	if (!outFile)
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    {
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        printf("Cannot open output file %s\n", argv[2]);
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        return(-1);
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	printf("Output RTP file: %s\n\n",argv[2]);
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // read file header and write directly to output file
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	const unsigned int kRtpDumpHeaderSize = 4 + 4 + 4 + 2 + 2;
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	char firstline[FIRSTLINELEN];
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	EXPECT_TRUE(fgets(firstline, FIRSTLINELEN, inFile) != NULL);
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	EXPECT_GT(fputs(firstline, outFile), 0);
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	EXPECT_EQ(kRtpDumpHeaderSize,
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	          fread(firstline, 1, kRtpDumpHeaderSize, inFile));
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	EXPECT_EQ(kRtpDumpHeaderSize,
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	          fwrite(firstline, 1, kRtpDumpHeaderSize, outFile));
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	NETEQTEST_RTPpacket packet;
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	int packLen = packet.readFromFile(inFile);
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	if (packLen < 0)
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	{
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	    exit(1);
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org	}
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    // get new start TS and start SeqNo from arguments
66fbda0fcf2f9e82c82bcaac138f44d4e5144f6e0dpbos@webrtc.org	uint32_t TSdiff = atoi(argv[3]) - packet.timeStamp();
67fbda0fcf2f9e82c82bcaac138f44d4e5144f6e0dpbos@webrtc.org	uint16_t SNdiff = 0;
68fbda0fcf2f9e82c82bcaac138f44d4e5144f6e0dpbos@webrtc.org	uint32_t ATdiff = 0;
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    if (argc > 4)
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    {
715424c16e0db8e17cd2aef84da96702e2e6e5695efischman@webrtc.org        int startSN = atoi(argv[4]);
725424c16e0db8e17cd2aef84da96702e2e6e5695efischman@webrtc.org        if (startSN >= 0)
735424c16e0db8e17cd2aef84da96702e2e6e5695efischman@webrtc.org            SNdiff = startSN - packet.sequenceNumber();
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        if (argc > 5)
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        {
765424c16e0db8e17cd2aef84da96702e2e6e5695efischman@webrtc.org            int startTS = atoi(argv[5]);
775424c16e0db8e17cd2aef84da96702e2e6e5695efischman@webrtc.org            if (startTS >= 0)
785424c16e0db8e17cd2aef84da96702e2e6e5695efischman@webrtc.org                ATdiff = startTS - packet.time();
799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        }
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    while (packLen >= 0)
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    {
845424c16e0db8e17cd2aef84da96702e2e6e5695efischman@webrtc.org
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        packet.setTimeStamp(packet.timeStamp() + TSdiff);
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        packet.setSequenceNumber(packet.sequenceNumber() + SNdiff);
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        packet.setTime(packet.time() + ATdiff);
889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        packet.writeToFile(outFile);
909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org        packLen = packet.readFromFile(inFile);
929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    }
949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    fclose(inFile);
969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    fclose(outFile);
979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org    return 0;
999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}
100