32 #include <tdeapplication.h>
34 #include <tdeio/job.h>
35 #include <tdeio/kmimetype.h>
36 #include <tdeio/jobclasses.h>
38 #include <tqdatastream.h>
39 #include "artsversion.h"
40 #include "kioinputstream_impl.moc"
44 const unsigned int TDEIOInputStream_impl::PACKET_COUNT = 10;
46 TDEIOInputStream_impl::TDEIOInputStream_impl() : m_packetSize(2048)
50 m_firstBuffer =
false;
52 m_streamStarted =
false;
53 m_streamSuspended =
false;
54 m_streamPulled =
false;
58 TDEIOInputStream_impl::~TDEIOInputStream_impl()
64 void TDEIOInputStream_impl::streamStart()
67 if (m_streamStarted) {
68 kdDebug( 400 ) <<
"not restarting stream!\n";
69 if (m_job->isSuspended())
74 kdDebug( 400 ) <<
"(re)starting stream\n";
78 m_job = TDEIO::get(m_url,
false,
false);
80 m_job->addMetaData(
"accept",
"audio/x-mp3, video/mpeg, application/ogg");
81 m_job->addMetaData(
"UserAgent", TQString::fromLatin1(
"aRts/") + TQString::fromLatin1(ARTS_VERSION));
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)));
92 m_streamStarted =
true;
95 void TDEIOInputStream_impl::streamEnd()
97 kdDebug( 400 ) <<
"streamEnd()\n";
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)));
110 if ( m_streamPulled )
117 m_streamStarted =
false;
120 bool TDEIOInputStream_impl::openURL(
const std::string& url)
122 m_url =
KURL(url.c_str());
127 void TDEIOInputStream_impl::slotData(TDEIO::Job *,
const TQByteArray &data)
132 TQDataStream dataStream(m_data, IO_WriteOnly | IO_Append);
133 dataStream.writeRawBytes(data.data(), data.size());
139 void TDEIOInputStream_impl::slotResult(TDEIO::Job *job)
143 m_streamStarted =
false;
149 emit mimeTypeFound(
"application/x-zerosize");
150 job->showErrorDialog();
154 void TDEIOInputStream_impl::slotScanMimeType(TDEIO::Job *,
const TQString &mimetype)
156 kdDebug( 400 ) <<
"got mimetype: " << mimetype <<
endl;
157 emit mimeTypeFound(mimetype);
160 void TDEIOInputStream_impl::slotTotalSize(TDEIO::Job *, TDEIO::filesize_t size)
165 bool TDEIOInputStream_impl::eof()
167 return (m_finished && m_data.size() == 0);
170 bool TDEIOInputStream_impl::seekOk()
175 long TDEIOInputStream_impl::size()
177 return m_size ? m_size : m_data.size();
180 long TDEIOInputStream_impl::seek(
long)
185 void TDEIOInputStream_impl::processQueue()
189 if(m_data.size() > (m_packetBuffer * m_packetSize * 2) && !m_job->isSuspended())
191 kdDebug( 400 ) <<
"STREAMING: suspend job" <<
endl;
194 else if(m_data.size() < (m_packetBuffer * m_packetSize) && m_job->isSuspended())
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;
206 m_firstBuffer =
true;
207 m_streamPulled =
true;
208 outdata.setPull(PACKET_COUNT, m_packetSize);
213 void TDEIOInputStream_impl::request_outdata(DataPacket<mcopbyte> *packet)
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;
220 if( (
unsigned)packet->size < m_packetSize || ! m_firstBuffer) {
221 m_firstBuffer =
false;
227 if (packet->size > 0)
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);
236 REGISTER_IMPLEMENTATION(TDEIOInputStream_impl);
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)