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 <AudioList.h>
31*0Sstevel@tonic-gate #include <AudioDebug.h>
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate // class AudioList methods
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate // class AudioListEntry Constructor
37*0Sstevel@tonic-gate AudioList::AudioListEntry::
AudioListEntry(Audio * obj)38*0Sstevel@tonic-gate AudioListEntry(
39*0Sstevel@tonic-gate Audio* obj): // audio object to point to
40*0Sstevel@tonic-gate aptr(0), next(0), prev(0)
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate // A NULL object is only valid in dummy entries, such as list heads
43*0Sstevel@tonic-gate newptr(obj);
44*0Sstevel@tonic-gate }
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate // class AudioListEntry Destructor
47*0Sstevel@tonic-gate AudioList::AudioListEntry::
~AudioListEntry()48*0Sstevel@tonic-gate ~AudioListEntry()
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate newptr(0);
51*0Sstevel@tonic-gate if (next != 0) {
52*0Sstevel@tonic-gate next->prev = prev;
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate if (prev != 0) {
55*0Sstevel@tonic-gate prev->next = next;
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate // Set a new extent pointer in an AudioListEntry
60*0Sstevel@tonic-gate void AudioList::AudioListEntry::
newptr(Audio * newa)61*0Sstevel@tonic-gate newptr(
62*0Sstevel@tonic-gate Audio* newa) // new object
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate if (aptr != 0)
65*0Sstevel@tonic-gate aptr->Dereference();
66*0Sstevel@tonic-gate aptr = newa;
67*0Sstevel@tonic-gate if (aptr != 0)
68*0Sstevel@tonic-gate aptr->Reference();
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate // Link object into list
72*0Sstevel@tonic-gate // Link in a new AudioListEntry
73*0Sstevel@tonic-gate void AudioList::AudioListEntry::
link(AudioListEntry * after)74*0Sstevel@tonic-gate link(
75*0Sstevel@tonic-gate AudioListEntry* after) // link after this one
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate // Link object into list
78*0Sstevel@tonic-gate prev = after;
79*0Sstevel@tonic-gate next = after->next;
80*0Sstevel@tonic-gate after->next = this;
81*0Sstevel@tonic-gate if (next != 0)
82*0Sstevel@tonic-gate next->prev = this;
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate // Split an AudioListEntry at the specified offset
86*0Sstevel@tonic-gate void AudioList::AudioListEntry::
split(Double pos)87*0Sstevel@tonic-gate split(
88*0Sstevel@tonic-gate Double pos) // split offset
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate AudioExtent* e1;
91*0Sstevel@tonic-gate AudioExtent* e2;
92*0Sstevel@tonic-gate AudioListEntry* newp;
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate // Create two extents referencing this object
95*0Sstevel@tonic-gate e1 = new AudioExtent(aptr, 0., pos);
96*0Sstevel@tonic-gate e2 = new AudioExtent(aptr, pos, AUDIO_UNKNOWN_TIME);
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate // Set the current entry to the first extent and append the second
99*0Sstevel@tonic-gate newptr(e1);
100*0Sstevel@tonic-gate newp = new AudioListEntry(e2);
101*0Sstevel@tonic-gate newp->link(this);
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate // class AudioList Constructor
106*0Sstevel@tonic-gate AudioList::
AudioList(const char * local_name)107*0Sstevel@tonic-gate AudioList(
108*0Sstevel@tonic-gate const char *local_name): // name string
109*0Sstevel@tonic-gate Audio(local_name), head(0)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate // class AudioList Destructor
114*0Sstevel@tonic-gate AudioList::
~AudioList()115*0Sstevel@tonic-gate ~AudioList()
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate // Delete all entries in the list
118*0Sstevel@tonic-gate while (first() != 0)
119*0Sstevel@tonic-gate delete first();
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate // Get the first entry in the list
123*0Sstevel@tonic-gate AudioList::AudioListEntry* AudioList::
first() const124*0Sstevel@tonic-gate first() const
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate return (head.next);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate // Get the extent and offset corresponding to a given position
130*0Sstevel@tonic-gate // Return FALSE if no extents in list or position is beyond eof
131*0Sstevel@tonic-gate Boolean AudioList::
getposition(Double & pos,AudioListEntry * & ep) const132*0Sstevel@tonic-gate getposition(
133*0Sstevel@tonic-gate Double& pos, // target position (updated)
134*0Sstevel@tonic-gate AudioListEntry*& ep) const // returned extent pointer
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate Double length;
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate // Position must be specified
139*0Sstevel@tonic-gate if (Undefined(pos))
140*0Sstevel@tonic-gate return (FALSE);
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate // Get the first extent in the list
143*0Sstevel@tonic-gate ep = first();
144*0Sstevel@tonic-gate while (ep != 0) {
145*0Sstevel@tonic-gate // Get length of extent
146*0Sstevel@tonic-gate length = ep->aptr->GetLength();
147*0Sstevel@tonic-gate if (Undefined(length)) {
148*0Sstevel@tonic-gate // Can't determine sizes beyond this
149*0Sstevel@tonic-gate return (TRUE);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate // If the remaining offset is inside the current extent
152*0Sstevel@tonic-gate if (length > pos)
153*0Sstevel@tonic-gate return (TRUE);
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate // Move on to the next extent
156*0Sstevel@tonic-gate pos -= length;
157*0Sstevel@tonic-gate ep = ep->next;
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate return (FALSE);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate // Get the total length of the audio list
163*0Sstevel@tonic-gate Double AudioList::
GetLength() const164*0Sstevel@tonic-gate GetLength() const
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate AudioListEntry* ep;
167*0Sstevel@tonic-gate Double sum;
168*0Sstevel@tonic-gate Double x;
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate for (sum = 0., ep = first(); ep != 0; ep = ep->next) {
171*0Sstevel@tonic-gate // Accumulate times for each extent
172*0Sstevel@tonic-gate // Indeterminate extents screw up the calculation
173*0Sstevel@tonic-gate x = ep->aptr->GetLength();
174*0Sstevel@tonic-gate if (Undefined(x))
175*0Sstevel@tonic-gate return (x);
176*0Sstevel@tonic-gate sum += x;
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate return (sum);
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate // Construct a name for the list
182*0Sstevel@tonic-gate char *AudioList::
GetName() const183*0Sstevel@tonic-gate GetName() const
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate // XXX - construct a better name
186*0Sstevel@tonic-gate return (Audio::GetName());
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate // Get the audio header for the current read position
190*0Sstevel@tonic-gate AudioHdr AudioList::
GetHeader()191*0Sstevel@tonic-gate GetHeader()
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate return (GetHeader(ReadPosition()));
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate // Get the audio header for the given position
197*0Sstevel@tonic-gate AudioHdr AudioList::
GetHeader(Double pos)198*0Sstevel@tonic-gate GetHeader(
199*0Sstevel@tonic-gate Double pos) // position
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate AudioListEntry* ep;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate // Get the extent pointer for the given position
204*0Sstevel@tonic-gate if (!getposition(pos, ep)) {
205*0Sstevel@tonic-gate AudioHdr h;
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate if (pos != 0.) {
208*0Sstevel@tonic-gate PrintMsg(_MGET_(
209*0Sstevel@tonic-gate "AudioHdr:GetHeader()...position is beyond eof"),
210*0Sstevel@tonic-gate Warning);
211*0Sstevel@tonic-gate return (h);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate if ((ep = first()) != 0)
214*0Sstevel@tonic-gate return (ep->aptr->GetHeader());
215*0Sstevel@tonic-gate return (h);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate // Get the header for the proper offset in the extent
218*0Sstevel@tonic-gate return (ep->aptr->GetDHeader(pos));
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate // Copy data from list into specified buffer.
222*0Sstevel@tonic-gate // No data format translation takes place.
223*0Sstevel@tonic-gate // The object's read position is not updated.
224*0Sstevel@tonic-gate //
225*0Sstevel@tonic-gate // Since list could contain extents of differing encodings,
226*0Sstevel@tonic-gate // clients should always use GetHeader() in combination with ReadData()
227*0Sstevel@tonic-gate AudioError AudioList::
ReadData(void * buf,size_t & len,Double & pos)228*0Sstevel@tonic-gate ReadData(
229*0Sstevel@tonic-gate void* buf, // destination buffer address
230*0Sstevel@tonic-gate size_t& len, // buffer size (updated)
231*0Sstevel@tonic-gate Double& pos) // start position (updated)
232*0Sstevel@tonic-gate {
233*0Sstevel@tonic-gate AudioListEntry* ep;
234*0Sstevel@tonic-gate size_t cnt;
235*0Sstevel@tonic-gate Double off;
236*0Sstevel@tonic-gate Double newpos;
237*0Sstevel@tonic-gate AudioError err;
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate // Save buffer size
240*0Sstevel@tonic-gate cnt = len;
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate // Position must be valid
243*0Sstevel@tonic-gate if (Undefined(pos) || (pos < 0.) || ((int)cnt < 0))
244*0Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_BADARG));
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate // Loop until data is returned or error
247*0Sstevel@tonic-gate // XXX - THIS IS WRONG! THE HEADER COULD CHANGE!
248*0Sstevel@tonic-gate do {
249*0Sstevel@tonic-gate // Get the extent/offset for read position; clear return count
250*0Sstevel@tonic-gate len = 0;
251*0Sstevel@tonic-gate off = pos;
252*0Sstevel@tonic-gate if (!getposition(off, ep)) {
253*0Sstevel@tonic-gate err = AUDIO_EOF;
254*0Sstevel@tonic-gate err.sys = AUDIO_COPY_INPUT_EOF;
255*0Sstevel@tonic-gate return (err);
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate // Save the offset and read some data
259*0Sstevel@tonic-gate newpos = off;
260*0Sstevel@tonic-gate len = cnt;
261*0Sstevel@tonic-gate err = ep->aptr->ReadData(buf, len, newpos);
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate // If no eof on this list entry, or no more data, we're done
264*0Sstevel@tonic-gate if ((err != AUDIO_EOF) || (err.sys != AUDIO_COPY_INPUT_EOF) ||
265*0Sstevel@tonic-gate (ep->next == 0)) {
266*0Sstevel@tonic-gate break;
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate // Advance to next list entry
270*0Sstevel@tonic-gate // XXX - Is this problemmatic, too?
271*0Sstevel@tonic-gate pos += ep->aptr->GetLength() - off;
272*0Sstevel@tonic-gate } while (TRUE);
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate // Update the byte count and position
275*0Sstevel@tonic-gate pos += (newpos - off); // XXX - recalculate?
276*0Sstevel@tonic-gate return (err);
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate // Write to AudioList is (currently) prohibited
280*0Sstevel@tonic-gate AudioError AudioList::
WriteData(void *,size_t & len,Double &)281*0Sstevel@tonic-gate WriteData(
282*0Sstevel@tonic-gate void*, // destination buffer address
283*0Sstevel@tonic-gate size_t& len, // buffer size (updated)
284*0Sstevel@tonic-gate Double&) // start position (updated)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate len = 0;
287*0Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_NOEFFECT));
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate // Insert an entry at the start
291*0Sstevel@tonic-gate AudioError AudioList::
Insert(Audio * obj)292*0Sstevel@tonic-gate Insert(
293*0Sstevel@tonic-gate Audio* obj) // object to insert
294*0Sstevel@tonic-gate {
295*0Sstevel@tonic-gate Double pos; // insertion offset, in seconds
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate return (Insert(obj, pos = 0.));
298*0Sstevel@tonic-gate }
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate // Insert an entry at a specified position
301*0Sstevel@tonic-gate AudioError AudioList::
Insert(Audio * obj,Double pos)302*0Sstevel@tonic-gate Insert(
303*0Sstevel@tonic-gate Audio* obj, // object to insert
304*0Sstevel@tonic-gate Double pos) // insertion offset, in seconds
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate AudioListEntry *ep;
307*0Sstevel@tonic-gate AudioListEntry *prev;
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate // Find the insertion point
310*0Sstevel@tonic-gate if (first() == 0) {
311*0Sstevel@tonic-gate prev = &head; // this is the first extent
312*0Sstevel@tonic-gate } else {
313*0Sstevel@tonic-gate if (!getposition(pos, prev)) {
314*0Sstevel@tonic-gate if (pos == 0.) {
315*0Sstevel@tonic-gate // Append extent to end of list
316*0Sstevel@tonic-gate return (Append(obj));
317*0Sstevel@tonic-gate } else {
318*0Sstevel@tonic-gate return (RaiseError(AUDIO_ERR_BADARG));
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate } else if (pos != 0.) {
321*0Sstevel@tonic-gate // The insertion is in an extent, split it in two
322*0Sstevel@tonic-gate prev->split(pos);
323*0Sstevel@tonic-gate } else {
324*0Sstevel@tonic-gate // Insert before the current position
325*0Sstevel@tonic-gate prev = prev->prev;
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate // Create object and link into list
329*0Sstevel@tonic-gate ep = new AudioListEntry(obj);
330*0Sstevel@tonic-gate ep->link(prev);
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate return (AUDIO_SUCCESS);
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gate // Append an entry to a list
336*0Sstevel@tonic-gate AudioError AudioList::
Append(Audio * obj)337*0Sstevel@tonic-gate Append(
338*0Sstevel@tonic-gate Audio* obj) // object to append
339*0Sstevel@tonic-gate {
340*0Sstevel@tonic-gate AudioListEntry *ep;
341*0Sstevel@tonic-gate AudioListEntry *prev;
342*0Sstevel@tonic-gate
343*0Sstevel@tonic-gate // Find the last extent in the list
344*0Sstevel@tonic-gate for (prev = &head; prev->next != 0; prev = prev->next)
345*0Sstevel@tonic-gate continue;
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate // Create object and link into list
348*0Sstevel@tonic-gate ep = new AudioListEntry(obj);
349*0Sstevel@tonic-gate ep->link(prev);
350*0Sstevel@tonic-gate return (AUDIO_SUCCESS);
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate // Copy routine for lists
354*0Sstevel@tonic-gate AudioError AudioList::
AsyncCopy(Audio * to,Double & frompos,Double & topos,Double & limit)355*0Sstevel@tonic-gate AsyncCopy(
356*0Sstevel@tonic-gate Audio* to, // audio object to copy to
357*0Sstevel@tonic-gate Double& frompos, // input pos (updated)
358*0Sstevel@tonic-gate Double& topos, // output pos (updated)
359*0Sstevel@tonic-gate Double& limit) // amt to copy (updated)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate AudioListEntry* ep;
362*0Sstevel@tonic-gate Double svlim;
363*0Sstevel@tonic-gate Double newpos;
364*0Sstevel@tonic-gate Double off;
365*0Sstevel@tonic-gate AudioError err;
366*0Sstevel@tonic-gate
367*0Sstevel@tonic-gate svlim = limit;
368*0Sstevel@tonic-gate // Loop until data is returned or error
369*0Sstevel@tonic-gate // XXX - THIS IS WRONG! THE HEADER COULD CHANGE!
370*0Sstevel@tonic-gate do {
371*0Sstevel@tonic-gate // Get the extent and offset for the read position
372*0Sstevel@tonic-gate off = frompos;
373*0Sstevel@tonic-gate if (!getposition(off, ep)) {
374*0Sstevel@tonic-gate // nothing written, limit should reflect this
375*0Sstevel@tonic-gate limit = 0.0;
376*0Sstevel@tonic-gate err = AUDIO_EOF;
377*0Sstevel@tonic-gate err.sys = AUDIO_COPY_INPUT_EOF;
378*0Sstevel@tonic-gate return (err);
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate // Save the offset and do a copy
382*0Sstevel@tonic-gate newpos = off;
383*0Sstevel@tonic-gate limit = svlim;
384*0Sstevel@tonic-gate err = ep->aptr->AsyncCopy(to, newpos, topos, limit);
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate // If no eof on this list entry, or no more data, we're done
387*0Sstevel@tonic-gate if ((err != AUDIO_EOF) || (err.sys != AUDIO_COPY_INPUT_EOF) ||
388*0Sstevel@tonic-gate (ep->next == 0)) {
389*0Sstevel@tonic-gate break;
390*0Sstevel@tonic-gate }
391*0Sstevel@tonic-gate
392*0Sstevel@tonic-gate // Advance to next list entry
393*0Sstevel@tonic-gate // XXX - Is this problemmatic, too?
394*0Sstevel@tonic-gate frompos += ep->aptr->GetLength() - off;
395*0Sstevel@tonic-gate } while (TRUE);
396*0Sstevel@tonic-gate
397*0Sstevel@tonic-gate // Update the byte count and position
398*0Sstevel@tonic-gate frompos += (newpos - off); // XXX - recalculate?
399*0Sstevel@tonic-gate return (err);
400*0Sstevel@tonic-gate }
401