17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 1993-2003 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <memory.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include <AudioDebug.h>
427c478bd9Sstevel@tonic-gate #include <AudioUnixfile.h>
437c478bd9Sstevel@tonic-gate #include <libaudio.h>
447c478bd9Sstevel@tonic-gate #include <audio_hdr.h>
457c478bd9Sstevel@tonic-gate #include <audio/au.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate // class AudioUnixfile methods
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate // Constructor with pathname and mode arg
507c478bd9Sstevel@tonic-gate AudioUnixfile::
AudioUnixfile(const char * path,const FileAccess acc)517c478bd9Sstevel@tonic-gate AudioUnixfile(
527c478bd9Sstevel@tonic-gate const char *path, // pathname
537c478bd9Sstevel@tonic-gate const FileAccess acc): // access mode
547c478bd9Sstevel@tonic-gate AudioStream(path), fd(-1), block(TRUE), mode(acc),
557c478bd9Sstevel@tonic-gate infostring(new char[1]), infolength(1)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate infostring[0] = '\0';
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate // Destructor
617c478bd9Sstevel@tonic-gate AudioUnixfile::
~AudioUnixfile()627c478bd9Sstevel@tonic-gate ~AudioUnixfile()
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate // If the file is open, close it
657c478bd9Sstevel@tonic-gate if (opened())
667c478bd9Sstevel@tonic-gate (void) Close();
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate // Deallocate the dynamic storage
697c478bd9Sstevel@tonic-gate delete infostring;
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate // Generic open with search path routine just calls default Open()
737c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
OpenPath(const char *)747c478bd9Sstevel@tonic-gate OpenPath(
757c478bd9Sstevel@tonic-gate const char *)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate return (Open());
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate // Decode an audio file header
817c478bd9Sstevel@tonic-gate // This routine reads the audio file header and decodes it.
827c478bd9Sstevel@tonic-gate //
837c478bd9Sstevel@tonic-gate // This method should be specialized by subclasses that are not files,
847c478bd9Sstevel@tonic-gate // like devices for instance.
857c478bd9Sstevel@tonic-gate //
867c478bd9Sstevel@tonic-gate // XXX - this routine should be rewritten for C++
877c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
decode_filehdr()887c478bd9Sstevel@tonic-gate decode_filehdr()
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate Boolean saveblock; // saved state of the blocking i/o flag
917c478bd9Sstevel@tonic-gate AudioHdr hdr_local; // local copy of header
927c478bd9Sstevel@tonic-gate Audio_hdr ohdr; // XXX - old libaudio hdr
937c478bd9Sstevel@tonic-gate au_filehdr_t fhdr;
947c478bd9Sstevel@tonic-gate char *ibuf;
957c478bd9Sstevel@tonic-gate int file_type;
967c478bd9Sstevel@tonic-gate int infosize;
977c478bd9Sstevel@tonic-gate int cnt;
987c478bd9Sstevel@tonic-gate struct stat st;
997c478bd9Sstevel@tonic-gate AudioError err;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate // If fd is not open, or file header already decoded, skip it
1027c478bd9Sstevel@tonic-gate if (!isfdset() || opened())
1037c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate // Stat the file, to see if it is a regular file
1067c478bd9Sstevel@tonic-gate if (fstat(getfd(), &st) < 0)
1077c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate // Make sure the file is not set for blocking i/o
1107c478bd9Sstevel@tonic-gate saveblock = GetBlocking();
1117c478bd9Sstevel@tonic-gate if (!saveblock)
1127c478bd9Sstevel@tonic-gate SetBlocking(TRUE);
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate // Read the file header, but not the info field
1157c478bd9Sstevel@tonic-gate // XXX - Should use C++ input method
1167c478bd9Sstevel@tonic-gate cnt = read(getfd(), (char *)&fhdr, sizeof (fhdr));
1177c478bd9Sstevel@tonic-gate if (cnt != sizeof (fhdr)) {
1187c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate // Check the validity of the header and get the size of the info field
1227c478bd9Sstevel@tonic-gate err = (AudioError) audio_decode_filehdr(getfd(), (unsigned char *)&fhdr,
1237c478bd9Sstevel@tonic-gate &file_type, &ohdr, &infosize);
1247c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS)
1257c478bd9Sstevel@tonic-gate return (RaiseError(err));
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate // Allocate and read in the info field
1287c478bd9Sstevel@tonic-gate ibuf = new char[infosize];
1297c478bd9Sstevel@tonic-gate cnt = read(getfd(), ibuf, infosize);
1307c478bd9Sstevel@tonic-gate if (cnt != infosize) {
131*7cad4b84SToomas Soome delete[] ibuf;
1327c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate SetBlocking(saveblock); // Restore the saved blocking i/o state
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate // XXX - convert from libaudio header
1377c478bd9Sstevel@tonic-gate hdr_local = GetHeader();
1387c478bd9Sstevel@tonic-gate hdr_local.sample_rate = ohdr.sample_rate;
1397c478bd9Sstevel@tonic-gate hdr_local.samples_per_unit = ohdr.samples_per_unit;
1407c478bd9Sstevel@tonic-gate hdr_local.bytes_per_unit = ohdr.bytes_per_unit;
1417c478bd9Sstevel@tonic-gate hdr_local.channels = ohdr.channels;
1427c478bd9Sstevel@tonic-gate hdr_local.encoding = (AudioEncoding) ohdr.encoding;
1437c478bd9Sstevel@tonic-gate hdr_local.endian = BIG_ENDIAN; // Files are always written in
1447c478bd9Sstevel@tonic-gate // big endian.
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate err = SetHeader(hdr_local);
1477c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
148*7cad4b84SToomas Soome delete[] ibuf;
1497c478bd9Sstevel@tonic-gate return (RaiseError(err));
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate SetInfostring(ibuf, infosize);
152*7cad4b84SToomas Soome delete[] ibuf;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate // Only trust the file size for regular files
1557c478bd9Sstevel@tonic-gate if (S_ISREG(st.st_mode)) {
1567c478bd9Sstevel@tonic-gate setlength(GetHeader().Bytes_to_Time(
1577c478bd9Sstevel@tonic-gate st.st_size - infosize - sizeof (au_filehdr_t)));
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate // Sanity check
1607c478bd9Sstevel@tonic-gate if ((ohdr.data_size != AUDIO_UNKNOWN_SIZE) &&
1617c478bd9Sstevel@tonic-gate (GetLength() != GetHeader().Bytes_to_Time(ohdr.data_size)))
1627c478bd9Sstevel@tonic-gate PrintMsg(_MGET_(
1637c478bd9Sstevel@tonic-gate "AudioUnixfile: header/file size mismatch"));
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate // always consider it to be unknown if not reading a real file
1667c478bd9Sstevel@tonic-gate // since there's no real way to verify if the header is
1677c478bd9Sstevel@tonic-gate // correct.
1687c478bd9Sstevel@tonic-gate } else {
1697c478bd9Sstevel@tonic-gate setlength(AUDIO_UNKNOWN_TIME);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate // set flag for opened() test
1737c478bd9Sstevel@tonic-gate filehdrset = TRUE;
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate // Write an audio file header
1797c478bd9Sstevel@tonic-gate // This routine encodes the audio file header and writes it out.
1807c478bd9Sstevel@tonic-gate // XXX - It assumes that the file pointer is set to the start of the file.
1817c478bd9Sstevel@tonic-gate //
1827c478bd9Sstevel@tonic-gate // This method should be specialized by subclasses that are not files,
1837c478bd9Sstevel@tonic-gate // like devices for instance.
1847c478bd9Sstevel@tonic-gate //
1857c478bd9Sstevel@tonic-gate // XXX - this routine should be rewritten for C++
1867c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
encode_filehdr()1877c478bd9Sstevel@tonic-gate encode_filehdr()
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate Boolean saveblock; // saved state of the blocking i/o flag
1907c478bd9Sstevel@tonic-gate AudioHdr hdr_local; // local copy of header
1917c478bd9Sstevel@tonic-gate Audio_hdr ohdr; // XXX - old libaudio hdr
1927c478bd9Sstevel@tonic-gate AudioError err;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate // If fd is not open, or file header already written, skip it
1957c478bd9Sstevel@tonic-gate if (!isfdset() || opened())
1967c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate // XXX - Set up the libaudio hdr
1997c478bd9Sstevel@tonic-gate hdr_local = GetHeader();
2007c478bd9Sstevel@tonic-gate hdr_local.endian = BIG_ENDIAN; // Files are always written big endian.
2017c478bd9Sstevel@tonic-gate err = SetHeader(hdr_local);
2027c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS) {
2037c478bd9Sstevel@tonic-gate return (RaiseError(err));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate ohdr.sample_rate = hdr_local.sample_rate;
2077c478bd9Sstevel@tonic-gate ohdr.samples_per_unit = hdr_local.samples_per_unit;
2087c478bd9Sstevel@tonic-gate ohdr.bytes_per_unit = hdr_local.bytes_per_unit;
2097c478bd9Sstevel@tonic-gate ohdr.channels = hdr_local.channels;
2107c478bd9Sstevel@tonic-gate ohdr.encoding = hdr_local.encoding;
2117c478bd9Sstevel@tonic-gate if (Undefined(GetLength()))
2127c478bd9Sstevel@tonic-gate ohdr.data_size = AUDIO_UNKNOWN_SIZE;
2137c478bd9Sstevel@tonic-gate else
2147c478bd9Sstevel@tonic-gate ohdr.data_size = (uint_t)GetHeader().Time_to_Bytes(GetLength());
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /* Make sure the file is not set for blocking i/o */
2177c478bd9Sstevel@tonic-gate saveblock = GetBlocking();
2187c478bd9Sstevel@tonic-gate if (!saveblock)
2197c478bd9Sstevel@tonic-gate SetBlocking(TRUE);
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate // XXX - Should use C++ output method
2227c478bd9Sstevel@tonic-gate err = (AudioError) audio_write_filehdr(getfd(), &ohdr, FILE_AU,
2237c478bd9Sstevel@tonic-gate infostring, infolength);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate // set flag for opened() test
2267c478bd9Sstevel@tonic-gate if (err == AUDIO_SUCCESS)
2277c478bd9Sstevel@tonic-gate filehdrset = TRUE;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate SetBlocking(saveblock); // Restore the saved blocking i/o state
2307c478bd9Sstevel@tonic-gate return (RaiseError(err));
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate // Set a file blocking/non-blocking
2347c478bd9Sstevel@tonic-gate // This method should be subclassed by objects that always block (eg, files)
2357c478bd9Sstevel@tonic-gate void AudioUnixfile::
SetBlocking(Boolean b)2367c478bd9Sstevel@tonic-gate SetBlocking(
2377c478bd9Sstevel@tonic-gate Boolean b) // FALSE to set non-blocking
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate int flag;
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate // If the file is open, set blocking/non-blocking now
2427c478bd9Sstevel@tonic-gate if (isfdset()) {
2437c478bd9Sstevel@tonic-gate flag = fcntl(getfd(), F_GETFL, 0);
2447c478bd9Sstevel@tonic-gate if ((flag < 0) && (errno == EOVERFLOW || errno == EINVAL)) {
2457c478bd9Sstevel@tonic-gate RaiseError(AUDIO_UNIXERROR, Fatal,
2467c478bd9Sstevel@tonic-gate (char *)"Large File");
2477c478bd9Sstevel@tonic-gate } else if (b) {
2487c478bd9Sstevel@tonic-gate flag &= ~(O_NDELAY | O_NONBLOCK); // set blocking
2497c478bd9Sstevel@tonic-gate } else {
2507c478bd9Sstevel@tonic-gate flag |= O_NONBLOCK; // set non-blocking
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate if (fcntl(getfd(), F_SETFL, flag) < 0) {
2537c478bd9Sstevel@tonic-gate RaiseError(AUDIO_UNIXERROR, Warning);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate // Set the blocking flag (this may affect the Open() behavior)
2577c478bd9Sstevel@tonic-gate block = b;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate // Return a pointer to the info string
2617c478bd9Sstevel@tonic-gate // XXX - returns a pointer to the string stored in the object
2627c478bd9Sstevel@tonic-gate // XXX - assumes ASCII data
2637c478bd9Sstevel@tonic-gate char *const AudioUnixfile::
GetInfostring(int & len) const2647c478bd9Sstevel@tonic-gate GetInfostring(
2657c478bd9Sstevel@tonic-gate int& len) const // returned length of string
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate len = infolength;
2687c478bd9Sstevel@tonic-gate return (infostring);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate // Set the info string
2727c478bd9Sstevel@tonic-gate void AudioUnixfile::
SetInfostring(const char * str,int len)2737c478bd9Sstevel@tonic-gate SetInfostring(
2747c478bd9Sstevel@tonic-gate const char *str, // new info string
2757c478bd9Sstevel@tonic-gate int len) // length of string
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate // If length defaulted, assume an ASCII string
2787c478bd9Sstevel@tonic-gate if (len == -1)
2797c478bd9Sstevel@tonic-gate len = strlen(str) + 1;
2807c478bd9Sstevel@tonic-gate delete infostring;
2817c478bd9Sstevel@tonic-gate infostring = new char[len];
2827c478bd9Sstevel@tonic-gate infolength = len;
2837c478bd9Sstevel@tonic-gate (void) memcpy(infostring, str, len);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate // Close file
2877c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
Close()2887c478bd9Sstevel@tonic-gate Close()
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate // If the file is open, close it
2917c478bd9Sstevel@tonic-gate if (isfdset()) {
2927c478bd9Sstevel@tonic-gate if (close(getfd()) < 0)
2937c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
2947c478bd9Sstevel@tonic-gate } else {
2957c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate // Init important values, in case the file is reopened
2997c478bd9Sstevel@tonic-gate setfd(-1);
3007c478bd9Sstevel@tonic-gate filehdrset = FALSE;
3017c478bd9Sstevel@tonic-gate (void) SetReadPosition((Double)0., Absolute);
3027c478bd9Sstevel@tonic-gate (void) SetWritePosition((Double)0., Absolute);
3037c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate // Read data from underlying file into specified buffer.
3077c478bd9Sstevel@tonic-gate // No data format translation takes place.
3087c478bd9Sstevel@tonic-gate // The object's read position is not updated (subclasses can change this)
3097c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
ReadData(void * buf,size_t & len,Double & pos)3107c478bd9Sstevel@tonic-gate ReadData(
3117c478bd9Sstevel@tonic-gate void* buf, // destination buffer address
3127c478bd9Sstevel@tonic-gate size_t& len, // buffer length (updated)
3137c478bd9Sstevel@tonic-gate Double& pos) // start position (updated)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate off_t offset;
3167c478bd9Sstevel@tonic-gate off_t cnt;
3177c478bd9Sstevel@tonic-gate AudioError err;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate // Save buffer size and zero transfer count
3207c478bd9Sstevel@tonic-gate cnt = (off_t)len;
3217c478bd9Sstevel@tonic-gate len = 0;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate // Cannot read if file is not open
3247c478bd9Sstevel@tonic-gate if (!opened() || !mode.Readable())
3257c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT));
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate // Position must be valid
3287c478bd9Sstevel@tonic-gate if (Undefined(pos) || (pos < 0.) || (cnt < 0))
3297c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_BADARG));
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate // Position the file pointer to the right place
3327c478bd9Sstevel@tonic-gate err = seekread(pos, offset);
3337c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS)
3347c478bd9Sstevel@tonic-gate return (err);
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate // Check for EOF
3377c478bd9Sstevel@tonic-gate if (pos >= GetLength()) {
3387c478bd9Sstevel@tonic-gate err = AUDIO_EOF;
3397c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_INPUT_EOF;
3407c478bd9Sstevel@tonic-gate return (err);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate // Zero-length reads are finished
3447c478bd9Sstevel@tonic-gate if (GetHeader().Bytes_to_Bytes(cnt) == 0) {
3457c478bd9Sstevel@tonic-gate err = AUDIO_SUCCESS;
3467c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_ZERO_LIMIT;
3477c478bd9Sstevel@tonic-gate return (err);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate // Read as much data as possible
3517c478bd9Sstevel@tonic-gate cnt = read(fd, (char *)buf, (int)cnt);
3527c478bd9Sstevel@tonic-gate if (cnt < 0) {
3537c478bd9Sstevel@tonic-gate if (errno == EOVERFLOW) {
3547c478bd9Sstevel@tonic-gate perror("read");
3557c478bd9Sstevel@tonic-gate exit(1);
3567c478bd9Sstevel@tonic-gate } else if ((errno == EINTR) ||
3577c478bd9Sstevel@tonic-gate (((errno == EWOULDBLOCK) || (errno == EAGAIN)) &&
3587c478bd9Sstevel@tonic-gate !GetBlocking())) {
3597c478bd9Sstevel@tonic-gate // Is this an interrupted or failed non-blocking request?
3607c478bd9Sstevel@tonic-gate err = AUDIO_SUCCESS;
3617c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_SHORT_INPUT;
3627c478bd9Sstevel@tonic-gate return (err);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate // End-of-file?
3687c478bd9Sstevel@tonic-gate if ((cnt == 0) && GetBlocking()) {
3697c478bd9Sstevel@tonic-gate if (isDevice() || isPipe()) {
3707c478bd9Sstevel@tonic-gate AUDIO_DEBUG((1,
3717c478bd9Sstevel@tonic-gate "Zero-length blocking device/pipe read?!\n"));
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate err = AUDIO_EOF;
3747c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_INPUT_EOF;
3757c478bd9Sstevel@tonic-gate return (err);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate err = AUDIO_SUCCESS;
3787c478bd9Sstevel@tonic-gate if (cnt == 0) {
3797c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_SHORT_INPUT;
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate // Return the updated byte count and position
3837c478bd9Sstevel@tonic-gate len = (size_t)cnt;
3847c478bd9Sstevel@tonic-gate if (GetHeader().Bytes_to_Bytes(cnt) != len) {
3857c478bd9Sstevel@tonic-gate AUDIO_DEBUG((1,
3867c478bd9Sstevel@tonic-gate "Read returned a partial sample frame?!\n"));
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate pos = GetHeader().Bytes_to_Time(offset + len);
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate // Check to see if the endian is right.
3917c478bd9Sstevel@tonic-gate coerceEndian((unsigned char *)buf, len, localByteOrder());
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate return (err);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gate // Write data to underlying file from specified buffer.
3977c478bd9Sstevel@tonic-gate // No data format translation takes place.
3987c478bd9Sstevel@tonic-gate // The object's write position is not updated (subclasses can change this)
3997c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
WriteData(void * buf,size_t & len,Double & pos)4007c478bd9Sstevel@tonic-gate WriteData(
4017c478bd9Sstevel@tonic-gate void* buf, // source buffer address
4027c478bd9Sstevel@tonic-gate size_t& len, // buffer length (updated)
4037c478bd9Sstevel@tonic-gate Double& pos) // start position (updated)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate off_t offset;
4067c478bd9Sstevel@tonic-gate off_t cnt;
4077c478bd9Sstevel@tonic-gate AudioError err;
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate // Save buffer size and zero transfer count
4107c478bd9Sstevel@tonic-gate cnt = (off_t)len;
4117c478bd9Sstevel@tonic-gate len = 0;
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate // Cannot write if file is not open
4147c478bd9Sstevel@tonic-gate if (!opened() || !mode.Writeable())
4157c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT));
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate // Position must be valid
4187c478bd9Sstevel@tonic-gate if (Undefined(pos) || (pos < 0.) || (cnt < 0))
4197c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_BADARG));
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate // Zero-length writes are easy
4227c478bd9Sstevel@tonic-gate if (GetHeader().Bytes_to_Bytes(cnt) == 0) {
4237c478bd9Sstevel@tonic-gate err = AUDIO_SUCCESS;
4247c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_ZERO_LIMIT;
4257c478bd9Sstevel@tonic-gate return (err);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate // Position the file pointer to the right place
4297c478bd9Sstevel@tonic-gate err = seekwrite(pos, offset);
4307c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS)
4317c478bd9Sstevel@tonic-gate return (err);
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate // Make sure data is in target's endian format before writing.
4347c478bd9Sstevel@tonic-gate // This conversion is done inplace so we need to change back.
4357c478bd9Sstevel@tonic-gate // We assume that the data in buf is in localByteOrder.
4367c478bd9Sstevel@tonic-gate // Only files should have order issues.
4377c478bd9Sstevel@tonic-gate if (localByteOrder() != GetHeader().endian)
4387c478bd9Sstevel@tonic-gate coerceEndian((unsigned char *)buf, (size_t)cnt, SWITCH_ENDIAN);
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate // Write as much data as possible
4417c478bd9Sstevel@tonic-gate err = AUDIO_SUCCESS;
4427c478bd9Sstevel@tonic-gate cnt = write(fd, (char *)buf, (int)cnt);
4437c478bd9Sstevel@tonic-gate if (cnt < 0) {
4447c478bd9Sstevel@tonic-gate if (errno == EFBIG) {
4457c478bd9Sstevel@tonic-gate perror("write");
4467c478bd9Sstevel@tonic-gate exit(1);
4477c478bd9Sstevel@tonic-gate } else if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
4487c478bd9Sstevel@tonic-gate // Is this a failed non-blocking request?
4497c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_SHORT_OUTPUT;
4507c478bd9Sstevel@tonic-gate return (err);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate if (cnt == 0)
4557c478bd9Sstevel@tonic-gate err.sys = AUDIO_COPY_SHORT_OUTPUT;
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate // Switch the endian back if local order doesn't match target order.
4587c478bd9Sstevel@tonic-gate if (localByteOrder() != GetHeader().endian)
4597c478bd9Sstevel@tonic-gate coerceEndian((unsigned char *)buf, (size_t)cnt, SWITCH_ENDIAN);
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate // Return the updated byte count and position
4627c478bd9Sstevel@tonic-gate len = (size_t)cnt;
4637c478bd9Sstevel@tonic-gate pos = GetHeader().Bytes_to_Time(offset + len);
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate // If the current position is beyond old EOF, update the size
4667c478bd9Sstevel@tonic-gate if (!Undefined(GetLength()) && (pos > GetLength())) {
4677c478bd9Sstevel@tonic-gate setlength(pos);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate
4707c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate // Seek in input stream
4747c478bd9Sstevel@tonic-gate // Ordinary streams (ie, pipes and devices) cannot be rewound.
4757c478bd9Sstevel@tonic-gate // A forward seek in them consumes data by reading it.
4767c478bd9Sstevel@tonic-gate //
4777c478bd9Sstevel@tonic-gate // This method should be specialized by subclasses that can actually seek,
4787c478bd9Sstevel@tonic-gate // like regular files for instance.
4797c478bd9Sstevel@tonic-gate //
4807c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
seekread(Double pos,off_t & offset)4817c478bd9Sstevel@tonic-gate seekread(
4827c478bd9Sstevel@tonic-gate Double pos, // position to seek to
4837c478bd9Sstevel@tonic-gate off_t& offset) // returned byte offset
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate char *bufp; // temporary input buffer
4867c478bd9Sstevel@tonic-gate size_t bufl; // input buffer size
4877c478bd9Sstevel@tonic-gate size_t cnt; // input byte count
4887c478bd9Sstevel@tonic-gate long icnt; // read size
4897c478bd9Sstevel@tonic-gate Boolean saveblock; // saved state of the blocking i/o flag
4907c478bd9Sstevel@tonic-gate Double buflen;
4917c478bd9Sstevel@tonic-gate AudioError err;
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate offset = GetHeader().Time_to_Bytes(pos);
4947c478bd9Sstevel@tonic-gate pos -= ReadPosition();
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate // If the seek is backwards, do nothing
4977c478bd9Sstevel@tonic-gate if (pos < 0.)
4987c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate // If the seek is to the current position, then do nothing.
5017c478bd9Sstevel@tonic-gate icnt = GetHeader().Time_to_Bytes(pos);
5027c478bd9Sstevel@tonic-gate if (icnt == 0)
5037c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate // The seek is determinate and forward.
5067c478bd9Sstevel@tonic-gate // We'll have to consume data to get there.
5077c478bd9Sstevel@tonic-gate // First allocate a buffer to stuff the data into.
5087c478bd9Sstevel@tonic-gate // Then set the stream for blocking i/o (saving the old state).
5097c478bd9Sstevel@tonic-gate buflen = max(pos, 1.);
5107c478bd9Sstevel@tonic-gate bufl = (size_t)GetHeader().Time_to_Bytes(buflen);
5117c478bd9Sstevel@tonic-gate bufp = new char[bufl];
5127c478bd9Sstevel@tonic-gate if (bufp == 0) { // allocation error, try a smaller buf
5137c478bd9Sstevel@tonic-gate bufl = (size_t)sysconf(_SC_PAGESIZE);
5147c478bd9Sstevel@tonic-gate bufp = new char[bufl];
5157c478bd9Sstevel@tonic-gate if (bufp == 0)
5167c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate // XXX - May have to realign to partial frame count!
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate saveblock = GetBlocking();
5217c478bd9Sstevel@tonic-gate if (!saveblock)
5227c478bd9Sstevel@tonic-gate SetBlocking(TRUE);
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate // Loop until the seek is satisfied (or an error occurs).
5257c478bd9Sstevel@tonic-gate do {
5267c478bd9Sstevel@tonic-gate // Limit the read to keep from going too far
5277c478bd9Sstevel@tonic-gate cnt = (icnt >= (long)bufl) ? bufl : (size_t)icnt;
5287c478bd9Sstevel@tonic-gate err = Read(bufp, cnt);
5297c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS)
5307c478bd9Sstevel@tonic-gate break;
5317c478bd9Sstevel@tonic-gate icnt -= (long)cnt;
5327c478bd9Sstevel@tonic-gate } while (icnt > 0);
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate SetBlocking(saveblock); // Restore the saved blocking i/o state
535*7cad4b84SToomas Soome delete[] bufp; // Free the temporary buffer
5367c478bd9Sstevel@tonic-gate return (RaiseError(err));
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate // Seek in output stream
5407c478bd9Sstevel@tonic-gate // Ordinary streams (ie, pipes and devices) cannot be rewound.
5417c478bd9Sstevel@tonic-gate // A forward seek in them writes NULL data.
5427c478bd9Sstevel@tonic-gate //
5437c478bd9Sstevel@tonic-gate // This method should be specialized by subclasses that can actually seek,
5447c478bd9Sstevel@tonic-gate // like regular files for instance.
5457c478bd9Sstevel@tonic-gate //
5467c478bd9Sstevel@tonic-gate AudioError AudioUnixfile::
seekwrite(Double pos,off_t & offset)5477c478bd9Sstevel@tonic-gate seekwrite(
5487c478bd9Sstevel@tonic-gate Double pos, // position to seek to
5497c478bd9Sstevel@tonic-gate off_t& offset) // returned byte offset
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate char *bufp; // temporary output buffer
5527c478bd9Sstevel@tonic-gate size_t bufl; // output buffer size
5537c478bd9Sstevel@tonic-gate size_t cnt; // output byte count
5547c478bd9Sstevel@tonic-gate long ocnt; // write size
5557c478bd9Sstevel@tonic-gate Boolean saveblock; // saved state of the blocking i/o flag
5567c478bd9Sstevel@tonic-gate Double buflen;
5577c478bd9Sstevel@tonic-gate AudioError err;
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate offset = GetHeader().Time_to_Bytes(pos);
5607c478bd9Sstevel@tonic-gate pos -= WritePosition();
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate // If the seek is backwards, do nothing
5637c478bd9Sstevel@tonic-gate if (pos < 0.)
5647c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate // If the seek is to the current position, then do nothing.
5677c478bd9Sstevel@tonic-gate ocnt = GetHeader().Time_to_Bytes(pos);
5687c478bd9Sstevel@tonic-gate if (ocnt == 0)
5697c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS);
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate // The seek is determinate and forward.
5727c478bd9Sstevel@tonic-gate // We'll have to produce NULL data to get there.
5737c478bd9Sstevel@tonic-gate // XXX - not implemented correctly yet
5747c478bd9Sstevel@tonic-gate buflen = max(pos, 1.);
5757c478bd9Sstevel@tonic-gate bufl = (size_t)GetHeader().Time_to_Bytes(buflen);
5767c478bd9Sstevel@tonic-gate bufp = new char[bufl];
5777c478bd9Sstevel@tonic-gate if (bufp == 0) { // allocation error, try a smaller buf
5787c478bd9Sstevel@tonic-gate bufl = (size_t)sysconf(_SC_PAGESIZE);
5797c478bd9Sstevel@tonic-gate bufp = new char[bufl];
5807c478bd9Sstevel@tonic-gate if (bufp == 0)
5817c478bd9Sstevel@tonic-gate return (RaiseError(AUDIO_UNIXERROR));
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate // XXX - May have to realign to partial frame count!
5857c478bd9Sstevel@tonic-gate saveblock = GetBlocking();
5867c478bd9Sstevel@tonic-gate if (!saveblock)
5877c478bd9Sstevel@tonic-gate SetBlocking(TRUE);
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate // Loop until the seek is satisfied (or an error occurs).
5907c478bd9Sstevel@tonic-gate do {
5917c478bd9Sstevel@tonic-gate // Limit the write to keep from going too far
5927c478bd9Sstevel@tonic-gate cnt = (ocnt >= (long)bufl) ? bufl : (size_t)ocnt;
5937c478bd9Sstevel@tonic-gate err = Write(bufp, cnt);
5947c478bd9Sstevel@tonic-gate if (err != AUDIO_SUCCESS)
5957c478bd9Sstevel@tonic-gate break;
5967c478bd9Sstevel@tonic-gate ocnt -= (long)cnt;
5977c478bd9Sstevel@tonic-gate } while (ocnt > 0);
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate SetBlocking(saveblock); // Restore the saved blocking i/o state
600*7cad4b84SToomas Soome delete[] bufp; // Free the temporary buffer
6017c478bd9Sstevel@tonic-gate return (RaiseError(err));
6027c478bd9Sstevel@tonic-gate }
603