File indexing completed on 2024-05-05 04:44:39

0001 /*  This file is part of the KDE project
0002     Copyright (C) 2011 Casian Andrei <skeletk13@gmail.com>
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 #include "capture.h"
0023 
0024 #include <QVBoxLayout>
0025 #include <QHBoxLayout>
0026 #include <QPushButton>
0027 #include <QMessageBox>
0028 
0029 #include <phonon/AudioOutput>
0030 #include <phonon/MediaObject>
0031 #include <phonon/VideoWidget>
0032 
0033 CaptureWidget::CaptureWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f)
0034 {
0035     // Create the objects used for capture
0036     m_media = new Phonon::MediaObject(this);
0037 
0038     // Create the audio and video outputs (sinks)
0039     m_audioOutput = new Phonon::AudioOutput(this);
0040     m_videoWidget = new Phonon::VideoWidget(this);
0041 
0042     /*
0043      * Set up the buttons and layouts and widgets
0044      */
0045     m_playButton = new QPushButton(this);
0046     m_playButton->setText(tr("Play"));
0047     connect(m_playButton, SIGNAL(clicked()), this, SLOT(playPause()));
0048 
0049     m_stopButton = new QPushButton(this);
0050     m_stopButton->setText(tr("Stop"));
0051     m_stopButton->setEnabled(false);
0052     connect(m_stopButton, SIGNAL(clicked()), m_media, SLOT(stop()));
0053 
0054     setLayout(new QVBoxLayout);
0055 
0056     // Configure the video widget a bit
0057     m_videoWidget->setMinimumSize(QSize(400, 300));
0058     m_videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0059     layout()->addWidget(m_videoWidget);
0060 
0061     QHBoxLayout *buttonsLayout = new QHBoxLayout();
0062     buttonsLayout->addWidget(m_playButton);
0063     buttonsLayout->addWidget(m_stopButton);
0064     layout()->addItem(buttonsLayout);
0065 
0066     /*
0067      * Create the paths from the capture object to the outputs
0068      * If the paths are invalid, then probably the backend doesn't support capture
0069      */
0070     Phonon::Path audioPath = Phonon::createPath(m_media, m_audioOutput);
0071     Phonon::Path videoPath = Phonon::createPath(m_media, m_videoWidget);
0072 
0073     if (!audioPath.isValid()) {
0074         QMessageBox::critical(this, "Error", "Your backend may not support audio capturing.");
0075     }
0076     if (!videoPath.isValid()) {
0077         QMessageBox::critical(this, "Error", "Your backend may not support video capturing.");
0078     }
0079 
0080     /*
0081      * Set up the devices used for capture
0082      * Phonon can easily get you the devices appropriate for a specific category.
0083      */
0084     Phonon::MediaSource source(Phonon::Capture::VideoType, Phonon::NoCaptureCategory);
0085     m_media->setCurrentSource(source);
0086 
0087     // Connect the stateChanged signal from the media object used for capture
0088     connect(m_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
0089             this, SLOT(mediaStateChanged(Phonon::State)));
0090 
0091     // Start capturing
0092     playPause();
0093 }
0094 
0095 void CaptureWidget::playPause()
0096 {
0097     if (m_media->state() == Phonon::PlayingState) {
0098         m_media->pause();
0099     } else {
0100         m_media->play();
0101     }
0102 }
0103 
0104 void CaptureWidget::mediaStateChanged(Phonon::State newState)
0105 {
0106     switch(newState) {
0107     case Phonon::LoadingState:
0108         break;
0109     case Phonon::StoppedState:
0110         m_playButton->setText(tr("Play"));
0111         m_stopButton->setEnabled(false);
0112         break;
0113     case Phonon::PlayingState:
0114         m_playButton->setText(tr("Pause"));
0115         m_stopButton->setEnabled(true);
0116         break;
0117     case Phonon::BufferingState:
0118         break;
0119     case Phonon::PausedState:
0120         m_playButton->setText(tr("Play"));
0121         break;
0122     case Phonon::ErrorState:
0123         break;
0124     }
0125 }