# Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.

import("../../webrtc.gni")
if (rtc_enable_protobuf) {
  import("//third_party/protobuf/proto_library.gni")
}

config("apm_debug_dump") {
  if (apm_debug_dump) {
    defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
  } else {
    defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
  }
}

rtc_library("api") {
  visibility = [ "*" ]
  sources = [
    "include/audio_processing.cc",
    "include/audio_processing.h",
  ]
  deps = [
    ":audio_frame_view",
    ":audio_processing_statistics",
    "../../api:array_view",
    "../../api:scoped_refptr",
    "../../api/audio:aec3_config",
    "../../api/audio:audio_frame_api",
    "../../api/audio:echo_control",
    "../../rtc_base:macromagic",
    "../../rtc_base:refcount",
    "../../rtc_base:stringutils",
    "../../rtc_base/system:arch",
    "../../rtc_base/system:file_wrapper",
    "../../rtc_base/system:rtc_export",
    "agc:gain_control_interface",
  ]
  absl_deps = [
    "//third_party/abseil-cpp/absl/strings",
    "//third_party/abseil-cpp/absl/types:optional",
  ]
}

rtc_library("audio_frame_proxies") {
  visibility = [ "*" ]
  sources = [
    "include/audio_frame_proxies.cc",
    "include/audio_frame_proxies.h",
  ]
  deps = [
    ":api",
    ":audio_frame_view",
    "../../api/audio:audio_frame_api",
  ]
}

rtc_library("audio_buffer") {
  visibility = [ "*" ]

  configs += [ ":apm_debug_dump" ]

  sources = [
    "audio_buffer.cc",
    "audio_buffer.h",
    "splitting_filter.cc",
    "splitting_filter.h",
    "three_band_filter_bank.cc",
    "three_band_filter_bank.h",
  ]

  defines = []

  deps = [
    ":api",
    "../../api:array_view",
    "../../common_audio",
    "../../common_audio:common_audio_c",
    "../../rtc_base:checks",
  ]
}

rtc_library("high_pass_filter") {
  visibility = [ "*" ]

  sources = [
    "high_pass_filter.cc",
    "high_pass_filter.h",
  ]

  defines = []

  deps = [
    ":audio_buffer",
    "../../api:array_view",
    "../../rtc_base:checks",
    "utility:cascaded_biquad_filter",
  ]
}

rtc_source_set("aec_dump_interface") {
  visibility = [ "*" ]
  sources = [
    "include/aec_dump.cc",
    "include/aec_dump.h",
  ]

  deps = [
    ":api",
    ":audio_frame_view",
  ]
  absl_deps = [
    "//third_party/abseil-cpp/absl/base:core_headers",
    "//third_party/abseil-cpp/absl/types:optional",
  ]
}

rtc_library("gain_controller2") {
  configs += [ ":apm_debug_dump" ]
  sources = [
    "gain_controller2.cc",
    "gain_controller2.h",
  ]
  defines = []
  deps = [
    ":aec_dump_interface",
    ":api",
    ":apm_logging",
    ":audio_buffer",
    ":audio_frame_view",
    "../../common_audio",
    "../../rtc_base:checks",
    "../../rtc_base:logging",
    "../../rtc_base:stringutils",
    "../../system_wrappers:field_trial",
    "agc2:adaptive_digital_gain_controller",
    "agc2:common",
    "agc2:cpu_features",
    "agc2:fixed_digital",
    "agc2:gain_applier",
    "agc2:input_volume_controller",
    "agc2:noise_level_estimator",
    "agc2:saturation_protector",
    "agc2:speech_level_estimator",
    "agc2:vad_wrapper",
  ]
}

