• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 "absl/flags/flag.h"
20 #include "absl/flags/parse.h"
21 #include "modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
22 #include "modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
23 #include "test/rtp_file_reader.h"
24 #include "test/rtp_header_parser.h"
25 
26 ABSL_FLAG(std::string,
27           extension_type,
28           "abs",
29           "Extension type, either abs for absolute send time or tsoffset "
30           "for timestamp offset.");
ExtensionType()31 std::string ExtensionType() {
32   return absl::GetFlag(FLAGS_extension_type);
33 }
34 
35 ABSL_FLAG(int, extension_id, 3, "Extension id.");
ExtensionId()36 int ExtensionId() {
37   return absl::GetFlag(FLAGS_extension_id);
38 }
39 
40 ABSL_FLAG(std::string, input_file, "", "Input file.");
InputFile()41 std::string InputFile() {
42   return absl::GetFlag(FLAGS_input_file);
43 }
44 
45 ABSL_FLAG(std::string,
46           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).");
SsrcFilter()50 std::set<uint32_t> SsrcFilter() {
51   std::string ssrc_filter_string = absl::GetFlag(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 
ParseArgsAndSetupEstimator(int argc,char ** argv,webrtc::Clock * clock,webrtc::RemoteBitrateObserver * observer,std::unique_ptr<webrtc::test::RtpFileReader> * rtp_reader,std::unique_ptr<webrtc::RemoteBitrateEstimator> * estimator,std::string * estimator_used)68 std::unique_ptr<webrtc::RtpHeaderParser> ParseArgsAndSetupEstimator(
69     int argc,
70     char** argv,
71     webrtc::Clock* clock,
72     webrtc::RemoteBitrateObserver* observer,
73     std::unique_ptr<webrtc::test::RtpFileReader>* rtp_reader,
74     std::unique_ptr<webrtc::RemoteBitrateEstimator>* estimator,
75     std::string* estimator_used) {
76   absl::ParseCommandLine(argc, argv);
77   std::string filename = InputFile();
78 
79   std::set<uint32_t> ssrc_filter = SsrcFilter();
80   fprintf(stderr, "Filter on SSRC: ");
81   for (auto& s : ssrc_filter) {
82     fprintf(stderr, "0x%08x, ", s);
83   }
84   fprintf(stderr, "\n");
85   if (filename.substr(filename.find_last_of('.')) == ".pcap") {
86     fprintf(stderr, "Opening as pcap\n");
87     rtp_reader->reset(webrtc::test::RtpFileReader::Create(
88         webrtc::test::RtpFileReader::kPcap, filename.c_str(), SsrcFilter()));
89   } else {
90     fprintf(stderr, "Opening as rtp\n");
91     rtp_reader->reset(webrtc::test::RtpFileReader::Create(
92         webrtc::test::RtpFileReader::kRtpDump, filename.c_str()));
93   }
94   if (!*rtp_reader) {
95     fprintf(stderr, "Cannot open input file %s\n", filename.c_str());
96     return nullptr;
97   }
98   fprintf(stderr, "Input file: %s\n\n", filename.c_str());
99 
100   webrtc::RTPExtensionType extension = webrtc::kRtpExtensionAbsoluteSendTime;
101   if (ExtensionType() == "tsoffset") {
102     extension = webrtc::kRtpExtensionTransmissionTimeOffset;
103     fprintf(stderr, "Extension: toffset\n");
104   } else if (ExtensionType() == "abs") {
105     fprintf(stderr, "Extension: abs\n");
106   } else {
107     fprintf(stderr, "Unknown extension type\n");
108     return nullptr;
109   }
110 
111   // Setup the RTP header parser and the bitrate estimator.
112   auto parser = webrtc::RtpHeaderParser::CreateForTest();
113   parser->RegisterRtpHeaderExtension(extension, ExtensionId());
114   if (estimator) {
115     switch (extension) {
116       case webrtc::kRtpExtensionAbsoluteSendTime: {
117         estimator->reset(
118             new webrtc::RemoteBitrateEstimatorAbsSendTime(observer, clock));
119         *estimator_used = "AbsoluteSendTimeRemoteBitrateEstimator";
120         break;
121       }
122       case webrtc::kRtpExtensionTransmissionTimeOffset: {
123         estimator->reset(
124             new webrtc::RemoteBitrateEstimatorSingleStream(observer, clock));
125         *estimator_used = "RemoteBitrateEstimator";
126         break;
127       }
128       default:
129         assert(false);
130         return nullptr;
131     }
132   }
133 
134   return parser;
135 }
136