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) 1993-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 <AudioExtent.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate // class AudioExtent methods
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate // class AudioExtent Constructor
36*0Sstevel@tonic-gate AudioExtent::
AudioExtent(Audio * obj,double s,double e)37*0Sstevel@tonic-gate AudioExtent(
38*0Sstevel@tonic-gate Audio* obj, // audio object to point to
39*0Sstevel@tonic-gate double s, // start time
40*0Sstevel@tonic-gate double e): // end time
41*0Sstevel@tonic-gate Audio("[extent]"), ref(obj)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate ref->Reference(); // reference audio object
44*0Sstevel@tonic-gate SetStart(s); // set start/end times
45*0Sstevel@tonic-gate SetEnd(e);
46*0Sstevel@tonic-gate }
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate // class AudioExtent Destructor
49*0Sstevel@tonic-gate AudioExtent::
~AudioExtent()50*0Sstevel@tonic-gate ~AudioExtent()
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate ref->Dereference(); // clear audio object reference
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate // Get referenced object
56*0Sstevel@tonic-gate Audio* AudioExtent::
GetRef() const57*0Sstevel@tonic-gate GetRef() const
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate return (ref);
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate // Set referenced object
63*0Sstevel@tonic-gate void AudioExtent::
SetRef(Audio * r)64*0Sstevel@tonic-gate SetRef(
65*0Sstevel@tonic-gate Audio* r) // new audio object
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate if (ref == r) // object is not changing
68*0Sstevel@tonic-gate return;
69*0Sstevel@tonic-gate ref->Dereference(); // dereference previous object
70*0Sstevel@tonic-gate if (r != 0) {
71*0Sstevel@tonic-gate ref = r;
72*0Sstevel@tonic-gate ref->Reference(); // reference new object
73*0Sstevel@tonic-gate } else {
74*0Sstevel@tonic-gate PrintMsg(_MGET_("AudioExtent:...NULL Audio object reference"),
75*0Sstevel@tonic-gate Fatal);
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate // Get start time
80*0Sstevel@tonic-gate Double AudioExtent::
GetStart() const81*0Sstevel@tonic-gate GetStart() const
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate return (start);
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate // Set start time
87*0Sstevel@tonic-gate void AudioExtent::
SetStart(Double s)88*0Sstevel@tonic-gate SetStart(
89*0Sstevel@tonic-gate Double s) // start time, in seconds
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate if (Undefined(s) || (s < 0.))
92*0Sstevel@tonic-gate start = 0.;
93*0Sstevel@tonic-gate else
94*0Sstevel@tonic-gate start = s;
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate // Get end time
98*0Sstevel@tonic-gate Double AudioExtent::
GetEnd() const99*0Sstevel@tonic-gate GetEnd() const
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate // If determinate endpoint, return it
102*0Sstevel@tonic-gate if (!Undefined(end))
103*0Sstevel@tonic-gate return (end);
104*0Sstevel@tonic-gate // Otherwise, return the endpoint of the underlying object
105*0Sstevel@tonic-gate return (ref->GetLength());
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate // Set end time
109*0Sstevel@tonic-gate void AudioExtent::
SetEnd(Double e)110*0Sstevel@tonic-gate SetEnd(
111*0Sstevel@tonic-gate Double e) // end time, in seconds
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate Double len;
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate // If known endpoint and object has known size, do not exceed size
116*0Sstevel@tonic-gate if (!Undefined(e)) {
117*0Sstevel@tonic-gate len = ref->GetLength();
118*0Sstevel@tonic-gate if (!Undefined(len) && (e > len))
119*0Sstevel@tonic-gate e = len;
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate end = e;
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate // Get the length of an audio extent
125*0Sstevel@tonic-gate Double AudioExtent::
GetLength() const126*0Sstevel@tonic-gate GetLength() const
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate Double x;
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate // If extent end is indeterminate, use the end of the target object
131*0Sstevel@tonic-gate x = GetEnd();
132*0Sstevel@tonic-gate // If the object length is indeterminate, then the length is
133*0Sstevel@tonic-gate if (Undefined(x))
134*0Sstevel@tonic-gate return (x);
135*0Sstevel@tonic-gate return (x - start);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate // Construct a name for the list
139*0Sstevel@tonic-gate char *AudioExtent::
GetName() const140*0Sstevel@tonic-gate GetName() const
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate // XXX - construct a better name
143*0Sstevel@tonic-gate return (ref->GetName());
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate // Get the audio header for the current read position
147*0Sstevel@tonic-gate AudioHdr AudioExtent::
GetHeader()148*0Sstevel@tonic-gate GetHeader()
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate return (ref->GetDHeader(ReadPosition() + start));
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate // Get the audio header for the given position
154*0Sstevel@tonic-gate AudioHdr AudioExtent::
GetHeader(Double pos)155*0Sstevel@tonic-gate GetHeader(
156*0Sstevel@tonic-gate Double pos) // position
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate return (ref->GetDHeader(pos + start));
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate // Copy data from extent into specified buffer.
162*0Sstevel@tonic-gate // No data format translation takes place.
163*0Sstevel@tonic-gate // The object's read position is not updated.
164*0Sstevel@tonic-gate //
165*0Sstevel@tonic-gate // Since the extent could refer to a list of extents of differing encodings,
166*0Sstevel@tonic-gate // clients should always use GetHeader() in combination with ReadData()
167*0Sstevel@tonic-gate AudioError AudioExtent::
ReadData(void * buf,size_t & len,Double & pos)168*0Sstevel@tonic-gate ReadData(
169*0Sstevel@tonic-gate void* buf, // destination buffer address
170*0Sstevel@tonic-gate size_t& len, // buffer size (updated)
171*0Sstevel@tonic-gate Double& pos) // start position (updated)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate size_t cnt;
174*0Sstevel@tonic-gate off_t buflen;
175*0Sstevel@tonic-gate Double off;
176*0Sstevel@tonic-gate Double newpos;
177*0Sstevel@tonic-gate AudioError err;
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate // Save buffer size and zero transfer count
180*0Sstevel@tonic-gate cnt = len;
181*0Sstevel@tonic-gate len = 0;
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate // Position must be valid
184*0Sstevel@tonic-gate if (Undefined(pos) || (pos < 0.) || ((int)cnt < 0))
185*0Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_BADARG));
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate // If the end is determinate, check start position and length
188*0Sstevel@tonic-gate off = pos + start;
189*0Sstevel@tonic-gate newpos = GetEnd();
190*0Sstevel@tonic-gate if (!Undefined(newpos)) {
191*0Sstevel@tonic-gate // If starting beyond eof, give up now
192*0Sstevel@tonic-gate if (off >= newpos) {
193*0Sstevel@tonic-gate err = AUDIO_EOF;
194*0Sstevel@tonic-gate err.sys = AUDIO_COPY_INPUT_EOF;
195*0Sstevel@tonic-gate return (err);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate
198*0Sstevel@tonic-gate // If the read would extend beyond end-of-extent, shorten it
199*0Sstevel@tonic-gate buflen = GetHeader(pos).Time_to_Bytes((Double)(newpos - off));
200*0Sstevel@tonic-gate if (buflen == 0) {
201*0Sstevel@tonic-gate err = AUDIO_EOF;
202*0Sstevel@tonic-gate err.sys = AUDIO_COPY_INPUT_EOF;
203*0Sstevel@tonic-gate return (err);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate if (buflen < cnt)
206*0Sstevel@tonic-gate cnt = (size_t)buflen;
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate // Zero-length reads are easy
209*0Sstevel@tonic-gate if (cnt == 0) {
210*0Sstevel@tonic-gate err = AUDIO_SUCCESS;
211*0Sstevel@tonic-gate err.sys = AUDIO_COPY_ZERO_LIMIT;
212*0Sstevel@tonic-gate return (err);
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gate // Save the offset, read data, and update the returned position
216*0Sstevel@tonic-gate newpos = off;
217*0Sstevel@tonic-gate len = cnt;
218*0Sstevel@tonic-gate err = ref->ReadData(buf, len, newpos);
219*0Sstevel@tonic-gate pos = (newpos - start); // XXX - calculate bytes and convert?
220*0Sstevel@tonic-gate return (err);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate // Write to AudioExtent is (currently) prohibited
224*0Sstevel@tonic-gate AudioError AudioExtent::
WriteData(void *,size_t & len,Double &)225*0Sstevel@tonic-gate WriteData(
226*0Sstevel@tonic-gate void*, // destination buffer address
227*0Sstevel@tonic-gate size_t& len, // buffer size (updated)
228*0Sstevel@tonic-gate Double&) // start position (updated)
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate len = 0;
231*0Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT));
232*0Sstevel@tonic-gate }
233