summaryrefslogtreecommitdiffstats
path: root/tests/manual/mediaframeinputs/previewrunner.cpp
blob: 415e44fd836b10751b0a4b23e338e561ef6fbc89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "previewrunner.h"

#include <QEvent>

PreviewRunner::PreviewRunner()
{
    m_player.setVideoOutput(&m_preview);
    m_player.setAudioOutput(&m_audioOutput);

    connect(&m_player, &QMediaPlayer::playbackStateChanged, this,
            &PreviewRunner::handlePlaybackStateChanged);
    connect(&m_player, &QMediaPlayer::errorOccurred, this, &PreviewRunner::handleError);
    connect(&m_player, &QMediaPlayer::positionChanged, this, &PreviewRunner::updateTitle);

    m_preview.installEventFilter(this);
}

bool PreviewRunner::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == &m_preview && event->type() == QEvent::Close) {
        emit finished();
        m_player.stop();
    }

    return QObject::eventFilter(watched, event);
}

void PreviewRunner::run(const QUrl &mediaLocation)
{
    m_preview.show();
    m_player.setSource(mediaLocation);
    m_player.play();
}

void PreviewRunner::handlePlaybackStateChanged(QMediaPlayer::PlaybackState state)
{
    if (state == QMediaPlayer::StoppedState) {
        m_preview.close();

        const bool noError = m_player.error() == QMediaPlayer::NoError;
        qInfo() << "Preview finished" << (noError ? "" : "with an error");
    } else if (state == QMediaPlayer::PlayingState) {
        qInfo() << "Running preview for media" << m_player.source();
        updateTitle();
    }
}

void PreviewRunner::handleError(QMediaPlayer::Error error, QString description)
{
    qWarning() << "Playing error occurred:" << error << description;

    if (m_player.playbackState() == QMediaPlayer::StoppedState)
        m_preview.close();
}

void PreviewRunner::updateTitle()
{
    m_preview.setWindowTitle(QStringLiteral("Playing %1; pos: %2; duration: %3")
                                     .arg(m_player.source().toString(),
                                          QString::number(m_player.position()),
                                          QString::number(m_player.duration())));
}

#include "moc_previewrunner.cpp"