1 /*
2 * Copyright (c) 2016 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/audio_coding/audio_network_adaptor/debug_dump_writer.h"
12
13 #include <string>
14
15 #include "absl/types/optional.h"
16 #include "rtc_base/checks.h"
17 #include "rtc_base/ignore_wundef.h"
18 #include "rtc_base/numerics/safe_conversions.h"
19 #include "rtc_base/system/file_wrapper.h"
20
21 #if WEBRTC_ENABLE_PROTOBUF
22 RTC_PUSH_IGNORING_WUNDEF()
23 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
24 #include "external/webrtc/webrtc/modules/audio_coding/audio_network_adaptor/debug_dump.pb.h"
25 #else
26 #include "modules/audio_coding/audio_network_adaptor/debug_dump.pb.h"
27 #endif
28 RTC_POP_IGNORING_WUNDEF()
29 #endif
30
31 namespace webrtc {
32
33 #if WEBRTC_ENABLE_PROTOBUF
34 namespace {
35
36 using audio_network_adaptor::debug_dump::EncoderRuntimeConfig;
37 using audio_network_adaptor::debug_dump::Event;
38 using audio_network_adaptor::debug_dump::NetworkMetrics;
39
DumpEventToFile(const Event & event,FileWrapper * dump_file)40 void DumpEventToFile(const Event& event, FileWrapper* dump_file) {
41 RTC_CHECK(dump_file->is_open());
42 std::string dump_data;
43 event.SerializeToString(&dump_data);
44 int32_t size = rtc::checked_cast<int32_t>(event.ByteSizeLong());
45 dump_file->Write(&size, sizeof(size));
46 dump_file->Write(dump_data.data(), dump_data.length());
47 }
48
49 } // namespace
50 #endif // WEBRTC_ENABLE_PROTOBUF
51
52 class DebugDumpWriterImpl final : public DebugDumpWriter {
53 public:
54 explicit DebugDumpWriterImpl(FILE* file_handle);
55 ~DebugDumpWriterImpl() override = default;
56
57 void DumpEncoderRuntimeConfig(const AudioEncoderRuntimeConfig& config,
58 int64_t timestamp) override;
59
60 void DumpNetworkMetrics(const Controller::NetworkMetrics& metrics,
61 int64_t timestamp) override;
62
63 #if WEBRTC_ENABLE_PROTOBUF
64 void DumpControllerManagerConfig(
65 const audio_network_adaptor::config::ControllerManager&
66 controller_manager_config,
67 int64_t timestamp) override;
68 #endif
69
70 private:
71 FileWrapper dump_file_;
72 };
73
DebugDumpWriterImpl(FILE * file_handle)74 DebugDumpWriterImpl::DebugDumpWriterImpl(FILE* file_handle) {
75 #if WEBRTC_ENABLE_PROTOBUF
76 dump_file_ = FileWrapper(file_handle);
77 RTC_CHECK(dump_file_.is_open());
78 #else
79 RTC_NOTREACHED();
80 #endif
81 }
82
DumpNetworkMetrics(const Controller::NetworkMetrics & metrics,int64_t timestamp)83 void DebugDumpWriterImpl::DumpNetworkMetrics(
84 const Controller::NetworkMetrics& metrics,
85 int64_t timestamp) {
86 #if WEBRTC_ENABLE_PROTOBUF
87 Event event;
88 event.set_timestamp(timestamp);
89 event.set_type(Event::NETWORK_METRICS);
90 auto dump_metrics = event.mutable_network_metrics();
91
92 if (metrics.uplink_bandwidth_bps)
93 dump_metrics->set_uplink_bandwidth_bps(*metrics.uplink_bandwidth_bps);
94
95 if (metrics.uplink_packet_loss_fraction) {
96 dump_metrics->set_uplink_packet_loss_fraction(
97 *metrics.uplink_packet_loss_fraction);
98 }
99
100 if (metrics.target_audio_bitrate_bps) {
101 dump_metrics->set_target_audio_bitrate_bps(
102 *metrics.target_audio_bitrate_bps);
103 }
104
105 if (metrics.rtt_ms)
106 dump_metrics->set_rtt_ms(*metrics.rtt_ms);
107
108 DumpEventToFile(event, &dump_file_);
109 #endif // WEBRTC_ENABLE_PROTOBUF
110 }
111
DumpEncoderRuntimeConfig(const AudioEncoderRuntimeConfig & config,int64_t timestamp)112 void DebugDumpWriterImpl::DumpEncoderRuntimeConfig(
113 const AudioEncoderRuntimeConfig& config,
114 int64_t timestamp) {
115 #if WEBRTC_ENABLE_PROTOBUF
116 Event event;
117 event.set_timestamp(timestamp);
118 event.set_type(Event::ENCODER_RUNTIME_CONFIG);
119 auto dump_config = event.mutable_encoder_runtime_config();
120
121 if (config.bitrate_bps)
122 dump_config->set_bitrate_bps(*config.bitrate_bps);
123
124 if (config.frame_length_ms)
125 dump_config->set_frame_length_ms(*config.frame_length_ms);
126
127 if (config.uplink_packet_loss_fraction) {
128 dump_config->set_uplink_packet_loss_fraction(
129 *config.uplink_packet_loss_fraction);
130 }
131
132 if (config.enable_fec)
133 dump_config->set_enable_fec(*config.enable_fec);
134
135 if (config.enable_dtx)
136 dump_config->set_enable_dtx(*config.enable_dtx);
137
138 if (config.num_channels)
139 dump_config->set_num_channels(*config.num_channels);
140
141 DumpEventToFile(event, &dump_file_);
142 #endif // WEBRTC_ENABLE_PROTOBUF
143 }
144
145 #if WEBRTC_ENABLE_PROTOBUF
DumpControllerManagerConfig(const audio_network_adaptor::config::ControllerManager & controller_manager_config,int64_t timestamp)146 void DebugDumpWriterImpl::DumpControllerManagerConfig(
147 const audio_network_adaptor::config::ControllerManager&
148 controller_manager_config,
149 int64_t timestamp) {
150 Event event;
151 event.set_timestamp(timestamp);
152 event.set_type(Event::CONTROLLER_MANAGER_CONFIG);
153 event.mutable_controller_manager_config()->CopyFrom(
154 controller_manager_config);
155 DumpEventToFile(event, &dump_file_);
156 }
157 #endif // WEBRTC_ENABLE_PROTOBUF
158
Create(FILE * file_handle)159 std::unique_ptr<DebugDumpWriter> DebugDumpWriter::Create(FILE* file_handle) {
160 return std::unique_ptr<DebugDumpWriter>(new DebugDumpWriterImpl(file_handle));
161 }
162
163 } // namespace webrtc
164