]> git.vomp.tv Git - vompclient.git/blob - stream.cc
Fix delete
[vompclient.git] / stream.cc
1 /*
2     Copyright 2005 Mark Calderbank
3
4     This file is part of VOMP.
5
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.
10
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.
15
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
19 */
20
21 #include "stream.h"
22
23 Stream::Stream()
24 {
25   initted = 0;
26 #ifdef NEW_DEMUXER
27   cur_packet_pos=0;
28   draintarget=NULL;
29 #endif
30 }
31
32 Stream::~Stream()
33 {
34   shutdown();
35 }
36
37 void Stream::shutdown()
38 {
39   if (initted) free(outbuf);
40   initted = 0;
41
42 }
43
44 int Stream::init(int bufsize)
45 {
46   outbuf = (UCHAR*) malloc(bufsize);
47   if (!outbuf) return 0;
48   bufferSize = bufsize;
49   bufferHead = 0;
50   bufferTail = 0;
51   bufferMark = -1;
52   initted = 1;
53   return 1;
54 }
55
56 void Stream::flush()
57 {
58 #ifdef NEW_DEMUXER
59   mediapackets.clear();
60   if (draintarget) draintarget->ResetTimeOffsets();
61 #endif
62   bufferHead = 0;
63   bufferTail = 0;
64   bufferMark = -1;
65 }
66
67 #ifndef NEW_DEMUXER
68 int Stream::put(UCHAR* inbuf, int len)
69 {
70   int ret = 0;
71   int tail = bufferTail;
72   int head = bufferHead;
73   if (tail == 0) tail = bufferSize;
74
75   if (head < tail)
76   {
77     // The free space is in one continuous chunk.
78     if (len < tail - head)
79     {
80       memcpy(outbuf + head, inbuf, len);
81       bufferHead += len;
82       ret = len;
83     }
84   }
85   else if (len <= bufferSize - head)
86   {
87     // There is enough space above the Head.
88     memcpy(outbuf + head, inbuf, len);
89     if (head + len == bufferSize)
90       bufferHead = 0;
91     else
92       bufferHead += len;
93     ret = len;
94   }
95   else if (len < tail)
96   {
97     bufferMark = head;
98     memcpy(outbuf, inbuf, len);
99     bufferHead = len;
100     ret = len;
101   }
102   return ret;
103 }
104 #else
105 int Stream::put(UCHAR* inbuf, int len,ULLONG curpos)
106 {
107   int ret = 0;
108   int tail = bufferTail;
109   int head = bufferHead;
110   if (tail == 0) tail = bufferSize;
111
112   if (!draintarget) return 0;
113   MediaPacket newPacket;
114   newPacket.length=len;
115   newPacket.pos_buffer=0;
116   newPacket.recording_byte_pos=curpos;
117   newPacket.synched=false;
118   newPacket.disconti=false;
119   newPacket.pts=0;
120   newPacket.presentation_time=0;
121   //Extract the pts...
122   if ((inbuf[7] & 0x80) && len>14 ) {
123     newPacket.synched=true;
124     newPacket.pts=((ULLONG)(inbuf[9] & 0x0E) << 29 ) |
125                 ( (ULLONG)(inbuf[10])        << 22 ) |
126                 ( (ULLONG)(inbuf[11] & 0xFE) << 14 ) |
127                 ( (ULLONG)(inbuf[12])        <<  7 ) |
128                 ( (ULLONG)(inbuf[13] & 0xFE) >>  1 );
129   //ok we have the pts now convert it to a continously time code in 100ns units
130     newPacket.presentation_time=(ULLONG)(newPacket.pts*10000LL/90LL);
131     newPacket.presentation_time-=draintarget->SetStartOffset(newPacket.presentation_time,&newPacket.disconti);
132   }
133
134   if (head < tail)
135   {
136     // The free space is in one continuous chunk.
137     if (len < tail - head)
138     {
139       memcpy(outbuf + head, inbuf, len);
140       bufferHead += len;
141       ret = len;
142       newPacket.pos_buffer=head;
143       mediapackets.push_front(newPacket);
144     }
145   }
146   else if (len <= bufferSize - head)
147   {
148     // There is enough space above the Head.
149     memcpy(outbuf + head, inbuf, len);
150     if (head + len == bufferSize)
151       bufferHead = 0;
152     else
153       bufferHead += len;
154
155     newPacket.pos_buffer=head;
156     mediapackets.push_front(newPacket);
157
158     ret = len;
159   }
160   else if (len < tail)
161   {
162     bufferMark = head;
163     memcpy(outbuf, inbuf, len);
164     bufferHead = len;
165     ret = len;
166     newPacket.pos_buffer=0;
167     mediapackets.push_front(newPacket);
168   }
169   return ret;
170 }
171 #endif
172
173 #ifndef NEW_DEMUXER
174
175 int Stream::drain(int fd)
176 {
177   int ret = 0;
178   int head = bufferHead;
179   int tail = bufferTail;
180   int mark = bufferMark;
181   int written;
182
183   if (mark == -1 && tail > head) mark = bufferSize;
184
185   if (mark >= 0)
186   {
187     // Drain up to the marker.
188 #ifndef WIN32
189     written = write(fd, outbuf + tail, (mark - tail));
190 #else
191     written=mark-tail;
192     MILLISLEEP(1);
193 #endif
194     if (written < 0) return ret;
195     ret += written;
196     if (written == (mark - tail))
197     {
198       bufferMark = -1;
199       bufferTail = tail = 0;
200     }
201     else
202     {
203       bufferTail += written;
204       return ret;
205     }
206   }
207
208   if (tail == head) return ret; // Empty
209 #ifndef WIN32
210   written = write(fd, outbuf + tail, (head - tail));
211 #else
212   written=(head - tail);
213   MILLISLEEP(1);
214 #endif
215   if (written < 0) return ret;
216   ret += written;
217   bufferTail = tail + written;
218   return ret;
219 }
220 #else
221 int Stream::drain(DrainTarget* dt)
222 {
223   int ret = 0;
224   int written=1;
225   draintarget=dt;
226
227
228
229   if (mediapackets.empty()) {
230     return 0;
231   }
232    // using mediapackets, may be this is slower but it is more flexible
233  // while (!mediapackets.empty() && written) {
234
235   int head = bufferHead;
236   int tail = bufferTail;
237   int mark = bufferMark;
238   if (mark == -1 && tail > head) mark = bufferSize;
239   MediaPacket cur_mp=mediapackets.back();
240   written=0;
241   written=dt->DeliverMediaSample(cur_mp,outbuf,&cur_packet_pos);
242
243   ret+=written;
244
245   if (cur_packet_pos==cur_mp.length) {
246     cur_packet_pos=0;
247     mediapackets.pop_back();
248     if ((((ULONG)tail)+cur_mp.length) < ((ULONG)mark)) {
249       bufferTail=tail+cur_mp.length;
250     } else {
251       bufferTail=0;
252       bufferMark=-1;
253     }
254
255   }
256
257   return ret;
258 }
259
260 #endif