Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling StelAudioMgr at runtime #3775

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/core/StelApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,13 @@ void StelApp::init(QSettings* conf)

// Init audio manager
SplashScreen::showMessage(q_("Initializing audio..."));
alex-w marked this conversation as resolved.
Show resolved Hide resolved
const bool audioOK = !(qApp->property("onetime_no-audio").toBool());
audioMgr = new StelAudioMgr(audioOK && confSettings->value("audio/enabled", true).toBool());
const bool audioOK = !(qApp->property("onetime_no-audio").toBool())
&& confSettings->value("audio/enabled", true).toBool();
audioMgr = new StelAudioMgr(audioOK);

// Init video manager
SplashScreen::showMessage(q_("Initializing video..."));
videoMgr = new StelVideoMgr();
videoMgr = new StelVideoMgr(audioOK);
videoMgr->init();
getModuleMgr().registerModule(videoMgr);

Expand Down
61 changes: 38 additions & 23 deletions src/core/StelVideoMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@
#endif


StelVideoMgr::StelVideoMgr() : StelModule()
StelVideoMgr::StelVideoMgr(bool withAudio) : StelModule()
{
setObjectName("StelVideoMgr");
audioEnabled = withAudio;
gzotti marked this conversation as resolved.
Show resolved Hide resolved
#ifdef ENABLE_MEDIA
// in case the property has not been set, getProperty() returns invalid.
verbose= (qApp->property("verbose") == true);
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
QMediaFormat fmt=QMediaFormat();
if (verbose)
{
QMediaFormat fmt=QMediaFormat();
qDebug() << "StelVideoMgr: Supported file formats: " << fmt.supportedFileFormats(QMediaFormat::Decode);
qDebug() << "StelVideoMgr: Supported video codecs: " << fmt.supportedVideoCodecs(QMediaFormat::Decode);
qDebug() << "StelVideoMgr: Supported audio codecs: " << fmt.supportedAudioCodecs(QMediaFormat::Decode);
if (audioEnabled)
qDebug() << "StelVideoMgr: Supported audio codecs: " << fmt.supportedAudioCodecs(QMediaFormat::Decode);
}
#endif
#endif
Expand All @@ -61,7 +63,7 @@ StelVideoMgr::~StelVideoMgr()
while (it.hasNext())
{
it.next();
if (it.value()!=Q_NULLPTR)
if (it.value()!=nullptr)
{
it.value()->player->stop();
StelMainView::getInstance().scene()->removeItem(it.value()->videoItem);
Expand Down Expand Up @@ -89,14 +91,20 @@ void StelVideoMgr::loadVideo(const QString& filename, const QString& id, const f
videoObjects[id]->videoItem->setSize(QSizeF(1,1));

#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
videoObjects[id]->player = new QMediaPlayer(Q_NULLPTR);
videoObjects[id]->audioOutput = new QAudioOutput();
videoObjects[id]->player->setAudioOutput(videoObjects[id]->audioOutput);
videoObjects[id]->player = new QMediaPlayer(nullptr);
if (audioEnabled)
{
videoObjects[id]->audioOutput = new QAudioOutput();
videoObjects[id]->player->setAudioOutput(videoObjects[id]->audioOutput);
}
else
videoObjects[id]->audioOutput = nullptr;

videoObjects[id]->resolution=QSize(200,200); // We must initialize with "valid" resolution, maybe resize when player is starting!
videoObjects[id]->targetFrameSize=QSizeF(100, 200); // depends on parameters given in playVideo(), playPopoutVideo() and resolution detected only after playing started.
#else
videoObjects[id]->player = new QMediaPlayer(Q_NULLPTR, QMediaPlayer::VideoSurface);
videoObjects[id]->player->setAudioRole(QAudio::VideoRole);
videoObjects[id]->player = new QMediaPlayer(nullptr, QMediaPlayer::VideoSurface);
videoObjects[id]->player->setAudioRole(audioEnabled ? QAudio::VideoRole : QAudio::UnknownRole);
videoObjects[id]->resolution=QSize(); // initialize with "invalid" empty resolution, we must detect this when player is starting!
videoObjects[id]->targetFrameSize=QSizeF(); // start with invalid, depends on parameters given in playVideo(), playPopoutVideo() and resolution detected only after playing started.
#endif
Expand Down Expand Up @@ -213,7 +221,7 @@ void StelVideoMgr::playVideo(const QString& id, const bool keepVisibleAtEnd)
if (videoObjects.contains(id))
{
videoObjects[id]->keepVisible=keepVisibleAtEnd;
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
// if already playing, stop and play from the start
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
Expand Down Expand Up @@ -273,7 +281,7 @@ void StelVideoMgr::playVideoPopout(const QString& id, float fromX, float fromY,
if (videoObjects.contains(id))
{
videoObjects[id]->keepVisible=frozenInTransition;
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
// if already playing, stop and play from the start
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
Expand Down Expand Up @@ -376,7 +384,7 @@ void StelVideoMgr::pauseVideo(const QString& id)
{
if (videoObjects.contains(id))
{
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
// Maybe we can only pause while already playing?
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
Expand All @@ -398,7 +406,7 @@ void StelVideoMgr::stopVideo(const QString& id)
{
if (videoObjects.contains(id))
{
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
videoObjects[id]->player->stop();
}
Expand All @@ -412,7 +420,7 @@ void StelVideoMgr::seekVideo(const QString& id, const qint64 ms, bool pause)
qDebug() << "StelVideoMgr::seekVideo: " << id << " to:" << ms << (pause ? " (pausing)" : " (playing)");
if (videoObjects.contains(id))
{
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
if (videoObjects[id]->player->isSeekable())
{
Expand All @@ -436,7 +444,7 @@ void StelVideoMgr::dropVideo(const QString& id)
{
if (!videoObjects.contains(id))
return;
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
if (verbose)
qDebug() << "About to drop (unload) video " << id << "(" << videoObjects[id]->player->mediaStatus() << ")";
Expand All @@ -462,7 +470,7 @@ void StelVideoMgr::setVideoXY(const QString& id, const float x, const float y, c
{
if (videoObjects.contains(id))
{
if (videoObjects[id]->videoItem!=Q_NULLPTR)
if (videoObjects[id]->videoItem!=nullptr)
{
// if w or h < 1 we scale proportional to mainview!
int viewportWidth=StelMainView::getInstance().size().width();
Expand Down Expand Up @@ -491,7 +499,7 @@ void StelVideoMgr::setVideoAlpha(const QString& id, const float alpha)
{
if (videoObjects.contains(id))
{
if (videoObjects[id]->videoItem!=Q_NULLPTR)
if (videoObjects[id]->videoItem!=nullptr)
{
videoObjects[id]->videoItem->setOpacity(alpha);
}
Expand All @@ -503,7 +511,7 @@ void StelVideoMgr::resizeVideo(const QString& id, float w, float h)
{
if (videoObjects.contains(id))
{
if (videoObjects[id]->videoItem!=Q_NULLPTR)
if (videoObjects[id]->videoItem!=nullptr)
{
// if w or h <= 1 we scale proportional to mainview!
// Note that w or h thus cannot be set to 1.0, i.e. single-pixel rows/columns!
Expand Down Expand Up @@ -561,7 +569,7 @@ void StelVideoMgr::showVideo(const QString& id, const bool show)
{
if (videoObjects.contains(id))
{
if (videoObjects[id]->videoItem!=Q_NULLPTR)
if (videoObjects[id]->videoItem!=nullptr)
{
videoObjects[id]->videoItem->setVisible(show);
}
Expand Down Expand Up @@ -629,9 +637,11 @@ int StelVideoMgr::getVideoHeight(const QString& id) const

void StelVideoMgr::muteVideo(const QString& id, bool mute)
{
if (!audioEnabled)
return;
if (videoObjects.contains(id))
{
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
videoObjects[id]->player->audioOutput()->setMuted(mute);
Expand All @@ -645,9 +655,11 @@ void StelVideoMgr::muteVideo(const QString& id, bool mute)

void StelVideoMgr::setVideoVolume(const QString& id, int newVolume)
{
if (!audioEnabled)
return;
if (videoObjects.contains(id))
{
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
videoObjects[id]->player->audioOutput()->setVolume(qBound(0,newVolume,100)/100.);
Expand All @@ -661,10 +673,13 @@ void StelVideoMgr::setVideoVolume(const QString& id, int newVolume)

int StelVideoMgr::getVideoVolume(const QString& id) const
{
if (!audioEnabled)
return 0;

int volume=-1;
if (videoObjects.contains(id))
{
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
volume=int(videoObjects[id]->player->audioOutput()->volume()*100.);
Expand All @@ -682,7 +697,7 @@ bool StelVideoMgr::isVideoPlaying(const QString& id) const
bool playing=false;
if (videoObjects.contains(id))
{
if (videoObjects[id]->player!=Q_NULLPTR)
if (videoObjects[id]->player!=nullptr)
{
#if (QT_VERSION>=QT_VERSION_CHECK(6,0,0))
playing= (videoObjects[id]->player->playbackState() == QMediaPlayer::PlayingState );
Expand Down
5 changes: 4 additions & 1 deletion src/core/StelVideoMgr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ class StelVideoMgr : public StelModule
Q_OBJECT

public:
StelVideoMgr();
//! Constructor.
//! @param withAudio usually true to allow audio output.
StelVideoMgr(bool withAudio);
~StelVideoMgr() override;

public slots:
Expand Down Expand Up @@ -281,6 +283,7 @@ private slots:
int lastPos; //!< This should not be required: We must track a bug in QtMultimedia where the QMediaPlayer is in playing state but does not progress the video position. In update() we try to let it run again.
} VideoPlayer;
QMap<QString, VideoPlayer*> videoObjects;
bool audioEnabled; //!< Allow audio output. May be disabled by command line or config arguments.
bool verbose; //!< true to write many more log entries (useful for script debugging) Activate with command-line option "--verbose"
#endif
};
Expand Down
Loading