xref: /onnv-gate/usr/src/cmd/audio/utilities/AudioRawPipe.cc (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1991-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <unistd.h>
31*0Sstevel@tonic-gate #include <sys/stat.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <AudioRawPipe.h>
34*0Sstevel@tonic-gate #include <libaudio.h>
35*0Sstevel@tonic-gate #include <audio_hdr.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate // class AudioPipe methods
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate // Constructor with file descriptor, mode, and optional name
40*0Sstevel@tonic-gate AudioRawPipe::
AudioRawPipe(const int desc,const FileAccess acc,const AudioHdr & hdr_local,const char * name_local,const off_t off)41*0Sstevel@tonic-gate AudioRawPipe(
42*0Sstevel@tonic-gate 	const int		desc,		// file descriptor
43*0Sstevel@tonic-gate 	const FileAccess	acc,		// access mode
44*0Sstevel@tonic-gate 	const AudioHdr&		hdr_local,	// header
45*0Sstevel@tonic-gate 	const char		*name_local,	// name
46*0Sstevel@tonic-gate 	const off_t		off		// offset
47*0Sstevel@tonic-gate ):AudioPipe(desc, acc, name_local), offset(off)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	isopened = FALSE;
50*0Sstevel@tonic-gate 	setfd(desc);
51*0Sstevel@tonic-gate 	SetHeader(hdr_local);
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate // The create routine for pipes writes a file header
55*0Sstevel@tonic-gate AudioError AudioRawPipe::
Create()56*0Sstevel@tonic-gate Create()
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	AudioError	err;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 	// Was the header properly set?
61*0Sstevel@tonic-gate 	err = GetHeader().Validate();
62*0Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS)
63*0Sstevel@tonic-gate 		return (RaiseError(err));
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	// Open fd supplied by constructor
66*0Sstevel@tonic-gate 	if (!isfdset() || opened()) {
67*0Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	// set flag for opened() test
71*0Sstevel@tonic-gate 	isopened = TRUE;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	// Set the actual output length to zero
74*0Sstevel@tonic-gate 	setlength(0.);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate // The open routine for raw pipes validates the header and
80*0Sstevel@tonic-gate // init's the read pos to offset and sets the opened flag.
81*0Sstevel@tonic-gate AudioError AudioRawPipe::
Open()82*0Sstevel@tonic-gate Open()
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	AudioError	err;
85*0Sstevel@tonic-gate 	struct stat	st;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	// The constructor should have supplied a valid fd
88*0Sstevel@tonic-gate 	// If fd is not open, or file header already decoded, skip it
89*0Sstevel@tonic-gate 	if (!isfdset() || opened())
90*0Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	// Stat the file, to see if it is a regular file
93*0Sstevel@tonic-gate 	if (fstat(getfd(), &st) < 0)
94*0Sstevel@tonic-gate 		return (RaiseError(AUDIO_UNIXERROR));
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	// check validity of file header
97*0Sstevel@tonic-gate 	err = GetHeader().Validate();
98*0Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS) {
99*0Sstevel@tonic-gate 		(void) close(getfd());
100*0Sstevel@tonic-gate 		setfd(-1);
101*0Sstevel@tonic-gate 		return (err);
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	// Only trust the file size for regular files
105*0Sstevel@tonic-gate 	if (S_ISREG(st.st_mode)) {
106*0Sstevel@tonic-gate 		// for raw files - no hdr, so it's the whole file minus
107*0Sstevel@tonic-gate 		// the offset.
108*0Sstevel@tonic-gate 		setlength(GetHeader().Bytes_to_Time(st.st_size - offset));
109*0Sstevel@tonic-gate 	} else {
110*0Sstevel@tonic-gate 		// don't know ...
111*0Sstevel@tonic-gate 		setlength(AUDIO_UNKNOWN_TIME);
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	// set flag for opened() test
115*0Sstevel@tonic-gate 	isopened = TRUE;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	err = SetOffset(offset);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	// reset logical position to 0.0, since this is, in effect,
120*0Sstevel@tonic-gate 	// the beginning of the file.
121*0Sstevel@tonic-gate 	SetReadPosition(0.0, Absolute);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	return (err);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate Boolean AudioRawPipe::
opened() const127*0Sstevel@tonic-gate opened() const
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate 	return (isopened);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate AudioError AudioRawPipe::
SetOffset(off_t val)133*0Sstevel@tonic-gate SetOffset(off_t val)
134*0Sstevel@tonic-gate {
135*0Sstevel@tonic-gate 	off_t		setting = 0;
136*0Sstevel@tonic-gate 	AudioError	err;
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	// only read only files for now
139*0Sstevel@tonic-gate 	if (GetAccess().Writeable()) {
140*0Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	// only allow this if we haven't read anything yet (i.e. current
144*0Sstevel@tonic-gate 	// position is 0).
145*0Sstevel@tonic-gate 	if (ReadPosition() != 0.) {
146*0Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if ((err = seekread(GetHeader().Bytes_to_Time(val), setting))
150*0Sstevel@tonic-gate 	    != AUDIO_SUCCESS) {
151*0Sstevel@tonic-gate 		return (err);
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	// this should *never* happen 'cause seekread just sets setting
155*0Sstevel@tonic-gate 	// to GetHeader().Time_to_Bytes....
156*0Sstevel@tonic-gate 	if (setting != val) {
157*0Sstevel@tonic-gate 		// don't really know what error is apropos for this.
158*0Sstevel@tonic-gate 		return (AUDIO_ERR_BADFRAME);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	offset = val;
162*0Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate off_t AudioRawPipe::
GetOffset() const166*0Sstevel@tonic-gate GetOffset() const
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	return (offset);
169*0Sstevel@tonic-gate }
170