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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qmediaplayer.h"
#include "qvideosurfaces_p.h"
#include "private/qobject_p.h"
#include <qmediaplayercontrol.h>
#include <private/qmediaplatformplayerinterface_p.h>
#include <private/qmediaplatformintegration_p.h>
#include <QtCore/qcoreevent.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qtimer.h>
#include <QtCore/qdebug.h>
#include <QtCore/qpointer.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qtemporaryfile.h>
#include <QDir>
#include <QUrl>
QT_BEGIN_NAMESPACE
/*!
\class QMediaPlayer
\brief The QMediaPlayer class allows the playing of a media source.
\inmodule QtMultimedia
\ingroup multimedia
\ingroup multimedia_playback
The QMediaPlayer class is a high level media playback class. It can be used
to playback such content as songs, movies and internet radio. The content
to playback is specified as a QUrl object, which can be thought of as a
main or canonical URL with additional information attached. When provided
with a QUrl playback may be able to commence.
\snippet multimedia-snippets/media.cpp Player
QVideoWidget can be used with QMediaPlayer for video rendering.
\sa QVideoWidget
*/
static void qRegisterMediaPlayerMetaTypes()
{
qRegisterMetaType<QMediaPlayer::State>("QMediaPlayer::State");
qRegisterMetaType<QMediaPlayer::MediaStatus>("QMediaPlayer::MediaStatus");
qRegisterMetaType<QMediaPlayer::Error>("QMediaPlayer::Error");
}
Q_CONSTRUCTOR_FUNCTION(qRegisterMediaPlayerMetaTypes)
class QMediaPlayerPrivate : public QObjectPrivate
{
Q_DECLARE_PUBLIC(QMediaPlayer)
public:
QMediaPlayerPrivate() : notifyTimer(nullptr) {}
QMediaPlatformPlayerInterface *playerInterface = nullptr;
QMediaPlayerControl* control = nullptr;
QString errorString;
QPointer<QObject> videoOutput;
QUrl qrcMedia;
QScopedPointer<QFile> qrcFile;
QUrl rootMedia;
QMediaPlayer::State state = QMediaPlayer::StoppedState;
QMediaPlayer::MediaStatus status = QMediaPlayer::UnknownMediaStatus;
QMediaPlayer::Error error = QMediaPlayer::NoError;
int ignoreNextStatusChange = -1;
bool hasStreamPlaybackFeature = false;
QAudio::Role audioRole = QAudio::UnknownRole;
QString customAudioRole;
void setMedia(const QUrl &media, QIODevice *stream = nullptr);
void _q_stateChanged(QMediaPlayer::State state);
void _q_mediaStatusChanged(QMediaPlayer::MediaStatus status);
void _q_error(int error, const QString &errorString);
void _q_notify();
QTimer* notifyTimer;
QSet<int> notifyProperties;
};
void QMediaPlayerPrivate::_q_notify()
{
Q_Q(QMediaPlayer);
const QMetaObject* m = q->metaObject();
// QTBUG-57045
// we create a copy of notifyProperties container to ensure that if a property is removed
// from the original container as a result of invoking propertyChanged signal, the iterator
// won't become invalidated
QSet<int> properties = notifyProperties;
for (int pi : qAsConst(properties)) {
QMetaProperty p = m->property(pi);
p.notifySignal().invoke(
q, QGenericArgument(p.metaType().name(), p.read(q).data()));
}
}
void QMediaPlayerPrivate::_q_stateChanged(QMediaPlayer::State ps)
{
Q_Q(QMediaPlayer);
if (ps != state) {
state = ps;
if (ps == QMediaPlayer::PlayingState)
q->addPropertyWatch("position");
else
q->removePropertyWatch("position");
emit q->stateChanged(ps);
}
}
void QMediaPlayerPrivate::_q_mediaStatusChanged(QMediaPlayer::MediaStatus s)
{
Q_Q(QMediaPlayer);
if (int(s) == ignoreNextStatusChange) {
ignoreNextStatusChange = -1;
return;
}
if (s != status) {
status = s;
switch (s) {
case QMediaPlayer::StalledMedia:
case QMediaPlayer::BufferingMedia:
q->addPropertyWatch("bufferStatus");
break;
default:
q->removePropertyWatch("bufferStatus");
break;
}
emit q->mediaStatusChanged(s);
}
}
void QMediaPlayerPrivate::_q_error(int error, const QString &errorString)
{
Q_Q(QMediaPlayer);
this->error = QMediaPlayer::Error(error);
this->errorString = errorString;
emit q->error(this->error);
}
void QMediaPlayerPrivate::setMedia(const QUrl &media, QIODevice *stream)
{
Q_Q(QMediaPlayer);
if (!control)
return;
QScopedPointer<QFile> file;
// Backends can't play qrc files directly.
// If the backend supports StreamPlayback, we pass a QFile for that resource.
// If it doesn't, we copy the data to a temporary file and pass its path.
if (!media.isEmpty() && !stream && media.scheme() == QLatin1String("qrc")) {
qrcMedia = media;
file.reset(new QFile(QLatin1Char(':') + media.path()));
if (!file->open(QFile::ReadOnly)) {
QMetaObject::invokeMethod(q, "_q_error", Qt::QueuedConnection,
Q_ARG(int, QMediaPlayer::ResourceError),
Q_ARG(QString, QMediaPlayer::tr("Attempting to play invalid Qt resource")));
QMetaObject::invokeMethod(q, "_q_mediaStatusChanged", Qt::QueuedConnection,
Q_ARG(QMediaPlayer::MediaStatus, QMediaPlayer::InvalidMedia));
file.reset();
// Ignore the next NoMedia status change, we just want to clear the current media
// on the backend side since we can't load the new one and we want to be in the
// InvalidMedia status.
ignoreNextStatusChange = QMediaPlayer::NoMedia;
control->setMedia(QUrl(), nullptr);
} else if (hasStreamPlaybackFeature) {
control->setMedia(media, file.data());
} else {
#if QT_CONFIG(temporaryfile)
#if defined(Q_OS_ANDROID)
QString tempFileName = QDir::tempPath() + media.path();
QDir().mkpath(QFileInfo(tempFileName).path());
QTemporaryFile *tempFile = QTemporaryFile::createNativeFile(*file);
if (!tempFile->rename(tempFileName))
qWarning() << "Could not rename temporary file to:" << tempFileName;
#else
QTemporaryFile *tempFile = new QTemporaryFile;
// Preserve original file extension, some backends might not load the file if it doesn't
// have an extension.
const QString suffix = QFileInfo(*file).suffix();
if (!suffix.isEmpty())
tempFile->setFileTemplate(tempFile->fileTemplate() + QLatin1Char('.') + suffix);
// Copy the qrc data into the temporary file
tempFile->open();
char buffer[4096];
while (true) {
qint64 len = file->read(buffer, sizeof(buffer));
if (len < 1)
break;
tempFile->write(buffer, len);
}
tempFile->close();
#endif
file.reset(tempFile);
control->setMedia(QUrl(QUrl::fromLocalFile(file->fileName())), nullptr);
#else
qWarning("Qt was built with -no-feature-temporaryfile: playback from resource file is not supported!");
#endif
}
} else {
qrcMedia = QUrl();
control->setMedia(media, stream);
}
qrcFile.swap(file); // Cleans up any previous file
}
/*!
Construct a QMediaPlayer instance
parented to \a parent and with \a flags.
*/
QMediaPlayer::QMediaPlayer(QObject *parent):
QObject(*new QMediaPlayerPrivate, parent)
{
Q_D(QMediaPlayer);
d->notifyTimer = new QTimer(this);
d->notifyTimer->setInterval(1000);
connect(d->notifyTimer, SIGNAL(timeout()), SLOT(_q_notify()));
d->playerInterface = QMediaPlatformIntegration::instance()->createPlayerInterface();
if (!d->playerInterface) {
qWarning() << "QPlatformMediaPlayerInterface not implemented!";
d->_q_error(QMediaPlayer::ServiceMissingError, QString::fromUtf8("QMediaPlayer is not supported."));
return;
}
d->control = d->playerInterface->player();
Q_ASSERT(d->control);
connect(d->control, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(_q_stateChanged(QMediaPlayer::State)));
connect(d->control, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
SLOT(_q_mediaStatusChanged(QMediaPlayer::MediaStatus)));
connect(d->control, SIGNAL(error(int,QString)), SLOT(_q_error(int,QString)));
connect(d->control, &QMediaPlayerControl::durationChanged, this, &QMediaPlayer::durationChanged);
connect(d->control, &QMediaPlayerControl::positionChanged, this, &QMediaPlayer::positionChanged);
connect(d->control, &QMediaPlayerControl::audioAvailableChanged, this, &QMediaPlayer::audioAvailableChanged);
connect(d->control, &QMediaPlayerControl::videoAvailableChanged, this, &QMediaPlayer::videoAvailableChanged);
connect(d->control, &QMediaPlayerControl::volumeChanged, this, &QMediaPlayer::volumeChanged);
connect(d->control, &QMediaPlayerControl::mutedChanged, this, &QMediaPlayer::mutedChanged);
connect(d->control, &QMediaPlayerControl::seekableChanged, this, &QMediaPlayer::seekableChanged);
connect(d->control, &QMediaPlayerControl::playbackRateChanged, this, &QMediaPlayer::playbackRateChanged);
connect(d->control, &QMediaPlayerControl::bufferStatusChanged, this, &QMediaPlayer::bufferStatusChanged);
connect(d->control, &QMediaPlayerControl::metaDataChanged, this, &QMediaPlayer::metaDataChanged);
d->state = d->control->state();
d->status = d->control->mediaStatus();
if (d->state == PlayingState)
addPropertyWatch("position");
if (d->status == StalledMedia || d->status == BufferingMedia)
addPropertyWatch("bufferStatus");
d->hasStreamPlaybackFeature = d->control->streamPlaybackSupported();
}
/*!
Destroys the player object.
*/
QMediaPlayer::~QMediaPlayer()
{
Q_D(QMediaPlayer);
// Disconnect everything to prevent notifying
// when a receiver is already destroyed.
disconnect();
delete d->playerInterface;
}
int QMediaPlayer::notifyInterval() const
{
return d_func()->notifyTimer->interval();
}
void QMediaPlayer::setNotifyInterval(int milliSeconds)
{
Q_D(QMediaPlayer);
if (d->notifyTimer->interval() != milliSeconds) {
d->notifyTimer->setInterval(milliSeconds);
emit notifyIntervalChanged(milliSeconds);
}
}
/*!
Watch the property \a name. The property's notify signal will be emitted
once every \c notifyInterval milliseconds.
\sa notifyInterval
*/
void QMediaPlayer::addPropertyWatch(QByteArray const &name)
{
Q_D(QMediaPlayer);
const QMetaObject* m = metaObject();
int index = m->indexOfProperty(name.constData());
if (index != -1 && m->property(index).hasNotifySignal()) {
d->notifyProperties.insert(index);
if (!d->notifyTimer->isActive())
d->notifyTimer->start();
}
}
/*!
Remove property \a name from the list of properties whose changes are
regularly signaled.
\sa notifyInterval
*/
void QMediaPlayer::removePropertyWatch(QByteArray const &name)
{
Q_D(QMediaPlayer);
int index = metaObject()->indexOfProperty(name.constData());
if (index != -1) {
d->notifyProperties.remove(index);
if (d->notifyProperties.isEmpty())
d->notifyTimer->stop();
}
}
QUrl QMediaPlayer::media() const
{
Q_D(const QMediaPlayer);
return d->rootMedia;
}
/*!
Returns the stream source of media data.
This is only valid if a stream was passed to setMedia().
\sa setMedia()
*/
const QIODevice *QMediaPlayer::mediaStream() const
{
Q_D(const QMediaPlayer);
// When playing a resource file, we might have passed a QFile to the backend. Hide it from
// the user.
if (d->control && d->qrcMedia.isEmpty())
return d->control->mediaStream();
return nullptr;
}
QMediaPlayer::State QMediaPlayer::state() const
{
Q_D(const QMediaPlayer);
// In case if EndOfMedia status is already received
// but state is not.
if (d->control != nullptr
&& d->status == QMediaPlayer::EndOfMedia
&& d->state != d->control->state()) {
return d->control->state();
}
return d->state;
}
QMediaPlayer::MediaStatus QMediaPlayer::mediaStatus() const
{
return d_func()->status;
}
qint64 QMediaPlayer::duration() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->duration();
return -1;
}
qint64 QMediaPlayer::position() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->position();
return 0;
}
int QMediaPlayer::volume() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->volume();
return 0;
}
bool QMediaPlayer::isMuted() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->isMuted();
return false;
}
int QMediaPlayer::bufferStatus() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->bufferStatus();
return 0;
}
bool QMediaPlayer::isAudioAvailable() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->isAudioAvailable();
return false;
}
bool QMediaPlayer::isVideoAvailable() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->isVideoAvailable();
return false;
}
bool QMediaPlayer::isSeekable() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->isSeekable();
return false;
}
qreal QMediaPlayer::playbackRate() const
{
Q_D(const QMediaPlayer);
if (d->control != nullptr)
return d->control->playbackRate();
return 0.0;
}
/*!
Returns the current error state.
*/
QMediaPlayer::Error QMediaPlayer::error() const
{
return d_func()->error;
}
QString QMediaPlayer::errorString() const
{
return d_func()->errorString;
}
//public Q_SLOTS:
/*!
Start or resume playing the current source.
*/
void QMediaPlayer::play()
{
Q_D(QMediaPlayer);
if (d->control == nullptr) {
QMetaObject::invokeMethod(this, "_q_error", Qt::QueuedConnection,
Q_ARG(int, QMediaPlayer::ServiceMissingError),
Q_ARG(QString, tr("The QMediaPlayer object does not have a valid service")));
return;
}
// Reset error conditions
d->error = NoError;
d->errorString = QString();
d->control->play();
}
/*!
Pause playing the current source.
*/
void QMediaPlayer::pause()
{
Q_D(QMediaPlayer);
if (d->control != nullptr)
d->control->pause();
}
/*!
Stop playing, and reset the play position to the beginning.
*/
void QMediaPlayer::stop()
{
Q_D(QMediaPlayer);
if (d->control != nullptr)
d->control->stop();
}
void QMediaPlayer::setPosition(qint64 position)
{
Q_D(QMediaPlayer);
if (d->control == nullptr)
return;
d->control->setPosition(qMax(position, 0ll));
}
void QMediaPlayer::setVolume(int v)
{
Q_D(QMediaPlayer);
if (d->control == nullptr)
return;
int clamped = qBound(0, v, 100);
if (clamped == volume())
return;
d->control->setVolume(clamped);
}
void QMediaPlayer::setMuted(bool muted)
{
Q_D(QMediaPlayer);
if (d->control == nullptr || muted == isMuted())
return;
d->control->setMuted(muted);
}
void QMediaPlayer::setPlaybackRate(qreal rate)
{
Q_D(QMediaPlayer);
if (d->control != nullptr)
d->control->setPlaybackRate(rate);
}
/*!
Sets the current \a media 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.
\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.
Since Qt 5.12.2, the url scheme \c gst-pipeline provides custom pipelines
for the GStreamer backend.
\snippet multimedia-snippets/media.cpp Pipeline
If QAbstractVideoSurface is used as the video output,
\c qtvideosink can be used as a video sink element directly in the pipeline.
After that the surface will receive the video frames in QAbstractVideoSurface::present().
\snippet multimedia-snippets/media.cpp Pipeline Surface
If QVideoWidget is used as the video output
and the pipeline contains a video sink element named \c qtvideosink,
current QVideoWidget will be used to render the video.
\snippet multimedia-snippets/media.cpp Pipeline Widget
If the pipeline contains appsrc element, it will be used to push data from \a stream.
\snippet multimedia-snippets/media.cpp Pipeline appsrc
*/
void QMediaPlayer::setMedia(const QUrl &media, QIODevice *stream)
{
Q_D(QMediaPlayer);
stop();
QUrl oldMedia = d->rootMedia;
d->rootMedia = media;
if (oldMedia != media) {
d->setMedia(media, stream);
emit mediaChanged(d->rootMedia);
}
}
/*!
Sets the audio output to \a device.
Setting a null QAudioDeviceInfo, sets the output to the system default.
Returns true if the output could be changed, false otherwise.
*/
bool QMediaPlayer::setAudioOutput(const QAudioDeviceInfo &device)
{
Q_D(QMediaPlayer);
return d->control->setAudioOutput(device);
}
QAudioDeviceInfo QMediaPlayer::audioOutput() const
{
Q_D(const QMediaPlayer);
return d->control->audioOutput();
}
/*!
Attach a video \a output to the media player.
If the media player has already video output attached,
it will be replaced with a new one.
*/
void QMediaPlayer::setVideoOutput(QObject *output)
{
auto *mo = output->metaObject();
QAbstractVideoSurface *surface = nullptr;
if (output && !mo->invokeMethod(output, "videoSurface", Q_RETURN_ARG(QAbstractVideoSurface *, surface))) {
qWarning() << "QMediaPlayer::setVideoOutput: Object" << output->metaObject()->className() << "does not have a videoSurface()";
return;
}
setVideoOutput(surface);
}
/*!
Sets a video \a surface as the video output of a media player.
If a video output has already been set on the media player the new surface
will replace it.
*/
void QMediaPlayer::setVideoOutput(QAbstractVideoSurface *surface)
{
Q_D(QMediaPlayer);
if (!d->control)
return;
d->control->setVideoSurface(surface);
}
/*!
\since 5.15
Sets multiple video surfaces as the video output of a media player.
This allows the media player to render video frames on different surfaces.
All video surfaces must support at least one shared \c QVideoFrame::PixelFormat.
If a video output has already been set on the media player the new surfaces
will replace it.
\sa QAbstractVideoSurface::supportedPixelFormats
*/
void QMediaPlayer::setVideoOutput(const QList<QAbstractVideoSurface *> &surfaces)
{
setVideoOutput(!surfaces.empty() ? new QVideoSurfaces(surfaces, this) : nullptr);
}
/*! \reimp */
bool QMediaPlayer::isAvailable() const
{
Q_D(const QMediaPlayer);
if (!d->control || !d->playerInterface)
return false;
return true;
}
QMediaMetaData QMediaPlayer::metaData() const
{
Q_D(const QMediaPlayer);
return d->control->metaData();
}
QAudio::Role QMediaPlayer::audioRole() const
{
Q_D(const QMediaPlayer);
return d->audioRole;
}
void QMediaPlayer::setAudioRole(QAudio::Role audioRole)
{
Q_D(QMediaPlayer);
if (d->audioRole == audioRole)
return;
d->audioRole = audioRole;
d->control->setAudioRole(audioRole);
if (!d->customAudioRole.isEmpty()) {
d->customAudioRole.clear();
emit customAudioRoleChanged(QString());
}
emit audioRoleChanged(audioRole);
}
/*!
Returns a list of supported audio roles.
If setting the audio role is not supported, an empty list is returned.
\since 5.6
\sa audioRole
*/
QList<QAudio::Role> QMediaPlayer::supportedAudioRoles() const
{
Q_D(const QMediaPlayer);
return d->control->supportedAudioRoles();
}
QString QMediaPlayer::customAudioRole() const
{
Q_D(const QMediaPlayer);
return d->customAudioRole;
}
void QMediaPlayer::setCustomAudioRole(const QString &audioRole)
{
Q_D(QMediaPlayer);
if (d->audioRole == QAudio::CustomRole && d->customAudioRole == audioRole)
return;
d->customAudioRole = audioRole;
d->control->setCustomAudioRole(audioRole);
if (d->audioRole != QAudio::CustomRole) {
d->audioRole = QAudio::CustomRole;
emit audioRoleChanged(QAudio::CustomRole);
}
emit customAudioRoleChanged(audioRole);
}
/*!
Returns a list of supported custom audio roles. An empty list may
indicate that the supported custom audio roles aren't known. The
list may not be complete.
\since 5.11
\sa customAudioRole
*/
QStringList QMediaPlayer::supportedCustomAudioRoles() const
{
Q_D(const QMediaPlayer);
return d->control->supportedCustomAudioRoles();
}
// Enums
/*!
\enum QMediaPlayer::State
Defines the current state of a media player.
\value StoppedState The media player is not playing content, playback will begin from the start
of the current track.
\value PlayingState The media player is currently playing content.
\value PausedState The media player has paused playback, playback of the current track will
resume from the position the player was paused at.
*/
/*!
\enum QMediaPlayer::MediaStatus
Defines the status of a media player's current media.
\value UnknownMediaStatus The status of the media cannot be determined.
\value NoMedia The is no current media. The player is in the StoppedState.
\value LoadingMedia The current media is being loaded. The player may be in any state.
\value LoadedMedia The current media has been loaded. The player is in the StoppedState.
\value StalledMedia Playback of the current media has stalled due to insufficient buffering or
some other temporary interruption. The player is in the PlayingState or PausedState.
\value BufferingMedia The player is buffering data but has enough data buffered for playback to
continue for the immediate future. The player is in the PlayingState or PausedState.
\value BufferedMedia The player has fully buffered the current media. The player is in the
PlayingState or PausedState.
\value EndOfMedia Playback has reached the end of the current media. The player is in the
StoppedState.
\value InvalidMedia The current media cannot be played. The player is in the StoppedState.
*/
/*!
\enum QMediaPlayer::Error
Defines a media player error condition.
\value NoError No error has occurred.
\value ResourceError A media resource couldn't be resolved.
\value FormatError The format of a media resource isn't (fully) supported. Playback may still
be possible, but without an audio or video component.
\value NetworkError A network error occurred.
\value AccessDeniedError There are not the appropriate permissions to play a media resource.
\value ServiceMissingError A valid playback service was not found, playback cannot proceed.
*/
// Signals
/*!
\fn QMediaPlayer::error(QMediaPlayer::Error error)
Signals that an \a error condition has occurred.
\sa errorString()
*/
/*!
\fn void QMediaPlayer::stateChanged(State state)
Signal the \a state of the Player object has changed.
*/
/*!
\fn QMediaPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)
Signals that the \a status of the current media has changed.
\sa mediaStatus()
*/
/*!
\fn void QMediaPlayer::mediaChanged(const QUrl &media);
Signals that the media source has been changed to \a media.
\sa media(), currentMediaChanged()
*/
/*!
\fn void QMediaPlayer::currentMediaChanged(const QUrl &media);
Signals that the current playing content has been changed to \a media.
\sa currentMedia(), mediaChanged()
*/
/*!
\fn void QMediaPlayer::playbackRateChanged(qreal rate);
Signals the playbackRate has changed to \a rate.
*/
/*!
\fn void QMediaPlayer::seekableChanged(bool seekable);
Signals the \a seekable status of the player object has changed.
*/
/*!
\fn void QMediaPlayer::audioRoleChanged(QAudio::Role role)
Signals that the audio \a role of the media player has changed.
\since 5.6
*/
/*!
\fn void QMediaPlayer::customAudioRoleChanged(const QString &role)
Signals that the audio \a role of the media player has changed.
\since 5.11
*/
// Properties
/*!
\property QMediaPlayer::state
\brief the media player's playback state.
By default this property is QMediaPlayer::Stopped
\sa mediaStatus(), play(), pause(), stop()
*/
/*!
\property QMediaPlayer::error
\brief a string describing the last error condition.
\sa error()
*/
/*!
\property QMediaPlayer::media
\brief the active media source being used by the player object.
The player object will use the QUrl for selection of the content to
be played.
By default this property has a null QUrl.
Setting this property 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.
\sa QUrl
*/
/*!
\property QMediaPlayer::mediaStatus
\brief the status of the current media stream.
The stream status describes how the playback of the current stream is
progressing.
By default this property is QMediaPlayer::NoMedia
\sa state
*/
/*!
\property QMediaPlayer::duration
\brief the duration of the current media.
The value is the total playback time in milliseconds of the current media.
The value may change across the life time of the QMediaPlayer object and
may not be available when initial playback begins, connect to the
durationChanged() signal to receive status notifications.
*/
/*!
\property QMediaPlayer::position
\brief the playback position of the current media.
The value is the current playback position, expressed in milliseconds since
the beginning of the media. Periodically changes in the position will be
indicated with the signal positionChanged(), the interval between updates
can be set with setNotifyInterval().
*/
/*!
\property QMediaPlayer::volume
\brief the current playback volume.
The playback volume is scaled linearly, ranging from \c 0 (silence) to \c 100 (full volume).
Values outside this range will be clamped.
By default the volume is \c 100.
UI volume controls should usually be scaled nonlinearly. For example, using a logarithmic scale
will produce linear changes in perceived loudness, which is what a user would normally expect
from a volume control. See QAudio::convertVolume() for more details.
*/
/*!
\property QMediaPlayer::muted
\brief the muted state of the current media.
The value will be true if the playback volume is muted; otherwise false.
*/
/*!
\property QMediaPlayer::bufferStatus
\brief the percentage of the temporary buffer filled before playback begins or resumes, from
\c 0 (empty) to \c 100 (full).
When the player object is buffering; this property holds the percentage of
the temporary buffer that is filled. The buffer will need to reach 100%
filled before playback can start or resume, at which time mediaStatus() will return
BufferedMedia or BufferingMedia. If the value is anything lower than \c 100, mediaStatus() will
return StalledMedia.
\sa mediaStatus()
*/
/*!
\property QMediaPlayer::audioAvailable
\brief the audio availabilty status for the current media.
As the life time of QMediaPlayer can be longer than the playback of one
QUrl, this property may change over time, the
audioAvailableChanged signal can be used to monitor it's status.
*/
/*!
\property QMediaPlayer::videoAvailable
\brief the video availability status for the current media.
If available, the QVideoWidget class can be used to view the video. As the
life time of QMediaPlayer can be longer than the playback of one
QUrl, this property may change over time, the
videoAvailableChanged signal can be used to monitor it's status.
\sa QVideoWidget, QUrl
*/
/*!
\property QMediaPlayer::seekable
\brief the seek-able status of the current media
If seeking is supported this property will be true; false otherwise. The
status of this property may change across the life time of the QMediaPlayer
object, use the seekableChanged signal to monitor changes.
*/
/*!
\property QMediaPlayer::playbackRate
\brief the playback rate of the current media.
This value is a multiplier applied to the media's standard play rate. By
default this value is 1.0, indicating that the media is playing at the
standard pace. Values higher than 1.0 will increase the rate of play.
Values less than zero can be set and indicate the media should rewind at the
multiplier of the standard pace.
Not all playback services support change of the playback rate. It is
framework defined as to the status and quality of audio and video
while fast forwarding or rewinding.
*/
/*!
\property QMediaPlayer::audioRole
\brief the role of the audio stream played by the media player.
It can be set to specify the type of audio being played, allowing the system to make
appropriate decisions when it comes to volume, routing or post-processing.
The audio role must be set before calling setMedia().
customAudioRole is cleared when this property is set to anything other than
QAudio::CustomRole.
\since 5.6
\sa supportedAudioRoles()
*/
/*!
\property QMediaPlayer::customAudioRole
\brief the role of the audio stream played by the media player.
It can be set to specify the type of audio being played when the backend supports
audio roles unknown to Qt. Specifying a role allows the system to make appropriate
decisions when it comes to volume, routing or post-processing.
The audio role must be set before calling setMedia().
audioRole is set to QAudio::CustomRole when this property is set.
\since 5.11
\sa supportedCustomAudioRoles()
*/
/*!
\fn void QMediaPlayer::durationChanged(qint64 duration)
Signal the duration of the content has changed to \a duration, expressed in milliseconds.
*/
/*!
\fn void QMediaPlayer::positionChanged(qint64 position)
Signal the position of the content has changed to \a position, expressed in
milliseconds.
*/
/*!
\fn void QMediaPlayer::volumeChanged(int volume)
Signal the playback volume has changed to \a volume.
*/
/*!
\fn void QMediaPlayer::mutedChanged(bool muted)
Signal the mute state has changed to \a muted.
*/
/*!
\fn void QMediaPlayer::videoAvailableChanged(bool videoAvailable)
Signal the availability of visual content has changed to \a videoAvailable.
*/
/*!
\fn void QMediaPlayer::audioAvailableChanged(bool available)
Signals the availability of audio content has changed to \a available.
*/
/*!
\fn void QMediaPlayer::bufferStatusChanged(int percentFilled)
Signal the amount of the local buffer filled as a percentage by \a percentFilled.
*/
QT_END_NAMESPACE
#include "moc_qmediaplayer.cpp"
|