xref: /titanic_54/usr/src/cmd/cdrw/write_audio.c (revision 6e16840296a75e42faa7d22cc93f56e4e8e2d301)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*6e168402Sarutz  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "bstream.h"
347c478bd9Sstevel@tonic-gate #include "trackio.h"
357c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
367c478bd9Sstevel@tonic-gate #include "util.h"
377c478bd9Sstevel@tonic-gate #include "msgs.h"
387c478bd9Sstevel@tonic-gate #include "main.h"
397c478bd9Sstevel@tonic-gate #include "trackio.h"
407c478bd9Sstevel@tonic-gate #include "mmc.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static  bstreamhandle
437c478bd9Sstevel@tonic-gate open_audio(char *fname)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	int at;
467c478bd9Sstevel@tonic-gate 	char *ext;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	/* No audio type specified, look at extension */
497c478bd9Sstevel@tonic-gate 	if (audio_type == AUDIO_TYPE_NONE) {
507c478bd9Sstevel@tonic-gate 		ext = (char *)(strrchr(fname, '.'));
517c478bd9Sstevel@tonic-gate 		if (ext) {
527c478bd9Sstevel@tonic-gate 			ext++;
537c478bd9Sstevel@tonic-gate 		}
547c478bd9Sstevel@tonic-gate 		if ((ext == NULL) || ((at = get_audio_type(ext)) == -1)) {
557c478bd9Sstevel@tonic-gate 			err_msg(gettext(
567c478bd9Sstevel@tonic-gate 			    "Cannot understand file extension for %s\n"),
577c478bd9Sstevel@tonic-gate 			    fname);
587c478bd9Sstevel@tonic-gate 			exit(1);
597c478bd9Sstevel@tonic-gate 		}
607c478bd9Sstevel@tonic-gate 	} else {
617c478bd9Sstevel@tonic-gate 		at = audio_type;
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_SUN)
647c478bd9Sstevel@tonic-gate 		return (open_au_read_stream(fname));
657c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_WAV)
667c478bd9Sstevel@tonic-gate 		return (open_wav_read_stream(fname));
677c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_CDA)
687c478bd9Sstevel@tonic-gate 		return (open_file_read_stream(fname));
697c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_AUR)
707c478bd9Sstevel@tonic-gate 		return (open_aur_read_stream(fname));
717c478bd9Sstevel@tonic-gate 	return (NULL);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate void
757c478bd9Sstevel@tonic-gate write_audio(char **argv, int start_argc, int argc)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	bstreamhandle *h_ptr;
787c478bd9Sstevel@tonic-gate 	int i, nfiles;
797c478bd9Sstevel@tonic-gate 	struct track_info *ti;
807c478bd9Sstevel@tonic-gate 	int blks_req, blks_avail;
817c478bd9Sstevel@tonic-gate 	off_t fsize;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/* number of tracks to write */
847c478bd9Sstevel@tonic-gate 	nfiles = argc - start_argc;
857c478bd9Sstevel@tonic-gate 	h_ptr = (bstreamhandle *)my_zalloc(nfiles * sizeof (bstreamhandle));
867c478bd9Sstevel@tonic-gate 	blks_req = 0;
877c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfiles; i++) {
887c478bd9Sstevel@tonic-gate 		h_ptr[i] = open_audio(argv[start_argc + i]);
897c478bd9Sstevel@tonic-gate 		if (h_ptr[i] == NULL) {
907c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot open %s: %s\n"),
917c478bd9Sstevel@tonic-gate 			    argv[start_argc + i], get_err_str());
927c478bd9Sstevel@tonic-gate 			exit(1);
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 		(void) (h_ptr[i])->bstr_size(h_ptr[i], &fsize);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		/* 2352 bytes per block, 75 blocks per second */
977c478bd9Sstevel@tonic-gate 		blks_req += 150 + fsize/2352; /* 2 sec gap per track */
987c478bd9Sstevel@tonic-gate 		if (fsize % 2352)
997c478bd9Sstevel@tonic-gate 			blks_req++;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	(void) check_device(target, CHECK_DEVICE_NOT_READY |
1027c478bd9Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
1037c478bd9Sstevel@tonic-gate 	    EXIT_IF_CHECK_FAILED);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* Put the device in track-at-once mode */
1067c478bd9Sstevel@tonic-gate 	write_init(TRACK_MODE_AUDIO);
107*6e168402Sarutz 	ti = (struct track_info *)my_zalloc(sizeof (*ti));
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* Build information for next invisible track, -1 */
1107c478bd9Sstevel@tonic-gate 	if ((build_track_info(target, -1, ti) == 0) ||
1117c478bd9Sstevel@tonic-gate 		((ti->ti_flags & TI_NWA_VALID) == 0)) {
1127c478bd9Sstevel@tonic-gate 		err_msg(gettext(
1137c478bd9Sstevel@tonic-gate 		    "Cannot get writable address for the media.\n"));
1147c478bd9Sstevel@tonic-gate 		exit(1);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	if (use_media_stated_capacity) {
1177c478bd9Sstevel@tonic-gate 		blks_avail = get_last_possible_lba(target);
1187c478bd9Sstevel@tonic-gate 		if (blks_avail == 0) {
1197c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot find out media capacity\n"));
1207c478bd9Sstevel@tonic-gate 			exit(1);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 		/* LBA is always one less */
1237c478bd9Sstevel@tonic-gate 		blks_avail++;
1247c478bd9Sstevel@tonic-gate 	} else {
1257c478bd9Sstevel@tonic-gate 		blks_avail = MAX_CD_BLKS;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * Actual number of blocks available based on nwa (next writable
1297c478bd9Sstevel@tonic-gate 	 * address) since there may already be information on the disc.
1307c478bd9Sstevel@tonic-gate 	 */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	blks_avail -= ti->ti_nwa;
1337c478bd9Sstevel@tonic-gate 	if (blks_avail < blks_req) {
1347c478bd9Sstevel@tonic-gate 		err_msg(gettext("Insufficient space on the media.\n"));
1357c478bd9Sstevel@tonic-gate 		exit(1);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfiles; i++) {
1387c478bd9Sstevel@tonic-gate 		write_next_track(TRACK_MODE_AUDIO, h_ptr[i]);
1397c478bd9Sstevel@tonic-gate 		if (simulation && (nfiles != 1)) {
1407c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
1417c478bd9Sstevel@tonic-gate 			    "Simulation mode : skipping remaining tracks\n"));
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfiles; i++)
1467c478bd9Sstevel@tonic-gate 		(h_ptr[i])->bstr_close(h_ptr[i]);
1477c478bd9Sstevel@tonic-gate 	free(ti);
1487c478bd9Sstevel@tonic-gate 	free(h_ptr);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	write_fini();
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	fini_device(target);
1537c478bd9Sstevel@tonic-gate 	exit(0);
1547c478bd9Sstevel@tonic-gate }
155