summaryrefslogtreecommitdiffstats
path: root/src/ifmedia/qifmediaplayer.cpp
blob: f73c612c513ace526185cdfae9f146d11652e048 (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
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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qifmediaplayer.h"
#include "qifmediaplayer_p.h"
#include "qifplayqueue.h"
#include "qifplayqueue_p.h"
#include "qifqmlconversion_helper.h"
#include <QtInterfaceFramework/QIfServiceObject>
#include <QtDebug>
#include <QDataStream>
#include <QMetaEnum>

using namespace Qt::StringLiterals;

QT_BEGIN_NAMESPACE

QIfMediaPlayerPrivate::QIfMediaPlayerPrivate(const QString &interface, QIfMediaPlayer *parent)
    : QIfAbstractFeaturePrivate(interface, parent)
    , q_ptr(parent)
    , m_playQueue(nullptr)
    , m_playMode(QIfMediaPlayer::Normal)
    , m_playState(QIfMediaPlayer::Stopped)
    , m_currentTrack(nullptr)
    , m_position(-1)
    , m_duration(-1)
    , m_volume(0)
    , m_muted(false)
{
    qRegisterMetaType<QIfPlayQueue*>();
    qRegisterMetaType<QIfPlayableItem>();
    qRegisterMetaType<QIfAudioTrackItem>();
}

void QIfMediaPlayerPrivate::initialize()
{
    QIfAbstractFeaturePrivate::initialize();
    m_playQueue = new QIfPlayQueue(q_ptr);
}

void QIfMediaPlayerPrivate::clearToDefaults()
{
    onPlayModeChanged(QIfMediaPlayer::Normal);
    onCurrentTrackChanged(QVariant());
    onPositionChanged(-1);
    onDurationChanged(-1);
    onVolumeChanged(0);
    onMutedChanged(false);
    m_playQueue->d_func()->clearToDefaults();
}

void QIfMediaPlayerPrivate::onPlayModeChanged(QIfMediaPlayer::PlayMode playMode)
{
    if (m_playMode == playMode)
        return;

    Q_Q(QIfMediaPlayer);
    m_playMode = playMode;
    emit q->playModeChanged(playMode);
}

void QIfMediaPlayerPrivate::onPlayStateChanged(QIfMediaPlayer::PlayState playState)
{
    if (m_playState == playState)
        return;

    Q_Q(QIfMediaPlayer);
    m_playState = playState;
    emit q->playStateChanged(playState);
}

void QIfMediaPlayerPrivate::onCurrentTrackChanged(const QVariant &currentTrack)
{
    if (m_currentTrackData == currentTrack)
        return;

    const QIfPlayableItem *currentItem = nullptr;

    if (currentTrack.isValid()) {
        currentItem = qtif_gadgetFromVariant<QIfPlayableItem>(q_ptr, currentTrack);
        if (m_currentTrack == currentItem)
            return;
    }

    Q_Q(QIfMediaPlayer);
    m_currentTrackData = currentTrack;
    m_currentTrack = currentItem;
    emit q->currentTrackChanged(m_currentTrackData);
}

void QIfMediaPlayerPrivate::onPositionChanged(qint64 position)
{
    if (m_position == position)
        return;
    Q_Q(QIfMediaPlayer);
    m_position = position;
    emit q->positionChanged(position);
}

void QIfMediaPlayerPrivate::onDurationChanged(qint64 duration)
{
    if (m_duration == duration)
        return;
    Q_Q(QIfMediaPlayer);
    m_duration = duration;
    emit q->durationChanged(duration);
}

void QIfMediaPlayerPrivate::onVolumeChanged(int volume)
{
    if (m_volume == volume)
        return;
    Q_Q(QIfMediaPlayer);
    m_volume = volume;
    emit q->volumeChanged(volume);
}

void QIfMediaPlayerPrivate::onMutedChanged(bool muted)
{
    if (m_muted == muted)
        return;
    Q_Q(QIfMediaPlayer);
    m_muted = muted;
    emit q->mutedChanged(muted);
}

QIfMediaPlayerBackendInterface *QIfMediaPlayerPrivate::playerBackend() const
{
    return backend<QIfMediaPlayerBackendInterface*>();
}

/*!
    \class QIfMediaPlayer
    \inmodule QtIfMedia
    \brief Provides an interface to control a media player.

    The QIfMediaPlayer provides methods to control a media player. This media player can
    be local or even a remote device you are connected to e.g. over bluetooth.

    By default the autoDiscovery is turned to Automatic for this feature and most likely will connect to
    a local media player instance.

    Uses \c qtifmedia as configurationId for \l QIfConfiguration based settings.
*/

/*!
    \qmltype MediaPlayer
    \instantiates QIfMediaPlayer
    \inqmlmodule QtInterfaceFramework.Media
    \inherits AbstractFeature
    \brief Provides an interface to control a media player.

    The MediaPlayer provides methods to control a media player. This media player can
    be local or even a remote device you are connected to e.g. over bluetooth.

    By default the autoDiscovery is turned to Automatic for this feature and most likely will connect to
    a local media player instance.

    Uses \c qtifmedia as configurationId for \l InterfaceFrameworkConfiguration based settings.
*/

/*!
    \enum QIfMediaPlayer::PlayMode
    \value Normal
          Each item in the queue is played in sequential order. Usually the playback stops when the end
          of the queue is reached.
    \value RepeatTrack
          Always repeat the current item. It should still be possible to change the current item
          using next() and previous(), but this depends on the implementation of the backend.
    \value RepeatAll
          When the end of the queue is reached, the first item starts to play.
    \value Shuffle
          The item in the queue are played in an random order.
*/

/*!
    \enum QIfMediaPlayer::PlayState
    \value Playing
          The media player is currently playing an item.
    \value Paused
          The playback is paused and can be continued at the same position.
    \value Stopped
          The playback hasn't been started yet. Starting it, will always start from the beginning.
*/

/*!
    Constructs a QIfMediaPlayer.

    The \a parent argument is passed on to the \l QIfAbstractFeature base class.
*/
QIfMediaPlayer::QIfMediaPlayer(QObject *parent)
    : QIfAbstractFeature(*new QIfMediaPlayerPrivate(QLatin1String(QIfMediaPlayer_iid), this), parent)
{
    setConfigurationId(u"qtifmedia"_s);
}

/*!
    \qmlproperty PlayQueue MediaPlayer::playQueue
    \brief Holds the play queue of this media player.

    \sa PlayQueue
*/
/*!
    \property QIfMediaPlayer::playQueue
    \brief Holds the play queue of this media player.

    \sa QIfPlayQueue
*/
QIfPlayQueue *QIfMediaPlayer::playQueue() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_playQueue;
}

