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()
51 int Stream::init(DrainTarget* tdt, int bufsize)
53 outbuf = (UCHAR*) malloc(bufsize);
54 if (!outbuf) return 0;
63 pthread_mutex_init(&mutex, NULL);
65 mutex=CreateMutex(NULL,FALSE,NULL);
77 if (draintarget) draintarget->ResetTimeOffsets();
85 int Stream::put(UCHAR* inbuf, int len)
88 int tail = bufferTail;
89 int head = bufferHead;
90 if (tail == 0) tail = bufferSize;
94 // The free space is in one continuous chunk.
95 if (len < tail - head)
97 memcpy(outbuf + head, inbuf, len);
102 else if (len <= bufferSize - head)
104 // There is enough space above the Head.
105 memcpy(outbuf + head, inbuf, len);
106 if (head + len == bufferSize)
115 memcpy(outbuf, inbuf, len);
122 int Stream::put(UCHAR* inbuf, int len,ULLONG curpos)
125 int tail = bufferTail;
126 int head = bufferHead;
127 if (tail == 0) tail = bufferSize;
129 if (!draintarget) return 0;
130 MediaPacket newPacket;
131 newPacket.length=len;
132 newPacket.pos_buffer=0;
133 newPacket.recording_byte_pos=curpos;
134 newPacket.synched=false;
135 newPacket.disconti=false;
137 newPacket.presentation_time=0;
139 if ((inbuf[7] & 0x80) && len>14 ) {
140 newPacket.synched=true;
141 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
142 ( (ULLONG)(inbuf[10]) << 22 ) |
143 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
144 ( (ULLONG)(inbuf[12]) << 7 ) |
145 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
146 //ok we have the pts now convert it to a continously time code in 100ns units
147 newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
148 newPacket.presentation_time-=draintarget->SetStartOffset(newPacket.presentation_time,&newPacket.disconti);
153 // The free space is in one continuous chunk.
154 if (len < tail - head)
156 memcpy(outbuf + head, inbuf, len);
159 newPacket.pos_buffer=head;
161 mediapackets.push_front(newPacket);
165 else if (len <= bufferSize - head)
167 // There is enough space above the Head.
168 memcpy(outbuf + head, inbuf, len);
169 if (head + len == bufferSize)
174 newPacket.pos_buffer=head;
176 mediapackets.push_front(newPacket);
184 memcpy(outbuf, inbuf, len);
187 newPacket.pos_buffer=0;
189 mediapackets.push_front(newPacket);
201 int head = bufferHead;
202 int tail = bufferTail;
203 int mark = bufferMark;
206 if (mark == -1 && tail > head) mark = bufferSize;
210 // Drain up to the marker.
212 written = draintarget->write(outbuf + tail, (mark - tail));
217 if (written < 0) return ret;
219 if (written == (mark - tail))
222 bufferTail = tail = 0;
226 bufferTail += written;
231 if (tail == head) return ret; // Empty
233 written = draintarget->write(outbuf + tail, (head - tail));
235 written=(head - tail);
238 if (written < 0) return ret;
240 bufferTail = tail + written;
250 // draintarget=dt; // this is now set in init, is this ok?
254 if (mediapackets.empty()) {
257 // using mediapackets, may be this is slower but it is more flexible
258 // while (!mediapackets.empty() && written) {
260 int head = bufferHead;
261 int tail = bufferTail;
262 int mark = bufferMark;
263 if (mark == -1 && tail > head) mark = bufferSize;
265 MediaPacket cur_mp=mediapackets.back();
268 written=draintarget->DeliverMediaSample(cur_mp,outbuf,&cur_packet_pos);
272 if (cur_packet_pos==cur_mp.length) {
275 mediapackets.pop_back();
277 if ((((ULONG)tail)+cur_mp.length) < ((ULONG)mark)) {
278 bufferTail=tail+cur_mp.length;
292 pthread_mutex_lock(&mutex);
293 logger->log("Player", Log::DEBUG, "LOCKED");
296 WaitForSingleObject(mutex, INFINITE );
300 void Stream::unLock()
303 logger->log("Player", Log::DEBUG, "UNLOCKING");
304 pthread_mutex_unlock(&mutex);