1/*
2 *  Copyright (c) 2014 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 "webrtc/modules/remote_bitrate_estimator/tools/bwe_rtp.h"
12
13#include <stdio.h>
14
15#include <set>
16#include <sstream>
17#include <string>
18
19#include "gflags/gflags.h"
20#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
21#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
22#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
23#include "webrtc/modules/rtp_rtcp/include/rtp_payload_registry.h"
24#include "webrtc/test/rtp_file_reader.h"
25
26namespace flags {
27
28DEFINE_string(extension_type,
29              "abs",
30              "Extension type, either abs for absolute send time or tsoffset "
31              "for timestamp offset.");
32std::string ExtensionType() {
33  return static_cast<std::string>(FLAGS_extension_type);
34}
35
36DEFINE_int32(extension_id, 3, "Extension id.");
37int ExtensionId() {
38  return static_cast<int>(FLAGS_extension_id);
39}
40
41DEFINE_string(input_file, "", "Input file.");
42std::string InputFile() {
43  return static_cast<std::string>(FLAGS_input_file);
44}
45
46DEFINE_string(ssrc_filter,
47              "",
48              "Comma-separated list of SSRCs in hexadecimal which are to be "
49              "used as input to the BWE (only applicable to pcap files).");
50std::set<uint32_t> SsrcFilter() {
51  std::string ssrc_filter_string = static_cast<std::string>(FLAGS_ssrc_filter);
52  if (ssrc_filter_string.empty())
53    return std::set<uint32_t>();
54  std::stringstream ss;
55  std::string ssrc_filter = ssrc_filter_string;
56  std::set<uint32_t> ssrcs;
57
58  // Parse the ssrcs in hexadecimal format.
59  ss << std::hex << ssrc_filter;
60  uint32_t ssrc;
61  while (ss >> ssrc) {
62    ssrcs.insert(ssrc);
63    ss.ignore(1, ',');
64  }
65  return ssrcs;
66}
67}  // namespace flags
68
69bool ParseArgsAndSetupEstimator(int argc,
70                                char** argv,
71                                webrtc::Clock* clock,
72                                webrtc::RemoteBitrateObserver* observer,
73                                webrtc::test::RtpFileReader** rtp_reader,
74                                webrtc::RtpHeaderParser** parser,
75                                webrtc::RemoteBitrateEstimator** estimator,
76                                std::string* estimator_used) {
77  google::ParseCommandLineFlags(&argc, &argv, true);
78  std::string filename = flags::InputFile();
79
80  std::set<uint32_t> ssrc_filter = flags::SsrcFilter();
81  fprintf(stderr, "Filter on SSRC: ");
82  for (auto& s : ssrc_filter) {
83    fprintf(stderr, "0x%08x, ", s);
84  }
85  fprintf(stderr, "\n");
86  if (filename.substr(filename.find_last_of(".")) == ".pcap") {
87    fprintf(stderr, "Opening as pcap\n");
88    *rtp_reader = webrtc::test::RtpFileReader::Create(
89        webrtc::test::RtpFileReader::kPcap, filename.c_str(),
90        flags::SsrcFilter());
91  } else {
92    fprintf(stderr, "Opening as rtp\n");
93    *rtp_reader = webrtc::test::RtpFileReader::Create(
94        webrtc::test::RtpFileReader::kRtpDump, filename.c_str());
95  }
96  if (!*rtp_reader) {
97    fprintf(stderr, "Cannot open input file %s\n", filename.c_str());
98    return false;
99  }
100  fprintf(stderr, "Input file: %s\n\n", filename.c_str());
101
102  webrtc::RTPExtensionType extension = webrtc::kRtpExtensionAbsoluteSendTime;
103  if (flags::ExtensionType() == "tsoffset") {
104    extension = webrtc::kRtpExtensionTransmissionTimeOffset;
105    fprintf(stderr, "Extension: toffset\n");
106  } else if (flags::ExtensionType() == "abs") {
107    fprintf(stderr, "Extension: abs\n");
108  } else {
109    fprintf(stderr, "Unknown extension type\n");
110    return false;
111  }
112
113  // Setup the RTP header parser and the bitrate estimator.
114  *parser = webrtc::RtpHeaderParser::Create();
115  (*parser)->RegisterRtpHeaderExtension(extension, flags::ExtensionId());
116  if (estimator) {
117    switch (extension) {
118      case webrtc::kRtpExtensionAbsoluteSendTime: {
119        *estimator =
120            new webrtc::RemoteBitrateEstimatorAbsSendTime(observer, clock);
121          *estimator_used = "AbsoluteSendTimeRemoteBitrateEstimator";
122          break;
123        }
124      case webrtc::kRtpExtensionTransmissionTimeOffset: {
125        *estimator =
126            new webrtc::RemoteBitrateEstimatorSingleStream(observer, clock);
127          *estimator_used = "RemoteBitrateEstimator";
128          break;
129        }
130      default:
131        assert(false);
132    }
133  }
134  return true;
135}
136