rtc_library("audio_processing") {
  visibility = [ "*" ]
  configs += [ ":apm_debug_dump" ]
  sources = [
    "audio_processing_builder_impl.cc",
    "audio_processing_impl.cc",
    "audio_processing_impl.h",
    "echo_control_mobile_impl.cc",
    "echo_control_mobile_impl.h",
    "gain_control_impl.cc",
    "gain_control_impl.h",
    "render_queue_item_verifier.h",
  ]

  defines = []
  deps = [
    ":aec_dump_interface",
    ":api",
    ":apm_logging",
    ":audio_buffer",
    ":audio_frame_proxies",
    ":audio_frame_view",
    ":audio_processing_statistics",
    ":gain_controller2",
    ":high_pass_filter",
    ":optionally_built_submodule_creators",
    ":rms_level",
    "../../api:array_view",
    "../../api:function_view",
    "../../api:make_ref_counted",
    "../../api/audio:aec3_config",
    "../../api/audio:audio_frame_api",
    "../../api/audio:echo_control",
    "../../audio/utility:audio_frame_operations",
    "../../common_audio:common_audio_c",
    "../../common_audio/third_party/ooura:fft_size_256",
    "../../rtc_base:checks",
    "../../rtc_base:event_tracer",
    "../../rtc_base:gtest_prod",
    "../../rtc_base:ignore_wundef",
    "../../rtc_base:logging",
    "../../rtc_base:macromagic",
    "../../rtc_base:safe_minmax",
    "../../rtc_base:sanitizer",
    "../../rtc_base:swap_queue",
    "../../rtc_base:timeutils",
    "../../rtc_base/experiments:field_trial_parser",
    "../../rtc_base/synchronization:mutex",
    "../../rtc_base/system:rtc_export",
    "../../system_wrappers",
    "../../system_wrappers:denormal_disabler",
    "../../system_wrappers:field_trial",
    "../../system_wrappers:metrics",
    "aec3",
    "aec_dump:aec_dump",
    "aecm:aecm_core",
    "agc",
    "agc:gain_control_interface",
    "agc:legacy_agc",
    "agc2:input_volume_stats_reporter",
    "capture_levels_adjuster",
    "ns",
    "transient:transient_suppressor_api",
    "vad",
  ]
  absl_deps = [
    "//third_party/abseil-cpp/absl/strings",
    "//third_party/abseil-cpp/absl/types:optional",
  ]

  deps += [
    "../../common_audio",
    "../../common_audio:fir_filter",
    "../../common_audio:fir_filter_factory",
    "../../system_wrappers",
  ]

  if (rtc_enable_protobuf) {
    deps += [ "aec_dump:aec_dump_impl" ]
  } else {
    deps += [ "aec_dump:null_aec_dump_factory" ]
  }
}

rtc_library("residual_echo_detector") {
  poisonous = [ "default_echo_detector" ]
  configs += [ ":apm_debug_dump" ]
  sources = [
    "echo_detector/circular_buffer.cc",
    "echo_detector/circular_buffer.h",
    "echo_detector/mean_variance_estimator.cc",
    "echo_detector/mean_variance_estimator.h",
    "echo_detector/moving_max.cc",
    "echo_detector/moving_max.h",
    "echo_detector/normalized_covariance_estimator.cc",
    "echo_detector/normalized_covariance_estimator.h",
    "residual_echo_detector.cc",
    "residual_echo_detector.h",
  ]
  deps = [
    ":api",
    ":apm_logging",
    "../../api:array_view",
    "../../rtc_base:checks",
    "../../rtc_base:logging",
    "../../system_wrappers:metrics",
  ]
  absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
}

rtc_library("optionally_built_submodule_creators") {
  sources = [
    "optionally_built_submodule_creators.cc",
    "optionally_built_submodule_creators.h",
  ]
  deps = [
    "transient:transient_suppressor_api",
    "transient:transient_suppressor_impl",
  ]
}

rtc_source_set("rms_level") {
  visibility = [ "*" ]
  sources = [
    "rms_level.cc",
    "rms_level.h",
  ]
  deps = [
    "../../api:array_view",
    "../../rtc_base:checks",
  ]
  absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
}

rtc_library("audio_processing_statistics") {
  visibility = [ "*" ]
  sources = [
    "include/audio_processing_statistics.cc",
    "include/audio_processing_statistics.h",
  ]
  deps = [ "../../rtc_base/system:rtc_export" ]
  absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
}

rtc_source_set("audio_frame_view") {
  sources = [ "include/audio_frame_view.h" ]
  deps = [ "../../api:array_view" ]
}

if (rtc_enable_protobuf) {
  proto_library("audioproc_debug_proto") {
    sources = [ "debug.proto" ]

    proto_out_dir = "modules/audio_processing"
  }
}

