# Copyright (c) 2016 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 (is_android) {
  import("//build/config/android/config.gni")
  import("//build/config/android/rules.gni")
}

group("pc") {
  public_deps = [
    ":rtc_pc",
  ]
}

config("rtc_pc_config") {
  defines = []
  if (rtc_enable_sctp) {
    defines += [ "HAVE_SCTP" ]
  }
}

rtc_static_library("rtc_pc_base") {
  defines = []
  sources = [
    "audiomonitor.cc",
    "audiomonitor.h",
    "bundlefilter.cc",
    "bundlefilter.h",
    "channel.cc",
    "channel.h",
    "channelmanager.cc",
    "channelmanager.h",
    "currentspeakermonitor.cc",
    "currentspeakermonitor.h",
    "dtlssrtptransport.cc",
    "dtlssrtptransport.h",
    "externalhmac.cc",
    "externalhmac.h",
    "mediamonitor.cc",
    "mediamonitor.h",
    "mediasession.cc",
    "mediasession.h",
    "rtcpmuxfilter.cc",
    "rtcpmuxfilter.h",
    "rtpmediautils.cc",
    "rtpmediautils.h",
    "rtptransport.cc",
    "rtptransport.h",
    "rtptransportinternal.h",
    "rtptransportinternaladapter.h",
    "srtpfilter.cc",
    "srtpfilter.h",
    "srtpsession.cc",
    "srtpsession.h",
    "srtptransport.cc",
    "srtptransport.h",
    "transportcontroller.cc",
    "transportcontroller.h",
    "voicechannel.h",
  ]

  deps = [
    "..:webrtc_common",
    "../api:call_api",
    "../api:libjingle_peerconnection_api",
    "../api:optional",
    "../api:ortc_api",
    "../media:rtc_data",
    "../media:rtc_h264_profile_id",
    "../media:rtc_media_base",
    "../p2p:rtc_p2p",
    "../rtc_base:rtc_base",
    "../rtc_base:rtc_task_queue",
  ]

  if (rtc_build_libsrtp) {
    deps += [ "//third_party/libsrtp" ]
  }

  public_configs = [ ":rtc_pc_config" ]

  if (!build_with_chromium && is_clang) {
    # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163).
    suppressed_configs += [ "//build/config/clang:find_bad_constructs" ]
  }
}

rtc_source_set("rtc_pc") {
  public_deps = [
    ":rtc_pc_base",
  ]

  deps = [
    "../media:rtc_audio_video",
  ]
}

config("libjingle_peerconnection_warnings_config") {
  # GN orders flags on a target before flags from configs. The default config
  # adds these flags so to cancel them out they need to come from a config and
  # cannot be on the target directly.
  if (!is_win && !is_clang) {
    cflags = [ "-Wno-maybe-uninitialized" ]  # Only exists for GCC.
  }
}

