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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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.
*
* Original license header:
*
* Cabal - Legacy Game Implementations
*
* Cabal is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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.
*
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include "backends/audiocd/linux/linux-audiocd.h"
#include "backends/audiocd/audiocd-stream.h"
#include "backends/audiocd/default/default-audiocd.h"
#include "common/array.h"
#include "common/config-manager.h"
#include "common/str.h"
#include "common/debug.h"
enum {
kLeadoutTrack = 0xAA
};
enum {
kBytesPerFrame = 2352,
kSamplesPerFrame = kBytesPerFrame / 2
};
enum {
kSecondsPerMinute = 60,
kFramesPerSecond = 75
};
enum {
// Keep about a second's worth of audio in the buffer
kBufferThreshold = kFramesPerSecond
};
static int getFrameCount(const cdrom_msf0 &msf) {
int time = msf.minute;
time *= kSecondsPerMinute;
time += msf.second;
time *= kFramesPerSecond;
time += msf.frame;
return time;
}
// Helper function to convert an error code into a human-readable message
static Common::String getErrorMessage(int errorCode) {
char buf[256];
buf[0] = 0;
#ifdef _GNU_SOURCE
// glibc sucks
return Common::String(strerror_r(errorCode, buf, sizeof(buf)));
#else
strerror_r(errorCode, buf, sizeof(buf));
return Common::String(buf);
#endif
}
class LinuxAudioCDStream : public AudioCDStream {
public:
LinuxAudioCDStream(int fd, const cdrom_tocentry &startEntry, const cdrom_tocentry &endEntry);
~LinuxAudioCDStream();
protected:
uint getStartFrame() const;
uint getEndFrame() const;
bool readFrame(int frame, int16 *buffer);
private:
int _fd;
const cdrom_tocentry &_startEntry, &_endEntry;
};
LinuxAudioCDStream::LinuxAudioCDStream(int fd, const cdrom_tocentry &startEntry, const cdrom_tocentry &endEntry) :
_fd(fd), _startEntry(startEntry), _endEntry(endEntry) {
// We fill the buffer here already to prevent any out of sync issues due
// to the CD not yet having spun up.
startTimer(true);
}
LinuxAudioCDStream::~LinuxAudioCDStream() {
stopTimer();
}
bool LinuxAudioCDStream::readFrame(int frame, int16 *buffer) {
// Create the argument
union {
cdrom_msf msf;
char buffer[kBytesPerFrame];
} arg;
int seconds = frame / kFramesPerSecond;
frame %= kFramesPerSecond;
int minutes = seconds / kSecondsPerMinute;
seconds %= kSecondsPerMinute;
// Request to read that frame
// We don't use CDROMREADAUDIO, as it seems to cause kernel
// panics on ejecting discs. Probably bad to eject the disc
// while playing, but at least let's try to prevent that case.
arg.msf.cdmsf_min0 = minutes;
arg.msf.cdmsf_sec0 = seconds;
arg.msf.cdmsf_frame0 = frame;
// The "end" part is irrelevant (why isn't cdrom_msf0 the type
// instead?)
if (ioctl(_fd, CDROMREADRAW, &arg) < 0) {
warning("Failed to CD read audio: %s", getErrorMessage(errno).c_str());
return false;
}
memcpy(buffer, arg.buffer, kBytesPerFrame);
return true;
}
uint LinuxAudioCDStream::getStartFrame() const {
return getFrameCount(_startEntry.cdte_addr.msf);
}
uint LinuxAudioCDStream::getEndFrame() const {
return getFrameCount(_endEntry.cdte_addr.msf);
}
class LinuxAudioCDManager : public DefaultAudioCDManager {
public:
LinuxAudioCDManager();
~LinuxAudioCDManager();
bool open();
void close();
bool play(int track, int numLoops, int startFrame, int duration, bool onlyEmulate = false);
protected:
bool openCD(int drive);
bool openCD(const Common::String &drive);
private:
struct Device {
Device(const Common::String &n, dev_t d) : name(n), device(d) {}
Common::String name;
dev_t device;
};
typedef Common::Array<Device> DeviceList;
DeviceList scanDevices();
bool tryAddDrive(DeviceList &devices, const Common::String &drive);
bool tryAddDrive(DeviceList &devices, const Common::String &drive, dev_t device);
bool tryAddDrive(DeviceList &devices, dev_t device);
bool tryAddPath(DeviceList &devices, const Common::String &path);
bool tryAddGamePath(DeviceList &devices);
bool loadTOC();
static bool hasDevice(const DeviceList &devices, dev_t device);
int _fd;
cdrom_tochdr _tocHeader;
Common::Array<cdrom_tocentry> _tocEntries;
};
static bool isTrayEmpty(int errorNumber) {
switch (errorNumber) {
case EIO:
case ENOENT:
case EINVAL:
#ifdef ENOMEDIUM
case ENOMEDIUM:
#endif
return true;
}
return false;
}
LinuxAudioCDManager::LinuxAudioCDManager() {
_fd = -1;
memset(&_tocHeader, 0, sizeof(_tocHeader));
}
LinuxAudioCDManager::~LinuxAudioCDManager() {
close();
}
bool LinuxAudioCDManager::open() {
close();
if (openRealCD())
return true;
return DefaultAudioCDManager::open();
}
void LinuxAudioCDManager::close() {
DefaultAudioCDManager::close();
if (_fd < 0)
return;
::close(_fd);
memset(&_tocHeader, 0, sizeof(_tocHeader));
_tocEntries.clear();
}
bool LinuxAudioCDManager::openCD(int drive) {
DeviceList devices = scanDevices();
if (drive >= (int)devices.size())
return false;
_fd = ::open(devices[drive].name.c_str(), O_RDONLY | O_NONBLOCK, 0);
if (_fd < 0)
return false;
if (!loadTOC()) {
close();
return false;
}
return true;
}
bool LinuxAudioCDManager::openCD(const Common::String &drive) {
DeviceList devices;
if (!tryAddDrive(devices, drive) && !tryAddPath(devices, drive))
return false;
_fd = ::open(devices[0].name.c_str(), O_RDONLY | O_NONBLOCK, 0);
if (_fd < 0)
return false;
if (!loadTOC()) {
close();
return false;
}
return true;
}
bool LinuxAudioCDManager::play(int track, int numLoops, int startFrame, int duration, bool onlyEmulate) {
// Prefer emulation
if (DefaultAudioCDManager::play(track, numLoops, startFrame, duration, onlyEmulate))
return true;
// If we're set to only emulate, or have no CD drive, return here
if (onlyEmulate || _fd < 0)
return false;
// HACK: For now, just assume that track number is right
// That only works because ScummVM uses the wrong track number anyway
if (track >= (int)_tocEntries.size() - 1) {
warning("No such track %d", track);
return false;
}
// Bail if the track isn't an audio track
if ((_tocEntries[track].cdte_ctrl & 0x04) != 0) {
warning("Track %d is not audio", track);
return false;
}
// Create the AudioStream and play it
debug(1, "Playing CD track %d", track);
Audio::SeekableAudioStream *audioStream = new LinuxAudioCDStream(_fd, _tocEntries[track], _tocEntries[track + 1]);
Audio::Timestamp start = Audio::Timestamp(0, startFrame, 75);
Audio::Timestamp end = (duration == 0) ? audioStream->getLength() : Audio::Timestamp(0, startFrame + duration, 75);
// Fake emulation since we're really playing an AudioStream
_emulating = true;
_mixer->playStream(
Audio::Mixer::kMusicSoundType,
&_handle,
Audio::makeLoopingAudioStream(audioStream, start, end, (numLoops < 1) ? numLoops + 1 : numLoops),
-1,
_cd.volume,
_cd.balance,
DisposeAfterUse::YES,
true);
return true;
}
LinuxAudioCDManager::DeviceList LinuxAudioCDManager::scanDevices() {
DeviceList devices;
// Try to use the game's path first as the device
tryAddGamePath(devices);
// Try adding the default CD-ROM
tryAddDrive(devices, "/dev/cdrom");
// TODO: Try others?
return devices;
}
bool LinuxAudioCDManager::tryAddDrive(DeviceList &devices, const Common::String &drive) {
struct stat stbuf;
if (stat(drive.c_str(), &stbuf) < 0)
return false;
// Must be a character or block device
if (!S_ISCHR(stbuf.st_mode) && !S_ISBLK(stbuf.st_mode))
return false;
return tryAddDrive(devices, drive, stbuf.st_rdev);
}
bool LinuxAudioCDManager::tryAddDrive(DeviceList &devices, const Common::String &drive, dev_t device) {
if (hasDevice(devices, device))
return true;
// Try opening the device and seeing if it is a CD-ROM drve
int fd = ::open(drive.c_str(), O_RDONLY | O_NONBLOCK, 0);
if (fd >= 0) {
cdrom_subchnl info;
info.cdsc_format = CDROM_MSF;
bool isCD = ioctl(fd, CDROMSUBCHNL, &info) == 0 || isTrayEmpty(errno);
::close(fd);
if (isCD) {
devices.push_back(Device(drive, device));
return true;
}
}
return false;
}
bool LinuxAudioCDManager::tryAddDrive(DeviceList &devices, dev_t device) {
// Construct the block name
// (Does anyone have a better way to do this? bdevname is kernel only)
Common::String name = Common::String::format("/dev/block/%d:%d", gnu_dev_major(device), gnu_dev_minor(device));
return tryAddDrive(devices, name, device);
}
bool LinuxAudioCDManager::tryAddPath(DeviceList &devices, const Common::String &path) {
struct stat stbuf;
if (stat(path.c_str(), &stbuf) < 0)
return false;
return tryAddDrive(devices, stbuf.st_dev);
}
bool LinuxAudioCDManager::tryAddGamePath(DeviceList &devices) {
if (!ConfMan.hasKey("path"))
return false;
return tryAddPath(devices, ConfMan.get("path"));
}
bool LinuxAudioCDManager::loadTOC() {
if (_fd < 0)
return false;
if (ioctl(_fd, CDROMREADTOCHDR, &_tocHeader) < 0)
return false;
debug(4, "CD: Start Track: %d, End Track %d", _tocHeader.cdth_trk0, _tocHeader.cdth_trk1);
for (int i = _tocHeader.cdth_trk0; i <= _tocHeader.cdth_trk1; i++) {
cdrom_tocentry entry;
memset(&entry, 0, sizeof(entry));
entry.cdte_track = i;
entry.cdte_format = CDROM_MSF;
if (ioctl(_fd, CDROMREADTOCENTRY, &entry) < 0)
return false;
#if 0
debug("Entry:");
debug("\tTrack: %d", entry.cdte_track);
debug("\tAdr: %d", entry.cdte_adr);
debug("\tCtrl: %d", entry.cdte_ctrl);
debug("\tFormat: %d", entry.cdte_format);
debug("\tMSF: %d:%d:%d", entry.cdte_addr.msf.minute, entry.cdte_addr.msf.second, entry.cdte_addr.msf.frame);
debug("\tMode: %d\n", entry.cdte_datamode);
#endif
_tocEntries.push_back(entry);
}
// Fetch the leadout so we can get the length of the last frame
cdrom_tocentry entry;
memset(&entry, 0, sizeof(entry));
entry.cdte_track = kLeadoutTrack;
entry.cdte_format = CDROM_MSF;
if (ioctl(_fd, CDROMREADTOCENTRY, &entry) < 0)
return false;
#if 0
debug("Lead out:");
debug("\tTrack: %d", entry.cdte_track);
debug("\tAdr: %d", entry.cdte_adr);
debug("\tCtrl: %d", entry.cdte_ctrl);
debug("\tFormat: %d", entry.cdte_format);
debug("\tMSF: %d:%d:%d", entry.cdte_addr.msf.minute, entry.cdte_addr.msf.second, entry.cdte_addr.msf.frame);
debug("\tMode: %d\n", entry.cdte_datamode);
#endif
_tocEntries.push_back(entry);
return true;
}
bool LinuxAudioCDManager::hasDevice(const DeviceList &devices, dev_t device) {
for (DeviceList::const_iterator it = devices.begin(); it != devices.end(); it++)
if (it->device == device)
return true;
return false;
}
AudioCDManager *createLinuxAudioCDManager() {
return new LinuxAudioCDManager();
}
|