chromium/media/audio/BUILD.gn

# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/chromeos/ui_mode.gni")
import("//build/config/linux/pkg_config.gni")
import("//media/media_options.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("//tools/generate_stubs/rules.gni")

# When libpulse is not directly linked, use stubs to allow for dlopening of the
# binary.
if (use_pulseaudio && !link_pulseaudio) {
  generate_stubs("libpulse_stubs") {
    extra_header = "pulse/pulse_stub_header.fragment"
    sigs = [ "pulse/pulse.sigs" ]
    output_name = "pulse/pulse_stubs"
    deps = [ "//base" ]
  }
}

config("platform_config") {
  defines = []
  if (use_alsa) {
    defines += [ "USE_ALSA" ]
  }
}

source_set("audio") {
  # Do not expand the visibility here without double-checking with OWNERS, this
  # is a roll-up target which is part of the //media component. Most other DEPs
  # should be using //media and not directly DEP this roll-up target.
  visibility = [
    ":flac_audio_handler_fuzzer",
    ":wav_audio_handler_fuzzer",
    "//media",
    "//media/renderers",

    # TODO(dalecurtis): CoreAudioUtil::IsCoreAudioSupported() should probably
    # move into media/base/win.
    "//media/device_monitors",

    # TODO(dalecurtis): Move android audio pieces into //media/audio.
    "//media/base/android",
  ]
  sources = [
    "aecdump_recording_manager.cc",
    "aecdump_recording_manager.h",
    "agc_audio_stream.h",
    "alive_checker.cc",
    "alive_checker.h",
    "audio_debug_file_writer.cc",
    "audio_debug_file_writer.h",
    "audio_debug_recording_helper.cc",
    "audio_debug_recording_helper.h",
    "audio_debug_recording_manager.cc",
    "audio_debug_recording_manager.h",
    "audio_debug_recording_session.h",
    "audio_device_description.cc",
    "audio_device_description.h",
    "audio_device_name.cc",
    "audio_device_name.h",
    "audio_device_stats_reporter.cc",
    "audio_device_stats_reporter.h",
    "audio_device_thread.cc",
    "audio_device_thread.h",
    "audio_features.cc",
    "audio_features.h",
    "audio_handler.h",
    "audio_input_delegate.cc",
    "audio_input_delegate.h",
    "audio_input_device.cc",
    "audio_input_device.h",
    "audio_input_ipc.cc",
    "audio_input_ipc.h",
    "audio_input_stream_data_interceptor.cc",
    "audio_input_stream_data_interceptor.h",
    "audio_io.cc",
    "audio_io.h",
    "audio_manager.cc",
    "audio_manager.h",
    "audio_manager_base.cc",
    "audio_manager_base.h",
    "audio_opus_encoder.cc",
    "audio_opus_encoder.h",
    "audio_output_device.cc",
    "audio_output_device.h",
    "audio_output_device_thread_callback.cc",
    "audio_output_device_thread_callback.h",
    "audio_output_dispatcher.cc",
    "audio_output_dispatcher.h",
    "audio_output_dispatcher_impl.cc",
    "audio_output_dispatcher_impl.h",
    "audio_output_ipc.cc",
    "audio_output_ipc.h",
    "audio_output_proxy.cc",
    "audio_output_proxy.h",
    "audio_output_resampler.cc",
    "audio_output_resampler.h",
    "audio_sink_parameters.cc",
    "audio_sink_parameters.h",
    "audio_source_parameters.cc",
    "audio_source_parameters.h",
    "audio_system.cc",
    "audio_system.h",
    "audio_system_helper.cc",
    "audio_system_helper.h",
    "audio_system_impl.cc",
    "audio_system_impl.h",
    "audio_thread.h",
    "audio_thread_hang_monitor.cc",
    "audio_thread_hang_monitor.h",
    "audio_thread_impl.cc",
    "audio_thread_impl.h",
    "clockless_audio_sink.cc",
    "clockless_audio_sink.h",
    "fake_audio_input_stream.cc",
    "fake_audio_input_stream.h",
    "fake_audio_log_factory.cc",
    "fake_audio_log_factory.h",
    "fake_audio_manager.cc",
    "fake_audio_manager.h",
    "fake_audio_output_stream.cc",
    "fake_audio_output_stream.h",
    "flac_audio_handler.cc",
    "flac_audio_handler.h",
    "null_audio_sink.cc",
    "null_audio_sink.h",
    "power_observer_helper.cc",
    "power_observer_helper.h",
    "reconfigurable_audio_bus_pool.cc",
    "reconfigurable_audio_bus_pool.h",
    "scoped_task_runner_observer.cc",
    "scoped_task_runner_observer.h",
    "simple_sources.cc",
    "simple_sources.h",
    "system_glitch_reporter.cc",
    "system_glitch_reporter.h",
    "wav_audio_handler.cc",
    "wav_audio_handler.h",
  ]
  deps = [
    "//base",
    "//build:chromecast_buildflags",
    "//build:chromeos_buildflags",
    "//media/base",
    "//third_party/flac",
    "//third_party/opus:opus",
    "//url",
  ]
  libs = []
  defines = []
  configs += [
    ":platform_config",
    "//media:subcomponent_config",
  ]

  if (is_apple) {
    sources += [
      "apple/audio_auhal.cc",
      "apple/audio_auhal.h",
      "apple/audio_input.cc",
      "apple/audio_input.h",
      "apple/audio_low_latency_input.cc",
      "apple/audio_low_latency_input.h",
      "apple/audio_manager_apple.cc",
      "apple/audio_manager_apple.h",
      "apple/scoped_audio_unit.cc",
      "apple/scoped_audio_unit.h",
    ]

    deps += [ "//media/base/mac" ]

    frameworks = [
      "AudioToolbox.framework",
      "CoreAudio.framework",
      "CoreFoundation.framework",
    ]
  }

  if (is_mac) {
    sources += [
      "mac/audio_device_listener_mac.cc",
      "mac/audio_device_listener_mac.h",
      "mac/audio_loopback_input_mac.h",
      "mac/audio_loopback_input_mac_impl.h",
      "mac/audio_loopback_input_mac_impl.mm",
      "mac/audio_manager_mac.cc",
      "mac/audio_manager_mac.h",
      "mac/core_audio_util_mac.cc",
      "mac/core_audio_util_mac.h",
      "mac/screen_capture_kit_swizzler.h",
      "mac/screen_capture_kit_swizzler.mm",
    ]
    frameworks += [
      "AudioUnit.framework",
      "CoreMedia.framework",
    ]
    weak_frameworks = [ "ScreenCaptureKit.framework" ]  # macOS 13.0
  }

  if (is_ios) {
    sources += [
      "ios/audio_manager_ios.cc",
      "ios/audio_manager_ios.h",
      "ios/audio_private_api.h",
      "ios/audio_session_manager_ios.h",
      "ios/audio_session_manager_ios.mm",
    ]

    frameworks += [ "AVFoundation.framework" ]
  }

  if (is_win) {
    sources += [
      "win/audio_device_listener_win.cc",
      "win/audio_device_listener_win.h",
      "win/audio_low_latency_input_win.cc",
      "win/audio_low_latency_input_win.h",
      "win/audio_low_latency_output_win.cc",
      "win/audio_low_latency_output_win.h",
      "win/audio_manager_win.cc",
      "win/audio_manager_win.h",
      "win/audio_session_event_listener_win.cc",
      "win/audio_session_event_listener_win.h",
      "win/avrt_wrapper_win.cc",
      "win/avrt_wrapper_win.h",
      "win/core_audio_util_win.cc",
      "win/core_audio_util_win.h",
      "win/device_enumeration_win.cc",
      "win/device_enumeration_win.h",
      "win/waveout_output_win.cc",
      "win/waveout_output_win.h",
    ]

    deps += [ "//media/base/win:media_foundation_util" ]

    libs += [
      "dxguid.lib",
      "setupapi.lib",
      "winmm.lib",
    ]
  }

  if (is_android) {
    sources += [
      "android/aaudio_input.cc",
      "android/aaudio_input.h",
      "android/aaudio_output.cc",
      "android/aaudio_output.h",
      "android/aaudio_stream_wrapper.cc",
      "android/aaudio_stream_wrapper.h",
      "android/audio_manager_android.cc",
      "android/audio_manager_android.h",
      "android/audio_track_output_stream.cc",
      "android/audio_track_output_stream.h",
      "android/muteable_audio_output_stream.h",
      "android/opensles_input.cc",
      "android/opensles_input.h",
      "android/opensles_output.cc",
      "android/opensles_output.h",
      "android/opensles_util.cc",
      "android/opensles_util.h",
      "android/opensles_wrapper.cc",
    ]
    libs += [ "aaudio" ]

    deps += [ "//media/base/android:media_jni_headers" ]
  }

  if (is_linux || is_chromeos) {
    sources += [ "linux/audio_manager_linux.cc" ]
  }

  if (use_alsa) {
    libs += [ "asound" ]
    sources += [
      "alsa/alsa_input.cc",
      "alsa/alsa_input.h",
      "alsa/alsa_output.cc",
      "alsa/alsa_output.h",
      "alsa/alsa_util.cc",
      "alsa/alsa_util.h",
      "alsa/alsa_wrapper.cc",
      "alsa/alsa_wrapper.h",
      "alsa/audio_manager_alsa.cc",
      "alsa/audio_manager_alsa.h",
    ]
  }

  if (use_cras) {
    sources += [
      "cras/audio_manager_cras.cc",
      "cras/audio_manager_cras.h",
      "cras/audio_manager_cras_base.cc",
      "cras/audio_manager_cras_base.h",
      "cras/cras_input.cc",
      "cras/cras_input.h",
      "cras/cras_unified.cc",
      "cras/cras_unified.h",
      "cras/cras_util.cc",
      "cras/cras_util.h",
    ]
    configs += [ ":libcras" ]
  }

  if (use_pulseaudio) {
    sources += [
      "pulse/audio_manager_pulse.cc",
      "pulse/audio_manager_pulse.h",
      "pulse/pulse_input.cc",
      "pulse/pulse_input.h",
      "pulse/pulse_loopback.cc",
      "pulse/pulse_loopback.h",
      "pulse/pulse_loopback_manager.cc",
      "pulse/pulse_loopback_manager.h",
      "pulse/pulse_output.cc",
      "pulse/pulse_output.h",
      "pulse/pulse_util.cc",
      "pulse/pulse_util.h",
    ]

    deps += [ "//build:branding_buildflags" ]

    if (link_pulseaudio) {
      configs += [ ":libpulse" ]
    } else {
      deps += [ ":libpulse_stubs" ]
    }
  }

  if (is_fuchsia) {
    sources += [
      "fuchsia/audio_input_stream_fuchsia.cc",
      "fuchsia/audio_input_stream_fuchsia.h",
      "fuchsia/audio_manager_fuchsia.cc",
      "fuchsia/audio_manager_fuchsia.h",
      "fuchsia/audio_output_stream_fuchsia.cc",
      "fuchsia/audio_output_stream_fuchsia.h",
    ]
    deps += [
      "//media/fuchsia/common",
      "//third_party/fuchsia-sdk/sdk/fidl/fuchsia.media:fuchsia.media_hlcpp",
      "//third_party/fuchsia-sdk/sdk/pkg/sys_cpp",
    ]
  }
}

if (use_cras) {
  pkg_config("libcras") {
    packages = [ "libcras" ]
  }
}

if (use_pulseaudio && link_pulseaudio) {
  pkg_config("libpulse") {
    packages = [ "libpulse" ]
  }
}

# Note: This is a roll-up only target; do not expand the visibility. DEPS should
# depend on the //media:test_support target instead.
static_library("test_support") {
  visibility = [ "//media:test_support" ]
  testonly = true
  sources = [
    "audio_debug_recording_test.cc",
    "audio_debug_recording_test.h",
    "audio_device_info_accessor_for_tests.cc",
    "audio_device_info_accessor_for_tests.h",
    "audio_system_test_util.cc",
    "audio_system_test_util.h",
    "audio_unittest_util.h",
    "mock_aecdump_recording_manager.cc",
    "mock_aecdump_recording_manager.h",
    "mock_audio_debug_recording_manager.cc",
    "mock_audio_debug_recording_manager.h",
    "mock_audio_manager.cc",
    "mock_audio_manager.h",
    "mock_audio_source_callback.cc",
    "mock_audio_source_callback.h",
    "test_audio_thread.cc",
    "test_audio_thread.h",
  ]
  configs += [
    ":platform_config",
    "//media:media_config",
  ]
  deps = [
    "//base",
    "//base/test:test_support",
    "//build:chromeos_buildflags",

    # Do not add any other //media deps except this; it will automatically pull
    # a dep on //media which is required to ensure test_support targets all use
    # the same //media component and not build a target's sources individually.
    "//media/base:test_support",
    "//testing/gmock",
    "//testing/gtest",
  ]
  if (use_alsa) {
    sources += [
      "alsa/mock_alsa_wrapper.cc",
      "alsa/mock_alsa_wrapper.h",
    ]
  }
}

source_set("unit_tests") {
  testonly = true
  sources = [
    "aecdump_recording_manager_unittest.cc",
    "alive_checker_unittest.cc",
    "audio_debug_file_writer_unittest.cc",
    "audio_debug_recording_helper_unittest.cc",
    "audio_debug_recording_manager_unittest.cc",
    "audio_device_description_unittest.cc",
    "audio_device_stats_reporter_unittest.cc",
    "audio_encoders_unittest.cc",
    "audio_input_device_unittest.cc",
    "audio_input_stream_data_interceptor_unittest.cc",
    "audio_input_unittest.cc",
    "audio_manager_unittest.cc",
    "audio_output_device_unittest.cc",
    "audio_output_proxy_unittest.cc",
    "audio_output_unittest.cc",
    "audio_system_impl_unittest.cc",
    "audio_thread_hang_monitor_unittest.cc",
    "power_observer_helper_unittest.cc",
    "reconfigurable_audio_bus_pool_unittest.cc",
    "simple_sources_unittest.cc",
  ]

  deps = [
    "//base",
    "//base/test:test_support",
    "//build:chromeos_buildflags",
    "//media:test_support",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/opus:opus",
    "//url",
  ]

  configs += [
    ":platform_config",
    "//media:media_config",
  ]

  if (is_android) {
    sources += [ "android/audio_android_unittest.cc" ]
    deps += [
      "//media/gpu:gpu",
      "//ui/gl",
    ]
  }

  if (is_apple) {
    sources += [
      "apple/audio_auhal_unittest.cc",
      "apple/audio_low_latency_input_unittest.cc",
    ]
  }

  if (is_mac) {
    sources += [
      "mac/audio_device_listener_mac_unittest.cc",
      "mac/audio_loopback_input_mac_unittest.mm",
      "mac/audio_manager_mac_unittest.cc",
    ]
    deps += [ "//third_party/ocmock" ]
    frameworks = [ "CoreMedia.framework" ]
    weak_frameworks = [ "ScreenCaptureKit.framework" ]  # macOS 13.0
  }

  if (is_chromeos_ash || is_castos || is_cast_android) {
    sources += [
      "flac_audio_handler_unittest.cc",
      "test_data.h",
      "wav_audio_handler_unittest.cc",
    ]
  }

  if (use_cras) {
    sources += [
      "cras/audio_manager_cras_base_unittest.cc",
      "cras/audio_manager_cras_unittest.cc",
      "cras/cras_input_unittest.cc",
      "cras/cras_unified_unittest.cc",
    ]
  }

  if (is_win) {
    sources += [
      "win/audio_device_listener_win_unittest.cc",
      "win/audio_low_latency_input_win_unittest.cc",
      "win/audio_low_latency_output_win_unittest.cc",
      "win/audio_output_win_unittest.cc",
      "win/audio_session_event_listener_win_unittest.cc",
      "win/core_audio_util_win_unittest.cc",
      "win/device_enumeration_win_unittest.cc",
    ]

    deps += [ "//media/gpu:gpu" ]
  }

  if (is_fuchsia) {
    sources += [ "fuchsia/audio_input_stream_fuchsia_test.cc" ]
    deps += [ "//media/fuchsia/audio:test_support" ]
  }

  if (use_alsa) {
    sources += [
      "alsa/alsa_output_unittest.cc",
      "alsa/alsa_util_unittest.cc",
      "audio_low_latency_input_output_unittest.cc",
    ]
  }
}

fuzzer_test("wav_audio_handler_fuzzer") {
  sources = [ "wav_audio_handler_fuzzer.cc" ]
  deps = [
    ":audio",
    "//base",
    "//media:test_support",
  ]
}

fuzzer_test("flac_audio_handler_fuzzer") {
  sources = [ "flac_audio_handler_fuzzer.cc" ]
  deps = [
    ":audio",
    "//base",
    "//media:test_support",
  ]
}