1 /*****************************************************************
2 |
3 | Platinum - Frame Stream
4 |
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
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.
13 |
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
21 |
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.
26 |
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
32 |
33 ****************************************************************/
34
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "PltFrameStream.h"
39
40 NPT_SET_LOCAL_LOGGER("platinum.core.framestream")
41
42 /*----------------------------------------------------------------------
43 | PLT_InputFrameStream::PLT_InputFrameStream
44 +---------------------------------------------------------------------*/
PLT_InputFrameStream(NPT_Reference<PLT_FrameBuffer> & frame_buffer,const char * boundary)45 PLT_InputFrameStream::PLT_InputFrameStream(NPT_Reference<PLT_FrameBuffer>& frame_buffer,
46 const char* boundary) :
47 m_FrameBuffer(frame_buffer),
48 m_LastFrameIndex(0),
49 m_Boundary(boundary),
50 m_Eos(false)
51 {
52 m_FrameBuffer->AddReader();
53 }
54
55 /*----------------------------------------------------------------------
56 | PLT_InputFrameStream::~PLT_InputFrameStream
57 +---------------------------------------------------------------------*/
~PLT_InputFrameStream()58 PLT_InputFrameStream::~PLT_InputFrameStream()
59 {
60 m_FrameBuffer->RemoveReader();
61 }
62
63 /*----------------------------------------------------------------------
64 | PLT_InputFrameStream::GetAvailable
65 +---------------------------------------------------------------------*/
66 NPT_Result
GetAvailable(NPT_LargeSize & available)67 PLT_InputFrameStream::GetAvailable(NPT_LargeSize& available)
68 {
69 NPT_CHECK_WARNING(m_Part.GetAvailable(available));
70
71 if (available == 0 && !m_Eos) {
72 NPT_CHECK_WARNING(FillBuffer());
73 NPT_CHECK_WARNING(m_Part.GetAvailable(available));
74 }
75
76 return NPT_SUCCESS;
77 }
78
79 /*----------------------------------------------------------------------
80 | PLT_InputFrameStream::FillBuffer
81 +---------------------------------------------------------------------*/
82 NPT_Result
FillBuffer()83 PLT_InputFrameStream::FillBuffer()
84 {
85 // reset memorystream
86 m_Part.SetDataSize(0);
87
88 // fetch next frame
89 NPT_DataBuffer frame;
90 NPT_Result result = m_FrameBuffer->GetNextFrame(m_LastFrameIndex, frame);
91
92 // error (EOS) or empty frame means we're done
93 if (NPT_FAILED(result) || frame.GetDataSize() == 0) {
94 m_Part.WriteLine("--" + m_Boundary + "--");
95 m_Eos = true;
96 return NPT_SUCCESS;
97 }
98
99 m_Part.WriteLine("--" + m_Boundary);
100 m_Part.WriteLine("Content-Type: " + NPT_String(m_FrameBuffer->GetMimeType()));
101 m_Part.WriteLine("Content-Length: "+NPT_String::FromInteger(frame.GetDataSize()));
102 m_Part.WriteLine("");
103 m_Part.Write(frame.GetData(), frame.GetDataSize());
104 m_Part.WriteLine("");
105 return NPT_SUCCESS;
106 }
107
108 /*----------------------------------------------------------------------
109 | PLT_InputFrameStream::Read
110 +---------------------------------------------------------------------*/
111 NPT_Result
Read(void * buffer,NPT_Size bytes_to_read,NPT_Size * bytes_read)112 PLT_InputFrameStream::Read(void* buffer,
113 NPT_Size bytes_to_read,
114 NPT_Size* bytes_read /*= 0*/)
115 {
116
117 if (bytes_read) *bytes_read = 0;
118
119 if (bytes_to_read == 0) {
120 return NPT_SUCCESS;
121 }
122
123 // make sure we have data
124 NPT_LargeSize available;
125 NPT_CHECK_WARNING(GetAvailable(available));
126
127 return m_Part.Read(buffer, bytes_to_read, bytes_read);
128 }
129