• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • arts
 

arts

  • arts
  • kde
kioinputstream_impl.cpp
1  /*
2 
3  Copyright (C) 2001 Nikolas Zimmermann <wildfox@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 
20  */
21 
22 /*
23  * How does it work?
24  * -----------------
25  *
26  * First the buffer has to be filled. When it reaches a defined size the outdata
27  * stream has to start pulling packets. If the buffer reaches a size of zero the
28  * stream has to stop. If the buffer gets to big the job has to be suspended
29  * until the buffer is small enough again.
30  */
31 
32 #include <tdeapplication.h>
33 #include <kdebug.h>
34 #include <tdeio/job.h>
35 #include <tdeio/kmimetype.h>
36 #include <tdeio/jobclasses.h>
37 #include <tqtimer.h>
38 #include <tqdatastream.h>
39 #include "artsversion.h"
40 #include "kioinputstream_impl.moc"
41 
42 using namespace Arts;
43 
44 const unsigned int TDEIOInputStream_impl::PACKET_COUNT = 10;
45 
46 TDEIOInputStream_impl::TDEIOInputStream_impl() : m_packetSize(2048)
47 {
48  m_job = 0;
49  m_finished = false;
50  m_firstBuffer = false;
51  m_packetBuffer = 16;
52  m_streamStarted = false;
53  m_streamSuspended = false;
54  m_streamPulled = false;
55  m_size = 0;
56 }
57 
58 TDEIOInputStream_impl::~TDEIOInputStream_impl()
59 {
60  if(m_job != 0)
61  m_job->kill();
62 }
63 
64 void TDEIOInputStream_impl::streamStart()
65 {
66  // prevent kill/reconnect
67  if (m_streamStarted) {
68  kdDebug( 400 ) << "not restarting stream!\n";
69  if (m_job->isSuspended())
70  m_job->resume();
71  return;
72  }
73 
74  kdDebug( 400 ) << "(re)starting stream\n";
75 
76  if(m_job != 0)
77  m_job->kill();
78  m_job = TDEIO::get(m_url, false, false);
79 
80  m_job->addMetaData("accept", "audio/x-mp3, video/mpeg, application/ogg");
81  m_job->addMetaData("UserAgent", TQString::fromLatin1("aRts/") + TQString::fromLatin1(ARTS_VERSION));
82 
83  TQObject::connect(m_job, TQ_SIGNAL(data(TDEIO::Job *, const TQByteArray &)),
84  this, TQ_SLOT(slotData(TDEIO::Job *, const TQByteArray &)));
85  TQObject::connect(m_job, TQ_SIGNAL(result(TDEIO::Job *)),
86  this, TQ_SLOT(slotResult(TDEIO::Job *)));
87  TQObject::connect(m_job, TQ_SIGNAL(mimetype(TDEIO::Job *, const TQString &)),
88  this, TQ_SLOT(slotScanMimeType(TDEIO::Job *, const TQString &)));
89  TQObject::connect(m_job, TQ_SIGNAL(totalSize( TDEIO::Job *, TDEIO::filesize_t)),
90  this, TQ_SLOT(slotTotalSize(TDEIO::Job *, TDEIO::filesize_t)));
91 
92  m_streamStarted = true;
93 }
94 
95 void TDEIOInputStream_impl::streamEnd()
96 {
97  kdDebug( 400 ) << "streamEnd()\n";
98 
99  if(m_job != 0)
100  {
101  TQObject::disconnect(m_job, TQ_SIGNAL(data(TDEIO::Job *, const TQByteArray &)),
102  this, TQ_SLOT(slotData(TDEIO::Job *, const TQByteArray &)));
103  TQObject::disconnect(m_job, TQ_SIGNAL(result(TDEIO::Job *)),
104  this, TQ_SLOT(slotResult(TDEIO::Job *)));
105  TQObject::disconnect(m_job, TQ_SIGNAL(mimetype(TDEIO::Job *, const TQString &)),
106  this, TQ_SLOT(slotScanMimeType(TDEIO::Job *, const TQString &)));
107  TQObject::disconnect(m_job, TQ_SIGNAL(totalSize( TDEIO::Job *, TDEIO::filesize_t)),
108  this, TQ_SLOT(slotTotalSize(TDEIO::Job *, TDEIO::filesize_t)));
109 
110  if ( m_streamPulled )
111  outdata.endPull();
112 
113  m_job->kill();
114  m_job = 0;
115  }
116 
117  m_streamStarted = false;
118 }
119 
120 bool TDEIOInputStream_impl::openURL(const std::string& url)
121 {
122  m_url = KURL(url.c_str());
123  m_size = 0;
124  return true;
125 }
126 
127 void TDEIOInputStream_impl::slotData(TDEIO::Job *, const TQByteArray &data)
128 {
129  if(m_finished)
130  m_finished = false;
131 
132  TQDataStream dataStream(m_data, IO_WriteOnly | IO_Append);
133  dataStream.writeRawBytes(data.data(), data.size());
134  //kdDebug( 400 ) << "STREAMING: buffersize = " << m_data.size() << " bytes" << endl;
135 
136  processQueue();
137 }
138 
139 void TDEIOInputStream_impl::slotResult(TDEIO::Job *job)
140 {
141  // jobs delete themselves after emitting their result
142  m_finished = true;
143  m_streamStarted = false;
144  m_job = 0;
145 
146  if(job->error()) {
147  // break out of the event loop in case of
148  // connection error
149  emit mimeTypeFound("application/x-zerosize");
150  job->showErrorDialog();
151  }
152 }
153 
154 void TDEIOInputStream_impl::slotScanMimeType(TDEIO::Job *, const TQString &mimetype)
155 {
156  kdDebug( 400 ) << "got mimetype: " << mimetype << endl;
157  emit mimeTypeFound(mimetype);
158 }
159 
160 void TDEIOInputStream_impl::slotTotalSize(TDEIO::Job *, TDEIO::filesize_t size)
161 {
162  m_size = size;
163 }
164 
165 bool TDEIOInputStream_impl::eof()
166 {
167  return (m_finished && m_data.size() == 0);
168 }
169 
170 bool TDEIOInputStream_impl::seekOk()
171 {
172  return false;
173 }
174 
175 long TDEIOInputStream_impl::size()
176 {
177  return m_size ? m_size : m_data.size();
178 }
179 
180 long TDEIOInputStream_impl::seek(long)
181 {
182  return -1;
183 }
184 
185 void TDEIOInputStream_impl::processQueue()
186 {
187  if(m_job != 0)
188  {
189  if(m_data.size() > (m_packetBuffer * m_packetSize * 2) && !m_job->isSuspended())
190  {
191  kdDebug( 400 ) << "STREAMING: suspend job" << endl;
192  m_job->suspend();
193  }
194  else if(m_data.size() < (m_packetBuffer * m_packetSize) && m_job->isSuspended())
195  {
196  kdDebug( 400 ) << "STREAMING: resume job" << endl;
197  m_job->resume();
198  }
199  }
200 
201  if (!m_firstBuffer) {
202  if(m_data.size() < (m_packetBuffer * m_packetSize * 2) ) {
203  kdDebug( 400 ) << "STREAMING: Buffering in progress... (Needed bytes before it starts to play: " << ((m_packetBuffer * m_packetSize * 2) - m_data.size()) << ")" << endl;
204  return;
205  } else {
206  m_firstBuffer = true;
207  m_streamPulled = true;
208  outdata.setPull(PACKET_COUNT, m_packetSize);
209  }
210  }
211 }
212 
213 void TDEIOInputStream_impl::request_outdata(DataPacket<mcopbyte> *packet)
214 {
215  processQueue();
216  packet->size = std::min(m_packetSize, (unsigned int)m_data.size());
217  kdDebug( 400 ) << "STREAMING: Filling one DataPacket with " << packet->size << " bytes of the stream!" << endl;
218 
219  if (!m_finished) {
220  if( (unsigned)packet->size < m_packetSize || ! m_firstBuffer) {
221  m_firstBuffer = false;
222  packet->size = 0;
223  outdata.endPull();
224  }
225  }
226 
227  if (packet->size > 0)
228  {
229  memcpy(packet->contents, m_data.data(), packet->size);
230  memmove(m_data.data(), m_data.data() + packet->size, m_data.size() - packet->size);
231  m_data.resize(m_data.size() - packet->size);
232  }
233  packet->send();
234 }
235 
236 REGISTER_IMPLEMENTATION(TDEIOInputStream_impl);
KURL
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)

arts

Skip menu "arts"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

arts

Skip menu "arts"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for arts by doxygen 1.9.1
This website is maintained by Timothy Pearson.