~ubuntu-branches/ubuntu/quantal/kx11grab/quantal

« back to all changes in this revision

Viewing changes to src/audio/pulseaudiodialog.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Thomas
  • Date: 2012-05-28 00:05:08 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120528000508-fhxswnrq2zoo983r
Tags: 0.4.4~rc3-0ubuntu1
* New upstream release candidate:
  - Refresh kubuntu_01_fix_linker.diff
  - Add new build-depends: pkg-config, libasound2-dev, libavformat-dev,
    libavcodec-dev, libavutil-dev, libavfilter-dev, libfreetype6-dev,
    libfontconfig1-dev, libpulse-dev, libxrandr-dev, libv4l-dev
  - Build with -DENABLE_KDE_SUPPORT to retain KDE integration.
* Bump debhelper version to 9
* Bump Standards-Version to 3.9.3, no changes necessary

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* This file is part of the qx11grab project
 
3
*
 
4
* Copyright (C) Juergen Heinemann (Undefined) http://qx11grab.hjcms.de, (C) 2007-2012
 
5
*
 
6
* This library is free software; you can redistribute it and/or
 
7
* modify it under the terms of the GNU Library General Public
 
8
* License as published by the Free Software Foundation; either
 
9
* version 2 of the License, or (at your option) any later version.
 
10
*
 
11
* This library is distributed in the hope that it will be useful,
 
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
* Library General Public License for more details.
 
15
*
 
16
* You should have received a copy of the GNU Library General Public License
 
17
* along with this library; see the file COPYING.LIB.  If not, write to
 
18
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
* Boston, MA 02110-1301, USA.
 
20
*/
 
21
 
 
22
#include "pulseaudiodialog.h"
 
23
 
 
24
/* QtCore */
 
25
#include <QtCore/QCoreApplication>
 
26
#include <QtCore/QByteArray>
 
27
#include <QtCore/QDebug>
 
28
#include <QtCore/QGlobalStatic>
 
29
#include <QtCore/QList>
 
30
#include <QtCore/QVariant>
 
31
 
 
32
/* QtDBus */
 
33
#include <QtDBus/QDBusConnection>
 
34
#include <QtDBus/QDBusInterface>
 
35
 
 
36
/* QtGui */
 
37
#include <QtGui/QListWidgetItem>
 
38
 
 
39
extern "C"
 
40
{
 
41
#include <pulse/context.h>
 
42
#include <pulse/mainloop.h>
 
43
#include <pulse/introspect.h>
 
44
#include <pulse/error.h>
 
45
}
 
46
 
 
47
static pa_mainloop_api* mainloop_api = NULL;
 
48
static pa_context* context = NULL;
 
49
 
 
50
/**
 
51
* send errors to mainWindow
 
52
*/
 
53
static void sendPulseErrorToMainWindow ( const char* error )
 
54
{
 
55
  QString message ( "Pulse:" );
 
56
  message.append ( QString::fromUtf8 ( error ) );
 
57
 
 
58
  QDBusInterface iface ( "de.hjcms.qx11grab", "/", "de.hjcms.qx11grab" );
 
59
  iface.call ( "message", message );
 
60
}
 
61
 
 
62
/**
 
63
* struct for simple list of Card Input Sources
 
64
* \note struct for pa_source_info
 
65
*/
 
66
typedef struct PaDevInfo
 
67
{
 
68
  quint32 index;
 
69
  QString name;
 
70
  QString description;
 
71
  bool isInputDevice;
 
72
} padevinfo_t;
 
73
 
 
74
/** Insert all input cards into pulseInfo */
 
75
static QList<PaDevInfo> pulseInfo;
 
76
 
 
77
/** Pulse "pa_context_get_source_info_list" callback function */
 
78
static void getPulseSourceInfo ( pa_context *co, const pa_source_info *i, int success, void * state )
 
