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
|
/*
Copyright (C) 2001 Jeff Tranter
tranter@kde.org
2001 Stefan Westerfeld
stefan@space.twc.de
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.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Permission is also granted to link this program with the Qt
library, treating Qt like a library that normally accompanies the
operating system kernel, whether or not that is in fact the case.
*/
#include "artsflow.h"
#include "flowsystem.h"
#include "audiosubsys.h"
#include "connect.h"
#include "debug.h"
#include "soundserverv2_impl.h"
#include "artsversion.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fstream>
#include <set>
#include <cstring>
#include <cstdlib>
#include "config.h"
using namespace Arts;
using namespace std;
SoundServerV2_impl::SoundServerV2_impl()
: _sampleStorage(SampleStorage(
MCOPUtils::createFilePath("artsd-samples"),true))
{
checkNewObjects();
}
std::string SoundServerV2_impl:: audioMethod() {
return AudioSubSystem::the()->audioIO();
}
long SoundServerV2_impl:: samplingRate() {
return AudioSubSystem::the()->samplingRate();
}
long SoundServerV2_impl:: channels() {
return AudioSubSystem::the()->channels();
}
long SoundServerV2_impl:: bits() {
return AudioSubSystem::the()->bits();
}
bool SoundServerV2_impl:: fullDuplex() {
return AudioSubSystem::the()->fullDuplex();
}
std::string SoundServerV2_impl:: audioDevice() {
return AudioSubSystem::the()->deviceName();
}
long SoundServerV2_impl::fragments() {
return AudioSubSystem::the()->fragmentCount();
}
long SoundServerV2_impl::fragmentSize() {
return AudioSubSystem::the()->fragmentSize();
}
long SoundServerV2_impl::autoSuspendSeconds() {
return autoSuspendTime;
}
void SoundServerV2_impl::autoSuspendSeconds(long int newValue) {
autoSuspendTime = newValue;
}
std::string SoundServerV2_impl::version() {
return ARTS_VERSION;
}
long SoundServerV2_impl::bufferSizeMultiplier() {
return bufferMultiplier;
}
void SoundServerV2_impl::bufferSizeMultiplier(long newValue) {
bufferMultiplier = newValue;
}
StereoVolumeControl SoundServerV2_impl::outVolume() {
return _outVolume;
}
SampleStorage SoundServerV2_impl::sampleStorage() {
return _sampleStorage;
}
PlayObject SoundServerV2_impl::createPlayObjectForURL(const std::string& url, const std::string& mimetype, bool createBUS)
{
arts_debug("search playobject, mimetype = %s", mimetype.c_str());
TraderQuery query;
query.supports("Interface","Arts::PlayObject");
query.supports("MimeType", mimetype);
string objectType;
vector<TraderOffer> *offers = query.query();
if(!offers->empty())
objectType = offers->front().interfaceName(); // first offer
delete offers;
/*
* create a PlayObject and connect it
*/
if(!objectType.empty())
{
arts_debug("creating %s to play file", objectType.c_str());
PlayObject result = SubClass(objectType);
if(result.loadMedia(url))
{
if(createBUS)
{
// TODO: check for existence of left & right streams
Synth_BUS_UPLINK uplink;
uplink.busname("out_soundcard");
connect(result,"left",uplink,"left");
connect(result,"right",uplink,"right");
uplink.start();
result._node()->start();
result._addChild(uplink,"uplink");
return result;
}
else
return result;
}
else arts_warning("couldn't load file %s", url.c_str());
}
else arts_warning("mimetype %s unsupported", mimetype.c_str());
return PlayObject::null();
}
PlayObject SoundServerV2_impl::createPlayObjectForStream(InputStream instream, const std::string& mimetype, bool createBUS)
{
arts_debug("search streamplayobject, mimetype = %s", mimetype.c_str());
TraderQuery query;
query.supports("Interface","Arts::StreamPlayObject");
query.supports("MimeType", mimetype);
string objectType;
vector<TraderOffer> *offers = query.query();
if(!offers->empty())
objectType = offers->front().interfaceName(); // first offer
delete offers;
/*
* create a PlayObject and connect it
*/
if(!objectType.empty())
{
arts_debug("creating %s to play file", objectType.c_str());
StreamPlayObject result = SubClass(objectType);
result.streamMedia(instream);
if(createBUS)
{
// TODO: check for existence of left & right streams
Synth_BUS_UPLINK uplink;
uplink.busname("out_soundcard");
connect(result,"left",uplink,"left");
connect(result,"right",uplink,"right");
uplink.start();
result._node()->start();
result._addChild(uplink,"uplink");
return result;
}
else
return result;
}
else arts_warning("mimetype %s unsupported for streaming", mimetype.c_str());
return PlayObject::null();
}
static void clearDirectory(const string& directory)
{
DIR *dir = opendir(directory.c_str());
if(!dir) return;
struct dirent *de;
while((de = readdir(dir)) != 0)
{
string currentEntry = directory + "/" + de->d_name;
if(de->d_name[0] != '.')
{
unlink(currentEntry.c_str());
}
}
closedir(dir);
}
/* copied from mcopidl */
static void doTypeIndex(string dir, string prefix, ModuleDef& module)
{
FILE *typeIndex = fopen((dir+"/"+prefix+".mcopclass").c_str(),"w");
vector<string> supportedTypes;
vector<InterfaceDef>::iterator ii;
for(ii = module.interfaces.begin(); ii != module.interfaces.end(); ii++)
supportedTypes.push_back(ii->name);
vector<TypeDef>::iterator ti;
for(ti = module.types.begin(); ti != module.types.end(); ti++)
supportedTypes.push_back(ti->name);
string supportedTypesList;
vector<string>::iterator si;
bool first = true;
for(si = supportedTypes.begin(); si != supportedTypes.end(); si++)
{
if(!first) supportedTypesList += ",";
supportedTypesList += (*si);
first = false;
}
fprintf(typeIndex, "# this file was generated by artsd - do not edit\n");
fprintf(typeIndex,"Type=%s\n",supportedTypesList.c_str());
fprintf(typeIndex,"TypeFile=%s.mcoptype\n",prefix.c_str());
fclose(typeIndex);
}
void SoundServerV2_impl::checkNewObjects()
{
const char *home = getenv("HOME");
arts_return_if_fail(home != 0);
string dir = home + string("/.mcop/trader-cache");
string dataVersionFileName = dir + "/cache-data-version";
mkdir(home,0755);
mkdir((home+string("/.mcop")).c_str(),0755);
if(mkdir(dir.c_str(),0755) != 0)
{
string why = strerror(errno);
struct stat st;
stat(dir.c_str(),&st);
if(!S_ISDIR(st.st_mode))
{
arts_warning("can't create directory %s to fill it with"
" trader data (%s)", dir.c_str(), why.c_str());
return;
}
}
TraderQuery query;
query.supports("Interface", "Arts::Loader");
vector<TraderOffer> *offers = query.query();
vector<TraderOffer>::iterator i;
set<string> newDataVersion, cacheDataVersion;
for(i = offers->begin(); i != offers->end(); i++)
{
// TODO: error checking?
Arts::Loader loader = SubClass(i->interfaceName());
newDataVersion.insert(loader.dataVersion());
}
/* change this line if you change the cache update code */
newDataVersion.insert("Cache-Update-Code-Version:1.0");
/* load cache-data-version file */
{
ifstream infile(dataVersionFileName.c_str());
string line;
while(infile >> line)
cacheDataVersion.insert(line);
}
/* if it differs, rebuild trader cache */
if(cacheDataVersion != newDataVersion)
{
clearDirectory(dir);
/* save new cache-data-version file */
{
ofstream out(dataVersionFileName.c_str());
set<string>::iterator i;
for(i = newDataVersion.begin(); i != newDataVersion.end(); i++)
out << *i << endl;
}
rebuildTraderCache(dir, offers);
}
delete offers;
}
void SoundServerV2_impl::rebuildTraderCache(const string& directory,
vector<TraderOffer> *offers)
{
vector<TraderOffer>::iterator i;
for(i = offers->begin(); i != offers->end(); i++)
{
// TODO: error checking?
Arts::Loader loader = SubClass(i->interfaceName());
/* put trader-information in ~/.mcop/trader-cache */
vector<TraderEntry> *entries = loader.traderEntries();
vector<TraderEntry>::iterator ei;
for(ei = entries->begin(); ei != entries->end(); ei++)
{
const TraderEntry& entry = *ei;
FILE *traderFile = fopen((directory+"/"+entry.interfaceName+".mcopclass").c_str(),"w");
fprintf(traderFile, "# this file was generated by artsd - do not edit\n");
vector<string>::const_iterator li;
for(li = entry.lines.begin(); li != entry.lines.end(); li++)
fprintf(traderFile,"%s\n", li->c_str());
fclose(traderFile);
}
delete entries;
/* put type-information in ~/.mcop/trader-cache */
vector<ModuleDef> *modules = loader.modules();
vector<ModuleDef>::iterator mi;
for(mi = modules->begin(); mi != modules->end(); mi++)
{
Arts::ModuleDef& module = *mi;
Buffer b;
module.writeType(b);
FILE *typeFile = fopen((directory + "/" + module.moduleName+".arts.mcoptype").c_str(),"w");
unsigned long towrite = b.size();
fwrite(b.read(towrite),1,towrite,typeFile);
fclose(typeFile);
doTypeIndex(directory,module.moduleName+".arts",module);
}
delete modules;
}
Dispatcher::the()->reloadTraderData();
}
float SoundServerV2_impl::cpuUsage()
{
return CPUUsage::the()->usage() * 100.0;
}
#ifndef __SUNPRO_CC
/* See bottom of simplesoundserver_impl.cc for the reason this is here. */
REGISTER_IMPLEMENTATION(SoundServerV2_impl);
#endif
|