summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cdrw/write_audio.c
blob: bed1da8b49464c6b9f810dd9e625a29cf89fc474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <string.h>
#include <stdlib.h>
#include <libintl.h>

#include "bstream.h"
#include "trackio.h"
#include "misc_scsi.h"
#include "util.h"
#include "msgs.h"
#include "main.h"
#include "trackio.h"
#include "mmc.h"

static  bstreamhandle
open_audio(char *fname)
{
	int at;
	char *ext;

	/* No audio type specified, look at extension */
	if (audio_type == AUDIO_TYPE_NONE) {
		ext = (char *)(strrchr(fname, '.'));
		if (ext) {
			ext++;
		}
		if ((ext == NULL) || ((at = get_audio_type(ext)) == -1)) {
			err_msg(gettext(
			    "Cannot understand file extension for %s\n"),
			    fname);
			exit(1);
		}
	} else {
		at = audio_type;
	}
	if (at == AUDIO_TYPE_SUN)
		return (open_au_read_stream(fname));
	if (at == AUDIO_TYPE_WAV)
		return (open_wav_read_stream(fname));
	if (at == AUDIO_TYPE_CDA)
		return (open_file_read_stream(fname));
	if (at == AUDIO_TYPE_AUR)
		return (open_aur_read_stream(fname));
	return (NULL);
}

void
write_audio(char **argv, int start_argc, int argc)
{
	bstreamhandle *h_ptr;
	int i, nfiles;
	struct track_info *ti;
	uint32_t blks_req, blks_avail;
	off_t fsize;

	/* number of tracks to write */
	nfiles = argc - start_argc;
	h_ptr = (bstreamhandle *)my_zalloc(nfiles * sizeof (bstreamhandle));
	blks_req = 0;
	for (i = 0; i < nfiles; i++) {
		h_ptr[i] = open_audio(argv[start_argc + i]);
		if (h_ptr[i] == NULL) {
			err_msg(gettext("Cannot open %s: %s\n"),
			    argv[start_argc + i], get_err_str());
			exit(1);
		}
		(void) (h_ptr[i])->bstr_size(h_ptr[i], &fsize);

		/* 2352 bytes per block, 75 blocks per second */
		blks_req += 150 + fsize/2352; /* 2 sec gap per track */
		if (fsize % 2352)
			blks_req++;
	}
	(void) check_device(target, CHECK_DEVICE_NOT_READY |
	    CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
	    EXIT_IF_CHECK_FAILED);

	/* Put the device in track-at-once mode */
	write_init(TRACK_MODE_AUDIO);
	ti = (struct track_info *)my_zalloc(sizeof (*ti));

	/* Build information for next invisible track, -1 */
	if ((build_track_info(target, -1, ti) == 0) ||
	    ((ti->ti_flags & TI_NWA_VALID) == 0)) {
		err_msg(gettext(
		    "Cannot get writable address for the media.\n"));
		exit(1);
	}
	if ((blks_avail = get_last_possible_lba(target)) == 0) {
		err_msg(gettext("Unable to determine media capacity. "
		    "Defaulting to 650 MB (74 minute) disc.\n"));
		blks_avail = MAX_CD_BLKS;
	} else {
		/* LBA is always one less */
		blks_avail++;
	}
	/*
	 * Actual number of blocks available based on nwa (next writable
	 * address) since there may already be information on the disc.
	 */

	blks_avail -= ti->ti_nwa;
	if (blks_avail < blks_req) {
		err_msg(gettext("Insufficient space on the media.\n"));
		exit(1);
	}
	for (i = 0; i < nfiles; i++) {
		write_next_track(TRACK_MODE_AUDIO, h_ptr[i]);
		if (simulation && (nfiles != 1)) {
			(void) printf(gettext(
			    "Simulation mode : skipping remaining tracks\n"));
			break;
		}
	}
	for (i = 0; i < nfiles; i++)
		(h_ptr[i])->bstr_close(h_ptr[i]);
	free(ti);
	free(h_ptr);

	write_fini();

	fini_device(target);
	exit(0);
}