1/*
2 *  Copyright (c) 2012 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/*
12 * Parses an rtpdump file and outputs a text table parsable by parseLog.m.
13 * The output file will have .txt appended to the specified base name.
14 * $ rtp_to_text [-d] <input_rtp_file> <output_base_name>
15 *
16 * -d   RTP headers only
17 *
18 */
19
20#include "webrtc/system_wrappers/include/data_log.h"
21#include "NETEQTEST_DummyRTPpacket.h"
22#include "NETEQTEST_RTPpacket.h"
23
24#include <stdio.h>
25#include <string.h>
26
27#include <iostream>
28#include <string>
29#include <vector>
30
31/*********************/
32/* Misc. definitions */
33/*********************/
34
35#define FIRSTLINELEN 40
36
37using ::webrtc::DataLog;
38
39int main(int argc, char* argv[])
40{
41    int arg_count = 1;
42    NETEQTEST_RTPpacket* packet;
43
44    if (argc < 3)
45    {
46      printf("Usage: %s [-d] <input_rtp_file> <output_base_name>\n", argv[0]);
47      return -1;
48    }
49
50    // Parse dummy option
51    if (argc >= 3 && strcmp(argv[arg_count], "-d") == 0)
52    {
53        packet = new NETEQTEST_DummyRTPpacket;
54        ++arg_count;
55    }
56    else
57    {
58        packet = new NETEQTEST_RTPpacket;
59    }
60
61    std::string input_filename = argv[arg_count++];
62    std::string table_name = argv[arg_count];
63
64    std::cout << "Input file: " << input_filename << std::endl;
65    std::cout << "Output file: " << table_name << ".txt" << std::endl;
66
67    FILE *inFile=fopen(input_filename.c_str(),"rb");
68    if (!inFile)
69    {
70        std::cout << "Cannot open input file " << input_filename << std::endl;
71        return -1;
72    }
73
74    // Set up the DataLog and define the table
75    DataLog::CreateLog();
76    if (DataLog::AddTable(table_name) < 0)
77    {
78        std::cout << "Error adding table " << table_name << ".txt" << std::endl;
79        return -1;
80    }
81
82    DataLog::AddColumn(table_name, "seq", 1);
83    DataLog::AddColumn(table_name, "ssrc", 1);
84    DataLog::AddColumn(table_name, "payload type", 1);
85    DataLog::AddColumn(table_name, "length", 1);
86    DataLog::AddColumn(table_name, "timestamp", 1);
87    DataLog::AddColumn(table_name, "marker bit", 1);
88    DataLog::AddColumn(table_name, "arrival", 1);
89
90    // read file header
91    char firstline[FIRSTLINELEN];
92    if (fgets(firstline, FIRSTLINELEN, inFile) == NULL)
93    {
94        std::cout << "Error reading file " << input_filename << std::endl;
95        return -1;
96    }
97
98    // start_sec + start_usec + source + port + padding
99    if (fread(firstline, 4+4+4+2+2, 1, inFile) != 1)
100    {
101        std::cout << "Error reading file " << input_filename << std::endl;
102        return -1;
103    }
104
105    while (packet->readFromFile(inFile) >= 0)
106    {
107        // write packet headers to
108        DataLog::InsertCell(table_name, "seq", packet->sequenceNumber());
109        DataLog::InsertCell(table_name, "ssrc", packet->SSRC());
110        DataLog::InsertCell(table_name, "payload type", packet->payloadType());
111        DataLog::InsertCell(table_name, "length", packet->dataLen());
112        DataLog::InsertCell(table_name, "timestamp", packet->timeStamp());
113        DataLog::InsertCell(table_name, "marker bit", packet->markerBit());
114        DataLog::InsertCell(table_name, "arrival", packet->time());
115        DataLog::NextRow(table_name);
116        return -1;
117    }
118
119    DataLog::ReturnLog();
120
121    fclose(inFile);
122
123    return 0;
124}
125