summaryrefslogtreecommitdiffstats
path: root/plugins/audiooutput/alsa/k3balsaoutputplugin.cpp
blob: 437f18847ae8c0e68669ddfacf71c75abd7bfcdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* 
 *
 * $Id: k3bartsoutputplugin.cpp 369057 2004-12-07 14:05:11Z trueg $
 * Copyright (C) 2004 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include <config.h>

#include "k3balsaoutputplugin.h"
#include <k3bpluginfactory.h>
#include <k3bcore.h>

#include <kdebug.h>
#include <kcombobox.h>
#include <klocale.h>
#include <kconfig.h>
#include <kdialog.h>

#include <tqlayout.h>
#include <tqlabel.h>

#include <alsa/asoundlib.h>
#include <alsa/pcm.h>


K_EXPORT_COMPONENT_FACTORY( libk3balsaoutputplugin, K3bPluginFactory<K3bAlsaOutputPlugin>( "k3balsaoutputplugin" ) )


class K3bAlsaOutputPlugin::Private
{
public:
  Private()
    : pcm_playback(0),
      error(false) {
  }

  snd_pcm_t *pcm_playback;

  bool error;  
  TQString lastErrorMessage;

  bool swap;

  unsigned int sampleRate;
};


K3bAlsaOutputPlugin::K3bAlsaOutputPlugin( TQObject* tqparent, const char* name )
  : K3bAudioOutputPlugin( tqparent, name )
{
  d = new Private;
}


K3bAlsaOutputPlugin::~K3bAlsaOutputPlugin()
{
  cleanup();

  delete d;
}


int K3bAlsaOutputPlugin::write( char* data, int len )
{
  if( d->error )
    return -1;

  char* buffer = data;
  if( d->swap ) {
    buffer = new char[len];
    for( int i = 0; i < len-1; i+=2 ) {
      buffer[i] = data[i+1];
      buffer[i+1] = data[i];
    }
  }

  int written = 0;
  while( written < len ) {
    snd_pcm_sframes_t frames = snd_pcm_writei( d->pcm_playback, 
					       buffer+written, 
					       snd_pcm_bytes_to_frames( d->pcm_playback, len-written ) );

    if( frames < 0 ) {
      if( !recoverFromError( frames ) ) {
	d->error = true;
	return -1;
      }
    }
    else {
      written += snd_pcm_frames_to_bytes( d->pcm_playback, frames );
    }
  }

  return written;
}


bool K3bAlsaOutputPlugin::recoverFromError( int err )
{
  if( err == -EPIPE ) {
    err = snd_pcm_prepare( d->pcm_playback );
    if( err < 0 ) {
      d->lastErrorMessage = i18n("Internal Alsa problem: %1").tqarg(snd_strerror(err));
      return false;
    }
  }
  else if( err == -ESTRPIPE ) {
    while( ( err = snd_pcm_resume( d->pcm_playback ) ) == -EAGAIN )
      sleep( 1 );

    if (err < 0) {
      // unable to wake up pcm device, restart it
      err = snd_pcm_prepare( d->pcm_playback );
      if( err < 0 ) {
	d->lastErrorMessage = i18n("Internal Alsa problem: %1").tqarg(snd_strerror(err));
	return false;
      }
    }

    return true;
  }

  return false;
}


void K3bAlsaOutputPlugin::cleanup()
{
  if( d->pcm_playback ) {
    snd_pcm_drain( d->pcm_playback );
    snd_pcm_close( d->pcm_playback );
  }
  d->pcm_playback = 0;
  d->error = false;
}


bool K3bAlsaOutputPlugin::init()
{
  cleanup();

  KConfigGroup c( k3bcore->config(), "Alsa Output Plugin" );
  TQString alsaDevice = c.readEntry( "output device", "default" );

  int err = snd_pcm_open( &d->pcm_playback, alsaDevice.local8Bit(), SND_PCM_STREAM_PLAYBACK, 0 );
  if( err < 0 ) {
    d->lastErrorMessage = i18n("Could not open alsa audio device '%1' (%2).").tqarg(alsaDevice).tqarg(snd_strerror(err));
    d->error = true;
    return false;
  }

  if( !setupHwParams() ) {
    d->error = true;
    return false;
  }  

  d->error = false;
  return true;
}


