File indexing completed on 2025-02-09 05:31:54
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004-2007 Matthias Kretz <kretz@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Lesser General Public 0006 License as published by the Free Software Foundation; either 0007 version 2.1 of the License, or (at your option) version 3, or any 0008 later version accepted by the membership of KDE e.V. (or its 0009 successor approved by the membership of KDE e.V.), Nokia Corporation 0010 (or its successors, if any) and the KDE Free Qt Foundation, which shall 0011 act as a proxy defined in Section 6 of version 3 of the license. 0012 0013 This library is distributed in the hope that it will be useful, 0014 but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0016 Lesser General Public License for more details. 0017 0018 You should have received a copy of the GNU Lesser General Public 0019 License along with this library. If not, see <http://www.gnu.org/licenses/>. 0020 0021 */ 0022 0023 #include "videoplayer.h" 0024 #include "mediaobject.h" 0025 #include "audiooutput.h" 0026 #include "videowidget.h" 0027 #include "path.h" 0028 #include <QBoxLayout> 0029 #include <QEvent> 0030 0031 #ifndef QT_NO_PHONON_VIDEOPLAYER 0032 0033 namespace Phonon 0034 { 0035 0036 class VideoPlayerPrivate 0037 { 0038 public: 0039 VideoPlayerPrivate() 0040 : player(nullptr) 0041 , aoutput(nullptr) 0042 , voutput(nullptr) 0043 , category(Phonon::NoCategory) 0044 , initialized(false) {} 0045 0046 void ensureCreated() const; 0047 0048 mutable MediaObject *player; 0049 mutable AudioOutput *aoutput; 0050 mutable VideoWidget *voutput; 0051 0052 mutable MediaSource src; 0053 mutable Phonon::Category category; 0054 mutable bool initialized; 0055 VideoPlayer *q_ptr; 0056 }; 0057 0058 void VideoPlayerPrivate::ensureCreated() const 0059 { 0060 if (!initialized) { 0061 initialized = true; 0062 QVBoxLayout *layout = new QVBoxLayout(q_ptr); 0063 layout->setContentsMargins(QMargins()); 0064 0065 aoutput = new AudioOutput(category, q_ptr); 0066 voutput = new VideoWidget(q_ptr); 0067 layout->addWidget(voutput); 0068 0069 player = new MediaObject(q_ptr); 0070 Phonon::createPath(player, aoutput); 0071 Phonon::createPath(player, voutput); 0072 0073 q_ptr->connect(player, SIGNAL(finished()), SIGNAL(finished())); 0074 } 0075 } 0076 0077 VideoPlayer::VideoPlayer(Phonon::Category category, QWidget *parent) 0078 : QWidget(parent) 0079 , d(new VideoPlayerPrivate) 0080 { 0081 d->q_ptr = this; 0082 d->category = category; 0083 } 0084 0085 VideoPlayer::VideoPlayer(QWidget *parent) 0086 : QWidget(parent) 0087 , d(new VideoPlayerPrivate) 0088 { 0089 d->q_ptr = this; 0090 d->category = Phonon::VideoCategory; 0091 } 0092 0093 VideoPlayer::~VideoPlayer() 0094 { 0095 delete d; 0096 } 0097 0098 MediaObject *VideoPlayer::mediaObject() const 0099 { 0100 d->ensureCreated(); 0101 return d->player; 0102 } 0103 0104 AudioOutput *VideoPlayer::audioOutput() const 0105 { 0106 d->ensureCreated(); 0107 return d->aoutput; 0108 } 0109 0110 VideoWidget *VideoPlayer::videoWidget() const 0111 { 0112 d->ensureCreated(); 0113 return d->voutput; 0114 } 0115 0116 void VideoPlayer::load(const MediaSource &source) 0117 { 0118 d->ensureCreated(); 0119 d->player->setCurrentSource(source); 0120 } 0121 0122 void VideoPlayer::play(const MediaSource &source) 0123 { 0124 d->ensureCreated(); 0125 if (source == d->player->currentSource()) { 0126 if (!isPlaying()) 0127 d->player->play(); 0128 return; 0129 } 0130 // new URL 0131 d->player->setCurrentSource(source); 0132 0133 if (ErrorState == d->player->state()) 0134 return; 0135 0136 d->player->play(); 0137 } 0138 0139 void VideoPlayer::play() 0140 { 0141 d->ensureCreated(); 0142 d->player->play(); 0143 } 0144 0145 void VideoPlayer::pause() 0146 { 0147 d->ensureCreated(); 0148 d->player->pause(); 0149 } 0150 0151 void VideoPlayer::stop() 0152 { 0153 d->ensureCreated(); 0154 d->player->stop(); 0155 } 0156 0157 qint64 VideoPlayer::totalTime() const 0158 { 0159 d->ensureCreated(); 0160 return d->player->totalTime(); 0161 } 0162 0163 qint64 VideoPlayer::currentTime() const 0164 { 0165 d->ensureCreated(); 0166 return d->player->currentTime(); 0167 } 0168 0169 void VideoPlayer::seek(qint64 ms) 0170 { 0171 d->ensureCreated(); 0172 d->player->seek(ms); 0173 } 0174 0175 float VideoPlayer::volume() const 0176 { 0177 d->ensureCreated(); 0178 return d->aoutput->volume(); 0179 } 0180 0181 void VideoPlayer::setVolume(float v) 0182 { 0183 d->ensureCreated(); 0184 d->aoutput->setVolume(v); 0185 } 0186 0187 bool VideoPlayer::isPlaying() const 0188 { 0189 d->ensureCreated(); 0190 return (d->player->state() == PlayingState); 0191 } 0192 0193 bool VideoPlayer::isPaused() const 0194 { 0195 d->ensureCreated(); 0196 return (d->player->state() == PausedState); 0197 } 0198 0199 bool VideoPlayer::event(QEvent *e) { 0200 if (e->type() == QEvent::Show) 0201 d->ensureCreated(); 0202 return QWidget::event(e); 0203 } 0204 0205 } // namespaces 0206 0207 #endif //QT_NO_PHONON_VIDEOPLAYER 0208 0209 #include "moc_videoplayer.cpp" 0210 0211 // vim: sw=4 ts=4