rtc_static_library("peerconnection") {
  cflags = []
  sources = [
    "audiotrack.cc",
    "audiotrack.h",
    "datachannel.cc",
    "datachannel.h",
    "dtmfsender.cc",
    "dtmfsender.h",
    "iceserverparsing.cc",
    "iceserverparsing.h",
    "jsepicecandidate.cc",
    "jsepsessiondescription.cc",
    "localaudiosource.cc",
    "localaudiosource.h",
    "mediastream.cc",
    "mediastream.h",
    "mediastreamobserver.cc",
    "mediastreamobserver.h",
    "mediastreamtrack.h",
    "peerconnection.cc",
    "peerconnection.h",
    "peerconnectionfactory.cc",
    "peerconnectionfactory.h",
    "remoteaudiosource.cc",
    "remoteaudiosource.h",
    "rtcstatscollector.cc",
    "rtcstatscollector.h",
    "rtpreceiver.cc",
    "rtpreceiver.h",
    "rtpsender.cc",
    "rtpsender.h",
    "rtptransceiver.cc",
    "rtptransceiver.h",
    "sctputils.cc",
    "sctputils.h",
    "sdputils.cc",
    "sdputils.h",
    "statscollector.cc",
    "statscollector.h",
    "streamcollection.h",
    "trackmediainfomap.cc",
    "trackmediainfomap.h",
    "videocapturertracksource.cc",
    "videocapturertracksource.h",
    "videotrack.cc",
    "videotrack.h",
    "videotracksource.cc",
    "videotracksource.h",
    "webrtcsdp.cc",
    "webrtcsdp.h",
    "webrtcsessiondescriptionfactory.cc",
    "webrtcsessiondescriptionfactory.h",
  ]

  configs += [ ":libjingle_peerconnection_warnings_config" ]

  if (!build_with_chromium && is_clang) {
    # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163).
    suppressed_configs += [ "//build/config/clang:find_bad_constructs" ]
  }

  deps = [
    ":rtc_pc_base",
    "..:webrtc_common",
    "../api:call_api",
    "../api:optional",
    "../api:rtc_stats_api",
    "../api/video_codecs:video_codecs_api",
    "../call:call_interfaces",
    "../logging:rtc_event_log_api",
    "../media:rtc_data",
    "../media:rtc_media_base",
    "../p2p:rtc_p2p",
    "../rtc_base:rtc_base",
    "../rtc_base:rtc_base_approved",
    "../stats",
    "../system_wrappers:system_wrappers",
  ]

  public_deps = [
    "../api:libjingle_peerconnection_api",
  ]
}

# This target implements CreatePeerConnectionFactory methods that will create a
# PeerConnection will full functionality (audio, video and data). Applications
# that wish to reduce their binary size by ommitting functionality they don't
# need should use CreateModularCreatePeerConnectionFactory instead, using the
# "peerconnection" build target and other targets specific to their
# requrements. See comment in peerconnectionfactoryinterface.h.
rtc_static_library("create_pc_factory") {
  sources = [
    "createpeerconnectionfactory.cc",
  ]

  deps = [
    "../api:audio_mixer_api",
    "../api:libjingle_peerconnection_api",
    "../api:peerconnection_and_implicit_call_api",
    "../api/audio_codecs:audio_codecs_api",
    "../api/video_codecs:video_codecs_api",
    "../call",
    "../call:call_interfaces",
    "../logging:rtc_event_log_api",
    "../media:rtc_audio_video",
    "../media:rtc_media_base",
    "../modules/audio_device:audio_device",
    "../modules/audio_processing:audio_processing",
    "../rtc_base:rtc_base",
    "../rtc_base:rtc_base_approved",
  ]

  configs += [ ":libjingle_peerconnection_warnings_config" ]

  if (!build_with_chromium && is_clang) {
    # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163).
    suppressed_configs += [ "//build/config/clang:find_bad_constructs" ]
  }
}

rtc_source_set("libjingle_peerconnection") {
  public_deps = [
    ":create_pc_factory",
    ":peerconnection",
    "../api:libjingle_peerconnection_api",
  ]
}

