xref: /onnv-gate/usr/src/cmd/audio/utilities/AudioPipe.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 (c) 1990-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
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 <AudioPipe.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate // class AudioPipe methods
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate // Constructor with file descriptor, mode, and optional name
37*0Sstevel@tonic-gate AudioPipe::
AudioPipe(const int desc,const FileAccess acc,const char * name_local)38*0Sstevel@tonic-gate AudioPipe(
39*0Sstevel@tonic-gate 	const int		desc,		// file descriptor
40*0Sstevel@tonic-gate 	const FileAccess	acc,		// access mode
41*0Sstevel@tonic-gate 	const char		*name_local):	// name
42*0Sstevel@tonic-gate 	AudioUnixfile(name_local, acc)
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate 	setfd(desc);
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate // The create routine for pipes writes a file header
48*0Sstevel@tonic-gate AudioError AudioPipe::
Create()49*0Sstevel@tonic-gate Create()
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	AudioError	err;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	// Was the header properly set?
54*0Sstevel@tonic-gate 	err = GetHeader().Validate();
55*0Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS)
56*0Sstevel@tonic-gate 		return (RaiseError(err));
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	// Open fd supplied by constructor
59*0Sstevel@tonic-gate 	if (!isfdset())
60*0Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT));
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	// Write the file header with current (usually unknown) size
63*0Sstevel@tonic-gate 	err = encode_filehdr();
64*0Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS) {
65*0Sstevel@tonic-gate 		(void) close(getfd());		// If error, remove file
66*0Sstevel@tonic-gate 		setfd(-1);
67*0Sstevel@tonic-gate 		return (err);
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	// Set the actual output length to zero
71*0Sstevel@tonic-gate 	setlength(0.);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate // The open routine for pipes decodes the header
77*0Sstevel@tonic-gate AudioError AudioPipe::
Open()78*0Sstevel@tonic-gate Open()
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	AudioError		err;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	// The constructor should have supplied a valid fd
83*0Sstevel@tonic-gate 	if (!isfdset())
84*0Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT));
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	// Decode a file header
87*0Sstevel@tonic-gate 	err = decode_filehdr();
88*0Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS) {
89*0Sstevel@tonic-gate 		(void) close(getfd());
90*0Sstevel@tonic-gate 		setfd(-1);
91*0Sstevel@tonic-gate 		return (err);
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate // Read data from underlying pipe into specified buffer.
98*0Sstevel@tonic-gate // No data format translation takes place.
99*0Sstevel@tonic-gate // Since there's no going back, the object's read position pointer is updated.
100*0Sstevel@tonic-gate AudioError AudioPipe::
ReadData(void * buf,size_t & len,Double & pos)101*0Sstevel@tonic-gate ReadData(
102*0Sstevel@tonic-gate 	void*		buf,		// destination buffer address
103*0Sstevel@tonic-gate 	size_t&		len,		// buffer length (updated)
104*0Sstevel@tonic-gate 	Double&		pos)		// start position (updated)
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate 	AudioError	err;
107*0Sstevel@tonic-gate 	char		*tbuf;		// current buffer pointer
108*0Sstevel@tonic-gate 	size_t		remain;		// number of bytes remaining
109*0Sstevel@tonic-gate 	size_t		cnt;		// accumulated number of bytes read
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	tbuf = (char *)buf;
112*0Sstevel@tonic-gate 	remain = len;
113*0Sstevel@tonic-gate 	cnt = 0;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	// Pipes return short reads.  If non-blocking i/o, try to read all.
116*0Sstevel@tonic-gate 	do {
117*0Sstevel@tonic-gate 		// Call the real routine
118*0Sstevel@tonic-gate 		err = AudioUnixfile::ReadData((void*)tbuf, remain, pos);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 		// Update the object's read position
121*0Sstevel@tonic-gate 		if (!err) {
122*0Sstevel@tonic-gate 			(void) SetReadPosition(pos, Absolute);
123*0Sstevel@tonic-gate 			if (remain == 0)
124*0Sstevel@tonic-gate 				break;
125*0Sstevel@tonic-gate 			cnt += remain;
126*0Sstevel@tonic-gate 			tbuf += remain;
127*0Sstevel@tonic-gate 			remain = len - cnt;
128*0Sstevel@tonic-gate 		}
129*0Sstevel@tonic-gate 	} while (!err && (remain > 0) && GetBlocking());
130*0Sstevel@tonic-gate 	len = cnt;
131*0Sstevel@tonic-gate 	if (len > 0)
132*0Sstevel@tonic-gate 		return (AUDIO_SUCCESS);
133*0Sstevel@tonic-gate 	return (err);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate // Write data to underlying file from specified buffer.
137*0Sstevel@tonic-gate // No data format translation takes place.
138*0Sstevel@tonic-gate // Since there's no going back, the object's write position pointer is updated.
139*0Sstevel@tonic-gate AudioError AudioPipe::
WriteData(void * buf,size_t & len,Double & pos)140*0Sstevel@tonic-gate WriteData(
141*0Sstevel@tonic-gate 	void*		buf,		// source buffer address
142*0Sstevel@tonic-gate 	size_t&		len,		// buffer length (updated)
143*0Sstevel@tonic-gate 	Double&		pos)		// start position (updated)
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	AudioError	err;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	// Call the real routine
148*0Sstevel@tonic-gate 	err = AudioUnixfile::WriteData(buf, len, pos);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	// Update the object's write position
151*0Sstevel@tonic-gate 	if (err == AUDIO_SUCCESS)
152*0Sstevel@tonic-gate 		(void) SetWritePosition(pos, Absolute);
153*0Sstevel@tonic-gate 	return (err);
154*0Sstevel@tonic-gate }
155