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);
70 if (draintarget) draintarget->ResetTimeOffsets();
73 int Stream::put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index)
76 if (!draintarget) return 0;
77 MediaPacket newPacket;
78 newPacket.length = len;
79 newPacket.pos_buffer = 0;
80 newPacket.type = type;
83 newPacket.synched=false;
84 newPacket.index=index;
85 #ifndef VOMP_PLATTFORM_MVP
86 newPacket.disconti=false;
87 newPacket.presentation_time=0;
89 if (type!=MPTYPE_MPEG_AUDIO_LAYER3) {//no PES
92 if ((inbuf[7] & 0x80) && len>14 ) {
93 newPacket.synched=true;
94 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
95 ( (ULLONG)(inbuf[10]) << 22 ) |
96 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
97 ( (ULLONG)(inbuf[12]) << 7 ) |
98 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
99 if ((inbuf[7] & 0x40) && len>19) {
100 newPacket.dts=((ULLONG)(inbuf[14] & 0x0E) << 29 ) |
101 ( (ULLONG)(inbuf[15]) << 22 ) |
102 ( (ULLONG)(inbuf[16] & 0xFE) << 14 ) |
103 ( (ULLONG)(inbuf[17]) << 7 ) |
104 ( (ULLONG)(inbuf[18] & 0xFE) >> 1 );
107 #ifndef VOMP_PLATTFORM_MVP
108 //ok we have the pts now convert it to a continously time code in 100ns units
109 if (hasdts && draintarget->dtsTimefix()) newPacket.presentation_time=(ULLONG)(newPacket.dts*10000LL/90LL);
110 else newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
112 //newPacket.presentation_time-=draintarget->SetStartOffset((ULLONG)(newPacket.pts*10000LL/90LL),&newPacket.disconti);
113 newPacket.presentation_time-=draintarget->SetStartOffset((ULLONG)(newPacket.pts*10000LL/90LL),&newPacket.disconti);
120 if (mediapackets.empty())
122 back = 0; front = bufferSize;
126 front = mediapackets.front().pos_buffer;
127 back = mediapackets.back().pos_buffer + mediapackets.back().length;
128 if (back == bufferSize) back = 0;
134 // The free space (if any) is in one continuous chunk.
135 if (len <= front - back) ret = len; // Is there enough of it?
137 else if (len <= bufferSize - back)
139 // There is enough space at the end of the buffer
142 else if (len <= front)
144 // There is enough space at the start of the buffer
149 if (ret) // Nonzero if we managed to find room for the packet
151 memcpy(outbuf + back, inbuf, len);
152 newPacket.pos_buffer = back;
154 mediapackets.push_back(newPacket);
157 // Log::getInstance()->log("Stream", Log::DEBUG, "We are full %d!",bufferSize);
163 bool Stream::drain(bool * dataavail)
166 if (dataavail) *dataavail=false;
167 if (draintarget->DrainTargetBufferFull()) return false; //We are full, no need to do something else
169 UINT listlength = mediapackets.size();
172 draintarget->PrepareMediaSample(mediapackets, cur_packet_pos);
174 if (dataavail && draintarget->DrainTargetReady()) *dataavail=true;
175 UINT consumed = draintarget->DeliverMediaSample(outbuf, &cur_packet_pos);
177 if (consumed != 0) ret = true;
178 if (consumed > listlength) consumed = listlength;
181 mediapackets.pop_front();
191 pthread_mutex_lock(&mutex);
192 //logger->log("Player", Log::DEBUG, "LOCKED");
195 WaitForSingleObject(mutex, INFINITE );
199 void Stream::unLock()
202 //logger->log("Player", Log::DEBUG, "UNLOCKING");
203 pthread_mutex_unlock(&mutex);