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);
43 int Stream::init(int bufsize)
45 outbuf = (UCHAR*) malloc(bufsize);
46 if (!outbuf) return 0;
66 int Stream::put(UCHAR* inbuf, int len)
69 int tail = bufferTail;
70 int head = bufferHead;
71 if (tail == 0) tail = bufferSize;
75 // The free space is in one continuous chunk.
76 if (len < tail - head)
78 memcpy(outbuf + head, inbuf, len);
83 else if (len <= bufferSize - head)
85 // There is enough space above the Head.
86 memcpy(outbuf + head, inbuf, len);
87 if (head + len == bufferSize)
96 memcpy(outbuf, inbuf, len);
103 int Stream::put(UCHAR* inbuf, int len)
106 int tail = bufferTail;
107 int head = bufferHead;
108 if (tail == 0) tail = bufferSize;
110 if (!draintarget) return 0;
111 MediaPacket newPacket;
112 newPacket.length=len;
113 newPacket.pos_buffer=0;
114 newPacket.recording_byte_pos=0;
115 newPacket.synched=false;
116 newPacket.disconti=false;
118 newPacket.presentation_time=0;
120 if ((inbuf[7] & 0x80) && len>14 ) {
121 newPacket.synched=true;
122 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
123 ( (ULLONG)(inbuf[10]) << 22 ) |
124 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
125 ( (ULLONG)(inbuf[12]) << 7 ) |
126 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
127 //ok we have the pts now convert it to a continously time code in 100ns units
128 newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
129 newPacket.presentation_time-=draintarget->SetStartOffset(newPacket.presentation_time,&newPacket.disconti);
134 // The free space is in one continuous chunk.
135 if (len < tail - head)
137 memcpy(outbuf + head, inbuf, len);
140 newPacket.pos_buffer=head;
141 mediapackets.push_front(newPacket);
144 else if (len <= bufferSize - head)
146 // There is enough space above the Head.
147 memcpy(outbuf + head, inbuf, len);
148 if (head + len == bufferSize)
153 newPacket.pos_buffer=head;
154 mediapackets.push_front(newPacket);
161 memcpy(outbuf, inbuf, len);
164 newPacket.pos_buffer=0;
165 mediapackets.push_front(newPacket);
173 int Stream::drain(int fd)
176 int head = bufferHead;
177 int tail = bufferTail;
178 int mark = bufferMark;
181 if (mark == -1 && tail > head) mark = bufferSize;
185 // Drain up to the marker.
187 written = write(fd, outbuf + tail, (mark - tail));
192 if (written < 0) return ret;
194 if (written == (mark - tail))
197 bufferTail = tail = 0;
201 bufferTail += written;
206 if (tail == head) return ret; // Empty
208 written = write(fd, outbuf + tail, (head - tail));
210 written=(head - tail);
213 if (written < 0) return ret;
215 bufferTail = tail + written;
219 int Stream::drain(DrainTarget* dt)
227 if (mediapackets.empty()) {
230 // using mediapackets, may be this is slower but it is more flexible
231 // while (!mediapackets.empty() && written) {
233 int head = bufferHead;
234 int tail = bufferTail;
235 int mark = bufferMark;
236 if (mark == -1 && tail > head) mark = bufferSize;
237 MediaPacket cur_mp=mediapackets.back();
239 written=dt->DeliverMediaSample(cur_mp,outbuf,&cur_packet_pos);
243 if (cur_packet_pos==cur_mp.length) {
245 mediapackets.pop_back();
246 // if ((int)(tail+cur_mp.length) < mark) {
247 if ((((ULONG)tail)+cur_mp.length) < ((ULONG)mark))
249 bufferTail=tail+cur_mp.length;