xref: /titanic_44/usr/src/cmd/audio/utilities/AudioRawPipe.cc (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 1991-2003 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 <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <AudioRawPipe.h>
34*7c478bd9Sstevel@tonic-gate #include <libaudio.h>
35*7c478bd9Sstevel@tonic-gate #include <audio_hdr.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate // class AudioPipe methods
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate // Constructor with file descriptor, mode, and optional name
40*7c478bd9Sstevel@tonic-gate AudioRawPipe::
AudioRawPipe(const int desc,const FileAccess acc,const AudioHdr & hdr_local,const char * name_local,const off_t off)41*7c478bd9Sstevel@tonic-gate AudioRawPipe(
42*7c478bd9Sstevel@tonic-gate 	const int		desc,		// file descriptor
43*7c478bd9Sstevel@tonic-gate 	const FileAccess	acc,		// access mode
44*7c478bd9Sstevel@tonic-gate 	const AudioHdr&		hdr_local,	// header
45*7c478bd9Sstevel@tonic-gate 	const char		*name_local,	// name
46*7c478bd9Sstevel@tonic-gate 	const off_t		off		// offset
47*7c478bd9Sstevel@tonic-gate ):AudioPipe(desc, acc, name_local), offset(off)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	isopened = FALSE;
50*7c478bd9Sstevel@tonic-gate 	setfd(desc);
51*7c478bd9Sstevel@tonic-gate 	SetHeader(hdr_local);
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate // The create routine for pipes writes a file header
55*7c478bd9Sstevel@tonic-gate AudioError AudioRawPipe::
Create()56*7c478bd9Sstevel@tonic-gate Create()
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	AudioError	err;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	// Was the header properly set?
61*7c478bd9Sstevel@tonic-gate 	err = GetHeader().Validate();
62*7c478bd9Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS)
63*7c478bd9Sstevel@tonic-gate 		return (RaiseError(err));
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	// Open fd supplied by constructor
66*7c478bd9Sstevel@tonic-gate 	if (!isfdset() || opened()) {
67*7c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	// set flag for opened() test
71*7c478bd9Sstevel@tonic-gate 	isopened = TRUE;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	// Set the actual output length to zero
74*7c478bd9Sstevel@tonic-gate 	setlength(0.);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate // The open routine for raw pipes validates the header and
80*7c478bd9Sstevel@tonic-gate // init's the read pos to offset and sets the opened flag.
81*7c478bd9Sstevel@tonic-gate AudioError AudioRawPipe::
Open()82*7c478bd9Sstevel@tonic-gate Open()
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	AudioError	err;
85*7c478bd9Sstevel@tonic-gate 	struct stat	st;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	// The constructor should have supplied a valid fd
88*7c478bd9Sstevel@tonic-gate 	// If fd is not open, or file header already decoded, skip it
89*7c478bd9Sstevel@tonic-gate 	if (!isfdset() || opened())
90*7c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	// Stat the file, to see if it is a regular file
93*7c478bd9Sstevel@tonic-gate 	if (fstat(getfd(), &st) < 0)
94*7c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_UNIXERROR));
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	// check validity of file header
97*7c478bd9Sstevel@tonic-gate 	err = GetHeader().Validate();
98*7c478bd9Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS) {
99*7c478bd9Sstevel@tonic-gate 		(void) close(getfd());
100*7c478bd9Sstevel@tonic-gate 		setfd(-1);
101*7c478bd9Sstevel@tonic-gate 		return (err);
102*7c478bd9Sstevel@tonic-gate 	}
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	// Only trust the file size for regular files
105*7c478bd9Sstevel@tonic-gate 	if (S_ISREG(st.st_mode)) {
106*7c478bd9Sstevel@tonic-gate 		// for raw files - no hdr, so it's the whole file minus
107*7c478bd9Sstevel@tonic-gate 		// the offset.
108*7c478bd9Sstevel@tonic-gate 		setlength(GetHeader().Bytes_to_Time(st.st_size - offset));
109*7c478bd9Sstevel@tonic-gate 	} else {
110*7c478bd9Sstevel@tonic-gate 		// don't know ...
111*7c478bd9Sstevel@tonic-gate 		setlength(AUDIO_UNKNOWN_TIME);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	// set flag for opened() test
115*7c478bd9Sstevel@tonic-gate 	isopened = TRUE;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	err = SetOffset(offset);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	// reset logical position to 0.0, since this is, in effect,
120*7c478bd9Sstevel@tonic-gate 	// the beginning of the file.
121*7c478bd9Sstevel@tonic-gate 	SetReadPosition(0.0, Absolute);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	return (err);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate Boolean AudioRawPipe::
opened() const127*7c478bd9Sstevel@tonic-gate opened() const
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	return (isopened);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate AudioError AudioRawPipe::
SetOffset(off_t val)133*7c478bd9Sstevel@tonic-gate SetOffset(off_t val)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	off_t		setting = 0;
136*7c478bd9Sstevel@tonic-gate 	AudioError	err;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	// only read only files for now
139*7c478bd9Sstevel@tonic-gate 	if (GetAccess().Writeable()) {
140*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	// only allow this if we haven't read anything yet (i.e. current
144*7c478bd9Sstevel@tonic-gate 	// position is 0).
145*7c478bd9Sstevel@tonic-gate 	if (ReadPosition() != 0.) {
146*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if ((err = seekread(GetHeader().Bytes_to_Time(val), setting))
150*7c478bd9Sstevel@tonic-gate 	    != AUDIO_SUCCESS) {
151*7c478bd9Sstevel@tonic-gate 		return (err);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	// this should *never* happen 'cause seekread just sets setting
155*7c478bd9Sstevel@tonic-gate 	// to GetHeader().Time_to_Bytes....
156*7c478bd9Sstevel@tonic-gate 	if (setting != val) {
157*7c478bd9Sstevel@tonic-gate 		// don't really know what error is apropos for this.
158*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_BADFRAME);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	offset = val;
162*7c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate off_t AudioRawPipe::
GetOffset() const166*7c478bd9Sstevel@tonic-gate GetOffset() const
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	return (offset);
169*7c478bd9Sstevel@tonic-gate }
170