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/event_log_writer.h"
12
13 #include <math.h>
14
15 #include <algorithm>
16 #include <cstdlib>
17 #include <memory>
18 #include <utility>
19
20 #include "absl/types/optional.h"
21 #include "api/rtc_event_log/rtc_event.h"
22 #include "api/rtc_event_log/rtc_event_log.h"
23 #include "logging/rtc_event_log/events/rtc_event_audio_network_adaptation.h"
24 #include "rtc_base/checks.h"
25
26 namespace webrtc {
27
EventLogWriter(RtcEventLog * event_log,int min_bitrate_change_bps,float min_bitrate_change_fraction,float min_packet_loss_change_fraction)28 EventLogWriter::EventLogWriter(RtcEventLog* event_log,
29 int min_bitrate_change_bps,
30 float min_bitrate_change_fraction,
31 float min_packet_loss_change_fraction)
32 : event_log_(event_log),
33 min_bitrate_change_bps_(min_bitrate_change_bps),
34 min_bitrate_change_fraction_(min_bitrate_change_fraction),
35 min_packet_loss_change_fraction_(min_packet_loss_change_fraction) {
36 RTC_DCHECK(event_log_);
37 }
38
39 EventLogWriter::~EventLogWriter() = default;
40
MaybeLogEncoderConfig(const AudioEncoderRuntimeConfig & config)41 void EventLogWriter::MaybeLogEncoderConfig(
42 const AudioEncoderRuntimeConfig& config) {
43 if (last_logged_config_.num_channels != config.num_channels)
44 return LogEncoderConfig(config);
45 if (last_logged_config_.enable_dtx != config.enable_dtx)
46 return LogEncoderConfig(config);
47 if (last_logged_config_.enable_fec != config.enable_fec)
48 return LogEncoderConfig(config);
49 if (last_logged_config_.frame_length_ms != config.frame_length_ms)
50 return LogEncoderConfig(config);
51 if ((!last_logged_config_.bitrate_bps && config.bitrate_bps) ||
52 (last_logged_config_.bitrate_bps && config.bitrate_bps &&
53 std::abs(*last_logged_config_.bitrate_bps - *config.bitrate_bps) >=
54 std::min(static_cast<int>(*last_logged_config_.bitrate_bps *
55 min_bitrate_change_fraction_),
56 min_bitrate_change_bps_))) {
57 return LogEncoderConfig(config);
58 }
59 if ((!last_logged_config_.uplink_packet_loss_fraction &&
60 config.uplink_packet_loss_fraction) ||
61 (last_logged_config_.uplink_packet_loss_fraction &&
62 config.uplink_packet_loss_fraction &&
63 fabs(*last_logged_config_.uplink_packet_loss_fraction -
64 *config.uplink_packet_loss_fraction) >=
65 min_packet_loss_change_fraction_ *
66 *last_logged_config_.uplink_packet_loss_fraction)) {
67 return LogEncoderConfig(config);
68 }
69 }
70
LogEncoderConfig(const AudioEncoderRuntimeConfig & config)71 void EventLogWriter::LogEncoderConfig(const AudioEncoderRuntimeConfig& config) {
72 auto config_copy = std::make_unique<AudioEncoderRuntimeConfig>(config);
73 event_log_->Log(
74 std::make_unique<RtcEventAudioNetworkAdaptation>(std::move(config_copy)));
75 last_logged_config_ = config;
76 }
77
78 } // namespace webrtc
79