summaryrefslogtreecommitdiffstats
path: root/arts/kde/kaudioconverter.cc
blob: 1eb8c20e5297c70abc9e4db1ec17437d27d135db (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
	/*

	Copyright (C) 2002 Nikolas Zimmermann <wildfox@kde.org>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Library General Public
	License as published by the Free Software Foundation; either
	version 2 of the License, or (at your option) any later version.
  
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Library General Public License for more details.

	You should have received a copy of the GNU Library General Public License
	along with this library; see the file COPYING.LIB.  If not, write to
	the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
	Boston, MA 02110-1301, USA.

	*/

#include "config.h"
#include "artskde.h"
#include "connect.h"
#include "flowsystem.h"
#include "audiosubsys.h"
#include "dynamicrequest.h"
#include "kdatarequest_impl.h"
#include "kioinputstream_impl.h"
#include "kaudioconverter.moc"

#include <iostream>

#include <tqfile.h>
#include <tqtimer.h>

#include <kurl.h>
#include <kdebug.h>
#include <kmimetype.h>
#include <tdeapplication.h>

using namespace std;

KAudioConverter::KAudioConverter()
{
	m_incoming = 0;
	m_started = false;
}

KAudioConverter::~KAudioConverter()
{
	delete m_incoming;
}

bool KAudioConverter::setup(int samplingRate)
{
	string backupAudioIO = Arts::AudioSubSystem::the()->audioIO();
	int backupSamplingRate = Arts::AudioSubSystem::the()->samplingRate();
	
	Arts::AudioSubSystem::the()->audioIO("null");
	Arts::AudioSubSystem::the()->samplingRate(samplingRate);
	
	if(!Arts::AudioSubSystem::the()->open())
	{
		Arts::AudioSubSystem::the()->audioIO(backupAudioIO);
		Arts::AudioSubSystem::the()->samplingRate(backupSamplingRate);
		
		return false;
	}

	return true;
}

void KAudioConverter::slotMimeType(const TQString &mimeType)
{
	m_mimeType = mimeType;
	kapp->exit_loop();
}

void KAudioConverter::requestPlayObject(const KURL &url)
{	
	string queryInterface = "Arts::PlayObject";
	
	Arts::TDEIOInputStream inputStream;
	
	if(!url.isLocalFile())
	{
		Arts::TDEIOInputStream_impl *inputStreamImpl = new Arts::TDEIOInputStream_impl();
		inputStream = Arts::TDEIOInputStream::_from_base(inputStreamImpl);

		TQObject::connect(inputStreamImpl, TQT_SIGNAL(mimeTypeFound(const TQString &)), TQT_SLOT(slotMimeType(const TQString &)));

		inputStream.openURL(url.url().latin1());
		inputStream.streamStart();

		// ugly hacks.. :/
		kapp->enter_loop();

		queryInterface = "Arts::StreamPlayObject";		
	}
	else
	{
		KMimeType::Ptr mimetype = KMimeType::findByURL(url);
		m_mimeType = mimetype->name();
	}
	
	Arts::TraderQuery query;
	query.supports("Interface", queryInterface);
	query.supports("MimeType", string(m_mimeType.latin1()));

	string objectType;

	vector<Arts::TraderOffer> *offers = query.query();
	if(!offers->empty())
		objectType = offers->front().interfaceName(); // first offer

	delete offers;

	if(objectType.empty())
	{
		m_incoming = 0;
		return;
	}

	if(!url.isLocalFile())
	{
		Arts::StreamPlayObject result = Arts::SubClass(objectType);
		result.streamMedia(inputStream);
		result._node()->start();

		m_incoming = new KPlayObject(result, true);
	}
	else
	{
		Arts::PlayObject result = Arts::SubClass(objectType);

		if(result.loadMedia(string(TQFile::encodeName(url.path()))))
		{
			result._node()->start();
			m_incoming = new KPlayObject(result, false);
		}
		else
			m_incoming = 0;
	}
}

void KAudioConverter::start()
{
	if(m_started || !m_incoming)
		return;

	m_started = true;
	
	emit rawStreamStart();

	m_incoming->play();

	Arts::KDataRequest_impl *requestImpl = new Arts::KDataRequest_impl();
	m_request = Arts::KDataRequest::_from_base(requestImpl);

	Arts::connect(m_incoming->object(), "left", m_request, "left");
	Arts::connect(m_incoming->object(), "right", m_request, "right");

	TQObject::connect(requestImpl, TQT_SIGNAL(newBlockSize(long)), TQT_SIGNAL(newBlockSize(long)));
	TQObject::connect(requestImpl, TQT_SIGNAL(newBlockPointer(long)), TQT_SIGNAL(newBlockPointer(long)));
	TQObject::connect(requestImpl, TQT_SIGNAL(newData()), TQT_SIGNAL(newData()));

	// Special mpeglib case
	// TODO: needed at all??
	usleep(100000);
	if(m_incoming->object()._base()->_isCompatibleWith("DecoderBaseObject"))
		if(!Arts::DynamicRequest(m_incoming->object()).method("_set_blocking").param(true).invoke())
			cerr << "mpeglib, and blocking attribute can't be changed?" << endl;
	
	m_request.start();
		
	// TODO: Maybe do this async, using TQTimer::singleShot
	// But jowenn i think jowenn is right -> this would
	// lead to problems in slotNewData() when accessing the data
	// (could already be overwritten...)
	while(m_incoming->state() != Arts::posIdle)
		m_request.goOn();
		
	stop();
}

void KAudioConverter::stop()
{
	if(!m_started || !m_incoming)
		return;

	m_incoming->halt();
	m_request.streamEnd();

	m_started = false;

	emit rawStreamFinished();
}