summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/phonon/mmf/mediaobject.cpp
blob: f004fd71899d54420239e75da97c6c19a1ddf2ae (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
/*  This file is part of the KDE project.

Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 or 3 of the License.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this library.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "audiooutput.h"
#include "audioplayer.h"
#include "defs.h"
#include "dummyplayer.h"
#include "utils.h"
#include "utils.h"
#include "mmf_videoplayer.h"
#include "videowidget.h"

#include "mediaobject.h"

#include <QDir>
#include <QUrl>

QT_BEGIN_NAMESPACE

using namespace Phonon;
using namespace Phonon::MMF;

/*! \class MMF::MediaObject
  \internal
*/

//-----------------------------------------------------------------------------
// Constructor / destructor
//-----------------------------------------------------------------------------

MMF::MediaObject::MediaObject(QObject *parent) : MMF::MediaNode::MediaNode(parent)
                                               , m_recognizerOpened(false)
{
    m_player.reset(new DummyPlayer());

    TRACE_CONTEXT(MediaObject::MediaObject, EAudioApi);
    TRACE_ENTRY_0();

    Q_UNUSED(parent);

    TRACE_EXIT_0();
}

MMF::MediaObject::~MediaObject()
{
    TRACE_CONTEXT(MediaObject::~MediaObject, EAudioApi);
    TRACE_ENTRY_0();

    m_file.Close();
    m_fileServer.Close();
    m_recognizer.Close();

    TRACE_EXIT_0();
}


//-----------------------------------------------------------------------------
// Recognizer
//-----------------------------------------------------------------------------

bool MMF::MediaObject::openRecognizer()
{
    TRACE_CONTEXT(MediaObject::openRecognizer, EAudioInternal);

    if (!m_recognizerOpened) {
        TInt err = m_recognizer.Connect();
        if (KErrNone != err) {
            TRACE("RApaLsSession::Connect error %d", err);
            return false;
        }

        err = m_fileServer.Connect();
        if (KErrNone != err) {
            TRACE("RFs::Connect error %d", err);
            return false;
        }

        // This must be called in order to be able to share file handles with
        // the recognizer server (see fileMediaType function).
        err = m_fileServer.ShareProtected();
        if (KErrNone != err) {
            TRACE("RFs::ShareProtected error %d", err);
            return false;
        }

        m_recognizerOpened = true;
    }

    return true;
}

MMF::MediaType MMF::MediaObject::fileMediaType
(const QString& fileName)
{
    TRACE_CONTEXT(MediaObject::fileMediaType, EAudioInternal);

    MediaType result = MediaTypeUnknown;

    if (openRecognizer()) {

        const QHBufC fileNameSymbian(QDir::toNativeSeparators(fileName));

        m_file.Close();
        TInt err = m_file.Open(m_fileServer, *fileNameSymbian, EFileRead | EFileShareReadersOnly);

        if (KErrNone == err) {
            TDataRecognitionResult recognizerResult;
            err = m_recognizer.RecognizeData(m_file, recognizerResult);
            if (KErrNone == err) {
                const TPtrC mimeType = recognizerResult.iDataType.Des();
                result = Utils::mimeTypeToMediaType(mimeType);
            } else {
                TRACE("RApaLsSession::RecognizeData filename %S error %d", fileNameSymbian.data(), err);
            }
        } else {
            TRACE("RFile::Open filename %S error %d", fileNameSymbian.data(), err);
        }
    }

    return result;
}


//-----------------------------------------------------------------------------
// MediaObjectInterface
//-----------------------------------------------------------------------------

void MMF::MediaObject::play()
{
    m_player->play();
}

void MMF::MediaObject::pause()
{
    m_player->pause();
}

void MMF::MediaObject::stop()
{
    m_player->stop();
}

void MMF::MediaObject::seek(qint64 ms)
{
    m_player->seek(ms);

    if (state() == PausedState or state() == PlayingState) {
        emit tick(currentTime());
    }
}

qint32 MMF::MediaObject::tickInterval() const
{
    return m_player->tickInterval();
}

void MMF::MediaObject::setTickInterval(qint32 interval)
{
    m_player->setTickInterval(interval);
}

bool MMF::MediaObject::hasVideo() const
{
    return m_player->hasVideo();
}

bool MMF::MediaObject::isSeekable() const
{
    return m_player->isSeekable();
}

Phonon::State MMF::MediaObject::state() const
{
    return m_player->state();
}

qint64 MMF::MediaObject::currentTime() const
{
    return m_player->currentTime();
}

QString MMF::MediaObject::errorString() const
{
    return m_player->errorString();
}

Phonon::ErrorType MMF::MediaObject::errorType() const
{
    return m_player->errorType();
}

qint64 MMF::MediaObject::totalTime() const
{
    return m_player->totalTime();
}

MediaSource MMF::MediaObject::source() const
{
    return m_player->source();
}

void MMF::MediaObject::setSource(const MediaSource &source)
{
    createPlayer(source);

    // This is a hack to work around KErrInUse from MMF client utility
    // OpenFileL calls
    m_player->setFileSource(source, m_file);

    emit currentSourceChanged(source);
}

void MMF::MediaObject::createPlayer(const MediaSource &source)
{
    TRACE_CONTEXT(MediaObject::createPlayer, EAudioApi);
    TRACE_ENTRY("state %d source.type %d", state(), source.type());
    TRACE_ENTRY("source.type %d", source.type());

    MediaType mediaType = MediaTypeUnknown;

    AbstractPlayer* oldPlayer = m_player.data();

    const bool oldPlayerHasVideo = oldPlayer->hasVideo();
    const bool oldPlayerSeekable = oldPlayer->isSeekable();

    Phonon::ErrorType error = NoError;
    QString errorMessage;

    // Determine media type
    switch (source.type()) {
    case MediaSource::LocalFile:
        mediaType = fileMediaType(source.fileName());
        break;

    case MediaSource::Url:
        {
            const QUrl url(source.url());
            if (url.scheme() == QLatin1String("file")) {
                mediaType = fileMediaType(url.toLocalFile());
            }
            else {
                errorMessage = QLatin1String("Network streaming not supported yet");
                error = NormalError;
            }
        }
        break;

    case MediaSource::Invalid:
    case MediaSource::Disc:
    case MediaSource::Stream:
        TRACE_0("Unsupported media type");
        error = NormalError;
        break;

    case MediaSource::Empty:
        TRACE_0("Empty media source");
        break;
    }

    AbstractPlayer* newPlayer = 0;

    // Construct newPlayer using oldPlayer (if not 0) in order to copy
    // parameters (volume, prefinishMark, transitionTime) which may have
    // been set on oldPlayer.

    switch (mediaType) {
    case MediaTypeUnknown:
        TRACE_0("Media type could not be determined");
        if (oldPlayer) {
            newPlayer = new DummyPlayer(*oldPlayer);
        } else {
            newPlayer = new DummyPlayer();
        }

        error = NormalError;
        errorMessage = tr("Media type could not be determined");
        break;

    case MediaTypeAudio:
        if (oldPlayer) {
            newPlayer = new AudioPlayer(*oldPlayer);
        } else {
            newPlayer = new AudioPlayer();
        }
        break;

    case MediaTypeVideo:
        if (oldPlayer) {
            newPlayer = new VideoPlayer(*oldPlayer);
        } else {
            newPlayer = new VideoPlayer();
        }
        break;
    }

    m_player.reset(newPlayer);

    if (oldPlayerHasVideo != hasVideo()) {
        emit hasVideoChanged(hasVideo());
    }

    if (oldPlayerSeekable != isSeekable()) {
        emit seekableChanged(isSeekable());
    }

    connect(m_player.data(), SIGNAL(totalTimeChanged(qint64)), SIGNAL(totalTimeChanged(qint64)));
    connect(m_player.data(), SIGNAL(stateChanged(Phonon::State, Phonon::State)), SIGNAL(stateChanged(Phonon::State, Phonon::State)));
    connect(m_player.data(), SIGNAL(finished()), SIGNAL(finished()));
    connect(m_player.data(), SIGNAL(tick(qint64)), SIGNAL(tick(qint64)));
    connect(m_player.data(), SIGNAL(metaDataChanged(const QMultiMap<QString, QString>&)), SIGNAL(metaDataChanged(const QMultiMap<QString, QString>&)));

    // We need to call setError() after doing the connects, otherwise the
    // error won't be received.
    if (error != NoError) {
        Q_ASSERT(m_player);
        m_player->setError(error, errorMessage);
    }

    TRACE_EXIT_0();
}

void MMF::MediaObject::setNextSource(const MediaSource &source)
{
    m_player->setNextSource(source);
}

qint32 MMF::MediaObject::prefinishMark() const
{
    return m_player->prefinishMark();
}

void MMF::MediaObject::setPrefinishMark(qint32 mark)
{
    m_player->setPrefinishMark(mark);
}

qint32 MMF::MediaObject::transitionTime() const
{
    return m_player->transitionTime();
}

void MMF::MediaObject::setTransitionTime(qint32 time)
{
    m_player->setTransitionTime(time);
}


//-----------------------------------------------------------------------------
// VolumeObserver
//-----------------------------------------------------------------------------

void MMF::MediaObject::volumeChanged(qreal volume)
{
    m_player->volumeChanged(volume);
}

//-----------------------------------------------------------------------------
// Video output
//-----------------------------------------------------------------------------

void MMF::MediaObject::setVideoOutput(VideoOutput* videoOutput)
{
    m_player->setVideoOutput(videoOutput);
}


AbstractPlayer *MMF::MediaObject::abstractPlayer() const
{
    return m_player.data();
}

bool MMF::MediaObject::activateOnMediaObject(MediaObject *)
{
    // Guess what, we do nothing.
    return true;
}

QT_END_NAMESPACE