if (rtc_include_tests) {
  config("rtc_pc_unittests_config") {
    # GN orders flags on a target before flags from configs. The default config
    # adds -Wall, and this flag have to be after -Wall -- so they need to
    # come from a config and can't be on the target directly.
    if (!is_win && !is_clang) {
      cflags = [ "-Wno-maybe-uninitialized" ]  # Only exists for GCC.
    }
  }

  rtc_test("rtc_pc_unittests") {
    testonly = true

    sources = [
      "bundlefilter_unittest.cc",
      "channel_unittest.cc",
      "channelmanager_unittest.cc",
      "currentspeakermonitor_unittest.cc",
      "dtlssrtptransport_unittest.cc",
      "mediasession_unittest.cc",
      "rtcpmuxfilter_unittest.cc",
      "rtptransport_unittest.cc",
      "rtptransporttestutil.h",
      "srtpfilter_unittest.cc",
      "srtpsession_unittest.cc",
      "srtptestutil.h",
      "srtptransport_unittest.cc",
      "transportcontroller_unittest.cc",
    ]

    include_dirs = [ "//third_party/libsrtp/srtp" ]

    configs += [ ":rtc_pc_unittests_config" ]

    if (!build_with_chromium && is_clang) {
      # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163).
      suppressed_configs += [ "//build/config/clang:find_bad_constructs" ]
    }

    if (is_win) {
      libs = [ "strmiids.lib" ]
    }

    deps = [
      ":libjingle_peerconnection",
      ":pc_test_utils",
      ":rtc_pc",
      "../api:array_view",
      "../logging:rtc_event_log_api",
      "../media:rtc_media_base",
      "../media:rtc_media_tests_utils",
      "../p2p:p2p_test_utils",
      "../p2p:rtc_p2p",
      "../rtc_base:rtc_base",
      "../rtc_base:rtc_base_approved",
      "../rtc_base:rtc_base_tests_main",
      "../rtc_base:rtc_base_tests_utils",
      "../system_wrappers:metrics_default",
      "../test:test_support",
    ]

    if (rtc_build_libsrtp) {
      deps += [ "//third_party/libsrtp" ]
    }

    if (is_android) {
      deps += [ "//testing/android/native_test:native_test_support" ]
    }
  }

  rtc_source_set("pc_test_utils") {
    testonly = true
    sources = [
      "test/fakeaudiocapturemodule.cc",
      "test/fakeaudiocapturemodule.h",
      "test/fakedatachannelprovider.h",
      "test/fakeperiodicvideocapturer.h",
      "test/fakertccertificategenerator.h",
      "test/fakesctptransport.h",
      "test/faketransportcontroller.h",
      "test/fakevideotrackrenderer.h",
      "test/fakevideotracksource.h",
      "test/mock_datachannel.h",
      "test/mock_peerconnection.h",
      "test/mockpeerconnectionobservers.h",
      "test/peerconnectiontestwrapper.cc",
      "test/peerconnectiontestwrapper.h",
      "test/rtcstatsobtainer.h",
      "test/testsdpstrings.h",
    ]

    deps = [
      ":libjingle_peerconnection",
      ":rtc_pc_base",
      "..:webrtc_common",
      "../api:libjingle_peerconnection_test_api",
      "../api:peerconnection_and_implicit_call_api",
      "../api:rtc_stats_api",
      "../call:call_interfaces",
      "../logging:rtc_event_log_api",
      "../media:rtc_media",
      "../media:rtc_media_base",
      "../media:rtc_media_tests_utils",
      "../modules/audio_device:audio_device",
      "../p2p:p2p_test_utils",
      "../rtc_base:rtc_base",
      "../rtc_base:rtc_base_approved",
      "../rtc_base:rtc_base_tests_utils",
      "../test:test_support",
      "//testing/gmock",
    ]

    if (!build_with_chromium && is_clang) {
      # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163).
      suppressed_configs += [ "//build/config/clang:find_bad_constructs" ]
    }
  }

  config("peerconnection_unittests_config") {
    # The warnings below are enabled by default. Since GN orders compiler flags
    # for a target before flags from configs, the only way to disable such
    # warnings is by having them in a separate config, loaded from the target.
    # TODO(kjellander): Make the code compile without disabling these flags.
    # See https://bugs.webrtc.org/3307.
    if (is_clang && is_win) {
      cflags = [
        # See https://bugs.chromium.org/p/webrtc/issues/detail?id=6267
        # for -Wno-sign-compare
        "-Wno-sign-compare",
      ]
    }

    if (!is_win) {
      cflags = [ "-Wno-sign-compare" ]
    }
  }

  rtc_test("peerconnection_unittests") {
    testonly = true
    sources = [
      "datachannel_unittest.cc",
      "dtmfsender_unittest.cc",
      "iceserverparsing_unittest.cc",
      "jsepsessiondescription_unittest.cc",
      "localaudiosource_unittest.cc",
      "mediaconstraintsinterface_unittest.cc",
      "mediastream_unittest.cc",
      "peerconnection_bundle_unittest.cc",
      "peerconnection_crypto_unittest.cc",
      "peerconnection_datachannel_unittest.cc",
      "peerconnection_ice_unittest.cc",
      "peerconnection_integrationtest.cc",
      "peerconnection_media_unittest.cc",
      "peerconnection_rtp_unittest.cc",
      "peerconnection_signaling_unittest.cc",
      "peerconnectionendtoend_unittest.cc",
      "peerconnectionfactory_unittest.cc",
      "peerconnectioninterface_unittest.cc",
      "peerconnectionwrapper.cc",
      "peerconnectionwrapper.h",
      "proxy_unittest.cc",
      "rtcstats_integrationtest.cc",
      "rtcstatscollector_unittest.cc",
      "rtpmediautils_unittest.cc",
      "rtpsenderreceiver_unittest.cc",
      "sctputils_unittest.cc",
      "statscollector_unittest.cc",
      "test/fakeaudiocapturemodule_unittest.cc",
      "test/testsdpstrings.h",
      "trackmediainfomap_unittest.cc",
      "videocapturertracksource_unittest.cc",
      "videotrack_unittest.cc",
      "webrtcsdp_unittest.cc",
    ]

    if (rtc_enable_sctp) {
      defines = [ "HAVE_SCTP" ]
    }

    configs += [ ":peerconnection_unittests_config" ]

    if (!build_with_chromium && is_clang) {
      # Suppress warnings from the Chromium Clang plugin (bugs.webrtc.org/163).
      suppressed_configs += [ "//build/config/clang:find_bad_constructs" ]
    }

    # TODO(jschuh): Bug 1348: fix this warning.
    configs += [ "//build/config/compiler:no_size_t_to_int_warning" ]

    if (is_win) {
      cflags = [
        "/wd4245",  # conversion from int to size_t, signed/unsigned mismatch.
        "/wd4389",  # signed/unsigned mismatch.
      ]
    }

    deps = []
    if (is_android) {
      deps += [ ":android_black_magic" ]
    }

    deps += [
      ":libjingle_peerconnection",
      ":pc_test_utils",
      "..:webrtc_common",
      "../api:fakemetricsobserver",
      "../api:libjingle_peerconnection_test_api",
      "../api:optional",
      "../api:rtc_stats_api",
      "../api/audio_codecs:audio_codecs_api",
      "../api/audio_codecs:builtin_audio_decoder_factory",
      "../api/audio_codecs:builtin_audio_encoder_factory",
      "../api/audio_codecs/L16:audio_decoder_L16",
      "../api/audio_codecs/L16:audio_encoder_L16",
      "../call:call_interfaces",
      "../logging:rtc_event_log_api",
      "../logging:rtc_event_log_impl",
      "../media:rtc_audio_video",
      "../media:rtc_data",  # TODO(phoglund): AFAIK only used for one sctp constant.
      "../media:rtc_media_base",
      "../media:rtc_media_tests_utils",
      "../modules/audio_processing:audio_processing",
      "../modules/utility:utility",
      "../p2p:p2p_test_utils",
      "../p2p:rtc_p2p",
      "../pc:rtc_pc",
      "../rtc_base:rtc_base",
      "../rtc_base:rtc_base_approved",
      "../rtc_base:rtc_base_tests_main",
      "../rtc_base:rtc_base_tests_utils",
      "../system_wrappers:metrics_default",
      "../test:audio_codec_mocks",
      "../test:test_support",
    ]

    if (is_android) {
      deps += [
        "//testing/android/native_test:native_test_support",

        # We need to depend on this one directly, or classloads will fail for
        # the voice engine BuildInfo, for instance.
        "../sdk/android:libjingle_peerconnection_java",
      ]

      shard_timeout = 900
    }
  }

  if (is_android) {
    rtc_source_set("android_black_magic") {
      # The android code uses hacky includes to chromium-base and the ssl code;
      # having this in a separate target enables us to keep the peerconnection
      # unit tests clean.
      check_includes = false
      testonly = true
      sources = [
        "test/androidtestinitializer.cc",
        "test/androidtestinitializer.h",
      ]
      deps = [
        "../sdk/android:libjingle_peerconnection_jni",
        "//testing/android/native_test:native_test_support",
      ]
    }
  }
}