From 36fbb6ab08734aff1a73ea4ed255e06bd5aa99f8 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 11 Jun 2021 15:09:43 +0200 Subject: QMediaPlayer API cleanups Remove the autoPlay property, and split up the setSource() method into setSource(QUrl) and setSourceDevice(QIODevice) for more clarity. Change-Id: I3e973fb5b6aa50f192af898f1b07769a201c6845 Reviewed-by: Volker Hilsheimer Reviewed-by: Doris Verria --- src/multimedia/playback/qmediaplayer.cpp | 70 ++++++++++------------ src/multimedia/playback/qmediaplayer.h | 15 ++--- src/multimedia/playback/qmediaplayer_p.h | 1 - .../tst_qmediaplayerbackend.cpp | 2 +- .../multimedia/qmediaplayer/tst_qmediaplayer.cpp | 4 +- 5 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/multimedia/playback/qmediaplayer.cpp b/src/multimedia/playback/qmediaplayer.cpp index 4ee85506a..39f3d2f83 100644 --- a/src/multimedia/playback/qmediaplayer.cpp +++ b/src/multimedia/playback/qmediaplayer.cpp @@ -101,8 +101,6 @@ void QMediaPlayerPrivate::setError(int error, const QString &errorString) void QMediaPlayerPrivate::setMedia(const QUrl &media, QIODevice *stream) { - Q_Q(QMediaPlayer); - if (!control) return; @@ -163,9 +161,6 @@ void QMediaPlayerPrivate::setMedia(const QUrl &media, QIODevice *stream) } qrcFile.swap(file); // Cleans up any previous file - - if (autoPlay) - q->play(); } QList QMediaPlayerPrivate::trackMetaData(QPlatformMediaPlayer::TrackType s) const @@ -232,7 +227,7 @@ QUrl QMediaPlayer::source() const \sa setSource() */ -const QIODevice *QMediaPlayer::sourceStream() const +const QIODevice *QMediaPlayer::sourceDevice() const { Q_D(const QMediaPlayer); @@ -444,23 +439,6 @@ void QMediaPlayer::setPosition(qint64 position) d->control->setPosition(qMax(position, 0ll)); } -/*! - If \a autoPlay is set to true, playback will start immediately after calling - setSource() on the media player. Otherwise the media player will enter the - Stopped state after loading the file. - - The default is false. -*/ -void QMediaPlayer::setAutoPlay(bool autoPlay) -{ - Q_D(QMediaPlayer); - if (d->autoPlay == autoPlay) - return; - - d->autoPlay = autoPlay; - emit autoPlayChanged(autoPlay); -} - void QMediaPlayer::setPlaybackRate(qreal rate) { Q_D(QMediaPlayer); @@ -472,11 +450,6 @@ void QMediaPlayer::setPlaybackRate(qreal rate) /*! Sets the current \a source. - If a \a stream is supplied; media data will be read from it instead of resolving the media - source. In this case the url should be provided to resolve additional information - about the media such as mime type. The \a stream must be open and readable. - For macOS the \a stream should be also seekable. - Setting the media to a null QUrl will cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media. @@ -487,18 +460,45 @@ void QMediaPlayer::setPlaybackRate(qreal rate) when an error occurs during loading. */ -void QMediaPlayer::setSource(const QUrl &source, QIODevice *stream) +void QMediaPlayer::setSource(const QUrl &source) { Q_D(QMediaPlayer); stop(); - if (d->source == source && d->stream == stream) + if (d->source == source && d->stream == nullptr) return; d->source = source; - d->stream = stream; + d->stream = nullptr; - d->setMedia(source, stream); + d->setMedia(source, nullptr); + emit sourceChanged(d->source); +} + +/*! + Sets the current source \a device. + + The media data will be read from \a device. The url can be provided to resolve additional + information about the media such as mime type. The \a device must be open and readable. + For macOS the \a device should be also seekable. + + \note This function returns immediately after recording the specified source of the media. + It does not wait for the media to finish loading and does not check for errors. Listen for + the mediaStatusChanged() and error() signals to be notified when the media is loaded and + when an error occurs during loading. +*/ +void QMediaPlayer::setSourceDevice(QIODevice *device, const QUrl &sourceUrl) +{ + Q_D(QMediaPlayer); + stop(); + + if (d->source == sourceUrl && d->stream == device) + return; + + d->source = sourceUrl; + d->stream = device; + + d->setMedia(d->source, device); emit sourceChanged(d->source); } @@ -724,12 +724,6 @@ QMediaMetaData QMediaPlayer::metaData() const return d->control->metaData(); } -bool QMediaPlayer::autoPlay() const -{ - Q_D(const QMediaPlayer); - return d->autoPlay; -} - // Enums /*! \enum QMediaPlayer::State diff --git a/src/multimedia/playback/qmediaplayer.h b/src/multimedia/playback/qmediaplayer.h index 7eb66ea1f..13d0bd90b 100644 --- a/src/multimedia/playback/qmediaplayer.h +++ b/src/multimedia/playback/qmediaplayer.h @@ -41,6 +41,7 @@ #define QMEDIAPLAYER_H #include +#include #include #include #include @@ -66,7 +67,6 @@ class Q_MULTIMEDIA_EXPORT QMediaPlayer : public QObject Q_PROPERTY(bool seekable READ isSeekable NOTIFY seekableChanged) Q_PROPERTY(qreal playbackRate READ playbackRate WRITE setPlaybackRate NOTIFY playbackRateChanged) Q_PROPERTY(PlaybackState playbackState READ playbackState NOTIFY playbackStateChanged) - Q_PROPERTY(bool autoPlay READ autoPlay WRITE setAutoPlay NOTIFY autoPlayChanged) Q_PROPERTY(MediaStatus mediaStatus READ mediaStatus NOTIFY mediaStatusChanged) Q_PROPERTY(QMediaMetaData metaData READ metaData NOTIFY metaDataChanged) Q_PROPERTY(Error error READ error NOTIFY errorChanged) @@ -109,10 +109,6 @@ public: explicit QMediaPlayer(QObject *parent = nullptr); ~QMediaPlayer(); - // new API -// bool enableLowLatencyPlayback(bool tryEnable); -// bool isLowLatencyPlaybackEnabled() const; - QList audioTracks() const; QList videoTracks() const; QList subtitleTracks() const; @@ -138,7 +134,7 @@ public: QVideoSink *videoSink() const; QUrl source() const; - const QIODevice *sourceStream() const; + const QIODevice *sourceDevice() const; PlaybackState playbackState() const; MediaStatus mediaStatus() const; @@ -161,19 +157,17 @@ public: bool isAvailable() const; QMediaMetaData metaData() const; - bool autoPlay() const; - public Q_SLOTS: void play(); void pause(); void stop(); void setPosition(qint64 position); - void setAutoPlay(bool autoPlay); void setPlaybackRate(qreal rate); - void setSource(const QUrl &media, QIODevice *stream = nullptr); + void setSource(const QUrl &source); + void setSourceDevice(QIODevice *device, const QUrl &sourceUrl = QUrl()); Q_SIGNALS: void sourceChanged(const QUrl &media); @@ -183,7 +177,6 @@ Q_SIGNALS: void durationChanged(qint64 duration); void positionChanged(qint64 position); - void autoPlayChanged(bool autoPlay); void hasAudioChanged(bool available); void hasVideoChanged(bool videoAvailable); diff --git a/src/multimedia/playback/qmediaplayer_p.h b/src/multimedia/playback/qmediaplayer_p.h index 5c3282e67..5da23fa00 100644 --- a/src/multimedia/playback/qmediaplayer_p.h +++ b/src/multimedia/playback/qmediaplayer_p.h @@ -87,7 +87,6 @@ public: QMediaPlayer::PlaybackState state = QMediaPlayer::StoppedState; QMediaPlayer::Error error = QMediaPlayer::NoError; - bool autoPlay = false; void setMedia(const QUrl &media, QIODevice *stream = nullptr); diff --git a/tests/auto/integration/qmediaplayerbackend/tst_qmediaplayerbackend.cpp b/tests/auto/integration/qmediaplayerbackend/tst_qmediaplayerbackend.cpp index 0a4897c25..d58c33ab6 100644 --- a/tests/auto/integration/qmediaplayerbackend/tst_qmediaplayerbackend.cpp +++ b/tests/auto/integration/qmediaplayerbackend/tst_qmediaplayerbackend.cpp @@ -1080,7 +1080,7 @@ void tst_QMediaPlayerBackend::playFromBuffer() QFile file(localVideoFile.toLocalFile()); if (!file.open(QIODevice::ReadOnly)) QSKIP("Could not open file"); - player.setSource(localVideoFile, &file); + player.setSourceDevice(&file, localVideoFile); player.play(); QTRY_VERIFY(player.position() >= 1000); QVERIFY2(surface.m_totalFrames >= 25, qPrintable(QString("Expected >= 25, got %1").arg(surface.m_totalFrames))); diff --git a/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp b/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp index 4cb12b398..28e7f5baf 100644 --- a/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp +++ b/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp @@ -250,9 +250,9 @@ void tst_QMediaPlayer::testMedia() QCOMPARE(player->source(), mediaContent); QBuffer stream; - player->setSource(mediaContent, &stream); + player->setSourceDevice(&stream, mediaContent); QCOMPARE(player->source(), mediaContent); - QCOMPARE(player->sourceStream(), &stream); + QCOMPARE(player->sourceDevice(), &stream); } void tst_QMediaPlayer::testDuration_data() -- cgit v1.2.3