/*!
    \qmlproperty enumeration MediaPlayer::playMode
    \brief Holds the current playback mode of the media player.
    Available values are:
    \value Normal
           Each item in the queue is played in sequential order. Usually the playback stops when the end
           of the queue is reached.
    \value RepeatTrack
           Always repeat the current item. It should still be possible to change the current item
           using next() and previous(), but this depends on the implementation of the backend.
    \value RepeatAll
           When the end of the queue is reached, the first item starts to play.
    \value Shuffle
           The item in the queue are played in an random order.
*/
/*!
    \property QIfMediaPlayer::playMode
    \brief Holds the current playback mode of the media player.
*/
QIfMediaPlayer::PlayMode QIfMediaPlayer::playMode() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_playMode;
}

/*!
    \qmlproperty enumeration MediaPlayer::playState
    \brief Holds the current playback state of the media player.
    Available values are:
    \value Playing
           The media player is currently playing an item.
    \value Paused
           The playback is paused and can be continued at the same position.
    \value Stopped
           The playback hasn't been started yet. Starting it, will always start from the beginning.
*/
/*!
    \property QIfMediaPlayer::playState
    \brief Holds the current playback state of the media player.
*/
QIfMediaPlayer::PlayState QIfMediaPlayer::playState() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_playState;
}

/*!
    \qmlproperty object MediaPlayer::currentTrack
    \brief Holds the current track represented as QVariant.

    \note This will be replaced by soon.
*/
/*!
    \property QIfMediaPlayer::currentTrack
    \brief Holds the current track represented as QVariant.

    \note This will be replaced by soon.
*/
QVariant QIfMediaPlayer::currentTrack() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_currentTrackData;
}

