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(int bufsize)
46 outbuf = (UCHAR*) malloc(bufsize);
47 if (!outbuf) return 0;
60 if (draintarget) draintarget->ResetTimeOffsets();
68 int Stream::put(UCHAR* inbuf, int len)
71 int tail = bufferTail;
72 int head = bufferHead;
73 if (tail == 0) tail = bufferSize;
77 // The free space is in one continuous chunk.
78 if (len < tail - head)
80 memcpy(outbuf + head, inbuf, len);
85 else if (len <= bufferSize - head)
87 // There is enough space above the Head.
88 memcpy(outbuf + head, inbuf, len);
89 if (head + len == bufferSize)
98 memcpy(outbuf, inbuf, len);
105 int Stream::put(UCHAR* inbuf, int len,ULLONG curpos)
108 int tail = bufferTail;
109 int head = bufferHead;
110 if (tail == 0) tail = bufferSize;
112 if (!draintarget) return 0;
113 MediaPacket newPacket;
114 newPacket.length=len;
115 newPacket.pos_buffer=0;
116 newPacket.recording_byte_pos=curpos;
117 newPacket.synched=false;
118 newPacket.disconti=false;
120 newPacket.presentation_time=0;
122 if ((inbuf[7] & 0x80) && len>14 ) {
123 newPacket.synched=true;
124 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
125 ( (ULLONG)(inbuf[10]) << 22 ) |
126 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
127 ( (ULLONG)(inbuf[12]) << 7 ) |
128 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
129 //ok we have the pts now convert it to a continously time code in 100ns units
130 newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
131 newPacket.presentation_time-=draintarget->SetStartOffset(newPacket.presentation_time,&newPacket.disconti);
136 // The free space is in one continuous chunk.
137 if (len < tail - head)
139 memcpy(outbuf + head, inbuf, len);
142 newPacket.pos_buffer=head;
143 mediapackets.push_front(newPacket);
146 else if (len <= bufferSize - head)
148 // There is enough space above the Head.
149 memcpy(outbuf + head, inbuf, len);
150 if (head + len == bufferSize)
155 newPacket.pos_buffer=head;
156 mediapackets.push_front(newPacket);
163 memcpy(outbuf, inbuf, len);
166 newPacket.pos_buffer=0;
167 mediapackets.push_front(newPacket);
175 int Stream::drain(int fd)
178 int head = bufferHead;
179 int tail = bufferTail;
180 int mark = bufferMark;
183 if (mark == -1 && tail > head) mark = bufferSize;
187 // Drain up to the marker.
189 written = write(fd, outbuf + tail, (mark - tail));
194 if (written < 0) return ret;
196 if (written == (mark - tail))
199 bufferTail = tail = 0;
203 bufferTail += written;
208 if (tail == head) return ret; // Empty
210 written = write(fd, outbuf + tail, (head - tail));
212 written=(head - tail);
215 if (written < 0) return ret;
217 bufferTail = tail + written;
221 int Stream::drain(DrainTarget* dt)
229 if (mediapackets.empty()) {
232 // using mediapackets, may be this is slower but it is more flexible
233 // while (!mediapackets.empty() && written) {
235 int head = bufferHead;
236 int tail = bufferTail;
237 int mark = bufferMark;
238 if (mark == -1 && tail > head) mark = bufferSize;
239 MediaPacket cur_mp=mediapackets.back();
241 written=dt->DeliverMediaSample(cur_mp,outbuf,&cur_packet_pos);
245 if (cur_packet_pos==cur_mp.length) {
247 mediapackets.pop_back();
248 if ((((ULONG)tail)+cur_mp.length) < ((ULONG)mark)) {
249 bufferTail=tail+cur_mp.length;