1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-16 02:54:33 +02:00

QIODevice (playing from peers) should be ok now

This commit is contained in:
dridri
2014-10-01 16:26:10 +02:00
committed by Uwe L. Korn
parent ebf58c1a57
commit 667996715a
4 changed files with 97 additions and 22 deletions

View File

@@ -27,20 +27,31 @@
static QString s_aeInfoIdentifier = QString( "MEDIASTREAM" ); static QString s_aeInfoIdentifier = QString( "MEDIASTREAM" );
MediaStream::MediaStream() MediaStream::MediaStream( QObject* parent )
: m_type( Unknown ) : QObject( parent )
, m_type( Unknown )
, m_url( QUrl() ) , m_url( QUrl() )
, m_ioDevice ( 0 ) , m_ioDevice ( 0 )
, m_started( false )
, m_bufferingFinished( false )
, m_eos( false ) , m_eos( false )
, m_pos( 0 ) , m_pos( 0 )
, m_streamSize( 0 ) , m_streamSize( 0 )
{ {
tDebug() << Q_FUNC_INFO; tDebug() << Q_FUNC_INFO;
} }
MediaStream::MediaStream( const QUrl &url ) MediaStream::MediaStream( const QUrl &url )
: m_type(Url) : QObject( 0 )
, m_type(Url)
, m_ioDevice ( 0 )
, m_started( false )
, m_bufferingFinished( false )
, m_eos( false )
, m_pos( 0 )
, m_streamSize( 0 )
{ {
tDebug() << Q_FUNC_INFO; tDebug() << Q_FUNC_INFO;
@@ -49,11 +60,38 @@ MediaStream::MediaStream( const QUrl &url )
MediaStream::MediaStream( QIODevice* device ) MediaStream::MediaStream( QIODevice* device )
: m_type(IODevice) : QObject( 0 )
, m_type(IODevice)
, m_url( QUrl() )
, m_ioDevice ( 0 )
, m_started( false )
, m_bufferingFinished( false )
, m_eos( false )
, m_pos( 0 )
, m_streamSize( 0 )
{ {
tDebug() << Q_FUNC_INFO; tDebug() << Q_FUNC_INFO;
m_ioDevice = device; m_ioDevice = device;
QObject::connect( m_ioDevice, SIGNAL( readChannelFinished() ), this, SLOT( bufferingFinished() ) );
}
MediaStream::MediaStream( const MediaStream& copy )
: QObject( copy.parent() )
{
m_type = copy.m_type;
m_url = copy.m_url;
m_ioDevice = copy.m_ioDevice;
m_started = copy.m_started;
m_bufferingFinished = copy.m_bufferingFinished;
m_eos = copy.m_eos;
m_pos = copy.m_pos;
m_streamSize = copy.m_streamSize;
if ( m_type == IODevice ) {
QObject::connect( m_ioDevice, SIGNAL( readChannelFinished() ), this, SLOT( bufferingFinished() ) );
}
} }
@@ -106,32 +144,58 @@ MediaStream::endOfData()
} }
void MediaStream::bufferingFinished()
{
tDebug() << Q_FUNC_INFO;
m_bufferingFinished = true;
}
int int
MediaStream::readCallback ( void* data, const char* cookie, int64_t* dts, int64_t* pts, unsigned* flags, size_t* bufferSize, void** buffer ) MediaStream::readCallback ( void* data, const char* cookie, int64_t* dts, int64_t* pts, unsigned* flags, size_t* bufferSize, void** buffer )
{ {
// tDebug() << Q_FUNC_INFO;
Q_UNUSED(cookie); Q_UNUSED(cookie);
Q_UNUSED(dts); Q_UNUSED(dts);
Q_UNUSED(pts); Q_UNUSED(pts);
Q_UNUSED(flags); Q_UNUSED(flags);
MediaStream* that = static_cast < MediaStream * > ( data ); MediaStream* that = static_cast < MediaStream * > ( data );
qint64 bufsize = 0;
*bufferSize = 0;
// tDebug() << Q_FUNC_INFO << " ---- type : " << that->m_type;
if ( that->m_eos == true ) { if ( that->m_eos == true ) {
return -1; return -1;
} }
if ( that->m_type == Stream ) { if ( that->m_type == Stream ) {
*bufferSize = that->needData(buffer); bufsize = that->needData(buffer);
} }
else if ( that->m_type == IODevice ) { else if ( that->m_type == IODevice ) {
QByteArray data = that->m_ioDevice->read(BLOCK_SIZE); /*
*buffer = new char[data.size()]; *buffer = new char[BLOCK_SIZE];
memcpy(*buffer, data.data(), data.size()); bufsize = that->m_ioDevice->read( (char*)*buffer, BLOCK_SIZE );
*bufferSize = data.size(); */
bufsize = that->m_ioDevice->read( that->m_buffer, BLOCK_SIZE );
*buffer = that->m_buffer;
// tDebug() << "readCallback(QIODevice) returning bufsize : " << bufsize;
} }
if ( bufsize > 0 ) {
that->m_started = true;
}
if ( bufsize == 0 && that->m_started && that->m_bufferingFinished == true ) {
that->m_eos = true;
return -1;
}
if ( bufsize < 0 ) {
that->m_eos = true;
return -1;
}
*bufferSize = bufsize;
return 0; return 0;
} }
@@ -146,9 +210,9 @@ MediaStream::readDoneCallback ( void *data, const char *cookie, size_t bufferSiz
MediaStream* that = static_cast < MediaStream * > ( data ); MediaStream* that = static_cast < MediaStream * > ( data );
if ( ( that->m_type == Stream || that->m_type == IODevice ) && buffer != 0 && bufferSize > 0 ) { if ( ( that->m_type == Stream/* || that->m_type == IODevice*/ ) && buffer != 0 && bufferSize > 0 ) {
// TODO : causes segfault // TODO : causes segfault
tDebug() << "buffer : " << buffer; // tDebug() << "buffer : " << buffer;
delete static_cast<char *>(buffer); delete static_cast<char *>(buffer);
} }
@@ -159,7 +223,7 @@ MediaStream::readDoneCallback ( void *data, const char *cookie, size_t bufferSiz
int int
MediaStream::seekCallback ( void *data, const uint64_t pos ) MediaStream::seekCallback ( void *data, const uint64_t pos )
{ {
// tDebug() << Q_FUNC_INFO; tDebug() << Q_FUNC_INFO;
MediaStream* that = static_cast < MediaStream * > ( data ); MediaStream* that = static_cast < MediaStream * > ( data );
@@ -167,6 +231,7 @@ MediaStream::seekCallback ( void *data, const uint64_t pos )
return -1; return -1;
} }
that->m_started = false;
that->m_pos = pos; that->m_pos = pos;
if ( that->m_type == IODevice ) { if ( that->m_type == IODevice ) {
that->m_ioDevice->seek(pos); that->m_ioDevice->seek(pos);

View File

@@ -27,16 +27,19 @@
#include "utils/Logger.h" #include "utils/Logger.h"
#include <stdint.h> #include <stdint.h>
#include <QObject>
#include <QUrl> #include <QUrl>
#include <QIODevice> #include <QIODevice>
class DLLEXPORT MediaStream class DLLEXPORT MediaStream : public QObject
{ {
Q_OBJECT
public: public:
enum MediaType { Unknown = -1, Empty = 0, Url = 1, Stream = 2, IODevice = 3 }; enum MediaType { Unknown = -1, Empty = 0, Url = 1, Stream = 2, IODevice = 3 };
MediaStream(); MediaStream( QObject* parent = 0 );
MediaStream( const MediaStream& copy );
MediaStream( const QUrl &url ); MediaStream( const QUrl &url );
MediaStream( QIODevice* device ); MediaStream( QIODevice* device );
virtual ~MediaStream(); virtual ~MediaStream();
@@ -48,12 +51,15 @@ public:
qint64 streamSize(); qint64 streamSize();
virtual void seekStream( qint64 offset ) { (void)offset; } virtual void seekStream( qint64 offset ) { (void)offset; }
virtual size_t needData ( void** buffer ) { (void)buffer; tDebug() << Q_FUNC_INFO; return 0; } virtual qint64 needData ( void** buffer ) { (void)buffer; tDebug() << Q_FUNC_INFO; return 0; }
static int readCallback ( void* data, const char* cookie, int64_t* dts, int64_t* pts, unsigned* flags, size_t* bufferSize, void** buffer ); static int readCallback ( void* data, const char* cookie, int64_t* dts, int64_t* pts, unsigned* flags, size_t* bufferSize, void** buffer );
static int readDoneCallback ( void *data, const char *cookie, size_t bufferSize, void *buffer ); static int readDoneCallback ( void *data, const char *cookie, size_t bufferSize, void *buffer );
static int seekCallback ( void *data, const uint64_t pos ); static int seekCallback ( void *data, const uint64_t pos );
public slots:
void bufferingFinished();
protected: protected:
void endOfData(); void endOfData();
@@ -61,9 +67,13 @@ protected:
QUrl m_url; QUrl m_url;
QIODevice* m_ioDevice; QIODevice* m_ioDevice;
bool m_started;
bool m_bufferingFinished;
bool m_eos; bool m_eos;
qint64 m_pos; qint64 m_pos;
qint64 m_streamSize; qint64 m_streamSize;
char m_buffer[1048576];
}; };
#endif // MEDIASTREAM_H #endif // MEDIASTREAM_H

View File

@@ -33,8 +33,7 @@ using namespace Tomahawk;
#define BLOCK_SIZE 1048576 #define BLOCK_SIZE 1048576
QNR_IODeviceStream::QNR_IODeviceStream( const QSharedPointer<QNetworkReply>& reply, QObject* parent ) QNR_IODeviceStream::QNR_IODeviceStream( const QSharedPointer<QNetworkReply>& reply, QObject* parent )
: QObject( parent ) : MediaStream( parent )
, MediaStream()
, m_networkReply( reply ) , m_networkReply( reply )
, m_timer( new QTimer( this ) ) , m_timer( new QTimer( this ) )
{ {
@@ -90,7 +89,7 @@ QNR_IODeviceStream::seekStream( qint64 offset )
} }
size_t qint64
QNR_IODeviceStream::needData ( void** buffer ) QNR_IODeviceStream::needData ( void** buffer )
{ {
// tDebug() << Q_FUNC_INFO; // tDebug() << Q_FUNC_INFO;

View File

@@ -38,15 +38,16 @@ class QTimer;
namespace Tomahawk namespace Tomahawk
{ {
class DLLEXPORT QNR_IODeviceStream : public QObject, public MediaStream class DLLEXPORT QNR_IODeviceStream : public MediaStream
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QNR_IODeviceStream( const QSharedPointer<QNetworkReply>& reply, QObject *parent = 0 ); explicit QNR_IODeviceStream( const QSharedPointer<QNetworkReply>& reply, QObject *parent = 0 );
~QNR_IODeviceStream(); ~QNR_IODeviceStream();
virtual void seekStream( qint64 offset ); virtual void seekStream( qint64 offset );
virtual size_t needData ( void** buffer ); virtual qint64 needData ( void** buffer );
private slots: private slots:
void readyRead(); void readyRead();