2 Copyright 2005 Mark Calderbank
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 void Stream::shutdown()
39 if (initted) free(outbuf);
44 int Stream::init(DrainTarget* tdt, int bufsize)
46 outbuf = (UCHAR*) malloc(bufsize);
47 if (!outbuf) return 0;
61 if (draintarget) draintarget->ResetTimeOffsets();
69 int Stream::put(UCHAR* inbuf, int len)
72 int tail = bufferTail;
73 int head = bufferHead;
74 if (tail == 0) tail = bufferSize;
78 // The free space is in one continuous chunk.
79 if (len < tail - head)
81 memcpy(outbuf + head, inbuf, len);
86 else if (len <= bufferSize - head)
88 // There is enough space above the Head.
89 memcpy(outbuf + head, inbuf, len);
90 if (head + len == bufferSize)
99 memcpy(outbuf, inbuf, len);
106 int Stream::put(UCHAR* inbuf, int len,ULLONG curpos)
109 int tail = bufferTail;
110 int head = bufferHead;
111 if (tail == 0) tail = bufferSize;
113 if (!draintarget) return 0;
114 MediaPacket newPacket;
115 newPacket.length=len;
116 newPacket.pos_buffer=0;
117 newPacket.recording_byte_pos=curpos;
118 newPacket.synched=false;
119 newPacket.disconti=false;
121 newPacket.presentation_time=0;
123 if ((inbuf[7] & 0x80) && len>14 ) {
124 newPacket.synched=true;
125 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
126 ( (ULLONG)(inbuf[10]) << 22 ) |
127 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
128 ( (ULLONG)(inbuf[12]) << 7 ) |
129 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
130 //ok we have the pts now convert it to a continously time code in 100ns units
131 newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
132 newPacket.presentation_time-=draintarget->SetStartOffset(newPacket.presentation_time,&newPacket.disconti);
137 // The free space is in one continuous chunk.
138 if (len < tail - head)
140 memcpy(outbuf + head, inbuf, len);
143 newPacket.pos_buffer=head;
144 mediapackets.push_front(newPacket);
147 else if (len <= bufferSize - head)
149 // There is enough space above the Head.
150 memcpy(outbuf + head, inbuf, len);
151 if (head + len == bufferSize)
156 newPacket.pos_buffer=head;
157 mediapackets.push_front(newPacket);
164 memcpy(outbuf, inbuf, len);
167 newPacket.pos_buffer=0;
168 mediapackets.push_front(newPacket);
179 int head = bufferHead;
180 int tail = bufferTail;
181 int mark = bufferMark;
184 if (mark == -1 && tail > head) mark = bufferSize;
188 // Drain up to the marker.
190 written = draintarget->write(outbuf + tail, (mark - tail));
195 if (written < 0) return ret;
197 if (written == (mark - tail))
200 bufferTail = tail = 0;
204 bufferTail += written;
209 if (tail == head) return ret; // Empty
211 written = draintarget->write(outbuf + tail, (head - tail));
213 written=(head - tail);
216 if (written < 0) return ret;
218 bufferTail = tail + written;
228 // draintarget=dt; // this is now set in init, is this ok?
232 if (mediapackets.empty()) {
235 // using mediapackets, may be this is slower but it is more flexible
236 // while (!mediapackets.empty() && written) {
238 int head = bufferHead;
239 int tail = bufferTail;
240 int mark = bufferMark;
241 if (mark == -1 && tail > head) mark = bufferSize;
242 MediaPacket cur_mp=mediapackets.back();
244 written=dt->DeliverMediaSample(cur_mp,outbuf,&cur_packet_pos);
248 if (cur_packet_pos==cur_mp.length) {
250 mediapackets.pop_back();
251 if ((((ULONG)tail)+cur_mp.length) < ((ULONG)mark)) {
252 bufferTail=tail+cur_mp.length;