/*!
    \qmlproperty int MediaPlayer::position
    \brief Holds the position of the current song of the media player in seconds.
*/
/*!
    \property QIfMediaPlayer::position
    \brief Holds the position of the current song of the media player in seconds.
*/
qint64 QIfMediaPlayer::position() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_position;
}

/*!
    \qmlproperty int MediaPlayer::duration
    \brief Holds the total duration of the current song in seconds.
*/
/*!
    \property QIfMediaPlayer::duration
    \brief Holds the total duration of the current song in seconds.
*/
qint64 QIfMediaPlayer::duration() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_duration;
}

/*!
    \qmlproperty int MediaPlayer::volume
    \brief Holds the sound volume level (0..100)
    \sa muted
*/
/*!
    \property QIfMediaPlayer::volume
    \brief Holds the sound volume level (0..100)
    \sa muted
*/
int QIfMediaPlayer::volume() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_volume;
}


/*!
    \qmlproperty bool MediaPlayer::muted
    \brief This property holds whether the audio output is muted.
    \sa volume
*/
/*!
    \property QIfMediaPlayer::muted
    \brief This property holds whether the audio output is muted.
    \sa volume
*/
bool QIfMediaPlayer::isMuted() const
{
    Q_D(const QIfMediaPlayer);
    return d->m_muted;
}

void QIfMediaPlayer::setPlayMode(QIfMediaPlayer::PlayMode playMode)
{
    Q_D(QIfMediaPlayer);
    QIfMediaPlayerBackendInterface *backend = d->playerBackend();
    if (!backend) {
        qWarning("Can't set the play mode without a connected backend");
        return;
    }

    backend->setPlayMode(playMode);
}

void QIfMediaPlayer::setPosition(qint64 position)
{
    Q_D(QIfMediaPlayer);
    QIfMediaPlayerBackendInterface *backend = d->playerBackend();
    if (!backend) {
        qWarning("Can't set the position without a connected backend");
        return;
    }

    backend->setPosition(position);
}

/*!
    \qmlmethod MediaPlayer::play()

    Starts to play the current track. If the playQueue is empty
    it's up to the backend to decide what to do.

    \sa pause() stop()
*/

/*!
    \fn void QIfMediaPlayer::play()

    Starts to play the current track. If the playQueue is empty
    it's up to the backend to decide what to do.

    \sa pause() stop()
*/
void QIfMediaPlayer::play()
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't start playing without a connected backend");

    backend->play();
}

/*!
    \qmlmethod MediaPlayer::pause()

    Pauses the currently ongoing playback.

    \sa play() stop()
*/

/*!
    \fn void QIfMediaPlayer::pause()

    Pauses the currently ongoing playback.

    \sa play() stop()
*/
void QIfMediaPlayer::pause()
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't pause playing without a connected backend");

    backend->pause();
}

/*!
    \qmlmethod MediaPlayer::stop()

    Stops the currently ongoing playback.

    \sa play() pause()
*/

/*!
    \fn void QIfMediaPlayer::stop()

    Stops the currently ongoing playback.

    \sa play() pause()
*/
void QIfMediaPlayer::stop()
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't stop playing without a connected backend");

    backend->stop();
}

/*!
    \qmlmethod MediaPlayer::seek(offset)

    Seeks into the current track using \a offset.

    The offset can be positive or negative to either seek forward
    or backward. A successful seek will result in a change of the
    position property.
*/

/*!
    \fn void QIfMediaPlayer::seek(qint64 offset)

    Seeks into the current track using \a offset.

    The offset can be positive or negative to either seek forward
    or backward. A successful seek will result in a change of the
    position property.
*/
void QIfMediaPlayer::seek(qint64 offset)
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't seek without a connected backend");

    backend->seek(offset);
}

/*!
    \qmlmethod MediaPlayer::next()

    Skips to the next track in the playQueue.

    \sa playMode
*/

/*!
    \fn void QIfMediaPlayer::next()

    Skips to the next track in the playQueue.

    \sa playMode
*/
void QIfMediaPlayer::next()
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't skip to the next track without a connected backend");

    backend->next();
}

/*!
    \qmlmethod MediaPlayer::previous()

    Skips to the previous track in the playQueue.

    \sa playMode
*/

