summaryrefslogtreecommitdiffstats
path: root/libk3b/plugin/k3baudioserver.cpp
blob: 85785516c07da09fdac6b9745e37a5be59b6cc7d (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
/* 
 *
 * $Id: k3baudioserver.cpp 619556 2007-01-03 17:38:12Z 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 <k3bcore.h>
#include <k3bthread.h>
#include <k3bprogressinfoevent.h>
#include "k3baudioserver.h"
#include "k3baudioclient.h"
#include "k3bpluginmanager.h"
#include "k3baudiooutputplugin.h"

#include <kdebug.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdemessagebox.h>

#include <k3bthread.h>



K3bAudioServer* K3bAudioServer::s_instance = 0;



class K3bAudioServer::Private : public K3bThread
{
public:
  Private( K3bAudioServer* s )
    : m_streaming(false),
      m_server(s) {
    setProgressInfoEventHandler( m_server );
  }


  void stop() {
    m_streaming = false;
  }

protected:
  void run() {
    m_streaming = true;

    char buffer[2048*10];

    while( m_streaming ) {
      int len = m_server->m_client->read( buffer, 2048*10 );
      if( len > 0 ) {
	if( m_server->m_pluginInitialized ) {
	  if( write( buffer, len ) < 0 ) {
	    kdDebug() << "Audio Streaming failed: " << m_server->m_usedOutputPlugin->lastErrorMessage() << endl;
	    emitInfoMessage( m_server->m_usedOutputPlugin->lastErrorMessage(), 0 );
	    return;
	  }
	}
	// else just drop the data into space...
      }
      else {
	// FIXME: no data or error... what to do?
      }
    }
  }

  int write( char* buffer, int len ) {
    int written = m_server->m_usedOutputPlugin->write( buffer, len );
    return written;
//     if( written < 0 )
//       return -1;
//     else if( written < len )
//       return write( buffer+written, len-written ) + written;
//     else
//       return len;
  }

private:
  bool m_streaming;
  K3bAudioServer* m_server;
};


K3bAudioServer::K3bAudioServer( TQObject* parent, const char* name )
  : TQObject( parent, name ),
    m_usedOutputPlugin(0),
    m_pluginInitialized(false),
    m_client(0)
{
  s_instance = this;
  d = new Private( this );
}


K3bAudioServer::~K3bAudioServer()
{
  delete d;
  s_instance = 0;
}


bool K3bAudioServer::setOutputMethod( const TQCString& name )
{
  if( K3bAudioOutputPlugin* p = findOutputPlugin( name ) ) {
    setOutputPlugin( p );
    return true;
  }
  else
    return false;
}


void K3bAudioServer::setOutputPlugin( K3bAudioOutputPlugin* p )
{
  if( p != m_usedOutputPlugin ) {
    bool restart = d->running();
    if( restart ) {
      d->stop();
      d->wait();
    }
    
    if( m_usedOutputPlugin ) {
      m_usedOutputPlugin->cleanup();    
      m_pluginInitialized = false;
    }
    
    m_usedOutputPlugin = p;
    
    if( restart )
      d->start();
  }
}


void K3bAudioServer::attachClient( K3bAudioClient* c )
{
  // for now we simply allow only one client and stop the old one
  if( m_client ) {
    kdDebug() << "(K3bAudioServer) leaving old client hanging. :(" << endl;
    detachClient( m_client );
  }

  m_client = c;

  if( m_usedOutputPlugin && !m_pluginInitialized ) {
    if( !m_usedOutputPlugin->init() ) {
      emit error( i18n("Could not initialize Audio Output plugin %1 (%2)")
		  .arg(m_usedOutputPlugin->pluginInfo().name())
		  .arg(m_usedOutputPlugin->lastErrorMessage()) );
    }
    else
      m_pluginInitialized = true;
  }
  else
    kdDebug() << "(K3bAudioServer::attachClient) no output plugin selected. Using null output." << endl;

  // start the streaming
  d->start();
}


void K3bAudioServer::detachClient( K3bAudioClient* c )
{
  if( m_client == c ) {
    m_client = 0;
    
    // stop the streaming
    d->stop();
    d->wait();
    
    if( m_usedOutputPlugin && m_pluginInitialized ) {
      m_usedOutputPlugin->cleanup();
      m_pluginInitialized = false;
    }
  }
}


K3bAudioOutputPlugin* K3bAudioServer::findOutputPlugin( const TQCString& name )
{
  TQPtrList<K3bPlugin> fl = k3bcore->pluginManager()->plugins( "AudioOutput" );
  
  for( TQPtrListIterator<K3bPlugin> it( fl ); it.current(); ++it ) {
    K3bAudioOutputPlugin* f = dynamic_cast<K3bAudioOutputPlugin*>( it.current() );

    if( f && f->soundSystem() == name ) {
      return f;
    }
  }

  kdDebug() << "(K3bAudioServer::findOutputPlugin) could not find output plugin " << name << endl;

  return 0;
}


void K3bAudioServer::customEvent( TQCustomEvent* e )
{
  if( K3bProgressInfoEvent* be = dynamic_cast<K3bProgressInfoEvent*>(e) ) {
    if( be->type() == K3bProgressInfoEvent::InfoMessage ) {
      emit error( be->firstString() );
    }
  }
}

#include "k3baudioserver.moc"