1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file KwMediaManager.cpp
22 * @brief Media management interface.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwMediaManager.h"
27 #include "KwMediaPreferencesSequence.h"
28 #include "KwMediaPreferencesAudio.h"
29 #include "KwMediaPreferencesVisual.h"
31 #include <phonon/mediaobject.h>
32 #include <phonon/audiooutput.h>
33 #include <phonon/videowidget.h>
38 * Constructors + destructors
41 /// Default constructor.
42 KwMediaManager::KwMediaManager()
49 KwMediaManager::~KwMediaManager()
58 /// Link the audio to an audio output.
59 void KwMediaManager::linkAudio(KwAudioOutput
* audioOut
)
64 path
= Phonon::createPath(m_audio
.object
, audioOut
);
65 assert(path
.isValid());
66 connectAudioOutput(audioOut
);
68 m_audio
.outputs
.insert(audioOut
, path
);
71 /// Unlink an audio output.
72 void KwMediaManager::unlinkAudio(KwAudioOutput
* audioOut
)
74 NowPlayingDataAudio::Outputs::iterator it
= m_audio
.outputs
.find(audioOut
);
77 assert((*it
).isValid());
79 disconnectAudioOutput(audioOut
);
81 m_audio
.outputs
.erase(it
);
84 /// Link the video to a video output.
85 void KwMediaManager::linkVideo(KwVideoWidget
* videoOut
)
90 path
= Phonon::createPath(m_video
.object
, videoOut
);
91 assert(path
.isValid());
92 connectVideoOutput(videoOut
);
94 m_video
.outputs
.insert(videoOut
, path
);
97 /// Unlink a video output.
98 void KwMediaManager::unlinkVideo(KwVideoWidget
* videoOut
)
100 NowPlayingDataVideo::Outputs::iterator it
= m_video
.outputs
.find(videoOut
);
103 assert((*it
).isValid());
105 disconnectVideoOutput(videoOut
);
107 m_video
.outputs
.erase(it
);
114 /// Set up some audio.
115 KwMediaObject
* KwMediaManager::setupAudio(const QUrl
& url
, KwMediaPreferencesSequence
* prefsSequence
, KwMediaPreferencesAudio
* prefsAudio
)
117 assert(prefsSequence
!= 0);
118 assert(prefsAudio
!= 0);
120 KwMediaObject
* mediaObject
= new KwMediaObject
;
121 mediaObject
->setCurrentSource(url
);
124 m_audio
.object
= mediaObject
;
125 m_audio
.prefsSequence
= prefsSequence
;
126 m_audio
.prefs
= prefsAudio
;
128 prefsSequence
->setManager(this);
129 prefsAudio
->setManager(this);
131 // Link media object to audio outputs
132 NowPlayingDataAudio::Outputs::iterator it
;
133 for (it
= m_audio
.outputs
.begin(); it
!= m_audio
.outputs
.end(); ++it
)
135 *it
= Phonon::createPath(mediaObject
, it
.key());
136 assert((*it
).isValid());
137 connectAudioOutput(it
.key());
140 connectAudioObject();
147 /// Set up some video.
148 KwMediaObject
* KwMediaManager::setupVideo(const QUrl
& url
, KwMediaPreferencesSequence
* prefsSequence
, KwMediaPreferencesVisual
* prefsVideo
, KwMediaPreferencesAudio
* prefsAudio
)
150 assert(prefsSequence
!= 0);
151 assert(prefsVideo
!= 0);
153 // Stop audio iff new video has audio
154 stopVideo(prefsAudio
!= 0);
156 KwMediaObject
* mediaObject
= new KwMediaObject
;
157 mediaObject
->setCurrentSource(url
);
158 m_video
.object
= mediaObject
;
159 m_video
.prefsSequence
= prefsSequence
;
160 m_video
.prefs
= prefsVideo
;
162 prefsSequence
->setManager(this);
163 prefsVideo
->setManager(this);
165 // Link media object to video widgets
166 NowPlayingDataVideo::Outputs::iterator it
;
167 for (it
= m_video
.outputs
.begin(); it
!= m_video
.outputs
.end(); ++it
)
169 *it
= Phonon::createPath(mediaObject
, it
.key());
170 assert((*it
).isValid());
171 connectVideoOutput(it
.key());
174 connectVideoObject();
176 // Keep audio playing if we don't want audio from this video
180 m_audio
.object
= mediaObject
;
181 m_audio
.prefsSequence
= prefsSequence
;
182 m_audio
.prefs
= prefsAudio
;
184 prefsAudio
->setManager(this);
186 // Link media object to audio outputs
187 NowPlayingDataAudio::Outputs::iterator it
;
188 for (it
= m_audio
.outputs
.begin(); it
!= m_audio
.outputs
.end(); ++it
)
190 *it
= Phonon::createPath(mediaObject
, it
.key());
191 assert((*it
).isValid());
192 connectAudioOutput(it
.key());
195 connectAudioObject();
198 videoAdded(prefsAudio
!= 0);
207 /// Stop all playing media.
208 void KwMediaManager::stopAll()
214 /// Stop any playing audio.
215 void KwMediaManager::stopAudio(bool stopLinkedVideo
)
219 disconnectAudioObject();
220 bool linked
= isLinked();
221 audioRemoved(linked
&& stopLinkedVideo
);
224 delete m_audio
.object
;
226 else if (!stopLinkedVideo
)
228 // If we're not deleting object and it remains in use, unlink audio
229 NowPlayingDataAudio::Outputs::iterator it
;
230 for (it
= m_audio
.outputs
.begin(); it
!= m_audio
.outputs
.end(); ++it
)
232 assert((*it
).isValid());
234 disconnectAudioOutput(it
.key());
238 m_audio
.prefsSequence
->setManager(0);
239 m_audio
.prefsSequence
= 0;
240 m_audio
.prefs
->setManager(0);
242 if (linked
&& stopLinkedVideo
)
249 /// Stop any playing video.
250 void KwMediaManager::stopVideo(bool stopLinkedAudio
)
254 disconnectVideoObject();
255 bool linked
= isLinked();
256 videoRemoved(linked
&& stopLinkedAudio
);
259 delete m_video
.object
;
261 else if (!stopLinkedAudio
)
263 // If we're not deleting object and it remains in use, unlink audio
264 NowPlayingDataVideo::Outputs::iterator it
;
265 for (it
= m_video
.outputs
.begin(); it
!= m_video
.outputs
.end(); ++it
)
267 assert((*it
).isValid());
268 bool disconnected
= (*it
).disconnect();
269 Q_UNUSED(disconnected
);
270 assert(disconnected
);
271 disconnectVideoOutput(it
.key());
275 m_video
.prefsSequence
->setManager(0);
276 m_video
.prefsSequence
= 0;
277 m_video
.prefs
->setManager(0);
279 if (linked
&& stopLinkedAudio
)
290 /// Get the audio object.
291 KwMediaObject
* KwMediaManager::getAudioObject()
293 return m_audio
.object
;
296 /// Get the video object.
297 KwMediaObject
* KwMediaManager::getVideoObject()
299 return m_video
.object
;
303 bool KwMediaManager::isLinked() const
305 return m_audio
.object
!= 0 && m_audio
.object
== m_video
.object
;
308 /// Is audio playing?
309 bool KwMediaManager::isAudio() const
311 return m_audio
.object
!= 0;
314 /// Is video playing?
315 bool KwMediaManager::isVideo() const
317 return m_video
.object
!= 0;
321 * Media control slots
324 /// Play/pause audio.
325 void KwMediaManager::audioPlayPause()
329 switch (m_audio
.object
->state())
331 case Phonon::PlayingState
:
332 case Phonon::BufferingState
:
333 m_audio
.object
->pause();
336 case Phonon::StoppedState
:
337 case Phonon::PausedState
:
338 m_audio
.object
->play();
347 /// Play/pause video.
348 void KwMediaManager::videoPlayPause()
352 switch (m_video
.object
->state())
354 case Phonon::PlayingState
:
355 case Phonon::BufferingState
:
356 m_video
.object
->pause();
359 case Phonon::StoppedState
:
360 case Phonon::PausedState
:
361 m_video
.object
->play();
371 void KwMediaManager::audioStop()
375 m_audio
.object
->stop();
380 void KwMediaManager::videoStop()
384 m_video
.object
->stop();
392 /// The audio object's metadata has changed.
393 void KwMediaManager::audioMetaDataHasChanged()
397 QStringList title
= m_audio
.object
->metaData("TITLE");
400 audioMediaNameChanged(title
.first());
404 audioMediaNameChanged("");
408 /// The video object's metadata has changed.
409 void KwMediaManager::videoMetaDataHasChanged()
413 QStringList title
= m_video
.object
->metaData("TITLE");
416 videoMediaNameChanged(title
.first());
420 videoMediaNameChanged("");
428 /// Make the internal connections for a new media object.
429 void KwMediaManager::connectAudioObject()
432 assert(m_audio
.object
!= 0);
433 connect(m_audio
.object
, SIGNAL(stateChanged(Phonon::State
,Phonon::State
)), this, SIGNAL(audioStateChanged(Phonon::State
,Phonon::State
)));
434 connect(m_audio
.object
, SIGNAL(metaDataChanged()), this, SLOT(audioMetaDataHasChanged()));
436 assert(m_audio
.prefs
!= 0);
437 connect(this, SIGNAL(setVolume(qreal
)), m_audio
.prefs
, SLOT(setVolume(qreal
)));
438 connect(this, SIGNAL(setMuted(bool)), m_audio
.prefs
, SLOT(setMuted(bool)));
439 connect(this, SIGNAL(setFadeoutMsec(qint32
)), m_audio
.prefs
, SLOT(setFadeoutMsec(qint32
)));
440 connect(m_audio
.prefs
, SIGNAL(volumeChanged(qreal
)), this, SIGNAL(volumeChanged(qreal
)));
441 connect(m_audio
.prefs
, SIGNAL(mutedChanged(bool)), this, SIGNAL(mutedChanged(bool)));
443 volumeChanged(m_audio
.prefs
->getVolume());
444 mutedChanged(m_audio
.prefs
->getMuted());
447 /// Remove the internal connections for an old media object.
448 void KwMediaManager::disconnectAudioObject()
451 assert(m_audio
.object
!= 0);
452 disconnect(m_audio
.object
, SIGNAL(stateChanged(Phonon::State
,Phonon::State
)), this, SIGNAL(audioStateChanged(Phonon::State
,Phonon::State
)));
453 disconnect(m_audio
.object
, SIGNAL(metaDataChanged()), this, SLOT(audioMetaDataHasChanged()));
455 assert(m_audio
.prefs
!= 0);
456 disconnect(this, SIGNAL(setVolume(qreal
)), m_audio
.prefs
, SLOT(setVolume(qreal
)));
457 disconnect(this, SIGNAL(setMuted(bool)), m_audio
.prefs
, SLOT(setMuted(bool)));
458 disconnect(this, SIGNAL(setFadeoutMsec(qint32
)), m_audio
.prefs
, SLOT(setFadeoutMsec(qint32
)));
459 disconnect(m_audio
.prefs
, SIGNAL(volumeChanged(qreal
)), this, SIGNAL(volumeChanged(qreal
)));
460 disconnect(m_audio
.prefs
, SIGNAL(mutedChanged(bool)), this, SIGNAL(mutedChanged(bool)));
463 /// Make the internal connections for a new video object.
464 void KwMediaManager::connectVideoObject()
466 connect(m_video
.object
, SIGNAL(stateChanged(Phonon::State
,Phonon::State
)), this, SIGNAL(videoStateChanged(Phonon::State
,Phonon::State
)));
467 connect(m_video
.object
, SIGNAL(metaDataChanged()), this, SLOT(videoMetaDataHasChanged()));
470 /// Remove the internal connections for an old video object.
471 void KwMediaManager::disconnectVideoObject()
473 disconnect(m_video
.object
, SIGNAL(stateChanged(Phonon::State
,Phonon::State
)), this, SIGNAL(videoStateChanged(Phonon::State
,Phonon::State
)));
474 disconnect(m_video
.object
, SIGNAL(metaDataChanged()), this, SLOT(videoMetaDataHasChanged()));
477 /// Make the internal connections for a new audio ouptut.
478 void KwMediaManager::connectAudioOutput(KwAudioOutput
* output
)
480 assert(m_audio
.prefs
!= 0);
481 connect(m_audio
.prefs
, SIGNAL(volumeChanged(qreal
)), output
, SLOT(setVolume(qreal
)));
482 connect(m_audio
.prefs
, SIGNAL(mutedChanged(bool)), output
, SLOT(setMuted(bool)));
485 /// Remove the internal connections for an old audio output.
486 void KwMediaManager::disconnectAudioOutput(KwAudioOutput
* output
)
488 assert(m_audio
.prefs
!= 0);
489 disconnect(m_audio
.prefs
, SIGNAL(volumeChanged(qreal
)), output
, SLOT(setVolume(qreal
)));
490 disconnect(m_audio
.prefs
, SIGNAL(mutedChanged(bool)), output
, SLOT(setMuted(bool)));
493 /// Make the internal connections for a new video ouptut.
494 void KwMediaManager::connectVideoOutput(KwVideoWidget
* output
)
500 /// Remove the internal connections for an old video output.
501 void KwMediaManager::disconnectVideoOutput(KwVideoWidget
* output
)