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