# Copyright (C) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/ohos.gni")
import("//foundation/multimedia/media_foundation/config.gni")

if (hst_is_standard_sys) {
  config("media_foundation_public_config") {
    include_dirs = [
      "$histreamer_root_dir/interface/kits/c",
      "$histreamer_root_dir/interface/inner_api",
      "$histreamer_root_dir/interface/inner_api/buffer",
      "$histreamer_root_dir/interface/inner_api/common",
      "$histreamer_root_dir/interface/inner_api/meta",
    ]
  }

  config("media_foundation_config") {
    defines = [
      "HST_ANY_WITH_NO_RTTI",
      "MEDIA_OHOS",
    ]

    if (media_foundation_enable_ffrt) {
      defines += [ "MEDIA_FOUNDATION_FFRT" ]
    }

    if (media_foundation_enable_rm_demuxer) {
      defines += [ "SUPPORT_CODEC_RM" ]
    }
    if (media_foundation_enable_cook_audio_decoder) {
      defines += [ "SUPPORT_CODEC_COOK" ]
    }
    if (media_foundation_enable_eac3_audio_decoder) {
      defines += [ "SUPPORT_CODEC_EAC3" ]
    }
    if (media_foundation_enable_truehd_audio_decoder) {
      defines += [ "SUPPORT_CODEC_TRUEHD" ]
    }
    if (media_foundation_enable_dts_audio_decoder) {
      defines += [ "SUPPORT_CODEC_DTS" ]
    }
    if (media_foundation_enable_lrc_demuxer) {
      defines += [ "SUPPORT_DEMUXER_LRC" ]
    }
    if (media_foundation_enable_sami_demuxer) {
      defines += [ "SUPPORT_DEMUXER_SAMI" ]
    }
    if (media_foundation_enable_ass_demuxer) {
      defines += [ "SUPPORT_DEMUXER_ASS" ]
    }
    if (media_foundation_enable_eac3_demuxer) {
      defines += [ "SUPPORT_DEMUXER_EAC3" ]
    }
    if (media_foundation_enable_dtshd_demuxer) {
      defines += [ "SUPPORT_DEMUXER_DTSHD" ]
    }
    if (media_foundation_enable_truehd_demuxer) {
      defines += [ "SUPPORT_DEMUXER_TRUEHD" ]
    }
    if (is_arkui_x) {
      defines += [ "CROSS_PLATFORM" ]
    }

    include_dirs = [
      "$histreamer_root_dir/osal/base",
      "$histreamer_root_dir/plugin",
      "$histreamer_root_dir/src/buffer/avbuffer/include",
      "$histreamer_root_dir/src/buffer/avbuffer_queue/include",
      "$histreamer_root_dir/src/capi/common",
    ]

    cflags = [
      "-O2",
      "-fPIC",
      "-Wall",
      "-fexceptions",
      "-fno-rtti",
      "-Wno-unused-but-set-variable",
      "-Wno-format",
    ]
    cflags_cc = cflags
  }

  ohos_shared_library("media_foundation") {
    branch_protector_ret = "pac_ret"
    install_enable = true

    public_configs = [ ":media_foundation_public_config" ]

    configs = [ ":media_foundation_config" ]

    defines = [
      "HST_ANY_WITH_NO_RTTI",
      "MEDIA_OHOS",
      "DYNAMIC_PLUGINS",
    ]

    sanitize = {
      ubsan = true
      boundary_sanitize = true
      cfi = true
      cfi_cross_dso = true
      debug = false
      integer_overflow = true
    }

    sources = [
      "$histreamer_root_dir/src/buffer/avbuffer/av_hardware_memory.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer/av_shared_memory_ext.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer/av_surface_memory.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer/av_virtual_memory.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer/avbuffer.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer/avbuffer_common.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer/avbuffer_utils.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer/avmemory.cpp",
    ]

    sources += [
      "$histreamer_root_dir/src/buffer/avbuffer_queue/avbuffer_queue.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer_queue/avbuffer_queue_consumer.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer_queue/avbuffer_queue_producer.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer_queue/avbuffer_queue_producer_proxy.cpp",
      "$histreamer_root_dir/src/buffer/avbuffer_queue/avbuffer_queue_producer_stub.cpp",
    ]

    sources += [
      "$histreamer_root_dir/src/buffer/avsharedmemory/avsharedmemorybase.cpp",
    ]

    sources += [
      "$histreamer_root_dir/src/common/avsharedmemorypool.cpp",
      "$histreamer_root_dir/src/common/fdsan_fd.cpp",
      "$histreamer_root_dir/src/common/interrupt_monitor.cpp",
      "$histreamer_root_dir/src/common/pcm_convert.cpp",
      "$histreamer_root_dir/src/common/scoped_timer.cpp",
    ]

    sources += [
      "$histreamer_root_dir/src/meta/any.cpp",
      "$histreamer_root_dir/src/meta/format.cpp",
      "$histreamer_root_dir/src/meta/media_source.cpp",
      "$histreamer_root_dir/src/meta/meta.cpp",
    ]

    sources += [ "$histreamer_root_dir/src/capi/common/native_mfmagic.cpp" ]

    sources += [
      "$histreamer_root_dir/src/osal/filesystem/file_system.cpp",
      "$histreamer_root_dir/src/osal/task/task.cpp",
      "$histreamer_root_dir/src/osal/utils/dump_buffer.cpp",
      "$histreamer_root_dir/src/osal/utils/steady_clock.cpp",
      "$histreamer_root_dir/src/osal/utils/util.cpp",
    ]

    if (media_foundation_enable_ffrt) {
      sources += [
        "$histreamer_root_dir/src/osal/task/ffrt/condition_variable.cpp",
        "$histreamer_root_dir/src/osal/task/ffrt/jobutils.cpp",
        "$histreamer_root_dir/src/osal/task/ffrt/task.cpp",
      ]
    } else {
      sources += [
        "$histreamer_root_dir/src/osal/task/pthread/autolock.cpp",
        "$histreamer_root_dir/src/osal/task/pthread/condition_variable.cpp",
        "$histreamer_root_dir/src/osal/task/pthread/jobutils.cpp",
        "$histreamer_root_dir/src/osal/task/pthread/mutex.cpp",
        "$histreamer_root_dir/src/osal/task/pthread/pipeline_threadpool.cpp",
        "$histreamer_root_dir/src/osal/task/pthread/taskInner.cpp",
        "$histreamer_root_dir/src/osal/task/pthread/thread.cpp",
      ]
    }

    sources += [
      "$histreamer_root_dir/src/filter/filter.cpp",
      "$histreamer_root_dir/src/filter/filter_factory.cpp",
      "$histreamer_root_dir/src/pipeline/pipeline.cpp",
    ]

    sources += [
      "$histreamer_root_dir/src/plugin/cached_plugin_package.cpp",
      "$histreamer_root_dir/src/plugin/plugin_buffer.cpp",
      "$histreamer_root_dir/src/plugin/plugin_list.cpp",
      "$histreamer_root_dir/src/plugin/plugin_manager_v2.cpp",
      "$histreamer_root_dir/src/plugin/plugin_package.cpp",
    ]

    public_deps = []

    external_deps = [
      "c_utils:utils",
      "ffmpeg:libohosffmpeg",
      "graphic_surface:sync_fence",
      "hilog:libhilog",
      "hitrace:hitrace_meter",
      "init:libbegetutil",
      "ipc:ipc_core",
      "samgr:samgr_proxy",
    ]

    public_external_deps = [
      "bounds_checking_function:libsec_static",
      "graphic_surface:surface",
      "qos_manager:qos",
      "resource_schedule_service:ressched_client",
    ]

    if (media_foundation_enable_ffrt) {
      external_deps += [ "ffrt:libffrt" ]
    }

    innerapi_tags = [
      "platformsdk",
      "sasdk",
    ]

    if (is_asan) {
      hst_plugin_path = "\"/system/asan/lib64/media/media_plugins\""
    } else if (target_cpu == "arm64" || is_emulator) {
      hst_plugin_path = "\"/system/lib64/media/media_plugins\""
    } else {
      hst_plugin_path = "\"/system/lib/media/media_plugins\""
    }

    defines += [
      "HST_PLUGIN_PATH=${hst_plugin_path}",
      "HST_PLUGIN_FILE_TAIL=\".z.so\"",
    ]

    cflags = [
      "-D_FORTIFY_SOURCE=2",
    ]
    
    subsystem_name = "multimedia"
    part_name = "media_foundation"
  }
} else {
  group("media_foundation") {
  }
}