rtc_library("apm_logging") {
  configs += [ ":apm_debug_dump" ]
  sources = [
    "logging/apm_data_dumper.cc",
    "logging/apm_data_dumper.h",
  ]
  deps = [
    "../../api:array_view",
    "../../common_audio",
    "../../rtc_base:checks",
    "../../rtc_base:stringutils",
  ]
  absl_deps = [
    "//third_party/abseil-cpp/absl/strings",
    "//third_party/abseil-cpp/absl/types:optional",
  ]
  defines = []
}

if (rtc_include_tests) {
  rtc_source_set("mocks") {
    testonly = true
    sources = [ "include/mock_audio_processing.h" ]
    deps = [
      ":aec_dump_interface",
      ":api",
      ":audio_buffer",
      ":audio_processing",
      ":audio_processing_statistics",
      "../../test:test_support",
    ]
    absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
  }

  if (!build_with_chromium) {
    group("audio_processing_tests") {
      testonly = true
      deps = [
        ":audioproc_test_utils",
        "transient:click_annotate",
        "transient:transient_suppression_test",
      ]

      if (rtc_enable_protobuf) {
        deps += [
          ":audioproc_unittest_proto",
          "aec_dump:aec_dump_unittests",
          "test/conversational_speech",
          "test/py_quality_assessment",
        ]
      }
    }

    rtc_library("audio_processing_unittests") {
      testonly = true

      configs += [ ":apm_debug_dump" ]
      sources = [
        "audio_buffer_unittest.cc",
        "audio_frame_view_unittest.cc",
        "echo_control_mobile_unittest.cc",
        "gain_controller2_unittest.cc",
        "splitting_filter_unittest.cc",
        "test/echo_canceller3_config_json_unittest.cc",
        "test/fake_recording_device_unittest.cc",
      ]

      deps = [
        ":aec3_config_json",
        ":analog_mic_simulation",
        ":api",
        ":apm_logging",
        ":audio_buffer",
        ":audio_frame_view",
        ":audio_processing",
        ":audioproc_test_utils",
        ":gain_controller2",
        ":high_pass_filter",
        ":mocks",
        "../../api:array_view",
        "../../api:make_ref_counted",
        "../../api:scoped_refptr",
        "../../api/audio:aec3_config",
        "../../api/audio:aec3_factory",
        "../../api/audio:echo_detector_creator",
        "../../common_audio",
        "../../common_audio:common_audio_c",
        "../../rtc_base:checks",
        "../../rtc_base:gtest_prod",
        "../../rtc_base:ignore_wundef",
        "../../rtc_base:macromagic",
        "../../rtc_base:platform_thread",
        "../../rtc_base:protobuf_utils",
        "../../rtc_base:random",
        "../../rtc_base:rtc_base_tests_utils",
        "../../rtc_base:rtc_event",
        "../../rtc_base:safe_conversions",
        "../../rtc_base:safe_minmax",
        "../../rtc_base:stringutils",
        "../../rtc_base:swap_queue",
        "../../rtc_base:task_queue_for_test",
        "../../rtc_base:threading",
        "../../rtc_base/synchronization:mutex",
        "../../rtc_base/system:arch",
        "../../rtc_base/system:file_wrapper",
        "../../system_wrappers",
        "../../system_wrappers:denormal_disabler",
        "../../test:field_trial",
        "../../test:fileutils",
        "../../test:rtc_expect_death",
        "../../test:test_support",
        "../audio_coding:neteq_input_audio_tools",
        "aec_dump:mock_aec_dump_unittests",
        "agc:agc_unittests",
        "agc2:adaptive_digital_gain_controller_unittest",
        "agc2:biquad_filter_unittests",
        "agc2:fixed_digital_unittests",
        "agc2:gain_applier_unittest",
        "agc2:input_volume_controller_unittests",
        "agc2:input_volume_stats_reporter_unittests",
        "agc2:noise_estimator_unittests",
        "agc2:saturation_protector_unittest",
        "agc2:speech_level_estimator_unittest",
        "agc2:test_utils",
        "agc2:vad_wrapper_unittests",
        "agc2/rnn_vad:unittests",
        "capture_levels_adjuster",
        "capture_levels_adjuster:capture_levels_adjuster_unittests",
        "test/conversational_speech:unittest",
        "transient:transient_suppression_unittests",
        "utility:legacy_delay_estimator_unittest",
        "utility:pffft_wrapper_unittest",
        "vad:vad_unittests",
        "//testing/gtest",
      ]
      absl_deps = [
        "//third_party/abseil-cpp/absl/strings",
        "//third_party/abseil-cpp/absl/types:optional",
      ]

      defines = []

      if (rtc_prefer_fixed_point) {
        defines += [ "WEBRTC_AUDIOPROC_FIXED_PROFILE" ]
      } else {
        defines += [ "WEBRTC_AUDIOPROC_FLOAT_PROFILE" ]
      }

      if (rtc_enable_protobuf) {
        defines += [ "WEBRTC_AUDIOPROC_DEBUG_DUMP" ]
        deps += [
          ":audioproc_debug_proto",
          ":audioproc_protobuf_utils",
          ":audioproc_test_utils",
          ":audioproc_unittest_proto",
          ":optionally_built_submodule_creators",
          ":residual_echo_detector",
          ":rms_level",
          ":runtime_settings_protobuf_utils",
          "../../api/audio:audio_frame_api",
          "../../api/audio:echo_control",
          "../../rtc_base:rtc_base_tests_utils",
          "../../rtc_base:rtc_task_queue",
          "aec_dump",
          "aec_dump:aec_dump_unittests",
        ]
        absl_deps += [ "//third_party/abseil-cpp/absl/flags:flag" ]
        sources += [
          "audio_processing_impl_locking_unittest.cc",
          "audio_processing_impl_unittest.cc",
          "audio_processing_unittest.cc",
          "echo_control_mobile_bit_exact_unittest.cc",
          "echo_detector/circular_buffer_unittest.cc",
          "echo_detector/mean_variance_estimator_unittest.cc",
          "echo_detector/moving_max_unittest.cc",
          "echo_detector/normalized_covariance_estimator_unittest.cc",
          "gain_control_unittest.cc",
          "high_pass_filter_unittest.cc",
          "residual_echo_detector_unittest.cc",
          "rms_level_unittest.cc",
          "test/debug_dump_replayer.cc",
          "test/debug_dump_replayer.h",
          "test/debug_dump_test.cc",
          "test/echo_canceller_test_tools.cc",
          "test/echo_canceller_test_tools.h",
          "test/echo_canceller_test_tools_unittest.cc",
          "test/echo_control_mock.h",
          "test/test_utils.h",
        ]
      }
    }
  }

  rtc_library("audio_processing_perf_tests") {
    testonly = true
    configs += [ ":apm_debug_dump" ]

    sources = [ "audio_processing_performance_unittest.cc" ]
    deps = [
      ":audio_processing",
      ":audioproc_test_utils",
      "../../api:array_view",
      "../../api/numerics",
      "../../api/test/metrics:global_metrics_logger_and_exporter",
      "../../api/test/metrics:metric",
      "../../rtc_base:platform_thread",
      "../../rtc_base:protobuf_utils",
      "../../rtc_base:random",
      "../../rtc_base:rtc_event",
      "../../rtc_base:safe_conversions",
      "../../system_wrappers",
      "../../test:test_support",
    ]
    absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
  }

  rtc_library("analog_mic_simulation") {
    sources = [
      "test/fake_recording_device.cc",
      "test/fake_recording_device.h",
    ]
    deps = [
      "../../api:array_view",
      "../../api/audio:audio_frame_api",
      "../../common_audio",
      "../../rtc_base:checks",
      "../../rtc_base:logging",
      "../../rtc_base:safe_conversions",
      "../../rtc_base:safe_minmax",
      "agc2:gain_map",
    ]
    absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
  }

  if (rtc_enable_protobuf && !build_with_chromium) {
    rtc_library("audioproc_f_impl") {
      testonly = true
      configs += [ ":apm_debug_dump" ]
      sources = [
        "test/aec_dump_based_simulator.cc",
        "test/aec_dump_based_simulator.h",
        "test/api_call_statistics.cc",
        "test/api_call_statistics.h",
        "test/audio_processing_simulator.cc",
        "test/audio_processing_simulator.h",
        "test/audioproc_float_impl.cc",
        "test/audioproc_float_impl.h",
        "test/wav_based_simulator.cc",
        "test/wav_based_simulator.h",
      ]

      deps = [
        ":aec3_config_json",
        ":analog_mic_simulation",
        ":api",
        ":apm_logging",
        ":audio_processing",
        ":audioproc_debug_proto",
        ":audioproc_protobuf_utils",
        ":audioproc_test_utils",
        ":runtime_settings_protobuf_utils",
        "../../api/audio:aec3_factory",
        "../../api/audio:echo_detector_creator",
        "../../common_audio",
        "../../rtc_base:checks",
        "../../rtc_base:ignore_wundef",
        "../../rtc_base:logging",
        "../../rtc_base:protobuf_utils",
        "../../rtc_base:rtc_json",
        "../../rtc_base:safe_conversions",
        "../../rtc_base:stringutils",
        "../../rtc_base:task_queue_for_test",
        "../../rtc_base:timeutils",
        "../../rtc_base/system:file_wrapper",
        "../../system_wrappers",
        "../../system_wrappers:field_trial",
        "../../test:test_support",
        "aec_dump",
        "aec_dump:aec_dump_impl",
        "//testing/gtest",
      ]
      absl_deps = [
        "//third_party/abseil-cpp/absl/flags:flag",
        "//third_party/abseil-cpp/absl/flags:parse",
        "//third_party/abseil-cpp/absl/strings",
        "//third_party/abseil-cpp/absl/types:optional",
      ]
    }  # audioproc_f_impl
  }

  if (rtc_enable_protobuf) {
    proto_library("audioproc_unittest_proto") {
      sources = [ "test/unittest.proto" ]
      proto_out_dir = "modules/audio_processing/test"
    }

    rtc_library("audioproc_protobuf_utils") {
      sources = [
        "test/protobuf_utils.cc",
        "test/protobuf_utils.h",
      ]

      deps = [
        ":audioproc_debug_proto",
        "../../rtc_base:checks",
        "../../rtc_base:ignore_wundef",
        "../../rtc_base:protobuf_utils",
        "../../rtc_base/system:arch",
      ]
    }

    rtc_library("runtime_settings_protobuf_utils") {
      testonly = true
      sources = [
        "test/runtime_setting_util.cc",
        "test/runtime_setting_util.h",
      ]

      deps = [
        ":api",
        ":audioproc_debug_proto",
        ":audioproc_protobuf_utils",
        "../../rtc_base:checks",
      ]
    }
  }
}

