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
|
/*
* PortBurn
* Mac OS X implementation
*
* Authors:
* Dominic Mazzoni
*
* With assistance from Apple's sample code:
* /Developer/Examples/DiscRecording/C/
*
* License: LGPL
*/
#include "portburn.h"
#include "portburn_staging.h"
#include <string.h>
#include <DiscRecording/DiscRecording.h>
typedef struct {
OSStatus err;
CFArrayRef deviceList;
DRDeviceRef device;
CFMutableArrayRef trackArray;
DRBurnRef burn;
CFArrayRef trackLayout;
void *staging;
float frac;
} PBHandle;
char *PortBurn_CStringFromCFString(CFStringRef cfname) {
char *name;
CFIndex len;
len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfname),
kCFStringEncodingASCII);
name = (char *)malloc(len + 1);
name[0] = 0;
CFStringGetCString(cfname, name, len + 1, kCFStringEncodingASCII);
return name;
}
void *PortBurn_Open()
{
PBHandle *h;
h = (PBHandle *)malloc(sizeof(PBHandle));
memset(h, 0, sizeof(PBHandle));
return h;
}
/* Cleanup */
void PortBurn_Close(void *handle) {
PBHandle *h = (PBHandle *)handle;
if (!h)
return;
if (h->deviceList)
CFRelease(h->deviceList);
free(h);
}
/* Return a human-readable error string for the last operating system
specific error (NOT human readable strings for the PortBurn error
codes). Caller should dispose of the returned string using free(). */
const char *PortBurn_LastError(void *handle)
{
PBHandle *h = (PBHandle *)handle;
CFStringRef cferr;
if (!h)
return NULL;
cferr = DRCopyLocalizedStringForDiscRecordingError(h->err);
if (cferr) {
char *result = PortBurn_CStringFromCFString(cferr);
CFRelease(cferr);
return result;
}
else
return NULL;
}
/* Get the number of devices capable of burning audio CDs.
If the result is N, then calls to GetDeviceName and OpenDevice
are guaranteed to be valid for indices from 0 up to N-1, until
the next time you call GetNumDevices. At that point, the list of
devices will be rescanned, and may be different. */
int PortBurn_GetNumDevices(void *handle)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return 0;
if (h->deviceList) {
CFRelease(h->deviceList);
h->deviceList = NULL;
}
h->deviceList = DRCopyDeviceArray();
if (!h->deviceList)
return 0;
return (int)CFArrayGetCount(h->deviceList);
}
/* Get the name of the device with a given index. Only valid
after a call to GetNumDevices. */
char *PortBurn_GetDeviceName(void *handle, int index)
{
PBHandle *h = (PBHandle *)handle;
CFDictionaryRef deviceInfo;
CFStringRef bus, vendor, product;
CFStringRef cfname;
char *result;
if (!h)
return NULL;
if (!h->deviceList)
return NULL;
DRDeviceRef device;
device = (DRDeviceRef)CFArrayGetValueAtIndex(h->deviceList, index);
deviceInfo = DRDeviceCopyInfo(device);
bus = (CFStringRef)CFDictionaryGetValue(deviceInfo,
kDRDevicePhysicalInterconnectKey);
if (CFEqual(bus, kDRDevicePhysicalInterconnectFireWire))
bus = CFSTR("FireWire: ");
else if (CFEqual(bus, kDRDevicePhysicalInterconnectUSB))
bus = CFSTR("USB: ");
else if (CFEqual(bus, kDRDevicePhysicalInterconnectATAPI))
bus = CFSTR("ATAPI: ");
else if (CFEqual(bus, kDRDevicePhysicalInterconnectSCSI))
bus = CFSTR("SCSI: ");
else
bus = CFSTR("");
vendor = CFDictionaryGetValue(deviceInfo, kDRDeviceVendorNameKey);
product = CFDictionaryGetValue(deviceInfo, kDRDeviceProductNameKey);
cfname = CFStringCreateWithFormat(NULL, NULL,
CFSTR("%@%@ %@"),
bus, vendor, product);
result = PortBurn_CStringFromCFString(cfname);
CFRelease(cfname);
CFRelease(deviceInfo);
return result;
}
/* Open a particular device by index number. Returns 0 on success;
any nonzero value indicates an error, for example if the device is
already open by some other program. */
int PortBurn_OpenDevice(void *handle, int index)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return pbErrNoHandle;
if (!h->deviceList)
return pbErrMustCallGetNumDevices;
h->device = (DRDeviceRef)CFArrayGetValueAtIndex(h->deviceList, index);
/* This just indicates interest; it doesn't return an error. */
DRDeviceAcquireMediaReservation(h->device);
return pbSuccess;
}
/* Close a device */
int PortBurn_CloseDevice(void *handle)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return pbErrNoHandle;
if (!h->device)
return pbErrDeviceNotOpen;
if (h->burn != NULL) {
CFRelease(h->burn);
h->burn = NULL;
}
if (h->trackArray) {
CFRelease(h->trackArray);
h->trackArray = NULL;
}
DRDeviceReleaseMediaReservation(h->device);
CFRelease(h->device);
h->device = NULL;
if (h->staging) {
PortBurn_CleanupStaging(h->staging);
h->staging = NULL;
}
return pbSuccess;
}
/* Eject the media in the currently opened device */
int PortBurn_EjectDevice(void *handle)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return pbErrNoHandle;
if (!h->device)
return pbErrDeviceNotOpen;
h->err = DRDeviceOpenTray(h->device);
if (noErr == h->err)
return pbSuccess; /* success */
h->err = DRDeviceEjectMedia(h->device);
if (noErr == h->err)
return pbSuccess; /* success */
return pbErrCannotEject;
}
/* This indicates you're ready to start staging audio data for the
currently opened device. At this point you are committing to
exclusive access to the CD burner, and this is the function that
will fail if another program is using the device, or if there is
no writable CD media in the device at this point.
You should pass in the path to a temporary directory that has at
least 700 MB of free space, to stage the audio, although note that
not all implementations will make use of this directory. */
int PortBurn_StartStaging(void *handle, const char *tmpdir)
{
PBHandle *h = (PBHandle *)handle;
CFDictionaryRef deviceStatus;
CFStringRef mediaState;
CFDictionaryRef mediaInfo;
CFBooleanRef blank;
CFBooleanRef writable;
CFBooleanRef reserved;
if (!h)
return pbErrNoHandle;
if (!h->device)
return pbErrDeviceNotOpen;
if (h->staging)
return pbErrAlreadyStagingOrBurning;
/* First we check to see that we have blank media in the drive. */
deviceStatus = DRDeviceCopyStatus(h->device);
mediaState = (CFStringRef)CFDictionaryGetValue(deviceStatus,
kDRDeviceMediaStateKey);
if (mediaState == NULL) {
CFRelease(deviceStatus);
return pbErrCannotAccessDevice;
}
if (!CFEqual(mediaState, kDRDeviceMediaStateMediaPresent)) {
CFRelease(deviceStatus);
return pbErrNoMediaInDrive;
}
mediaInfo = (CFDictionaryRef)CFDictionaryGetValue(deviceStatus,
kDRDeviceMediaInfoKey);
blank = (CFBooleanRef)CFDictionaryGetValue(mediaInfo,
kDRDeviceMediaIsBlankKey);
if (blank == NULL || !CFBooleanGetValue(blank)) {
CFRelease(deviceStatus);
return pbErrMediaIsNotBlankAndWritable;
}
writable = (CFBooleanRef)CFDictionaryGetValue(mediaInfo,
kDRDeviceMediaIsAppendableKey);
if (writable == NULL || !CFBooleanGetValue(writable)) {
CFRelease(deviceStatus);
return pbErrMediaIsNotBlankAndWritable;
}
reserved = (CFBooleanRef)CFDictionaryGetValue(mediaInfo,
kDRDeviceMediaIsReservedKey);
if (reserved == NULL || !CFBooleanGetValue(reserved)) {
CFRelease(deviceStatus);
return pbErrCannotReserveDevice;
}
CFRelease(deviceStatus);
h->staging = PortBurn_TempDirStaging(tmpdir);
if (!h->staging) {
return pbErrCannotCreateStagingDirectory;
}
h->trackArray = CFArrayCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeArrayCallBacks);
return pbSuccess;
}
/* Start a new audio track. Pass the name of the track, and the
length in CD Audio frames (each frame is 1/75.0 of a second, exactly). */
int PortBurn_StartTrack(void *handle, const char *name, int frames)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return pbErrNoHandle;
if (!h->staging)
return pbErrMustCallStartStaging;
if (0 == PortBurn_StartStagingTrack(h->staging, name, frames))
return pbSuccess;
else
return pbErrCannotCreateStagingFile;
}
/* Add one frame of audio to the current track. The buffer must be exactly
1176 elements long, containing interleaved left and right audio samples.
The values should be signed 16-bit numbers in the native endianness of
this computer. */
int PortBurn_AddFrame(void *handle, short *buffer)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return pbErrNoHandle;
if (!h->staging)
return pbErrMustCallStartTrack;
if (0 == PortBurn_AddStagingFrame(h->staging, buffer))
return pbSuccess;
else
return pbErrCannotWriteToStagingFile;
}
/* Finish the current audio track. */
int PortBurn_EndTrack(void *handle)
{
PBHandle *h = (PBHandle *)handle;
DRAudioTrackRef track;
Boolean isDirectory;
const char *filename;
FSRef fsref;
int index;
if (!h)
return pbErrNoHandle;
if (!h->staging)
return pbErrMustCallStartStaging;
if (0 != PortBurn_EndStagingTrack(h->staging))
return pbErrCannotStageTrack;
index = PortBurn_GetNumStagedTracks(h->staging);
if (index <= 0)
return pbErrCannotStageTrack;
filename = PortBurn_GetStagedFilename(h->staging, index - 1);
printf("Filename: '%s'\n", filename);
h->err = FSPathMakeRef((const UInt8*)filename, &fsref, &isDirectory);
if (h->err != noErr)
return pbErrCannotAccessStagedFile;
if (isDirectory)
return pbErrCannotAccessStagedFile;
track = DRAudioTrackCreate(&fsref);
CFArrayAppendValue(h->trackArray, track);
CFRelease(track);
if (track)
return pbSuccess;
else
return pbErrCannotUseStagedFileForBurning;
}
/* Begin burning the disc. */
int PortBurn_StartBurning(void *handle)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return pbErrNoHandle;
if (!h->device)
return pbErrDeviceNotOpen;
h->burn = DRBurnCreate(h->device);
if (!h->burn)
return pbErrCannotPrepareToBurn;
h->err = DRBurnWriteLayout(h->burn, h->trackArray);
if (h->err != noErr) {
DRBurnAbort(h->burn);
CFRelease(h->burn);
h->burn = NULL;
return pbErrCannotStartBurning;
}
h->frac = 0.0;
return pbSuccess;
}
/* Cancel if burning was in progress. It might take a while for
this to take effect; wait until GetStatus says 1.0 to close
the device. */
int PortBurn_CancelBurning(void *handle)
{
PBHandle *h = (PBHandle *)handle;
if (!h)
return pbErrNoHandle;
if (!h->burn)
return pbErrNotCurrentlyBurning;
DRBurnAbort(h->burn);
return pbSuccess;
}
/* During burning, returns the fraction complete in the given
float pointer, from 0.0 to 1.0. If this function returns
nonzero, the disc burning has failed and should be aborted.
If *out_fraction_complete is equal to 1.0, the burning is done;
you can call PortBurn_CloseDevice.
*/
int PortBurn_GetStatus(void *handle, float *out_fraction_complete)
{
PBHandle *h = (PBHandle *)handle;
CFDictionaryRef status;
CFStringRef stateRef;
CFNumberRef fracRef;
CFNumberRef trackNumRef;
float frac = 0.0;
int trackNum = 0;
*out_fraction_complete = h->frac;
if (!h)
return pbErrNoHandle;
if (!h->burn)
return pbErrNotCurrentlyBurning;
status = DRBurnCopyStatus(h->burn);
if (!status)
return pbErrCannotGetBurnStatus;
trackNumRef = (CFNumberRef)
CFDictionaryGetValue(status, kDRStatusCurrentTrackKey);
if (trackNumRef != NULL) {
CFNumberGetValue(trackNumRef, kCFNumberIntType, &trackNum);
}
fracRef = (CFNumberRef)
CFDictionaryGetValue(status, kDRStatusPercentCompleteKey);
if (fracRef != NULL) {
CFNumberGetValue(fracRef, kCFNumberFloatType, &frac);
}
stateRef = (CFStringRef)
CFDictionaryGetValue(status, kDRStatusStateKey);
if (stateRef == NULL) {
/* Stick with the last percentage */
return pbSuccess;
}
if (CFEqual(stateRef, kDRStatusStateNone)) {
/* Stick with the last percentage */
return pbSuccess;
}
if (CFEqual(stateRef, kDRStatusStatePreparing)) {
/* This takes about 1 second */
h->frac = 0.01;
}
if (CFEqual(stateRef, kDRStatusStateSessionOpen)) {
/* This takes about 3 seconds */
h->frac = 0.02;
}
if (CFEqual(stateRef, kDRStatusStateTrackOpen) ||
CFEqual(stateRef, kDRStatusStateTrackWrite) ||
CFEqual(stateRef, kDRStatusStateTrackClose) ||
CFEqual(stateRef, kDRStatusStateSessionClose)) {
/* The fraction ("percentage") complete will be valid during
the majority of this range */
float newFrac = 0.0;
if (frac > 0.0 && frac <= 1.0)
newFrac = frac;
/* Scale it to the range 0.05 - 0.99 */
newFrac = 0.05 + 0.94 * newFrac;
/* Only use that value if it's larger than the previous value */
if (newFrac > h->frac)
h->frac = newFrac;
}
if (CFEqual(stateRef, kDRStatusStateDone)) {
/* Returning a fraction complete of 1.0 means we're done! */
h->frac = 1.0;
}
if (CFEqual(stateRef, kDRStatusStateFailed)) {
CFRelease(status);
return pbErrBurnFailed;
}
*out_fraction_complete = h->frac;
CFRelease(status);
return pbSuccess;
}
|