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
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "s60mediaplayersession.h"
#include <QtCore/qdebug.h>
#include <QtCore/qdir.h>
#include <QtCore/qvariant.h>
#include <QtCore/qtimer.h>
#include <mmf/common/mmferrors.h>
#include <qmediatimerange.h>
QT_BEGIN_NAMESPACE
S60MediaPlayerSession::S60MediaPlayerSession(QObject *parent)
: QObject(parent)
, m_playbackRate(0)
, m_muted(false)
, m_volume(0)
, m_state(QMediaPlayer::StoppedState)
, m_mediaStatus(QMediaPlayer::UnknownMediaStatus)
, m_progressTimer(new QTimer(this))
, m_stalledTimer(new QTimer(this))
, m_error(KErrNone)
, m_play_requested(false)
, m_stream(false)
{
connect(m_progressTimer, SIGNAL(timeout()), this, SLOT(tick()));
connect(m_stalledTimer, SIGNAL(timeout()), this, SLOT(stalled()));
}
S60MediaPlayerSession::~S60MediaPlayerSession()
{
}
int S60MediaPlayerSession::volume() const
{
return m_volume;
}
void S60MediaPlayerSession::setVolume(int volume)
{
if (m_volume == volume)
return;
m_volume = volume;
// Dont set symbian players volume until media loaded.
// Leaves with KerrNotReady although documentation says otherwise.
if (!m_muted &&
( mediaStatus() == QMediaPlayer::LoadedMedia
|| mediaStatus() == QMediaPlayer::StalledMedia
|| mediaStatus() == QMediaPlayer::BufferingMedia
|| mediaStatus() == QMediaPlayer::BufferedMedia
|| mediaStatus() == QMediaPlayer::EndOfMedia)) {
TRAPD(err, doSetVolumeL(m_volume));
setError(err);
}
}
bool S60MediaPlayerSession::isMuted() const
{
return m_muted;
}
bool S60MediaPlayerSession::isSeekable() const
{
return (m_stream)?false:true;
}
void S60MediaPlayerSession::setMediaStatus(QMediaPlayer::MediaStatus status)
{
if (m_mediaStatus == status)
return;
m_mediaStatus = status;
emit mediaStatusChanged(m_mediaStatus);
if (m_play_requested)
play();
}
void S60MediaPlayerSession::setState(QMediaPlayer::State state)
{
if (m_state == state)
return;
m_state = state;
emit stateChanged(m_state);
}
QMediaPlayer::State S60MediaPlayerSession::state() const
{
return m_state;
}
QMediaPlayer::MediaStatus S60MediaPlayerSession::mediaStatus() const
{
return m_mediaStatus;
}
void S60MediaPlayerSession::load(QUrl url)
{
setMediaStatus(QMediaPlayer::LoadingMedia);
startStalledTimer();
m_stream = (url.scheme() == "file")?false:true;
TRAPD(err,
if(m_stream)
doLoadUrlL(QString2TPtrC(url.toString()));
else
doLoadL(QString2TPtrC(QDir::toNativeSeparators(url.toLocalFile()))));
setError(err);
}
void S60MediaPlayerSession::play()
{
if (state() == QMediaPlayer::PlayingState
|| mediaStatus() == QMediaPlayer::UnknownMediaStatus
|| mediaStatus() == QMediaPlayer::NoMedia
|| mediaStatus() == QMediaPlayer::InvalidMedia)
return;
if (mediaStatus() == QMediaPlayer::LoadingMedia) {
m_play_requested = true;
return;
}
m_play_requested = false;
setState(QMediaPlayer::PlayingState);
startProgressTimer();
doPlay();
}
void S60MediaPlayerSession::pause()
{
if (mediaStatus() == QMediaPlayer::NoMedia ||
mediaStatus() == QMediaPlayer::InvalidMedia)
return;
setState(QMediaPlayer::PausedState);
stopProgressTimer();
TRAP_IGNORE(doPauseL());
}
void S60MediaPlayerSession::stop()
{
m_play_requested = false;
setState(QMediaPlayer::StoppedState);
if (mediaStatus() == QMediaPlayer::BufferingMedia ||
mediaStatus() == QMediaPlayer::BufferedMedia)
setMediaStatus(QMediaPlayer::LoadedMedia);
if (mediaStatus() == QMediaPlayer::LoadingMedia)
setMediaStatus(QMediaPlayer::UnknownMediaStatus);
stopProgressTimer();
stopStalledTimer();
doStop();
emit positionChanged(0);
}
void S60MediaPlayerSession::reset()
{
m_play_requested = false;
setError(KErrNone, QString(), true);
stopProgressTimer();
stopStalledTimer();
doStop();
setState(QMediaPlayer::StoppedState);
setMediaStatus(QMediaPlayer::UnknownMediaStatus);
}
void S60MediaPlayerSession::setVideoRenderer(QObject *renderer)
{
Q_UNUSED(renderer);
}
int S60MediaPlayerSession::bufferStatus()
{
if( mediaStatus() == QMediaPlayer::LoadingMedia
|| mediaStatus() == QMediaPlayer::UnknownMediaStatus
|| mediaStatus() == QMediaPlayer::NoMedia
|| mediaStatus() == QMediaPlayer::InvalidMedia)
return 0;
int progress = 0;
TRAPD(err, progress = doGetBufferStatusL());
// If buffer status query not supported by codec return 100
// do not set error
if(err == KErrNotSupported)
return 100;
setError(err);
return progress;
}
bool S60MediaPlayerSession::isMetadataAvailable() const
{
return !m_metaDataMap.isEmpty();
}
QVariant S60MediaPlayerSession::metaData(const QString &key) const
{
return m_metaDataMap.value(key);
}
QMap<QString, QVariant> S60MediaPlayerSession::availableMetaData() const
{
return m_metaDataMap;
}
void S60MediaPlayerSession::setMuted(bool muted)
{
m_muted = muted;
if( m_mediaStatus == QMediaPlayer::LoadedMedia
|| m_mediaStatus == QMediaPlayer::StalledMedia
|| m_mediaStatus == QMediaPlayer::BufferingMedia
|| m_mediaStatus == QMediaPlayer::BufferedMedia
|| m_mediaStatus == QMediaPlayer::EndOfMedia) {
TRAPD(err, doSetVolumeL((m_muted)?0:m_volume));
setError(err);
}
}
qint64 S60MediaPlayerSession::duration() const
{
if( mediaStatus() == QMediaPlayer::LoadingMedia
|| mediaStatus() == QMediaPlayer::UnknownMediaStatus
|| mediaStatus() == QMediaPlayer::NoMedia
|| mediaStatus() == QMediaPlayer::InvalidMedia)
return -1;
qint64 pos = 0;
TRAP_IGNORE(pos = doGetDurationL());
return pos;
}
qint64 S60MediaPlayerSession::position() const
{
if( mediaStatus() == QMediaPlayer::LoadingMedia
|| mediaStatus() == QMediaPlayer::UnknownMediaStatus
|| mediaStatus() == QMediaPlayer::NoMedia
|| mediaStatus() == QMediaPlayer::InvalidMedia)
return 0;
qint64 pos = 0;
TRAP_IGNORE(pos = doGetPositionL());
return pos;
}
void S60MediaPlayerSession::setPosition(qint64 pos)
{
if (position() == pos)
return;
if (state() == QMediaPlayer::PlayingState)
pause();
TRAPD(err, doSetPositionL(pos * 1000));
setError(err);
if (state() == QMediaPlayer::PausedState)
play();
emit positionChanged(position());
}
void S60MediaPlayerSession::setAudioEndpoint(const QString& audioEndpoint)
{
doSetAudioEndpoint(audioEndpoint);
}
void S60MediaPlayerSession::loaded()
{
stopStalledTimer();
if (m_error == KErrNone || m_error == KErrMMPartialPlayback) {
setMediaStatus(QMediaPlayer::LoadedMedia);
TRAPD(err, updateMetaDataEntriesL());
setError(err);
setVolume(m_volume);
setMuted(m_muted);
emit durationChanged(duration());
emit videoAvailableChanged(isVideoAvailable());
emit audioAvailableChanged(isAudioAvailable());
}
}
void S60MediaPlayerSession::endOfMedia()
{
setMediaStatus(QMediaPlayer::EndOfMedia);
setState(QMediaPlayer::StoppedState);
emit positionChanged(0);
}
void S60MediaPlayerSession::buffering()
{
startStalledTimer();
setMediaStatus(QMediaPlayer::BufferingMedia);
}
void S60MediaPlayerSession::buffered()
{
stopStalledTimer();
setMediaStatus(QMediaPlayer::BufferedMedia);
}
void S60MediaPlayerSession::stalled()
{
setMediaStatus(QMediaPlayer::StalledMedia);
}
QMap<QString, QVariant>& S60MediaPlayerSession::metaDataEntries()
{
return m_metaDataMap;
}
QMediaPlayer::Error S60MediaPlayerSession::fromSymbianErrorToMultimediaError(int error)
{
switch(error) {
case KErrNoMemory:
case KErrNotFound:
case KErrBadHandle:
case KErrAbort:
case KErrNotSupported:
case KErrCorrupt:
case KErrGeneral:
case KErrArgument:
case KErrPathNotFound:
case KErrDied:
case KErrServerTerminated:
case KErrServerBusy:
case KErrCompletion:
case KErrBadPower:
return QMediaPlayer::ResourceError;
case KErrMMPartialPlayback:
return QMediaPlayer::FormatError;
case KErrMMAudioDevice:
case KErrMMVideoDevice:
case KErrMMDecoder:
case KErrUnknown:
return QMediaPlayer::ServiceMissingError;
case KErrMMNotEnoughBandwidth:
case KErrMMSocketServiceNotFound:
case KErrMMNetworkRead:
case KErrMMNetworkWrite:
case KErrMMServerSocket:
case KErrMMServerNotSupported:
case KErrMMUDPReceive:
case KErrMMInvalidProtocol:
case KErrMMInvalidURL:
case KErrMMMulticast:
case KErrMMProxyServer:
case KErrMMProxyServerNotSupported:
case KErrMMProxyServerConnect:
return QMediaPlayer::NetworkError;
case KErrNotReady:
case KErrInUse:
case KErrAccessDenied:
case KErrLocked:
case KErrMMDRMNotAuthorized:
case KErrPermissionDenied:
case KErrCancel:
case KErrAlreadyExists:
return QMediaPlayer::AccessDeniedError;
case KErrNone:
default:
return QMediaPlayer::NoError;
}
}
void S60MediaPlayerSession::setError(int error, const QString &errorString, bool forceReset)
{
if( forceReset ) {
m_error = KErrNone;
emit this->error(QMediaPlayer::NoError, QString());
return;
}
// If error does not change and m_error is reseted without forceReset flag
if (error == m_error ||
(m_error != KErrNone && error == KErrNone))
return;
m_error = error;
QMediaPlayer::Error mediaError = fromSymbianErrorToMultimediaError(m_error);
QString symbianError = QString(errorString);
if (mediaError != QMediaPlayer::NoError) {
// TODO: fix to user friendly string at some point
// These error string are only dev usable
symbianError.append("Symbian:");
symbianError.append(QString::number(m_error));
}
emit this->error(mediaError, symbianError);
switch(mediaError){
case QMediaPlayer::ResourceError:
case QMediaPlayer::NetworkError:
case QMediaPlayer::AccessDeniedError:
case QMediaPlayer::ServiceMissingError:
m_play_requested = false;
setMediaStatus(QMediaPlayer::InvalidMedia);
stop();
break;
}
}
void S60MediaPlayerSession::tick()
{
emit positionChanged(position());
if (bufferStatus() < 100)
emit bufferStatusChanged(bufferStatus());
}
void S60MediaPlayerSession::startProgressTimer()
{
m_progressTimer->start(500);
}
void S60MediaPlayerSession::stopProgressTimer()
{
m_progressTimer->stop();
}
void S60MediaPlayerSession::startStalledTimer()
{
m_stalledTimer->start(30000);
}
void S60MediaPlayerSession::stopStalledTimer()
{
m_stalledTimer->stop();
}
QString S60MediaPlayerSession::TDesC2QString(const TDesC& aDescriptor)
{
return QString::fromUtf16(aDescriptor.Ptr(), aDescriptor.Length());
}
TPtrC S60MediaPlayerSession::QString2TPtrC( const QString& string )
{
// Returned TPtrC is valid as long as the given parameter is valid and unmodified
return TPtrC16(static_cast<const TUint16*>(string.utf16()), string.length());
}
QRect S60MediaPlayerSession::TRect2QRect(const TRect& tr)
{
return QRect(tr.iTl.iX, tr.iTl.iY, tr.Width(), tr.Height());
}
TRect S60MediaPlayerSession::QRect2TRect(const QRect& qr)
{
return TRect(TPoint(qr.left(), qr.top()), TSize(qr.width(), qr.height()));
}
QT_END_NAMESPACE
|