1 /*  This file is part of the KDE project
2     Copyright (C) 2011 Casian Andrei <skeletk13@gmail.com>
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 #include "capture.h"
23 
24 #include <QVBoxLayout>
25 #include <QHBoxLayout>
26 #include <QPushButton>
27 #include <QMessageBox>
28 
29 #include <phonon/AudioOutput>
30 #include <phonon/MediaObject>
31 #include <phonon/VideoWidget>
32 
CaptureWidget(QWidget * parent,Qt::WindowFlags f)33 CaptureWidget::CaptureWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f)
34 {
35     // Create the objects used for capture
36     m_media = new Phonon::MediaObject(this);
37 
38     // Create the audio and video outputs (sinks)
39     m_audioOutput = new Phonon::AudioOutput(this);
40     m_videoWidget = new Phonon::VideoWidget(this);
41 
42     /*
43      * Set up the buttons and layouts and widgets
44      */
45     m_playButton = new QPushButton(this);
46     m_playButton->setText(tr("Play"));
47     connect(m_playButton, SIGNAL(clicked()), this, SLOT(playPause()));
48 
49     m_stopButton = new QPushButton(this);
50     m_stopButton->setText(tr("Stop"));
51     m_stopButton->setEnabled(false);
52     connect(m_stopButton, SIGNAL(clicked()), m_media, SLOT(stop()));
53 
54     setLayout(new QVBoxLayout);
55 
56     // Configure the video widget a bit
57     m_videoWidget->setMinimumSize(QSize(400, 300));
58     m_videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
59     layout()->addWidget(m_videoWidget);
60 
61     QHBoxLayout *buttonsLayout = new QHBoxLayout();
62     buttonsLayout->addWidget(m_playButton);
63     buttonsLayout->addWidget(m_stopButton);
64     layout()->addItem(buttonsLayout);
65 
66     /*
67      * Create the paths from the capture object to the outputs
68      * If the paths are invalid, then probably the backend doesn't support capture
69      */
70     Phonon::Path audioPath = Phonon::createPath(m_media, m_audioOutput);
71     Phonon::Path videoPath = Phonon::createPath(m_media, m_videoWidget);
72 
73     if (!audioPath.isValid()) {
74         QMessageBox::critical(this, "Error", "Your backend may not support audio capturing.");
75     }
76     if (!videoPath.isValid()) {
77         QMessageBox::critical(this, "Error", "Your backend may not support video capturing.");
78     }
79 
80     /*
81      * Set up the devices used for capture
82      * Phonon can easily get you the devices appropriate for a specific category.
83      */
84     Phonon::MediaSource source(Phonon::Capture::VideoType, Phonon::NoCaptureCategory);
85     m_media->setCurrentSource(source);
86 
87     // Connect the stateChanged signal from the media object used for capture
88     connect(m_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
89             this, SLOT(mediaStateChanged(Phonon::State)));
90 
91     // Start capturing
92     playPause();
93 }
94 
playPause()95 void CaptureWidget::playPause()
96 {
97     if (m_media->state() == Phonon::PlayingState) {
98         m_media->pause();
99     } else {
100         m_media->play();
101     }
102 }
103 
mediaStateChanged(Phonon::State newState)104 void CaptureWidget::mediaStateChanged(Phonon::State newState)
105 {
106     switch(newState) {
107     case Phonon::LoadingState:
108         break;
109     case Phonon::StoppedState:
110         m_playButton->setText(tr("Play"));
111         m_stopButton->setEnabled(false);
112         break;
113     case Phonon::PlayingState:
114         m_playButton->setText(tr("Pause"));
115         m_stopButton->setEnabled(true);
116         break;
117     case Phonon::BufferingState:
118         break;
119     case Phonon::PausedState:
120         m_playButton->setText(tr("Play"));
121         break;
122     case Phonon::ErrorState:
123         break;
124     }
125 }
126