2 Copyright 2005-2006 Mark Calderbank
\r
4 This file is part of VOMP.
\r
6 VOMP is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 VOMP is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with VOMP; if not, write to the Free Software
\r
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
36 void Stream::shutdown()
\r
49 int Stream::init(DrainTarget* tdt, int bufsize)
\r
51 outbuf = (UCHAR*) malloc(bufsize);
\r
52 if (!outbuf) return 0;
\r
54 bufferSize = bufsize;
\r
57 pthread_mutex_init(&mutex, NULL);
\r
59 mutex=CreateMutex(NULL,FALSE,NULL);
\r
64 void Stream::flush()
\r
68 mediapackets.clear();
\r
70 if (draintarget) draintarget->ResetTimeOffsets();
\r
73 int Stream::put(const UCHAR* inbuf, int len, UCHAR type,unsigned int index)
\r
76 if (!draintarget) return 0;
\r
77 MediaPacket newPacket;
\r
78 newPacket.length = len;
\r
79 newPacket.pos_buffer = 0;
\r
80 newPacket.type = type;
\r
83 newPacket.synched=false;
\r
84 newPacket.index=index;
\r
85 #ifndef VOMP_PLATTFORM_MVP
\r
86 newPacket.disconti=false;
\r
87 newPacket.presentation_time=0;
\r
89 if (type!=MPTYPE_MPEG_AUDIO_LAYER3) {//no PES
\r
90 //Extract the pts...
\r
92 if ((inbuf[7] & 0x80) && len>14 ) {
\r
93 newPacket.synched=true;
\r
94 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
\r
95 ( (ULLONG)(inbuf[10]) << 22 ) |
\r
96 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
\r
97 ( (ULLONG)(inbuf[12]) << 7 ) |
\r
98 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
\r
99 if ((inbuf[7] & 0x40) && len>19) {
\r
100 newPacket.dts=((ULLONG)(inbuf[14] & 0x0E) << 29 ) |
\r
101 ( (ULLONG)(inbuf[15]) << 22 ) |
\r
102 ( (ULLONG)(inbuf[16] & 0xFE) << 14 ) |
\r
103 ( (ULLONG)(inbuf[17]) << 7 ) |
\r
104 ( (ULLONG)(inbuf[18] & 0xFE) >> 1 );
\r
107 #ifndef VOMP_PLATTFORM_MVP
\r
108 //ok we have the pts now convert it to a continously time code in 100ns units
\r
109 if (hasdts && draintarget->dtsTimefix()) newPacket.presentation_time=(ULLONG)(newPacket.dts*10000LL/90LL);
\r
110 else newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
\r
112 //newPacket.presentation_time-=draintarget->SetStartOffset((ULLONG)(newPacket.pts*10000LL/90LL),&newPacket.disconti);
\r
113 newPacket.presentation_time-=draintarget->SetStartOffset((ULLONG)(newPacket.pts*10000LL/90LL),&newPacket.disconti);
\r
120 if (mediapackets.empty())
\r
122 back = 0; front = bufferSize;
\r
126 front = mediapackets.front().pos_buffer;
\r
127 back = mediapackets.back().pos_buffer + mediapackets.back().length;
\r
128 if (back == bufferSize) back = 0;
\r
134 // The free space (if any) is in one continuous chunk.
\r
135 if (len <= front - back) ret = len; // Is there enough of it?
\r
137 else if (len <= bufferSize - back)
\r
139 // There is enough space at the end of the buffer
\r
142 else if (len <= front)
\r
144 // There is enough space at the start of the buffer
\r
149 if (ret) // Nonzero if we managed to find room for the packet
\r
151 memcpy(outbuf + back, inbuf, len);
\r
152 newPacket.pos_buffer = back;
\r
154 mediapackets.push_back(newPacket);
\r
157 // Log::getInstance()->log("Stream", Log::DEBUG, "We are full %d!",bufferSize);
\r
163 bool Stream::drain(bool * dataavail)
\r
166 if (dataavail) *dataavail=false;
\r
168 UINT listlength = mediapackets.size();
\r
169 if (listlength != 0)
\r
171 draintarget->PrepareMediaSample(mediapackets, cur_packet_pos);
\r
173 if (dataavail && draintarget->DrainTargetReady()) *dataavail=true;
\r
174 UINT consumed = draintarget->DeliverMediaSample(outbuf, &cur_packet_pos);
\r
176 if (consumed != 0) ret = true;
\r
177 if (consumed > listlength) consumed = listlength;
\r
178 while (consumed--)
\r
180 mediapackets.pop_front();
\r
187 void Stream::lock()
\r
190 pthread_mutex_lock(&mutex);
\r
191 //logger->log("Player", Log::DEBUG, "LOCKED");
\r
194 WaitForSingleObject(mutex, INFINITE );
\r
198 void Stream::unLock()
\r
201 //logger->log("Player", Log::DEBUG, "UNLOCKING");
\r
202 pthread_mutex_unlock(&mutex);
\r
204 ReleaseMutex(mutex);
\r