aboutsummaryrefslogtreecommitdiffstats
path: root/src/ivimedia/qivimediaplayer.cpp
blob: 2c573dcc570c39d6e4a75f9da34660a254e5148f (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
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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtIvi module 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 "qivimediaplayer.h"
#include "qivimediaplayer_p.h"
#include "qiviplayqueue.h"
#include "qiviplayqueue_p.h"
#include "qiviqmlconversion_helper.h"
#include <QtIviCore/QIviServiceObject>
#include <QtDebug>
#include <QDataStream>
#include <QMetaEnum>

QT_BEGIN_NAMESPACE

QIviMediaPlayerPrivate::QIviMediaPlayerPrivate(const QString &interface, QIviMediaPlayer *parent)
    : QIviAbstractFeaturePrivate(interface, parent)
    , q_ptr(parent)
    , m_playQueue(nullptr)
    , m_playMode(QIviMediaPlayer::Normal)
    , m_playState(QIviMediaPlayer::Stopped)
    , m_currentTrack(nullptr)
    , m_position(-1)
    , m_duration(-1)
    , m_volume(0)
    , m_muted(false)
{
    qRegisterMetaType<QIviPlayQueue*>();
    qRegisterMetaType<QIviPlayableItem>();
    qRegisterMetaType<QIviAudioTrackItem>();
}

void QIviMediaPlayerPrivate::initialize()
{
    QIviAbstractFeaturePrivate::initialize();
    m_playQueue = new QIviPlayQueue(q_ptr);
}

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

void QIviMediaPlayerPrivate::onPlayModeChanged(QIviMediaPlayer::PlayMode playMode)
{
    if (m_playMode == playMode)
        return;

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

void QIviMediaPlayerPrivate::onPlayStateChanged(QIviMediaPlayer::PlayState playState)
{
    if (m_playState == playState)
        return;

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

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

    const QIviPlayableItem *currentItem = nullptr;

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

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

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

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

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

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

QIviMediaPlayerBackendInterface *QIviMediaPlayerPrivate::playerBackend() const
{
    return backend<QIviMediaPlayerBackendInterface*>();
}

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

    The QIviMediaPlayer 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.
*/

/*!
    \qmltype MediaPlayer
    \instantiates QIviMediaPlayer
    \inqmlmodule QtIvi.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.
*/

/*!
    \enum QIviMediaPlayer::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 QIviMediaPlayer::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 QIviMediaPlayer.

    The \a parent argument is passed on to the \l QIviAbstractFeature base class.
*/
QIviMediaPlayer::QIviMediaPlayer(QObject *parent)
    : QIviAbstractFeature(*new QIviMediaPlayerPrivate(QLatin1String(QIviMediaPlayer_iid), this), parent)
{
}

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

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

    \sa QIviPlayQueue
*/
QIviPlayQueue *QIviMediaPlayer::playQueue() const
{
    Q_D(const QIviMediaPlayer);
    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 QIviMediaPlayer::playMode
    \brief Holds the current playback mode of the media player.
*/
QIviMediaPlayer::PlayMode QIviMediaPlayer::playMode() const
{
    Q_D(const QIviMediaPlayer);
    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 QIviMediaPlayer::playState
    \brief Holds the current playback state of the media player.
*/
QIviMediaPlayer::PlayState QIviMediaPlayer::playState() const
{
    Q_D(const QIviMediaPlayer);
    return d->m_playState;
}

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

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

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

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

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

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


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

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

    backend->setPlayMode(playMode);
}

void QIviMediaPlayer::setPosition(qint64 position)
{
    Q_D(QIviMediaPlayer);
    QIviMediaPlayerBackendInterface *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 QIviMediaPlayer::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 QIviMediaPlayer::play()
{
    Q_IVI_BACKEND(QIviMediaPlayer, 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 QIviMediaPlayer::pause()

    Pauses the currently ongoing playback.

    \sa play() stop()
*/
void QIviMediaPlayer::pause()
{
    Q_IVI_BACKEND(QIviMediaPlayer, 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 QIviMediaPlayer::stop()

    Stops the currently ongoing playback.

    \sa play() pause()
*/
void QIviMediaPlayer::stop()
{
    Q_IVI_BACKEND(QIviMediaPlayer, 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 QIviMediaPlayer::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 QIviMediaPlayer::seek(qint64 offset)
{
    Q_IVI_BACKEND(QIviMediaPlayer, 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 QIviMediaPlayer::next()

    Skips to the next track in the playQueue.

    \sa playMode
*/
void QIviMediaPlayer::next()
{
    Q_IVI_BACKEND(QIviMediaPlayer, 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 QIviMediaPlayer::previous()

    Skips to the previous track in the playQueue.

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

    backend->previous();
}

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

    backend->setVolume(volume);
}

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

    backend->setMuted(muted);
}

/*!
    \internal
*/
QIviMediaPlayer::QIviMediaPlayer(QIviMediaPlayerPrivate &dd, QObject *parent)
    : QIviAbstractFeature(dd, parent)
{
}

/*!
    \reimp
*/
void QIviMediaPlayer::connectToServiceObject(QIviServiceObject *serviceObject)
{
    Q_UNUSED(serviceObject);

    Q_D(QIviMediaPlayer);

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

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

    QIviAbstractFeature::connectToServiceObject(serviceObject);
}

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

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

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

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

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

QT_END_NAMESPACE

#include "moc_qivimediaplayer.cpp"