rtc_library("audioproc_test_utils") {
  visibility = [ "*" ]
  testonly = true
  sources = [
    "test/audio_buffer_tools.cc",
    "test/audio_buffer_tools.h",
    "test/audio_processing_builder_for_testing.cc",
    "test/audio_processing_builder_for_testing.h",
    "test/bitexactness_tools.cc",
    "test/bitexactness_tools.h",
    "test/performance_timer.cc",
    "test/performance_timer.h",
    "test/simulator_buffers.cc",
    "test/simulator_buffers.h",
    "test/test_utils.cc",
    "test/test_utils.h",
  ]

  configs += [ ":apm_debug_dump" ]

  deps = [
    ":api",
    ":audio_buffer",
    ":audio_processing",
    "../../api:array_view",
    "../../api/audio:audio_frame_api",
    "../../common_audio",
    "../../rtc_base:checks",
    "../../rtc_base:random",
    "../../rtc_base/system:arch",
    "../../system_wrappers",
    "../../test:fileutils",
    "../../test:test_support",
    "../audio_coding:neteq_input_audio_tools",
    "//testing/gtest",
  ]
  absl_deps = [
    "//third_party/abseil-cpp/absl/strings",
    "//third_party/abseil-cpp/absl/types:optional",
  ]
}

rtc_library("aec3_config_json") {
  visibility = [ "*" ]
  testonly = true
  sources = [
    "test/echo_canceller3_config_json.cc",
    "test/echo_canceller3_config_json.h",
  ]
  deps = [
    "../../api/audio:aec3_config",
    "../../rtc_base:checks",
    "../../rtc_base:logging",
    "../../rtc_base:rtc_json",
    "../../rtc_base:stringutils",
    "../../rtc_base/system:rtc_export",
  ]
  absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
}