79
{
 
80
  if ( success > 0 )
 
81
  {
 
82
    if ( pa_context_errno ( co ) == PA_ERR_NOENTITY )
 
83
      return;
 
84
 
 
85
    mainloop_api->quit ( mainloop_api, 0 );
 
86
    return;
 
87
  }
 
88
 
 
89
  if ( success < 0 )
 
90
    return;
 
91
 
 
92
  bool inp = false;
 
93
  if ( i->proplist )
 
94
  {
 
95
    const char* key = pa_proplist_iterate ( i->proplist, &state );
 
96
    while ( key != NULL )
 
97
    {
 
98
      if ( QString::fromUtf8 ( key ).contains ( "device.class", Qt::CaseInsensitive ) )
 
99
      {
 
100
        inp = !QString::fromUtf8 ( pa_proplist_gets ( i->proplist, key ) ).contains ( "monitor", Qt::CaseInsensitive );
 
101
        break;
 
102
      }
 
103
      key = pa_proplist_iterate ( i->proplist, &state );
 
104
    }
 
105
  }
 
106
 
 
107
  // qDebug() <<  "Pulse::getPulseSourceInfo >> " << i->index << i->name << i->description << inp;
 
108
  PaDevInfo devinfo;
 
109
  devinfo.index = i->index;
 
110
  devinfo.name = QString ( i->name );
 
111
  devinfo.description = QString ( i->description );
 
112
  devinfo.isInputDevice = inp;
 
113
  pulseInfo.append ( devinfo );
 
114
}
 
115
 
 
116
/** Pulse "pa_context_set_state_callback" callback function */
 
117
static void pulseContextState ( pa_context *c, void *userdata )
 
118
{
 
119
  padevinfo_t* dummy = static_cast<padevinfo_t*> ( userdata );
 
120
 
 
121
  Q_ASSERT ( c );
 
122
 
 
123
  switch ( pa_context_get_state ( c ) )
 
124
  {
 
125
    case PA_CONTEXT_CONNECTING:
 
126
    case PA_CONTEXT_AUTHORIZING:
 
127
    case PA_CONTEXT_SETTING_NAME:
 
128
      break;
 
129
 
 
130
    case PA_CONTEXT_READY:
 
131
    {
 
132
      qDebug ( "QX11Grab: connection established." );
 
133
      pa_operation *o;
 
134
      if ( ! ( o = pa_context_get_source_info_list ( c, getPulseSourceInfo, dummy ) ) )
 
135
      {
 
136
        const char* errorMessage = pa_strerror ( pa_context_errno ( c ) );
 
137
        fprintf ( stderr, "QX11Grab: pulse context get card info list failed! - %s\n", errorMessage );
 
138
        sendPulseErrorToMainWindow ( errorMessage );
 
139
        mainloop_api->quit ( mainloop_api, 0 );
 
140
        return;
 
141
      }
 
142
      pa_operation_unref ( o );
 
143
      break;
 
144
    }
 
145
 
 
146
    case PA_CONTEXT_TERMINATED:
 
147
      mainloop_api->quit ( mainloop_api, 0 );
 
148
      break;
 
149
 
 
150
    case PA_CONTEXT_FAILED:
 
151
    default:
 
152
    {
 
153
      const char* errorMessage = pa_strerror ( pa_context_errno ( c ) );
 
154
      fprintf ( stderr, "QX11Grab: context error - %s\n", errorMessage );
 
155
      sendPulseErrorToMainWindow ( errorMessage );
 
156
      mainloop_api->quit ( mainloop_api, 0 );
 
157
      break;
 
158
    }
 
159
  }
 
160
}
 
161
 
 
162
/** \class PulseAudioDialog */
 
163
PulseAudioDialog::PulseAudioDialog ( QWidget * parent )
 
164
    : AbstractAudioDialog ( parent )
 
165
{
 
166
  setObjectName ( QLatin1String ( "PulseAudioDialog" ) );
 
167
  /*: WindowTitle */
 
168
  setWindowTitle ( trUtf8 ( "Pulse Device Selection" ) );
 
169
  m_deviceListWidget->clear();
 
170
  pulseInfo.clear();
 
171
  initInterface();
 
172
}
 
173
 
 
174
/**
 
175
* Füge Inhalte in \b DeviceListWidget
 
176
*/
 
177
void PulseAudioDialog::insertItems()
 
