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()
48 int Stream::init(DrainTarget* tdt, int bufsize)
50 outbuf = (UCHAR*) malloc(bufsize);
51 if (!outbuf) return 0;
56 pthread_mutex_init(&mutex, NULL);
58 mutex=CreateMutex(NULL,FALSE,NULL);
68 if (draintarget) draintarget->ResetTimeOffsets();
71 int Stream::put(const UCHAR* inbuf, int len, UCHAR type)
74 if (!draintarget) return 0;
75 MediaPacket newPacket;
76 newPacket.length = len;
77 newPacket.pos_buffer = 0;
78 newPacket.type = type;
80 newPacket.synched=false;
82 newPacket.disconti=false;
83 newPacket.presentation_time=0;
85 if (type!=MPTYPE_MPEG_AUDIO_LAYER3) {//no PES
87 if ((inbuf[7] & 0x80) && len>14 ) {
88 newPacket.synched=true;
89 newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
90 ( (ULLONG)(inbuf[10]) << 22 ) |
91 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
92 ( (ULLONG)(inbuf[12]) << 7 ) |
93 ( (ULLONG)(inbuf[13] & 0xFE) >> 1 );
95 //ok we have the pts now convert it to a continously time code in 100ns units
96 newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
97 newPacket.presentation_time-=draintarget->SetStartOffset(newPacket.presentation_time,&newPacket.disconti);
104 if (mediapackets.empty())
106 back = 0; front = bufferSize;
110 front = mediapackets.front().pos_buffer;
111 back = mediapackets.back().pos_buffer + mediapackets.back().length;
112 if (back == bufferSize) back = 0;
118 // The free space (if any) is in one continuous chunk.
119 if (len <= front - back) ret = len; // Is there enough of it?
121 else if (len <= bufferSize - back)
123 // There is enough space at the end of the buffer
126 else if (len <= front)
128 // There is enough space at the start of the buffer
133 if (ret) // Nonzero if we managed to find room for the packet
135 memcpy(outbuf + back, inbuf, len);
136 newPacket.pos_buffer = back;
138 mediapackets.push_back(newPacket);
148 UINT listlength = mediapackets.size();
151 draintarget->PrepareMediaSample(mediapackets, cur_packet_pos);
153 UINT consumed = draintarget->DeliverMediaSample(outbuf, &cur_packet_pos);
155 if (consumed != 0) ret = true;
156 if (consumed > listlength) consumed = listlength;
157 while (consumed--) mediapackets.pop_front();
166 pthread_mutex_lock(&mutex);
167 //logger->log("Player", Log::DEBUG, "LOCKED");
170 WaitForSingleObject(mutex, INFINITE );
174 void Stream::unLock()
177 //logger->log("Player", Log::DEBUG, "UNLOCKING");
178 pthread_mutex_unlock(&mutex);