From 370bae466c64a41dcfb382c34601a8e4e755d3b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=85hgren?= Date: Thu, 25 Oct 2018 11:32:39 +0200 Subject: [PATCH] APM: Adding more explicit handling of failures in the json config data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL creates a new API for the parser of APM json config that that provides an explicit way for the user to know when there has been an issue in the parsing of the json config data. Bug: webrtc:9921 Change-Id: Idd8f40529f40ab6871efb5b356c0fd2cea21b7d9 Reviewed-on: https://webrtc-review.googlesource.com/c/107841 Reviewed-by: Sam Zackrisson Commit-Queue: Per Ã…hgren Cr-Commit-Position: refs/heads/master@{#25355} --- api/audio/echo_canceller3_config_json.cc | 22 +++++++++++++++---- api/audio/echo_canceller3_config_json.h | 10 +++++++++ .../test/audio_processing_simulator.cc | 12 +++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/api/audio/echo_canceller3_config_json.cc b/api/audio/echo_canceller3_config_json.cc index 784f928f9c..d039c8b616 100644 --- a/api/audio/echo_canceller3_config_json.cc +++ b/api/audio/echo_canceller3_config_json.cc @@ -109,21 +109,29 @@ void ReadParam(const Json::Value& root, } } // namespace -EchoCanceller3Config Aec3ConfigFromJsonString(absl::string_view json_string) { - EchoCanceller3Config cfg; +void Aec3ConfigFromJsonString(absl::string_view json_string, + EchoCanceller3Config* config, + bool* parsing_successful) { + RTC_DCHECK(config); + RTC_DCHECK(parsing_successful); + EchoCanceller3Config& cfg = *config; + cfg = EchoCanceller3Config(); + *parsing_successful = true; Json::Value root; bool success = Json::Reader().parse(std::string(json_string), root); if (!success) { RTC_LOG(LS_ERROR) << "Incorrect JSON format: " << json_string; - return EchoCanceller3Config(); + *parsing_successful = false; + return; } Json::Value aec3_root; success = rtc::GetValueFromJsonObject(root, "aec3", &aec3_root); if (!success) { RTC_LOG(LS_ERROR) << "Missing AEC3 config field: " << json_string; - return EchoCanceller3Config(); + *parsing_successful = false; + return; } Json::Value section; @@ -325,6 +333,12 @@ EchoCanceller3Config Aec3ConfigFromJsonString(absl::string_view json_string) { ReadParam(section, "enforce_empty_higher_bands", &cfg.suppressor.enforce_empty_higher_bands); } +} + +EchoCanceller3Config Aec3ConfigFromJsonString(absl::string_view json_string) { + EchoCanceller3Config cfg; + bool not_used; + Aec3ConfigFromJsonString(json_string, &cfg, ¬_used); return cfg; } diff --git a/api/audio/echo_canceller3_config_json.h b/api/audio/echo_canceller3_config_json.h index 3c8b97589f..8973650f85 100644 --- a/api/audio/echo_canceller3_config_json.h +++ b/api/audio/echo_canceller3_config_json.h @@ -20,6 +20,16 @@ namespace webrtc { // Parses a JSON-encoded string into an Aec3 config. Fields corresponds to // substruct names, with the addition that there must be a top-level node +// "aec3". Produces default config values for anything that cannot be parsed +// from the string. If any error was found in the parsing, parsing_successful is +// set to false. +RTC_EXPORT void Aec3ConfigFromJsonString(absl::string_view json_string, + EchoCanceller3Config* config, + bool* parsing_successful); + +// To be deprecated. +// Parses a JSON-encoded string into an Aec3 config. Fields corresponds to +// substruct names, with the addition that there must be a top-level node // "aec3". Returns default config values for anything that cannot be parsed from // the string. RTC_EXPORT EchoCanceller3Config diff --git a/modules/audio_processing/test/audio_processing_simulator.cc b/modules/audio_processing/test/audio_processing_simulator.cc index e4ac57b069..aae936389b 100644 --- a/modules/audio_processing/test/audio_processing_simulator.cc +++ b/modules/audio_processing/test/audio_processing_simulator.cc @@ -48,7 +48,17 @@ EchoCanceller3Config ReadAec3ConfigFromJsonFile(const std::string& filename) { while (std::getline(f, s)) { json_string += s; } - return Aec3ConfigFromJsonString(json_string); + + bool parsing_successful; + EchoCanceller3Config cfg; + Aec3ConfigFromJsonString(json_string, &cfg, &parsing_successful); + if (!parsing_successful) { + std::cout << "Parsing of json string failed: " << std::endl + << json_string << std::endl; + RTC_CHECK(false); + } + + return cfg; } void CopyFromAudioFrame(const AudioFrame& src, ChannelBuffer* dest) {