summaryrefslogtreecommitdiffstats
path: root/noatun/library/engine.cpp
blob: 5cfd23edbb74876ffc83cb60d3d893c9e599d2cc (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// $Id$

#include <noatun/engine.h>
#include <noatun/equalizer.h>
#include <noatun/player.h>
#include <noatun/plugin.h>
#include <noatun/effects.h>
#include "titleproxy.h"

#include <string.h>

#include <kmessagebox.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kconfig.h>
#include <kdebug.h>
#include <tqfile.h>
#include <tqdir.h>
#include <sys/wait.h>
#include <kplayobject.h>
#include <kplayobjectfactory.h>

#include <dynamicrequest.h>
#include <soundserver.h>
#include <kmedia2.h>
#include <flowsystem.h>
#include <noatunarts.h>
#include <connect.h>
#include <cpuinfo.h>

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>


static Arts::PlayObject nullPO() { return Arts::PlayObject::null(); }

#define HARDWARE_VOLUME

#if defined(__osf__)
#undef HARDWARE_VOLUME
#elif defined(__linux__)
#include <sys/soundcard.h>
#elif defined(__FreeBSD__)
#include <sys/soundcard.h>
#elif defined(__NetBSD__)
#include <soundcard.h>
#elif defined(___SOMETHING_UNKNOWN__)
#include <sys/soundcard.h>
#elif defined(_UNIXWARE)
#include <sys/soundcard.h>
#else
#undef HARDWARE_VOLUME
#endif

using namespace std;

namespace VolumeControls
{
	struct VC
	{
		virtual ~VC() {}
		virtual void setVolume(int percent)=0;
		virtual int volume() const =0;
	};

#ifdef HARDWARE_VOLUME

	struct Hardware : public VC
	{
		Hardware(Engine *)
		{
			if ((fd=::open( "/dev/mixer", O_RDWR)) < 0)
			{
				return;
			}
			else
			{
#define ERROR { fd=-1; return; }
				int devmask, recmask, i_recsrc, stereodevs;
				// Mixer is open. Now define properties
				if (ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) ERROR
				if (ioctl(fd, SOUND_MIXER_READ_RECMASK, &recmask) == -1) ERROR
				if (ioctl(fd, SOUND_MIXER_READ_RECSRC, &i_recsrc) == -1) ERROR
				if (ioctl(fd, SOUND_MIXER_READ_STEREODEVS, &stereodevs) == -1) ERROR
				if (!devmask) ERROR
#undef ERROR
			}

		}

		virtual ~Hardware() { ::close(fd); }

		virtual void setVolume(int percent)
		{
			percent=percent+(percent<<8);
			ioctl(fd, MIXER_WRITE(4), &percent);
		}

		virtual int volume() const
		{
			int volume, left, right;
			left=100;
			if (ioctl(fd, MIXER_READ(4), &volume) != -1)
			{
				left=volume & 0x7f;
				right=(volume>>8) & 0x7f;
				left=(left+right)/2;
			}
			return left;
		}
	private:
		int fd;
	};
#endif

	struct SoftwareSSE : public VC
	{
		SoftwareSSE(Engine *e) : mVolume(100)
		{
			volumeControl = Arts::DynamicCast(e->server()->createObject("Noatun::StereoVolumeControlSSE"));

			if(volumeControl.isNull())
				volumeControl = Arts::DynamicCast(e->server()->createObject("Noatun::StereoVolumeControl"));

			volumeControl.start();

			id=e->globalEffectStack()->insertBottom(volumeControl,"Volume Control");
		}

		virtual void setVolume(int percent)
		{
			mVolume=percent;
			float vol = pow(2.0, ((400 - (100-percent)*12)/200.0))/4.0;
			if (percent == 0) vol = 0.0;
			if (!volumeControl.isNull())
				volumeControl.percent(vol);
		}

		virtual int volume() const
		{
			return mVolume;
		}

	private:
		Noatun::StereoVolumeControlSSE volumeControl;
		long id;
		int mVolume;
	};

	struct Software : public VC
	{
		Software(Engine *e) : mVolume(100)
		{
			volumeControl = Arts::DynamicCast(e->server()->createObject("Noatun::StereoVolumeControl"));
			volumeControl.start();

			id=e->globalEffectStack()->insertBottom(volumeControl,"Volume Control");
		}

		virtual void setVolume(int percent)
		{
			mVolume=percent;
			if (!volumeControl.isNull())
				volumeControl.percent((float)percent/100.0);
		}

		virtual int volume() const
		{
			return mVolume;
		}

	private:
		Noatun::StereoVolumeControl volumeControl;
		long id;
		int mVolume;
	};

	static VC *volumeControl(Engine *e)
	{
#ifdef HARDWARE_VOLUME
		if (napp->fastMixer())
			return new Hardware(e);
		else
		{
#endif
			if (!getenv("NO_SSE") && Arts::CpuInfo::flags()&Arts::CpuInfo::CpuSSE)
				return new SoftwareSSE(e);
			else
				return new Software(e);

#ifdef HARDWARE_VOLUME
		}
#endif
	}
}


class Engine::EnginePrivate
{
public:
	EnginePrivate()
		: playobj(0),
		  server(Arts::SoundServerV2::null()),
		  globalEffectStack(Noatun::StereoEffectStack::null()),
		  effectsStack(Noatun::StereoEffectStack::null()),
		  visStack(Noatun::StereoEffectStack::null()),
		  volumeControl(0), session(Noatun::Session::null()),
		  pProxy(0)
		{
		}

	~EnginePrivate()
	{
		visStack=Noatun::StereoEffectStack::null();
	}

	KDE::PlayObject *playobj;
	Arts::SoundServerV2 server;
	Arts::Synth_AMAN_PLAY amanPlay;

	// globalEffectStack
	//  |- effectsStack
	//      |- Effects...
	//  |- visStack
	//      |- Visualizations
	//  |- Volume Control
	//
	Noatun::StereoEffectStack globalEffectStack;
	Noatun::StereoEffectStack effectsStack;
	Noatun::StereoEffectStack visStack;
	Noatun::Equalizer equalizer;

	int volumeID;
	VolumeControls::VC *volumeControl;
	Noatun::Session session;
	TitleProxy::Proxy *pProxy;
};

Arts::SoundServerV2 *Engine::server() const               { return &d->server;}
Arts::PlayObject Engine::playObject() const                  { return d->playobj ? d->playobj->object() : nullPO(); }
Arts::SoundServerV2 *Engine::simpleSoundServer() const    { return &d->server; }
Noatun::StereoEffectStack *Engine::effectStack() const        { return &d->effectsStack; }
Noatun::StereoEffectStack *Engine::visualizationStack() const { return &d->visStack; }
Noatun::StereoEffectStack *Engine::globalEffectStack() const  { return &d->globalEffectStack; }
Noatun::Equalizer *Engine::equalizer() const { return &d->equalizer; }
Noatun::Session *Engine::session() const { return &d->session; }

Engine::Engine(TQObject *parent) : TQObject(parent, "Engine"), mPlay(false)
{
	d=new EnginePrivate;
	// Connect to aRts
	if (!initArts())
	{
		KMessageBox::error(0, i18n("There was an error communicating to the aRts daemon."), i18n("aRts error"));
		exit(0);
	}

}

Engine::~Engine()
{
	stop();
	delete d->volumeControl;
	d->server=Arts::SoundServerV2::null();
	delete d;
}

void Engine::setInitialized()
{
	mPlay=true;
}

bool Engine::initialized() const
{
	return mPlay;
}

bool Engine::open(const PlaylistItem &file)
{
	if(!initArts())
		return false;

	d->playobj = 0;

	KDE::PlayObjectFactory factory(d->server);

	if (file.isProperty("stream_") && file.url().protocol() == "http")
	{
		deleteProxy();
		d->pProxy = new TitleProxy::Proxy(KURL(file.property("stream_")));
		d->playobj = factory.createPlayObject(d->pProxy->proxyUrl(), false);

		connect(d->playobj, TQT_SIGNAL(destroyed()), this, TQT_SLOT(deleteProxy()));
		connect(
			d->pProxy, TQT_SIGNAL(
				metaData(
				const TQString &, const TQString &,
				const TQString &, const TQString &,
				const TQString &, const TQString &)),
			this, TQT_SIGNAL(
				receivedStreamMeta(const TQString &, const TQString &,
				const TQString &, const TQString &,
				const TQString &, const TQString &))
		);
		connect(d->pProxy, TQT_SIGNAL(proxyError()), this, TQT_SLOT(slotProxyError()));
	}
	else
	{
		d->playobj = factory.createPlayObject(file.url(), false);
	}

	if (!d->playobj || d->playobj->isNull())
	{
		kdDebug(66666) << k_funcinfo <<
			"No playobject for '" << file.url().prettyURL() << "'" << endl;
		delete d->playobj;
		d->playobj=0;
		emit playingFailed();
		return false;
	}

	if ( !d->playobj->object().isNull() )
	{
		connectPlayObject();
	}
	else
	{
		connect( d->playobj, TQT_SIGNAL( playObjectCreated() ), this, TQT_SLOT( connectPlayObject() ) );
	}

	if (mPlay)
		d->playobj->play();

	return true;
}

void Engine::slotProxyError()
{
	kdDebug(66666) << k_funcinfo << endl;
	emit playingFailed();
	deleteProxy();
}

void Engine::deleteProxy()
{
	delete d->pProxy;
	d->pProxy = 0;
}

void Engine::connectPlayObject()
{
	if (d->playobj->object().isNull())
	{
		emit playingFailed();
		return;
	}
	d->playobj->object()._node()->start();

	// TODO: check for existence of left & right streams
	Arts::connect(d->playobj->object(),"left",d->globalEffectStack,"inleft");
	Arts::connect(d->playobj->object(),"right",d->globalEffectStack,"inright");

	emit aboutToPlay();
}

bool Engine::play()
{
	if (!mPlay) return true;
	if(!d->playobj)
		return false;
	d->playobj->play();
	return true;
}

void Engine::pause()
{
	d->playobj->pause();
}

void Engine::stop()
{
	if(!d->playobj) return;

	d->playobj->halt();
	delete d->playobj;
	d->playobj=0;
}

void Engine::seek(int msec) // pass time in msecs
{
	if(!d->playobj) return;

	Arts::poTime t;

	t.custom = 0.0;
	t.ms = (long) msec % 1000;
	t.seconds = (long) (msec - t.ms) / 1000;

	if(d->playobj)
		d->playobj->seek(t);
}

int Engine::position()
{
	if(!d->playobj) return -1;

	Arts::poTime time(d->playobj->currentTime());
	return (int)(time.ms + (time.seconds*1000)); // return position in milliseconds
}

int Engine::length()
{
	if(!d->playobj) return -1;
	if (!(d->playobj->capabilities() & Arts::capSeek))
		return -1;

	Arts::poTime time(d->playobj->overallTime());
	return (int)(time.ms + (time.seconds*1000)); // return track-length in milliseconds
}

int Engine::state()
{
	if(d->playobj)
		return d->playobj->state();
	else
		return Arts::posIdle;
}

void Engine::setVolume(int percent)
{
	if (percent>100)
		percent=100;
	if (percent<0)
		percent=0;
	d->volumeControl->setVolume(percent);
}

int Engine::volume() const
{
	return d->volumeControl->volume();
}

void Engine::useHardwareMixer(bool)
{
	delete d->volumeControl;
	d->volumeControl=VolumeControls::volumeControl(this);
}

bool Engine::initArts()
{
	if ( d->server.isNull() || d->server.error()  )
	{
		d->server = Arts::Reference("global:Arts_SoundServerV2");
		int volume = d->volumeControl ? d->volumeControl->volume() : -1;
		delete d->volumeControl;
		d->volumeControl=0;

		if( d->server.isNull() || d->server.error() )
		{
			// aRts seems not to be running, let's try to run it
			// First, let's read the configuration as in kcmarts
			KConfig config("kcmartsrc");
			TQCString cmdline;

			config.setGroup("Arts");

			bool rt = config.readBoolEntry("StartRealtime",false);
			bool x11Comm = config.readBoolEntry("X11GlobalComm",false);

			// put the value of x11Comm into .mcoprc
			{
				KConfig X11CommConfig(TQDir::homeDirPath()+"/.mcoprc");

				if(x11Comm)
					X11CommConfig.writeEntry("GlobalComm", "Arts::X11GlobalComm");
				else
					X11CommConfig.writeEntry("GlobalComm", "Arts::TmpGlobalComm");

				X11CommConfig.sync();
			}

			cmdline = TQFile::encodeName(KStandardDirs::findExe(TQString::fromLatin1("tdeinit_wrapper")));
			cmdline += " ";

			if (rt)
				cmdline += TQFile::encodeName(KStandardDirs::findExe(
				TQString::fromLatin1("artswrapper")));
			else
				cmdline += TQFile::encodeName(KStandardDirs::findExe(
				TQString::fromLatin1("artsd")));

			cmdline += " ";
			cmdline += config.readEntry("Arguments","-F 10 -S 4096 -s 60 -m artsmessage -l 3 -f").utf8();

			int status=::system(cmdline);

			if ( status!=-1 && WIFEXITED(status) )
			{
				// We could have a race-condition here.
				// The correct way to do it is to make artsd fork-and-exit
				// after starting to listen to connections (and running artsd
				// directly instead of using tdeinit), but this is better
				// than nothing.
				int time = 0;
				do
				{
					// every time it fails, we should wait a little longer
					// between tries
					::sleep(1+time/2);
					d->server = Arts::Reference("global:Arts_SoundServerV2");
				} while(++time < 5 && (d->server.isNull()));
			}
		}

		if ( !d->server.isNull() )
		{
			d->amanPlay = Arts::DynamicCast(
					d->server.createObject("Arts::Synth_AMAN_PLAY")
				);

			if (d->amanPlay.isNull())
				goto crapOut;

			d->session = Arts::DynamicCast(
				d->server.createObject("Noatun::Session"));
			if (d->session.isNull())
			{
				kdWarning() << "Couldn't instanciate artsobject Noatun::Session. "
					<< "(This is normally caused by a broken package or "
					<< "compiling tdemultimedia in a --prefix different "
					<< "from arts.  It may also be from two conflicting "
					<< "packages, so uninstall every arts/artsd package "
					<< "you have installed and try again." << endl;
				goto crapOut;
			}

			d->amanPlay.title("noatun");
			d->amanPlay.autoRestoreID("noatun");
			d->amanPlay.start();

			d->globalEffectStack=Arts::DynamicCast(
			d->server.createObject("Noatun::StereoEffectStack"));;
			d->globalEffectStack.start();
			Arts::connect(d->globalEffectStack,d->amanPlay);

			d->effectsStack=Arts::DynamicCast(
				d->server.createObject("Noatun::StereoEffectStack"));
			d->effectsStack.start();
			d->globalEffectStack.insertBottom(d->effectsStack, "Effects Stack");

			d->equalizer=Arts::DynamicCast(d->server.createObject("Noatun::Equalizer"));
			d->equalizer.start();
			d->globalEffectStack.insertBottom(d->equalizer, "Equalizer");

			if (napp->equalizer())
			{
				napp->equalizer()->update(true);
				napp->equalizer()->setPreamp(napp->equalizer()->preamp());
				napp->equalizer()->setEnabled(napp->equalizer()->isEnabled());
			}

			d->visStack=Arts::DynamicCast(
				d->server.createObject("Noatun::StereoEffectStack"));
			d->visStack.start();

			d->globalEffectStack.insertBottom(d->visStack, "Visualization Stack");
			d->volumeControl=VolumeControls::volumeControl(this);
			if (volume != -1)
				d->volumeControl->setVolume(volume);
		}
		else
		{
		crapOut:
			KMessageBox::error( 0, i18n("Connecting/starting aRts soundserver failed. Make sure that artsd is configured properly."));
			exit(1);
		}
	}

	d->playobj=0;
	emit artsError();
	return true;
}


#include "engine.moc"