bool K3bAlsaOutputPlugin::setupHwParams()
{
  snd_pcm_hw_params_t* hw_params;
  int err = 0;

  if( ( err = snd_pcm_hw_params_malloc( &hw_params ) ) < 0 ) {
    d->lastErrorMessage = i18n("Could not allocate hardware parameter structure (%1)").tqarg(snd_strerror(err));
    d->error = true;
    return false;
  }
                                 
  if( (err = snd_pcm_hw_params_any( d->pcm_playback, hw_params )) < 0) {
    d->lastErrorMessage = i18n("Could not initialize hardware parameter structure (%1).").tqarg(snd_strerror(err));
    snd_pcm_hw_params_free( hw_params );
    d->error = true;
    return false;
  }
        
  if( (err = snd_pcm_hw_params_set_access( d->pcm_playback, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    d->lastErrorMessage = i18n("Could not set access type (%1).").tqarg(snd_strerror(err));
    snd_pcm_hw_params_free( hw_params );
    d->error = true;
    return false;
  }

  if( (err = snd_pcm_hw_params_set_format( d->pcm_playback, hw_params, SND_PCM_FORMAT_S16_BE)) < 0) {
    if( (err = snd_pcm_hw_params_set_format( d->pcm_playback, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
      d->lastErrorMessage = i18n("Could not set sample format (%1).").tqarg(snd_strerror(err));
      snd_pcm_hw_params_free( hw_params );
      d->error = true;
      return false;
    }
    else
      d->swap = true;
  }
  else
    d->swap = false;

  d->sampleRate = 44100;
  if( (err = snd_pcm_hw_params_set_rate_near( d->pcm_playback, hw_params, &d->sampleRate, 0)) < 0) {
    d->lastErrorMessage = i18n("Could not set sample rate (%1).").tqarg(snd_strerror(err));
    snd_pcm_hw_params_free( hw_params );
    d->error = true;
    return false;
  }

  kdDebug() << "(K3bAlsaOutputPlugin) samplerate set to " << d->sampleRate << endl;

  if( (err = snd_pcm_hw_params_set_channels( d->pcm_playback, hw_params, 2)) < 0) {
    d->lastErrorMessage = i18n("Could not set channel count (%1).").tqarg(snd_strerror(err));
    snd_pcm_hw_params_free( hw_params );
    d->error = true;
    return false;
  }

  if( (err = snd_pcm_hw_params( d->pcm_playback, hw_params )) < 0) {
    d->lastErrorMessage = i18n("Could not set parameters (%1).").tqarg(snd_strerror(err));
    snd_pcm_hw_params_free( hw_params );
    d->error = true;
    return false;
  }
        
  snd_pcm_hw_params_free(hw_params);

  return true;
}


TQString K3bAlsaOutputPlugin::lastErrorMessage() const
{
  return d->lastErrorMessage;
}


K3bPluginConfigWidget* K3bAlsaOutputPlugin::createConfigWidget( TQWidget* tqparent, 
								const char* name ) const
{
  return new K3bAlsaOutputPluginConfigWidget( tqparent, name );
}



K3bAlsaOutputPluginConfigWidget::K3bAlsaOutputPluginConfigWidget( TQWidget* tqparent, const char* name )
  : K3bPluginConfigWidget( tqparent, name )
{
  TQHBoxLayout* l = new TQHBoxLayout( this );
  l->setSpacing( KDialog::spacingHint() );
  l->setAutoAdd( true );

  (void)new TQLabel( i18n("Alsa device:"), this );

  m_comboDevice = new KComboBox( this );
  m_comboDevice->setEditable( true );
  // enable completion
  m_comboDevice->completionObject();

  // FIXME: initialize the list of devices
  m_comboDevice->insertItem( "default" );
}


K3bAlsaOutputPluginConfigWidget::~K3bAlsaOutputPluginConfigWidget()
{
}


void K3bAlsaOutputPluginConfigWidget::loadConfig()
{
  KConfigGroup c( k3bcore->config(), "Alsa Output Plugin" );

  m_comboDevice->setCurrentText( c.readEntry( "output device", "default" ) );
}


void K3bAlsaOutputPluginConfigWidget::saveConfig()
{
  KConfigGroup c( k3bcore->config(), "Alsa Output Plugin" );

  c.writeEntry( "output device", m_comboDevice->currentText() );
}


#include "k3balsaoutputplugin.moc"