178
{
 
179
  int size = pulseInfo.size();
 
180
  QIcon inputIcon = QIcon::fromTheme ( "audio-input-microphone" );
 
181
  QIcon monitorIcon = QIcon::fromTheme ( "audio-card" );
 
182
  if ( size > 0 )
 
183
  {
 
184
    for ( int i = 0; i < size; ++i )
 
185
    {
 
186
      QString index = QString::number ( pulseInfo.at ( i ).index );
 
187
      QString name = pulseInfo.at ( i ).name;
 
188
      QString desc = pulseInfo.at ( i ).description;
 
189
      QListWidgetItem* item = new QListWidgetItem ( m_deviceListWidget );
 
190
      item->setText ( desc );
 
191
      item->setData ( Qt::UserRole, name );
 
192
      item->setData ( Qt::ToolTipRole, QString ( "%1 = %2" ).arg ( index, name ) );
 
193
      item->setData ( Qt::StatusTipRole, desc );
 
194
      if ( pulseInfo.at ( i ).isInputDevice )
 
195
        item->setData ( Qt::DecorationRole, inputIcon );
 
196
      else
 
197
        item->setData ( Qt::DecorationRole, monitorIcon );
 
198
 
 
199
      m_deviceListWidget->addItem ( item );
 
200
    }
 
201
  }
 
202
}
 
203
 
 
204
/**
 
205
* main method to get interfaces from pulseaudio server
 
206
* http://freedesktop.org/software/pulseaudio/doxygen/async.html
 
207
*/
 
208
void PulseAudioDialog::initInterface()
 
209
{
 
210
  pa_mainloop* mloop = NULL;
 
211
  char* server = NULL;
 
212
  QByteArray client_name ( "qx11grab" );
 
213
  int ret = 0;
 
214
 
 
215
  // qDebug() <<  Q_FUNC_INFO << __LINE__ << "pa_mainloop_new";
 
216
  mloop = pa_mainloop_new();
 
217
  if ( ! mloop )
 
218
  {
 
219
    fprintf ( stderr, "QX11Grab: Set up a new pulse main loop failed with.\n" );
 
220
    return;
 
221
  }
 
222
 
 
223
  // qDebug() <<  Q_FUNC_INFO << __LINE__ << "pa_mainloop_get_api";
 
224
  mainloop_api = pa_mainloop_get_api ( mloop );
 
225
 
 
226
  // qDebug() <<  Q_FUNC_INFO << __LINE__ << "pa_context_new";
 
227
  context = pa_context_new ( mainloop_api, client_name.constData() );
 
228
  if ( ! context )
 
229
  {
 
230
    fprintf ( stderr, "QX11Grab: Create a new pulse connection context failed.\n" );
 
231
    pa_mainloop_free ( mloop );
 
232
    return;
 
233
  }
 
234
 
 
235
  // qDebug() <<  Q_FUNC_INFO << __LINE__ << "pa_context_set_state_callback";
 
236
  pa_context_set_state_callback ( context, pulseContextState, NULL );
 
237
 
 
238
  // qDebug() <<  Q_FUNC_INFO << __LINE__ << "pa_context_connect";
 
239
  if ( pa_context_connect ( context, server, PA_CONTEXT_NOFLAGS, NULL ) < 0 )
 
240
  {
 
241
    fprintf ( stderr, "QX11Grab: Pulse connection failed.\n" );
 
242
    pa_mainloop_free ( mloop );
 
243
    return;
 
244
  }
 
245
 
 
246
  // qDebug() <<  Q_FUNC_INFO << __LINE__ << "pa_mainloop_run";
 
247
  if ( pa_mainloop_run ( mloop, &ret ) < 0 )
 
248
  {
 
249
    fprintf ( stderr, "QX11Grab: Running pulse mainloop failed.\n" );
 
250
    pa_mainloop_free ( mloop );
 
251
    return;
 
252
  }
 
253
 
 
254
  // qDebug() <<  Q_FUNC_INFO << __LINE__ << "pa_mainloop_free";
 
255
  qDebug ( "QX11Grab: pulse source info done." );
 
256
  pa_context_unref ( context );
 
257
  pa_mainloop_free ( mloop );
 
258
 
 
259
  insertItems();
 
260
}
 
261
 
 
262
PulseAudioDialog::~PulseAudioDialog()
 
263
{}