Okular

audioplayer.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Pino Toscano <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "audioplayer.h"
8 #include "audioplayer_p.h"
9 
10 // qt/kde includes
11 #include <KRandom>
12 #include <QBuffer>
13 #include <QDebug>
14 #include <QDir>
15 #include <phonon/abstractmediastream.h>
16 #include <phonon/audiooutput.h>
17 #include <phonon/mediaobject.h>
18 #include <phonon/path.h>
19 
20 // local includes
21 #include "action.h"
22 #include "debug_p.h"
23 #include "sound.h"
24 
25 using namespace Okular;
26 
27 // helper class used to store info about a sound to be played
28 class SoundInfo
29 {
30 public:
31  explicit SoundInfo(const Sound *s = nullptr, const SoundAction *ls = nullptr)
32  : sound(s)
33  , volume(0.5)
34  , synchronous(false)
35  , repeat(false)
36  , mix(false)
37  {
38  if (ls) {
39  volume = ls->volume();
40  synchronous = ls->synchronous();
41  repeat = ls->repeat();
42  mix = ls->mix();
43  }
44  }
45 
46  const Sound *sound;
47  double volume;
48  bool synchronous;
49  bool repeat;
50  bool mix;
51 };
52 
53 class PlayData
54 {
55 public:
56  PlayData()
57  : m_mediaobject(nullptr)
58  , m_output(nullptr)
59  , m_buffer(nullptr)
60  {
61  }
62 
63  void play()
64  {
65  if (m_buffer) {
66  m_buffer->open(QIODevice::ReadOnly);
67  }
68  m_mediaobject->play();
69  }
70 
71  ~PlayData()
72  {
73  m_mediaobject->stop();
74  delete m_mediaobject;
75  delete m_output;
76  delete m_buffer;
77  }
78 
79  PlayData(const PlayData &) = delete;
80  PlayData &operator=(const PlayData &) = delete;
81 
82  Phonon::MediaObject *m_mediaobject;
83  Phonon::AudioOutput *m_output;
84  QBuffer *m_buffer;
85  SoundInfo m_info;
86 };
87 
88 AudioPlayerPrivate::AudioPlayerPrivate(AudioPlayer *qq)
89  : q(qq)
90  , m_state(AudioPlayer::StoppedState)
91 {
92 }
93 
94 AudioPlayerPrivate::~AudioPlayerPrivate()
95 {
96  stopPlayings();
97 }
98 
99 int AudioPlayerPrivate::newId() const
100 {
101  int newid = 0;
103  QHash<int, PlayData *>::const_iterator itEnd = m_playing.constEnd();
104  do {
105  newid = KRandom::random();
106  it = m_playing.constFind(newid);
107  } while (it != itEnd);
108  return newid;
109 }
110 
111 bool AudioPlayerPrivate::play(const SoundInfo &si)
112 {
113  qCDebug(OkularCoreDebug);
114  PlayData *data = new PlayData();
115  data->m_output = new Phonon::AudioOutput(Phonon::NotificationCategory);
116  data->m_output->setVolume(si.volume);
117  data->m_mediaobject = new Phonon::MediaObject();
118  Phonon::createPath(data->m_mediaobject, data->m_output);
119  data->m_info = si;
120  bool valid = false;
121 
122  switch (si.sound->soundType()) {
123  case Sound::External: {
124  QString url = si.sound->url();
125  qCDebug(OkularCoreDebug) << "External," << url;
126  if (!url.isEmpty()) {
127  int newid = newId();
128  QObject::connect(data->m_mediaobject, &Phonon::MediaObject::finished, q, [this, newid]() { finished(newid); });
129  QUrl newurl;
130  if (QUrl::fromUserInput(url).isRelative()) {
131  newurl = m_currentDocument.adjusted(QUrl::RemoveFilename);
132  newurl.setPath(newurl.path() + url);
133  } else {
134  newurl = QUrl::fromLocalFile(url);
135  }
136  data->m_mediaobject->setCurrentSource(newurl);
137  m_playing.insert(newid, data);
138  valid = true;
139  }
140  break;
141  }
142  case Sound::Embedded: {
143  QByteArray filedata = si.sound->data();
144  qCDebug(OkularCoreDebug) << "Embedded," << filedata.length();
145  if (!filedata.isEmpty()) {
146  qCDebug(OkularCoreDebug) << "Mediaobject:" << data->m_mediaobject;
147  int newid = newId();
148  QObject::connect(data->m_mediaobject, &Phonon::MediaObject::finished, q, [this, newid]() { finished(newid); });
149  data->m_buffer = new QBuffer();
150  data->m_buffer->setData(filedata);
151  data->m_mediaobject->setCurrentSource(Phonon::MediaSource(data->m_buffer));
152  m_playing.insert(newid, data);
153  valid = true;
154  }
155  break;
156  }
157  }
158  if (!valid) {
159  delete data;
160  data = nullptr;
161  }
162  if (data) {
163  qCDebug(OkularCoreDebug) << "PLAY";
164  data->play();
165  m_state = AudioPlayer::PlayingState;
166  }
167  return valid;
168 }
169 
170 void AudioPlayerPrivate::stopPlayings()
171 {
172  qDeleteAll(m_playing);
173  m_playing.clear();
174  m_state = AudioPlayer::StoppedState;
175 }
176 
177 void AudioPlayerPrivate::finished(int id)
178 {
179  QHash<int, PlayData *>::iterator it = m_playing.find(id);
180  if (it == m_playing.end())
181  return;
182 
183  SoundInfo si = it.value()->m_info;
184  // if the sound must be repeated indefinitely, then start the playback
185  // again, otherwise destroy the PlayData as it's no more useful
186  if (si.repeat) {
187  it.value()->play();
188  } else {
189  delete it.value();
190  m_playing.erase(it);
191  m_state = AudioPlayer::StoppedState;
192  }
193  qCDebug(OkularCoreDebug) << "finished," << m_playing.count();
194 }
195 
196 AudioPlayer::AudioPlayer()
197  : QObject()
198  , d(new AudioPlayerPrivate(this))
199 {
200 }
201 
202 AudioPlayer::~AudioPlayer()
203 {
204  delete d;
205 }
206 
208 {
209  static AudioPlayer ap;
210  return &ap;
211 }
212 
213 void AudioPlayer::playSound(const Sound *sound, const SoundAction *linksound)
214 {
215  // we can't play null pointers ;)
216  if (!sound)
217  return;
218 
219  // we don't play external sounds for remote documents
220  if (sound->soundType() == Sound::External && !d->m_currentDocument.isLocalFile())
221  return;
222 
223  qCDebug(OkularCoreDebug);
224  SoundInfo si(sound, linksound);
225 
226  // if the mix flag of the new sound is false, then the currently playing
227  // sounds must be stopped.
228  if (!si.mix)
229  d->stopPlayings();
230 
231  d->play(si);
232 }
233 
235 {
236  d->stopPlayings();
237 }
238 
240 {
241  return d->m_state;
242 }
243 
244 #include "moc_audioplayer.cpp"
RemoveFilename
static AudioPlayer * instance()
Gets the instance of the audio player.
State state() const
Return state of sound (playing/stopped)
qreal volume()
State
The state of AudioPlayer.
Definition: audioplayer.h:36
The AudioPlayer isn&#39;t playing a audio file.
Definition: audioplayer.h:44
bool isEmpty() const const
QAction * repeat(const QObject *recvr, const char *slot, QObject *parent)
KGUIADDONS_EXPORT QColor mix(const QColor &c1, const QColor &c2, qreal bias=0.5)
int length() const const
global.h
Definition: action.h:16
QUrl fromUserInput(const QString &userInput)
KCOREADDONS_EXPORT int random()
void setPath(const QString &path, QUrl::ParsingMode mode)
Is stored embedded in the document.
Definition: sound.h:31
bool isEmpty() const const
An audio player.
Definition: audioplayer.h:27
QString path(QUrl::ComponentFormattingOptions options) const const
Contains information about a sound object.
Definition: sound.h:23
Is stored at external resource (e.g. url)
Definition: sound.h:30
The AudioPlayer is playing a audio file.
Definition: audioplayer.h:40
SoundType soundType() const
Returns the type of the sound object.
Definition: sound.cpp:61
QUrl adjusted(QUrl::FormattingOptions options) const const
void stopPlaybacks()
Tell the AudioPlayer to stop all the playbacks.
void playSound(const Sound *sound, const SoundAction *linksound=nullptr)
Enqueue the specified sound for playing, optionally taking more information about the playing from th...
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The Sound action plays a sound on activation.
Definition: action.h:335
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Nov 29 2021 22:32:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.