Merge pull request #26354 from ksooo/pvr-fix-listitem-titleextrainfo
[xbmc.git] / lib / libUPnP / Platinum / Source / Extras / PltRingBufferStream.cpp
blob92eb355ff9f61fceb6a61c29d3287fc9df988815
1 /*****************************************************************
3 | Platinum - Ring Buffer Stream
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 | licensing@plutinosoft.com
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
33 ****************************************************************/
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "PltRingBufferStream.h"
39 #include "Neptune.h"
41 /*----------------------------------------------------------------------
42 | defines
43 +---------------------------------------------------------------------*/
44 #ifdef max
45 #undef max
46 #endif
47 #define max(a,b) (((a) > (b)) ? (a) : (b))
49 #ifdef min
50 #undef min
51 #endif
52 #define min(a,b) (((a) < (b)) ? (a) : (b))
54 /*----------------------------------------------------------------------
55 | PLT_RingBufferStream::PLT_RingBufferStream
56 +---------------------------------------------------------------------*/
57 PLT_RingBufferStream::PLT_RingBufferStream(NPT_Size buffer_size,
58 bool blocking /* = true */) :
59 m_TotalBytesRead(0),
60 m_TotalBytesWritten(0),
61 m_Blocking(blocking),
62 m_Eos(false),
63 m_Aborted(false)
65 m_RingBuffer = new NPT_RingBuffer(buffer_size);
68 /*----------------------------------------------------------------------
69 | PLT_RingBufferStream::PLT_RingBufferStream
70 +---------------------------------------------------------------------*/
71 PLT_RingBufferStream::PLT_RingBufferStream(NPT_RingBufferReference& buffer,
72 bool blocking /* = true */) :
73 m_RingBuffer(buffer),
74 m_TotalBytesRead(0),
75 m_TotalBytesWritten(0),
76 m_Blocking(blocking),
77 m_Eos(false),
78 m_Aborted(false)
82 /*----------------------------------------------------------------------
83 | PLT_RingBufferStream::~PLT_RingBufferStream
84 +---------------------------------------------------------------------*/
85 PLT_RingBufferStream::~PLT_RingBufferStream()
89 /*----------------------------------------------------------------------
90 | PLT_RingBufferStream::Read
91 +---------------------------------------------------------------------*/
92 NPT_Result
93 PLT_RingBufferStream::Read(void* buffer,
94 NPT_Size max_bytes_to_read,
95 NPT_Size* _bytes_read /*= NULL*/)
97 NPT_Size bytes_to_read;
98 NPT_Size bytes_read = 0;
100 // reset output param first
101 if (_bytes_read) *_bytes_read = 0;
103 // wait for data
104 do {
106 NPT_AutoLock autoLock(m_Lock);
108 if (m_Aborted) {
109 return NPT_ERROR_INTERRUPTED;
112 // check for data
113 if (m_RingBuffer->GetAvailable())
114 break;
116 if (m_Eos) {
117 return NPT_ERROR_EOS;
118 } else if (!m_Blocking) {
119 return NPT_ERROR_WOULD_BLOCK;
123 // sleep and try again
124 NPT_System::Sleep(NPT_TimeInterval(.1));
125 } while (1);
128 NPT_AutoLock autoLock(m_Lock);
130 // try twice in case available data was not contiguous
131 for (int i=0; i<2; i++) {
132 bytes_to_read = min(max_bytes_to_read - bytes_read, m_RingBuffer->GetContiguousAvailable());
134 // break if nothing to read the second time
135 if (bytes_to_read == 0) break;
137 // read into buffer and advance
138 NPT_CHECK(m_RingBuffer->Read((unsigned char*)buffer+bytes_read, bytes_to_read));
140 // keep track of the total bytes we have read so far
141 m_TotalBytesRead += bytes_to_read;
142 bytes_read += bytes_to_read;
144 if (_bytes_read) *_bytes_read += bytes_to_read;
148 // we have read some chars, so return success
149 // even if we have read less than asked
150 return NPT_SUCCESS;
153 /*----------------------------------------------------------------------
154 | PLT_RingBufferStream::Write
155 +---------------------------------------------------------------------*/
156 NPT_Result
157 PLT_RingBufferStream::Write(const void* buffer,
158 NPT_Size max_bytes_to_write,
159 NPT_Size* _bytes_written /*= NULL*/)
161 NPT_Size bytes_to_write;
162 NPT_Size bytes_written = 0;
164 // reset output param first
165 if (_bytes_written) *_bytes_written = 0;
167 // wait for space
168 do {
170 NPT_AutoLock autoLock(m_Lock);
172 if (m_Aborted) {
173 return NPT_ERROR_INTERRUPTED;
176 // return immediately if we are told we're finished
177 if (m_Eos) {
178 return NPT_ERROR_EOS;
181 if (m_RingBuffer->GetSpace())
182 break;
184 if (!m_Blocking) {
185 return NPT_ERROR_WOULD_BLOCK;
189 // sleep and try again
190 NPT_System::Sleep(NPT_TimeInterval(.1));
191 } while (1);
194 NPT_AutoLock autoLock(m_Lock);
196 // try twice in case available space was not contiguous
197 for (int i=0; i<2; i++) {
198 bytes_to_write = min(max_bytes_to_write - bytes_written, m_RingBuffer->GetContiguousSpace());
200 // break if no space to write the second time
201 if (bytes_to_write == 0) break;
203 // write into buffer
204 NPT_CHECK(m_RingBuffer->Write((unsigned char*)buffer+bytes_written, bytes_to_write));
206 m_TotalBytesWritten += bytes_to_write;
207 bytes_written += bytes_to_write;
209 if (_bytes_written) *_bytes_written += bytes_to_write;
213 // we have written some chars, so return success
214 // even if we have written less than provided
215 return NPT_SUCCESS;
218 /*----------------------------------------------------------------------
219 | PLT_RingBufferStream::Flush
220 +---------------------------------------------------------------------*/
221 NPT_Result
222 PLT_RingBufferStream::Flush()
224 NPT_AutoLock autoLock(m_Lock);
226 m_RingBuffer->Flush();
227 m_TotalBytesRead = 0;
228 m_TotalBytesWritten = 0;
229 return NPT_SUCCESS;
232 /*----------------------------------------------------------------------
233 | PLT_RingBufferStream::SetEOS
234 +---------------------------------------------------------------------*/
235 NPT_Result
236 PLT_RingBufferStream::SetEOS()
238 NPT_AutoLock autoLock(m_Lock);
240 m_Eos = true;
241 return NPT_SUCCESS;
245 /*----------------------------------------------------------------------
246 | PLT_RingBufferStream::Abort
247 +---------------------------------------------------------------------*/
248 NPT_Result
249 PLT_RingBufferStream::Abort()
251 NPT_AutoLock autoLock(m_Lock);
253 m_Aborted = true;
254 return NPT_SUCCESS;