/*!
    \fn void QIfMediaPlayer::previous()

    Skips to the previous track in the playQueue.

    \sa playMode
*/
void QIfMediaPlayer::previous()
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't skip to the previous track without a connected backend");

    backend->previous();
}

void QIfMediaPlayer::setVolume(int volume)
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't set the volume without a connected backend");

    backend->setVolume(volume);
}

void QIfMediaPlayer::setMuted(bool muted)
{
    Q_IF_BACKEND(QIfMediaPlayer, d->playerBackend(), "Can't set muted without a connected backend");

    backend->setMuted(muted);
}

/*!
    \internal
*/
QIfMediaPlayer::QIfMediaPlayer(QIfMediaPlayerPrivate &dd, QObject *parent)
    : QIfAbstractFeature(dd, parent)
{
}

/*!
    \reimp
*/
void QIfMediaPlayer::connectToServiceObject(QIfServiceObject *serviceObject)
{
    Q_UNUSED(serviceObject);

    Q_D(QIfMediaPlayer);

    QIfMediaPlayerBackendInterface *backend = d->playerBackend();
    if (!backend)
        return;

    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::playModeChanged,
                            d, &QIfMediaPlayerPrivate::onPlayModeChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::playStateChanged,
                            d, &QIfMediaPlayerPrivate::onPlayStateChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::positionChanged,
                            d, &QIfMediaPlayerPrivate::onPositionChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::currentTrackChanged,
                            d, &QIfMediaPlayerPrivate::onCurrentTrackChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::durationChanged,
                            d, &QIfMediaPlayerPrivate::onDurationChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::volumeChanged,
                            d, &QIfMediaPlayerPrivate::onVolumeChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::mutedChanged,
                            d, &QIfMediaPlayerPrivate::onMutedChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::canReportCountChanged,
                            d->m_playQueue->d_func(), &QIfPlayQueuePrivate::onCanReportCountChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::dataFetched,
                            d->m_playQueue->d_func(), &QIfPlayQueuePrivate::onDataFetched);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::countChanged,
                            d->m_playQueue->d_func(), &QIfPlayQueuePrivate::onCountChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::dataChanged,
                            d->m_playQueue->d_func(), &QIfPlayQueuePrivate::onDataChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::currentIndexChanged,
                            d->m_playQueue->d_func(), &QIfPlayQueuePrivate::onCurrentIndexChanged);
    QObjectPrivate::connect(backend, &QIfMediaPlayerBackendInterface::initializationDone,
                            d->m_playQueue->d_func(), &QIfPlayQueuePrivate::onInitializationDone);

    QIfAbstractFeature::connectToServiceObject(serviceObject);
}

/*!
    \reimp
*/
void QIfMediaPlayer::clearServiceObject()
{
    Q_D(QIfMediaPlayer);
    d->clearToDefaults();
}

QDataStream &operator <<(QDataStream &out, QIfMediaPlayer::PlayMode var)
{
    out << int(var);
    return out;
}

QDataStream &operator>>(QDataStream &in, QIfMediaPlayer::PlayMode &var)
{
    int val;
    in >> val;
    QMetaEnum metaEnum = QMetaEnum::fromType<QIfMediaPlayer::PlayMode>();
    if (metaEnum.valueToKey(val) == nullptr)
        qWarning() << "Received an invalid enum value for type QIfMediaPlayer::PlayMode, value =" << val;
    var = QIfMediaPlayer::PlayMode(val);
    return in;
}

QDataStream &operator <<(QDataStream &out, QIfMediaPlayer::PlayState var)
{
    out << int(var);
    return out;
}

QDataStream &operator>>(QDataStream &in, QIfMediaPlayer::PlayState &var)
{
    int val;
    in >> val;
    QMetaEnum metaEnum = QMetaEnum::fromType<QIfMediaPlayer::PlayState>();
    if (metaEnum.valueToKey(val) == nullptr)
        qWarning() << "Received an invalid enum value for type QIfMediaPlayer::PlayState, value =" << val;
    var = QIfMediaPlayer::PlayState(val);
    return in;
}

QT_END_NAMESPACE

#include "moc_qifmediaplayer.cpp"