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)
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.synched=false;
134 newPacket.disconti=false;
136 newPacket.presentation_time=0;
138 if ((inbuf[7] & 0x80) && len>14 ) {
139 newPacket.synched=true;
140 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
141 ( (ULLONG)(inbuf[10]) << 22 ) |
142 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
143 ( (ULLONG)(inbuf[12]) << 7 ) |
144 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
145 //ok we have the pts now convert it to a continously time code in 100ns units
146 newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
147 newPacket.presentation_time-=draintarget->SetStartOffset(newPacket.presentation_time,&newPacket.disconti);
152 // The free space is in one continuous chunk.
153 if (len < tail - head)
155 memcpy(outbuf + head, inbuf, len);
158 newPacket.pos_buffer=head;
160 mediapackets.push_front(newPacket);
164 else if (len <= bufferSize - head)
166 // There is enough space above the Head.
167 memcpy(outbuf + head, inbuf, len);
168 if (head + len == bufferSize)
173 newPacket.pos_buffer=head;
175 mediapackets.push_front(newPacket);
183 memcpy(outbuf, inbuf, len);
186 newPacket.pos_buffer=0;
188 mediapackets.push_front(newPacket);
200 int head = bufferHead;
201 int tail = bufferTail;
202 int mark = bufferMark;
205 if (mark == -1 && tail > head) mark = bufferSize;
209 // Drain up to the marker.
211 written = draintarget->write(outbuf + tail, (mark - tail));
216 if (written < 0) return ret;
218 if (written == (mark - tail))
221 bufferTail = tail = 0;
225 bufferTail += written;
230 if (tail == head) return ret; // Empty
232 written = draintarget->write(outbuf + tail, (head - tail));
234 written=(head - tail);
237 if (written < 0) return ret;
239 bufferTail = tail + written;
249 // draintarget=dt; // this is now set in init, is this ok?
253 if (mediapackets.empty()) {
256 // using mediapackets, may be this is slower but it is more flexible
257 // while (!mediapackets.empty() && written) {
259 int head = bufferHead;
260 int tail = bufferTail;
261 int mark = bufferMark;
262 if (mark == -1 && tail > head) mark = bufferSize;
264 MediaPacket cur_mp=mediapackets.back();
267 written=draintarget->DeliverMediaSample(cur_mp,outbuf,&cur_packet_pos);
271 if (cur_packet_pos==cur_mp.length) {
274 mediapackets.pop_back();
276 if ((((ULONG)tail)+cur_mp.length) < ((ULONG)mark)) {
277 bufferTail=tail+cur_mp.length;
291 pthread_mutex_lock(&mutex);
292 logger->log("Player", Log::DEBUG, "LOCKED");
295 WaitForSingleObject(mutex, INFINITE );
299 void Stream::unLock()
302 logger->log("Player", Log::DEBUG, "UNLOCKING");
303 pthread_mutex_unlock(&mutex);