2 Copyright 2005-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
36 void Stream::shutdown()
49 int Stream::init(DrainTarget* tdt, int bufsize)
51 outbuf = (UCHAR*) malloc(bufsize);
52 if (!outbuf) return 0;
57 pthread_mutex_init(&mutex, NULL);
59 mutex=CreateMutex(NULL,FALSE,NULL);
71 if (draintarget) draintarget->ResetTimeOffsets();
74 int Stream::put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index)
77 if (!draintarget) return 0;
78 MediaPacket newPacket;
79 newPacket.length = len;
80 newPacket.pos_buffer = 0;
81 newPacket.type = type;
84 newPacket.synched=false;
85 newPacket.index=index;
86 #ifndef VOMP_PLATTFORM_MVP
87 newPacket.disconti=false;
88 newPacket.presentation_time=0;
90 if (type!=MPTYPE_MPEG_AUDIO_LAYER3) {//no PES
93 if ((inbuf[7] & 0x80) && len>14 ) {
94 newPacket.synched=true;
95 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
96 ( (ULLONG)(inbuf[10]) << 22 ) |
97 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
98 ( (ULLONG)(inbuf[12]) << 7 ) |
99 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
100 if ((inbuf[7] & 0x40) && len>19) {
101 newPacket.dts=((ULLONG)(inbuf[14] & 0x0E) << 29 ) |
102 ( (ULLONG)(inbuf[15]) << 22 ) |
103 ( (ULLONG)(inbuf[16] & 0xFE) << 14 ) |
104 ( (ULLONG)(inbuf[17]) << 7 ) |
105 ( (ULLONG)(inbuf[18] & 0xFE) >> 1 );
108 #ifndef VOMP_PLATTFORM_MVP
109 //ok we have the pts now convert it to a continously time code in 100ns units
110 if (hasdts && draintarget->dtsTimefix()) newPacket.presentation_time=(ULLONG)(newPacket.dts*10000LL/90LL);
111 else newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
113 //newPacket.presentation_time-=draintarget->SetStartOffset((ULLONG)(newPacket.pts*10000LL/90LL),&newPacket.disconti);
114 newPacket.presentation_time-=draintarget->SetStartOffset((ULLONG)(newPacket.pts*10000LL/90LL),&newPacket.disconti);
121 if (mediapackets.empty())
123 back = 0; front = bufferSize;
127 front = mediapackets.front().pos_buffer;
128 back = mediapackets.back().pos_buffer + mediapackets.back().length;
129 if (back == bufferSize) back = 0;
135 // The free space (if any) is in one continuous chunk.
136 if (len <= front - back) ret = len; // Is there enough of it?
138 else if (len <= bufferSize - back)
140 // There is enough space at the end of the buffer
143 else if (len <= front)
145 // There is enough space at the start of the buffer
150 if (ret) // Nonzero if we managed to find room for the packet
152 memcpy(outbuf + back, inbuf, len);
153 newPacket.pos_buffer = back;
155 mediapackets.push_back(newPacket);
158 // Log::getInstance()->log("Stream", Log::DEBUG, "We are full %d!",bufferSize);
164 bool Stream::drain(bool * dataavail)
167 if (dataavail) *dataavail=false;
168 if (draintarget->DrainTargetBufferFull()) return false; //We are full, no need to do something else
170 UINT listlength = mediapackets.size();
173 draintarget->PrepareMediaSample(mediapackets, cur_packet_pos);
175 if (dataavail && draintarget->DrainTargetReady()) *dataavail=true;
176 UINT consumed = draintarget->DeliverMediaSample(outbuf, &cur_packet_pos);
178 if (consumed != 0) ret = true;
179 if (consumed > listlength) consumed = listlength;
182 mediapackets.pop_front();
192 pthread_mutex_lock(&mutex);
193 //logger->log("Player", Log::DEBUG, "LOCKED");
196 WaitForSingleObject(mutex, INFINITE );
200 void Stream::unLock()
203 //logger->log("Player", Log::DEBUG, "UNLOCKING");
204 pthread_mutex_unlock(&mutex);