xref: /illumos-gate/usr/src/cmd/cdrw/write_audio.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 #include <string.h>
27 #include <stdlib.h>
28 #include <libintl.h>
29 
30 #include "bstream.h"
31 #include "trackio.h"
32 #include "misc_scsi.h"
33 #include "util.h"
34 #include "msgs.h"
35 #include "main.h"
36 #include "trackio.h"
37 #include "mmc.h"
38 
39 static  bstreamhandle
open_audio(char * fname)40 open_audio(char *fname)
41 {
42 	int at;
43 	char *ext;
44 
45 	/* No audio type specified, look at extension */
46 	if (audio_type == AUDIO_TYPE_NONE) {
47 		ext = (char *)(strrchr(fname, '.'));
48 		if (ext) {
49 			ext++;
50 		}
51 		if ((ext == NULL) || ((at = get_audio_type(ext)) == -1)) {
52 			err_msg(gettext(
53 			    "Cannot understand file extension for %s\n"),
54 			    fname);
55 			exit(1);
56 		}
57 	} else {
58 		at = audio_type;
59 	}
60 	if (at == AUDIO_TYPE_SUN)
61 		return (open_au_read_stream(fname));
62 	if (at == AUDIO_TYPE_WAV)
63 		return (open_wav_read_stream(fname));
64 	if (at == AUDIO_TYPE_CDA)
65 		return (open_file_read_stream(fname));
66 	if (at == AUDIO_TYPE_AUR)
67 		return (open_aur_read_stream(fname));
68 	return (NULL);
69 }
70 
71 void
write_audio(char ** argv,int start_argc,int argc)72 write_audio(char **argv, int start_argc, int argc)
73 {
74 	bstreamhandle *h_ptr;
75 	int i, nfiles;
76 	struct track_info *ti;
77 	uint32_t blks_req, blks_avail;
78 	off_t fsize;
79 
80 	/* number of tracks to write */
81 	nfiles = argc - start_argc;
82 	h_ptr = (bstreamhandle *)my_zalloc(nfiles * sizeof (bstreamhandle));
83 	blks_req = 0;
84 	for (i = 0; i < nfiles; i++) {
85 		h_ptr[i] = open_audio(argv[start_argc + i]);
86 		if (h_ptr[i] == NULL) {
87 			err_msg(gettext("Cannot open %s: %s\n"),
88 			    argv[start_argc + i], get_err_str());
89 			exit(1);
90 		}
91 		(void) (h_ptr[i])->bstr_size(h_ptr[i], &fsize);
92 
93 		/* 2352 bytes per block, 75 blocks per second */
94 		blks_req += 150 + fsize/2352; /* 2 sec gap per track */
95 		if (fsize % 2352)
96 			blks_req++;
97 	}
98 	(void) check_device(target, CHECK_DEVICE_NOT_READY |
99 	    CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
100 	    EXIT_IF_CHECK_FAILED);
101 
102 	/* Put the device in track-at-once mode */
103 	write_init(TRACK_MODE_AUDIO);
104 	ti = (struct track_info *)my_zalloc(sizeof (*ti));
105 
106 	/* Build information for next invisible track, -1 */
107 	if ((build_track_info(target, -1, ti) == 0) ||
108 	    ((ti->ti_flags & TI_NWA_VALID) == 0)) {
109 		err_msg(gettext(
110 		    "Cannot get writable address for the media.\n"));
111 		exit(1);
112 	}
113 	if ((blks_avail = get_last_possible_lba(target)) == 0) {
114 		err_msg(gettext("Unable to determine media capacity. "
115 		    "Defaulting to 650 MB (74 minute) disc.\n"));
116 		blks_avail = MAX_CD_BLKS;
117 	} else {
118 		/* LBA is always one less */
119 		blks_avail++;
120 	}
121 	/*
122 	 * Actual number of blocks available based on nwa (next writable
123 	 * address) since there may already be information on the disc.
124 	 */
125 
126 	blks_avail -= ti->ti_nwa;
127 	if (blks_avail < blks_req) {
128 		err_msg(gettext("Insufficient space on the media.\n"));
129 		exit(1);
130 	}
131 	for (i = 0; i < nfiles; i++) {
132 		write_next_track(TRACK_MODE_AUDIO, h_ptr[i]);
133 		if (simulation && (nfiles != 1)) {
134 			(void) printf(gettext(
135 			    "Simulation mode : skipping remaining tracks\n"));
136 			break;
137 		}
138 	}
139 	for (i = 0; i < nfiles; i++)
140 		(h_ptr[i])->bstr_close(h_ptr[i]);
141 	free(ti);
142 	free(h_ptr);
143 
144 	write_fini();
145 
146 	fini_device(target);
147 	exit(0);
148 }
149