xref: /aosp_15_r20/external/webrtc/rtc_tools/network_tester/config_reader.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2017 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 #include "rtc_tools/network_tester/config_reader.h"
11 
12 #include <fstream>
13 #include <iterator>
14 #include <string>
15 
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 
ConfigReader(const std::string & config_file_path)20 ConfigReader::ConfigReader(const std::string& config_file_path)
21     : proto_config_index_(0) {
22   std::ifstream config_stream(config_file_path,
23                               std::ios_base::in | std::ios_base::binary);
24   RTC_DCHECK(config_stream.is_open())
25       << "Config " << config_file_path << " open failed";
26   RTC_DCHECK(config_stream.good());
27   std::string config_data((std::istreambuf_iterator<char>(config_stream)),
28                           (std::istreambuf_iterator<char>()));
29   if (config_data.size() > 0) {
30     proto_all_configs_.ParseFromString(config_data);
31   }
32 }
33 
34 ConfigReader::~ConfigReader() = default;
35 
GetNextConfig()36 absl::optional<ConfigReader::Config> ConfigReader::GetNextConfig() {
37 #ifdef WEBRTC_NETWORK_TESTER_PROTO
38   if (proto_config_index_ >= proto_all_configs_.configs_size())
39     return absl::nullopt;
40   auto proto_config = proto_all_configs_.configs(proto_config_index_++);
41   RTC_DCHECK(proto_config.has_packet_send_interval_ms());
42   RTC_DCHECK(proto_config.has_packet_size());
43   RTC_DCHECK(proto_config.has_execution_time_ms());
44   Config config;
45   config.packet_send_interval_ms = proto_config.packet_send_interval_ms();
46   config.packet_size = proto_config.packet_size();
47   config.execution_time_ms = proto_config.execution_time_ms();
48   return config;
49 #else
50   return absl::nullopt;
51 #endif  //  WEBRTC_NETWORK_TESTER_PROTO
52 }
53 
54 }  // namespace webrtc
55