1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qmlmediabase_p.h" |
|
43 |
|
44 #include <QtCore/qcoreevent.h> |
|
45 #include <QtCore/qurl.h> |
|
46 |
|
47 #include <qmediaplayercontrol.h> |
|
48 #include <qmediaservice.h> |
|
49 #include <qmediaserviceprovider.h> |
|
50 #include <qmetadatacontrol.h> |
|
51 #include <qmetadatacontrolmetaobject_p.h> |
|
52 |
|
53 QTM_BEGIN_NAMESPACE |
|
54 |
|
55 class QmlMediaBaseObject : public QMediaObject |
|
56 { |
|
57 public: |
|
58 QmlMediaBaseObject(QMediaService *service) |
|
59 : QMediaObject(0, service) |
|
60 { |
|
61 } |
|
62 }; |
|
63 |
|
64 class QmlMediaBasePlayerControl : public QMediaPlayerControl |
|
65 { |
|
66 public: |
|
67 QmlMediaBasePlayerControl(QObject *parent) |
|
68 : QMediaPlayerControl(parent) |
|
69 { |
|
70 } |
|
71 |
|
72 QMediaPlayer::State state() const { return QMediaPlayer::StoppedState; } |
|
73 QMediaPlayer::MediaStatus mediaStatus() const { return QMediaPlayer::NoMedia; } |
|
74 |
|
75 qint64 duration() const { return 0; } |
|
76 qint64 position() const { return 0; } |
|
77 void setPosition(qint64) {} |
|
78 int volume() const { return 100; } |
|
79 void setVolume(int) {} |
|
80 bool isMuted() const { return false; } |
|
81 void setMuted(bool) {} |
|
82 int bufferStatus() const { return 0; } |
|
83 bool isAudioAvailable() const { return false; } |
|
84 bool isVideoAvailable() const { return false; } |
|
85 bool isSeekable() const { return false; } |
|
86 QMediaTimeRange availablePlaybackRanges() const { return QMediaTimeRange(); } |
|
87 qreal playbackRate() const { return 1; } |
|
88 void setPlaybackRate(qreal) {} |
|
89 QMediaContent media() const { return QMediaContent(); } |
|
90 const QIODevice *mediaStream() const { return 0; } |
|
91 void setMedia(const QMediaContent &, QIODevice *) {} |
|
92 |
|
93 void play() {} |
|
94 void pause() {} |
|
95 void stop() {} |
|
96 }; |
|
97 |
|
98 class QmlMediaBaseAnimation : public QObject |
|
99 { |
|
100 public: |
|
101 QmlMediaBaseAnimation(QmlMediaBase *media) |
|
102 : m_media(media) |
|
103 { |
|
104 } |
|
105 |
|
106 void start() { if (!m_timer.isActive()) m_timer.start(500, this); } |
|
107 void stop() { m_timer.stop(); } |
|
108 |
|
109 protected: |
|
110 void timerEvent(QTimerEvent *event) |
|
111 { |
|
112 if (event->timerId() == m_timer.timerId()) { |
|
113 event->accept(); |
|
114 |
|
115 if (m_media->m_state == QMediaPlayer::PlayingState) |
|
116 emit m_media->positionChanged(); |
|
117 if (m_media->m_status == QMediaPlayer::BufferingMedia || QMediaPlayer::StalledMedia) |
|
118 emit m_media->bufferProgressChanged(); |
|
119 } else { |
|
120 QObject::timerEvent(event); |
|
121 } |
|
122 } |
|
123 |
|
124 private: |
|
125 QmlMediaBase *m_media; |
|
126 QBasicTimer m_timer; |
|
127 }; |
|
128 |
|
129 void QmlMediaBase::_q_stateChanged(QMediaPlayer::State state) |
|
130 { |
|
131 if (state != m_state) { |
|
132 QMediaPlayer::State oldState = state; |
|
133 |
|
134 m_state = state; |
|
135 |
|
136 switch (state) { |
|
137 case QMediaPlayer::PlayingState: |
|
138 if (oldState == QMediaPlayer::StoppedState) |
|
139 emit started(); |
|
140 else if (oldState == QMediaPlayer::PausedState) |
|
141 emit resumed(); |
|
142 break; |
|
143 case QMediaPlayer::PausedState: |
|
144 emit paused(); |
|
145 case QMediaPlayer::StoppedState: |
|
146 emit stopped(); |
|
147 break; |
|
148 default: |
|
149 break; |
|
150 } |
|
151 |
|
152 emit playingChanged(); |
|
153 emit pausedChanged(); |
|
154 |
|
155 if (m_state == QMediaPlayer::PlayingState |
|
156 || m_status == QMediaPlayer::BufferingMedia |
|
157 || m_status == QMediaPlayer::StalledMedia) { |
|
158 m_animation->start(); |
|
159 } else { |
|
160 m_animation->stop(); |
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 void QmlMediaBase::_q_mediaStatusChanged(QMediaPlayer::MediaStatus status) |
|
166 { |
|
167 if (status != m_status) { |
|
168 m_status = status; |
|
169 |
|
170 switch (status) { |
|
171 case QMediaPlayer::LoadedMedia: |
|
172 emit loaded(); |
|
173 break; |
|
174 case QMediaPlayer::BufferingMedia: |
|
175 emit buffering(); |
|
176 break; |
|
177 case QMediaPlayer::BufferedMedia: |
|
178 emit buffered(); |
|
179 break; |
|
180 case QMediaPlayer::StalledMedia: |
|
181 emit stalled(); |
|
182 break; |
|
183 case QMediaPlayer::EndOfMedia: |
|
184 emit endOfMedia(); |
|
185 break; |
|
186 default: |
|
187 break; |
|
188 } |
|
189 |
|
190 emit statusChanged(); |
|
191 |
|
192 if (m_state == QMediaPlayer::PlayingState |
|
193 || m_status == QMediaPlayer::BufferingMedia |
|
194 || m_status == QMediaPlayer::StalledMedia) { |
|
195 m_animation->start(); |
|
196 } else { |
|
197 m_animation->stop(); |
|
198 } |
|
199 } |
|
200 } |
|
201 |
|
202 void QmlMediaBase::_q_metaDataChanged() |
|
203 { |
|
204 m_metaObject->metaDataChanged(); |
|
205 } |
|
206 |
|
207 QmlMediaBase::QmlMediaBase() |
|
208 : m_mediaService(0) |
|
209 , m_playerControl(0) |
|
210 , m_mediaObject(0) |
|
211 , m_mediaProvider(0) |
|
212 , m_metaDataControl(0) |
|
213 , m_metaObject(0) |
|
214 , m_animation(0) |
|
215 , m_state(QMediaPlayer::StoppedState) |
|
216 , m_status(QMediaPlayer::NoMedia) |
|
217 , m_error(QMediaPlayer::NoError) |
|
218 { |
|
219 } |
|
220 |
|
221 QmlMediaBase::~QmlMediaBase() |
|
222 { |
|
223 } |
|
224 |
|
225 void QmlMediaBase::shutdown() |
|
226 { |
|
227 delete m_metaObject; |
|
228 delete m_mediaObject; |
|
229 |
|
230 if (m_mediaProvider) |
|
231 m_mediaProvider->releaseService(m_mediaService); |
|
232 |
|
233 delete m_animation; |
|
234 |
|
235 } |
|
236 |
|
237 void QmlMediaBase::setObject(QObject *object) |
|
238 { |
|
239 if ((m_mediaProvider = QMediaServiceProvider::defaultServiceProvider())) { |
|
240 if ((m_mediaService = m_mediaProvider->requestService(Q_MEDIASERVICE_MEDIAPLAYER))) { |
|
241 m_playerControl = qobject_cast<QMediaPlayerControl *>( |
|
242 m_mediaService->control(QMediaPlayerControl_iid)); |
|
243 m_metaDataControl = qobject_cast<QMetaDataControl *>( |
|
244 m_mediaService->control(QMetaDataControl_iid)); |
|
245 m_mediaObject = new QmlMediaBaseObject(m_mediaService); |
|
246 } |
|
247 } |
|
248 |
|
249 if (m_playerControl) { |
|
250 QObject::connect(m_playerControl, SIGNAL(stateChanged(QMediaPlayer::State)), |
|
251 object, SLOT(_q_stateChanged(QMediaPlayer::State))); |
|
252 QObject::connect(m_playerControl, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), |
|
253 object, SLOT(_q_mediaStatusChanged(QMediaPlayer::MediaStatus))); |
|
254 QObject::connect(m_playerControl, SIGNAL(mediaChanged(QMediaContent)), |
|
255 object, SIGNAL(sourceChanged())); |
|
256 QObject::connect(m_playerControl, SIGNAL(durationChanged(qint64)), |
|
257 object, SIGNAL(durationChanged())); |
|
258 QObject::connect(m_playerControl, SIGNAL(positionChanged(qint64)), |
|
259 object, SIGNAL(positionChanged())); |
|
260 QObject::connect(m_playerControl, SIGNAL(volumeChanged(int)), |
|
261 object, SIGNAL(volumeChanged())); |
|
262 QObject::connect(m_playerControl, SIGNAL(mutedChanged(bool)), |
|
263 object, SIGNAL(mutedChanged())); |
|
264 |
|
265 m_animation = new QmlMediaBaseAnimation(this); |
|
266 } else { |
|
267 m_playerControl = new QmlMediaBasePlayerControl(object); |
|
268 } |
|
269 |
|
270 if (m_metaDataControl) { |
|
271 m_metaObject = new QMetaDataControlMetaObject(m_metaDataControl, object); |
|
272 |
|
273 QObject::connect(m_metaDataControl, SIGNAL(metaDataChanged()), |
|
274 object, SLOT(_q_metaDataChanged())); |
|
275 } |
|
276 } |
|
277 |
|
278 QUrl QmlMediaBase::source() const |
|
279 { |
|
280 return m_playerControl->media().canonicalUrl(); |
|
281 } |
|
282 |
|
283 void QmlMediaBase::setSource(const QUrl &url) |
|
284 { |
|
285 m_playerControl->setMedia(QMediaContent(url), 0); |
|
286 } |
|
287 |
|
288 bool QmlMediaBase::isPlaying() const |
|
289 { |
|
290 return m_state != QMediaPlayer::StoppedState; |
|
291 } |
|
292 |
|
293 void QmlMediaBase::setPlaying(bool playing) |
|
294 { |
|
295 if (playing && m_state == QMediaPlayer::StoppedState) |
|
296 m_playerControl->play(); |
|
297 else if (!playing) |
|
298 m_playerControl->stop(); |
|
299 } |
|
300 |
|
301 bool QmlMediaBase::isPaused() const |
|
302 { |
|
303 return m_state == QMediaPlayer::PausedState; |
|
304 } |
|
305 |
|
306 void QmlMediaBase::setPaused(bool paused) |
|
307 { |
|
308 if (paused && m_state == QMediaPlayer::PlayingState) |
|
309 m_playerControl->pause(); |
|
310 if (!paused && m_state == QMediaPlayer::PausedState) |
|
311 m_playerControl->play(); |
|
312 } |
|
313 |
|
314 int QmlMediaBase::duration() const |
|
315 { |
|
316 return m_playerControl->duration(); |
|
317 } |
|
318 |
|
319 int QmlMediaBase::position() const |
|
320 { |
|
321 return m_playerControl->position(); |
|
322 |
|
323 } |
|
324 |
|
325 void QmlMediaBase::setPosition(int position) |
|
326 { |
|
327 m_playerControl->setPosition(position); |
|
328 } |
|
329 |
|
330 qreal QmlMediaBase::volume() const |
|
331 { |
|
332 return qreal(m_playerControl->volume()) / 100; |
|
333 } |
|
334 |
|
335 void QmlMediaBase::setVolume(qreal volume) |
|
336 { |
|
337 m_playerControl->setVolume(volume * 100); |
|
338 } |
|
339 |
|
340 bool QmlMediaBase::isMuted() const |
|
341 { |
|
342 return m_playerControl->isMuted(); |
|
343 } |
|
344 |
|
345 void QmlMediaBase::setMuted(bool muted) |
|
346 { |
|
347 m_playerControl->setMuted(muted); |
|
348 } |
|
349 |
|
350 qreal QmlMediaBase::bufferProgress() const |
|
351 { |
|
352 return qreal(m_playerControl->bufferStatus()) / 100; |
|
353 } |
|
354 |
|
355 bool QmlMediaBase::isSeekable() const |
|
356 { |
|
357 return m_playerControl->isSeekable(); |
|
358 } |
|
359 |
|
360 qreal QmlMediaBase::playbackRate() const |
|
361 { |
|
362 return m_playerControl->playbackRate(); |
|
363 } |
|
364 |
|
365 void QmlMediaBase::setPlaybackRate(qreal rate) |
|
366 { |
|
367 m_playerControl->setPlaybackRate(rate); |
|
368 } |
|
369 |
|
370 QString QmlMediaBase::errorString() const |
|
371 { |
|
372 return m_errorString; |
|
373 } |
|
374 |
|
375 QTM_END_NAMESPACE |
|
376 |
|