xref: /titanic_51/usr/src/cmd/cdrw/write_audio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <libintl.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "bstream.h"
34*7c478bd9Sstevel@tonic-gate #include "trackio.h"
35*7c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
36*7c478bd9Sstevel@tonic-gate #include "util.h"
37*7c478bd9Sstevel@tonic-gate #include "msgs.h"
38*7c478bd9Sstevel@tonic-gate #include "main.h"
39*7c478bd9Sstevel@tonic-gate #include "trackio.h"
40*7c478bd9Sstevel@tonic-gate #include "mmc.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate static  bstreamhandle
43*7c478bd9Sstevel@tonic-gate open_audio(char *fname)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	int at;
46*7c478bd9Sstevel@tonic-gate 	char *ext;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 	/* No audio type specified, look at extension */
49*7c478bd9Sstevel@tonic-gate 	if (audio_type == AUDIO_TYPE_NONE) {
50*7c478bd9Sstevel@tonic-gate 		ext = (char *)(strrchr(fname, '.'));
51*7c478bd9Sstevel@tonic-gate 		if (ext) {
52*7c478bd9Sstevel@tonic-gate 			ext++;
53*7c478bd9Sstevel@tonic-gate 		}
54*7c478bd9Sstevel@tonic-gate 		if ((ext == NULL) || ((at = get_audio_type(ext)) == -1)) {
55*7c478bd9Sstevel@tonic-gate 			err_msg(gettext(
56*7c478bd9Sstevel@tonic-gate 			    "Cannot understand file extension for %s\n"),
57*7c478bd9Sstevel@tonic-gate 			    fname);
58*7c478bd9Sstevel@tonic-gate 			exit(1);
59*7c478bd9Sstevel@tonic-gate 		}
60*7c478bd9Sstevel@tonic-gate 	} else {
61*7c478bd9Sstevel@tonic-gate 		at = audio_type;
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_SUN)
64*7c478bd9Sstevel@tonic-gate 		return (open_au_read_stream(fname));
65*7c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_WAV)
66*7c478bd9Sstevel@tonic-gate 		return (open_wav_read_stream(fname));
67*7c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_CDA)
68*7c478bd9Sstevel@tonic-gate 		return (open_file_read_stream(fname));
69*7c478bd9Sstevel@tonic-gate 	if (at == AUDIO_TYPE_AUR)
70*7c478bd9Sstevel@tonic-gate 		return (open_aur_read_stream(fname));
71*7c478bd9Sstevel@tonic-gate 	return (NULL);
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate void
75*7c478bd9Sstevel@tonic-gate write_audio(char **argv, int start_argc, int argc)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	bstreamhandle *h_ptr;
78*7c478bd9Sstevel@tonic-gate 	int i, nfiles;
79*7c478bd9Sstevel@tonic-gate 	struct track_info *ti;
80*7c478bd9Sstevel@tonic-gate 	int blks_req, blks_avail;
81*7c478bd9Sstevel@tonic-gate 	off_t fsize;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	/* number of tracks to write */
84*7c478bd9Sstevel@tonic-gate 	nfiles = argc - start_argc;
85*7c478bd9Sstevel@tonic-gate 	h_ptr = (bstreamhandle *)my_zalloc(nfiles * sizeof (bstreamhandle));
86*7c478bd9Sstevel@tonic-gate 	blks_req = 0;
87*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfiles; i++) {
88*7c478bd9Sstevel@tonic-gate 		h_ptr[i] = open_audio(argv[start_argc + i]);
89*7c478bd9Sstevel@tonic-gate 		if (h_ptr[i] == NULL) {
90*7c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot open %s: %s\n"),
91*7c478bd9Sstevel@tonic-gate 			    argv[start_argc + i], get_err_str());
92*7c478bd9Sstevel@tonic-gate 			exit(1);
93*7c478bd9Sstevel@tonic-gate 		}
94*7c478bd9Sstevel@tonic-gate 		(void) (h_ptr[i])->bstr_size(h_ptr[i], &fsize);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 		/* 2352 bytes per block, 75 blocks per second */
97*7c478bd9Sstevel@tonic-gate 		blks_req += 150 + fsize/2352; /* 2 sec gap per track */
98*7c478bd9Sstevel@tonic-gate 		if (fsize % 2352)
99*7c478bd9Sstevel@tonic-gate 			blks_req++;
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	(void) check_device(target, CHECK_DEVICE_NOT_READY |
102*7c478bd9Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
103*7c478bd9Sstevel@tonic-gate 	    EXIT_IF_CHECK_FAILED);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	/* Put the device in track-at-once mode */
106*7c478bd9Sstevel@tonic-gate 	write_init(TRACK_MODE_AUDIO);
107*7c478bd9Sstevel@tonic-gate 	ti = (struct track_info *)my_zalloc(TRACK_INFO_SIZE);
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	/* Build information for next invisible track, -1 */
110*7c478bd9Sstevel@tonic-gate 	if ((build_track_info(target, -1, ti) == 0) ||
111*7c478bd9Sstevel@tonic-gate 		((ti->ti_flags & TI_NWA_VALID) == 0)) {
112*7c478bd9Sstevel@tonic-gate 		err_msg(gettext(
113*7c478bd9Sstevel@tonic-gate 		    "Cannot get writable address for the media.\n"));
114*7c478bd9Sstevel@tonic-gate 		exit(1);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	if (use_media_stated_capacity) {
117*7c478bd9Sstevel@tonic-gate 		blks_avail = get_last_possible_lba(target);
118*7c478bd9Sstevel@tonic-gate 		if (blks_avail == 0) {
119*7c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot find out media capacity\n"));
120*7c478bd9Sstevel@tonic-gate 			exit(1);
121*7c478bd9Sstevel@tonic-gate 		}
122*7c478bd9Sstevel@tonic-gate 		/* LBA is always one less */
123*7c478bd9Sstevel@tonic-gate 		blks_avail++;
124*7c478bd9Sstevel@tonic-gate 	} else {
125*7c478bd9Sstevel@tonic-gate 		blks_avail = MAX_CD_BLKS;
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 	/*
128*7c478bd9Sstevel@tonic-gate 	 * Actual number of blocks available based on nwa (next writable
129*7c478bd9Sstevel@tonic-gate 	 * address) since there may already be information on the disc.
130*7c478bd9Sstevel@tonic-gate 	 */
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	blks_avail -= ti->ti_nwa;
133*7c478bd9Sstevel@tonic-gate 	if (blks_avail < blks_req) {
134*7c478bd9Sstevel@tonic-gate 		err_msg(gettext("Insufficient space on the media.\n"));
135*7c478bd9Sstevel@tonic-gate 		exit(1);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfiles; i++) {
138*7c478bd9Sstevel@tonic-gate 		write_next_track(TRACK_MODE_AUDIO, h_ptr[i]);
139*7c478bd9Sstevel@tonic-gate 		if (simulation && (nfiles != 1)) {
140*7c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
141*7c478bd9Sstevel@tonic-gate 			    "Simulation mode : skipping remaining tracks\n"));
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfiles; i++)
146*7c478bd9Sstevel@tonic-gate 		(h_ptr[i])->bstr_close(h_ptr[i]);
147*7c478bd9Sstevel@tonic-gate 	free(ti);
148*7c478bd9Sstevel@tonic-gate 	free(h_ptr);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	write_fini();
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	fini_device(target);
153*7c478bd9Sstevel@tonic-gate 	exit(0);
154*7c478bd9Sstevel@tonic-gate }
155