From f803e23fd2bca6106dcd072d8aeb2935b6c02258 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sat, 19 Feb 2022 21:45:31 +0100 Subject: [PATCH] Added theme support for disabling audio playback for each defined video. --- es-core/src/ThemeData.cpp | 1 + es-core/src/components/VideoComponent.cpp | 4 ++ es-core/src/components/VideoComponent.h | 1 + .../src/components/VideoFFmpegComponent.cpp | 69 ++++++++++--------- 4 files changed, 42 insertions(+), 33 deletions(-) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index b6a28457b..91f139afc 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -111,6 +111,7 @@ std::map> {"defaultImage", PATH}, {"imageType", STRING}, {"gameselector", STRING}, + {"audio", BOOLEAN}, {"interpolation", STRING}, {"pillarboxes", BOOLEAN}, {"scanlines", BOOLEAN}, diff --git a/es-core/src/components/VideoComponent.cpp b/es-core/src/components/VideoComponent.cpp index 39f9cdf54..ab7123a4d 100644 --- a/es-core/src/components/VideoComponent.cpp +++ b/es-core/src/components/VideoComponent.cpp @@ -32,6 +32,7 @@ VideoComponent::VideoComponent() , mMediaViewerMode {false} , mScreensaverMode {false} , mTargetIsMax {false} + , mPlayAudio {true} , mDrawPillarboxes {true} , mRenderScanlines {false} , mLegacyTheme {false} @@ -132,6 +133,9 @@ void VideoComponent::applyTheme(const std::shared_ptr& theme, mVideoAreaPos = elem->get("pos") * scale; } + if (elem->has("audio")) + mPlayAudio = elem->get("audio"); + if (elem->has("interpolation")) { const std::string interpolation {elem->get("interpolation")}; if (interpolation == "linear") { diff --git a/es-core/src/components/VideoComponent.h b/es-core/src/components/VideoComponent.h index f5fe43fa7..5709fada3 100644 --- a/es-core/src/components/VideoComponent.h +++ b/es-core/src/components/VideoComponent.h @@ -121,6 +121,7 @@ protected: bool mMediaViewerMode; bool mScreensaverMode; bool mTargetIsMax; + bool mPlayAudio; bool mDrawPillarboxes; bool mRenderScanlines; bool mLegacyTheme; diff --git a/es-core/src/components/VideoFFmpegComponent.cpp b/es-core/src/components/VideoFFmpegComponent.cpp index ebf68d86c..c3834b18b 100644 --- a/es-core/src/components/VideoFFmpegComponent.cpp +++ b/es-core/src/components/VideoFFmpegComponent.cpp @@ -1351,47 +1351,50 @@ void VideoFFmpegComponent::startVideoStream() } // Audio stream setup, optional as some videos do not have any audio tracks. + // Audio can also be disabled per video via the theme configuration. - mAudioStreamIndex = - av_find_best_stream(mFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0); + if (mPlayAudio) { + mAudioStreamIndex = + av_find_best_stream(mFormatContext, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0); - if (mAudioStreamIndex < 0) { - LOG(LogDebug) << "VideoFFmpegComponent::startVideoStream(): " - "File does not seem to contain any audio streams"; - } - - if (mAudioStreamIndex >= 0) { - mAudioStream = mFormatContext->streams[mAudioStreamIndex]; - mAudioCodec = - const_cast(avcodec_find_decoder(mAudioStream->codecpar->codec_id)); - - if (!mAudioCodec) { - LOG(LogError) << "Couldn't find a suitable audio codec for file \"" << mVideoPath - << "\""; - return; + if (mAudioStreamIndex < 0) { + LOG(LogDebug) << "VideoFFmpegComponent::startVideoStream(): " + "File does not seem to contain any audio streams"; } - mAudioCodecContext = avcodec_alloc_context3(mAudioCodec); + if (mAudioStreamIndex >= 0) { + mAudioStream = mFormatContext->streams[mAudioStreamIndex]; + mAudioCodec = + const_cast(avcodec_find_decoder(mAudioStream->codecpar->codec_id)); - if (mAudioCodec->capabilities & AV_CODEC_CAP_TRUNCATED) - mAudioCodecContext->flags |= AV_CODEC_FLAG_TRUNCATED; + if (!mAudioCodec) { + LOG(LogError) << "Couldn't find a suitable audio codec for file \"" + << mVideoPath << "\""; + return; + } - // Some formats want separate stream headers. - if (mAudioCodecContext->flags & AVFMT_GLOBALHEADER) - mAudioCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; + mAudioCodecContext = avcodec_alloc_context3(mAudioCodec); - if (avcodec_parameters_to_context(mAudioCodecContext, mAudioStream->codecpar)) { - LOG(LogError) << "VideoFFmpegComponent::startVideoStream(): " - "Couldn't fill the audio codec context parameters for file \"" - << mVideoPath << "\""; - return; - } + if (mAudioCodec->capabilities & AV_CODEC_CAP_TRUNCATED) + mAudioCodecContext->flags |= AV_CODEC_FLAG_TRUNCATED; - if (avcodec_open2(mAudioCodecContext, mAudioCodec, nullptr)) { - LOG(LogError) << "VideoFFmpegComponent::startVideoStream(): " - "Couldn't initialize the audio codec context for file \"" - << mVideoPath << "\""; - return; + // Some formats want separate stream headers. + if (mAudioCodecContext->flags & AVFMT_GLOBALHEADER) + mAudioCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; + + if (avcodec_parameters_to_context(mAudioCodecContext, mAudioStream->codecpar)) { + LOG(LogError) << "VideoFFmpegComponent::startVideoStream(): " + "Couldn't fill the audio codec context parameters for file \"" + << mVideoPath << "\""; + return; + } + + if (avcodec_open2(mAudioCodecContext, mAudioCodec, nullptr)) { + LOG(LogError) << "VideoFFmpegComponent::startVideoStream(): " + "Couldn't initialize the audio codec context for file \"" + << mVideoPath << "\""; + return; + } } }