| 1 | /* This file is part of the KDE project |
| 2 | Copyright (C) 2004-2007 Matthias Kretz <kretz@kde.org> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) version 3, or any |
| 8 | later version accepted by the membership of KDE e.V. (or its |
| 9 | successor approved by the membership of KDE e.V.), Nokia Corporation |
| 10 | (or its successors, if any) and the KDE Free Qt Foundation, which shall |
| 11 | act as a proxy defined in Section 6 of version 3 of the license. |
| 12 | |
| 13 | This library is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | */ |
| 22 | |
| 23 | #include "videoplayer.h" |
| 24 | #include "mediaobject.h" |
| 25 | #include "audiooutput.h" |
| 26 | #include "videowidget.h" |
| 27 | #include "path.h" |
| 28 | #include <QBoxLayout> |
| 29 | #include <QEvent> |
| 30 | |
| 31 | #ifndef QT_NO_PHONON_VIDEOPLAYER |
| 32 | |
| 33 | namespace Phonon |
| 34 | { |
| 35 | |
| 36 | class VideoPlayerPrivate |
| 37 | { |
| 38 | public: |
| 39 | VideoPlayerPrivate() |
| 40 | : player(nullptr) |
| 41 | , aoutput(nullptr) |
| 42 | , voutput(nullptr) |
| 43 | , category(Phonon::NoCategory) |
| 44 | , initialized(false) {} |
| 45 | |
| 46 | void ensureCreated() const; |
| 47 | |
| 48 | mutable MediaObject *player; |
| 49 | mutable AudioOutput *aoutput; |
| 50 | mutable VideoWidget *voutput; |
| 51 | |
| 52 | mutable MediaSource src; |
| 53 | mutable Phonon::Category category; |
| 54 | mutable bool initialized; |
| 55 | VideoPlayer *q_ptr; |
| 56 | }; |
| 57 | |
| 58 | void VideoPlayerPrivate::ensureCreated() const |
| 59 | { |
| 60 | if (!initialized) { |
| 61 | initialized = true; |
| 62 | QVBoxLayout *layout = new QVBoxLayout(q_ptr); |
| 63 | layout->setContentsMargins(QMargins()); |
| 64 | |
| 65 | aoutput = new AudioOutput(category, q_ptr); |
| 66 | voutput = new VideoWidget(q_ptr); |
| 67 | layout->addWidget(voutput); |
| 68 | |
| 69 | player = new MediaObject(q_ptr); |
| 70 | Phonon::createPath(source: player, sink: aoutput); |
| 71 | Phonon::createPath(source: player, sink: voutput); |
| 72 | |
| 73 | q_ptr->connect(asender: player, SIGNAL(finished()), SIGNAL(finished())); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | VideoPlayer::VideoPlayer(Phonon::Category category, QWidget *parent) |
| 78 | : QWidget(parent) |
| 79 | , d(new VideoPlayerPrivate) |
| 80 | { |
| 81 | d->q_ptr = this; |
| 82 | d->category = category; |
| 83 | } |
| 84 | |
| 85 | VideoPlayer::VideoPlayer(QWidget *parent) |
| 86 | : QWidget(parent) |
| 87 | , d(new VideoPlayerPrivate) |
| 88 | { |
| 89 | d->q_ptr = this; |
| 90 | d->category = Phonon::VideoCategory; |
| 91 | } |
| 92 | |
| 93 | VideoPlayer::~VideoPlayer() |
| 94 | { |
| 95 | delete d; |
| 96 | } |
| 97 | |
| 98 | MediaObject *VideoPlayer::mediaObject() const |
| 99 | { |
| 100 | d->ensureCreated(); |
| 101 | return d->player; |
| 102 | } |
| 103 | |
| 104 | AudioOutput *VideoPlayer::audioOutput() const |
| 105 | { |
| 106 | d->ensureCreated(); |
| 107 | return d->aoutput; |
| 108 | } |
| 109 | |
| 110 | VideoWidget *VideoPlayer::videoWidget() const |
| 111 | { |
| 112 | d->ensureCreated(); |
| 113 | return d->voutput; |
| 114 | } |
| 115 | |
| 116 | void VideoPlayer::load(const MediaSource &source) |
| 117 | { |
| 118 | d->ensureCreated(); |
| 119 | d->player->setCurrentSource(source); |
| 120 | } |
| 121 | |
| 122 | void VideoPlayer::play(const MediaSource &source) |
| 123 | { |
| 124 | d->ensureCreated(); |
| 125 | if (source == d->player->currentSource()) { |
| 126 | if (!isPlaying()) |
| 127 | d->player->play(); |
| 128 | return; |
| 129 | } |
| 130 | // new URL |
| 131 | d->player->setCurrentSource(source); |
| 132 | |
| 133 | if (ErrorState == d->player->state()) |
| 134 | return; |
| 135 | |
| 136 | d->player->play(); |
| 137 | } |
| 138 | |
| 139 | void VideoPlayer::play() |
| 140 | { |
| 141 | d->ensureCreated(); |
| 142 | d->player->play(); |
| 143 | } |
| 144 | |
| 145 | void VideoPlayer::pause() |
| 146 | { |
| 147 | d->ensureCreated(); |
| 148 | d->player->pause(); |
| 149 | } |
| 150 | |
| 151 | void VideoPlayer::stop() |
| 152 | { |
| 153 | d->ensureCreated(); |
| 154 | d->player->stop(); |
| 155 | } |
| 156 | |
| 157 | qint64 VideoPlayer::totalTime() const |
| 158 | { |
| 159 | d->ensureCreated(); |
| 160 | return d->player->totalTime(); |
| 161 | } |
| 162 | |
| 163 | qint64 VideoPlayer::currentTime() const |
| 164 | { |
| 165 | d->ensureCreated(); |
| 166 | return d->player->currentTime(); |
| 167 | } |
| 168 | |
| 169 | void VideoPlayer::seek(qint64 ms) |
| 170 | { |
| 171 | d->ensureCreated(); |
| 172 | d->player->seek(time: ms); |
| 173 | } |
| 174 | |
| 175 | float VideoPlayer::volume() const |
| 176 | { |
| 177 | d->ensureCreated(); |
| 178 | return d->aoutput->volume(); |
| 179 | } |
| 180 | |
| 181 | void VideoPlayer::setVolume(float v) |
| 182 | { |
| 183 | d->ensureCreated(); |
| 184 | d->aoutput->setVolume(v); |
| 185 | } |
| 186 | |
| 187 | bool VideoPlayer::isPlaying() const |
| 188 | { |
| 189 | d->ensureCreated(); |
| 190 | return (d->player->state() == PlayingState); |
| 191 | } |
| 192 | |
| 193 | bool VideoPlayer::isPaused() const |
| 194 | { |
| 195 | d->ensureCreated(); |
| 196 | return (d->player->state() == PausedState); |
| 197 | } |
| 198 | |
| 199 | bool VideoPlayer::event(QEvent *e) { |
| 200 | if (e->type() == QEvent::Show) |
| 201 | d->ensureCreated(); |
| 202 | return QWidget::event(event: e); |
| 203 | } |
| 204 | |
| 205 | } // namespaces |
| 206 | |
| 207 | #endif //QT_NO_PHONON_VIDEOPLAYER |
| 208 | |
| 209 | #include "moc_videoplayer.cpp" |
| 210 | |
| 211 | // vim: sw=4 ts=4 |
| 212 | |