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