From 667996715af4d5d35ba7de7ab40047107206309c Mon Sep 17 00:00:00 2001 From: dridri Date: Wed, 1 Oct 2014 16:26:10 +0200 Subject: [PATCH] QIODevice (playing from peers) should be ok now --- src/libtomahawk/utils/MediaStream.cpp | 93 +++++++++++++++++--- src/libtomahawk/utils/MediaStream.h | 16 +++- src/libtomahawk/utils/Qnr_IoDeviceStream.cpp | 5 +- src/libtomahawk/utils/Qnr_IoDeviceStream.h | 5 +- 4 files changed, 97 insertions(+), 22 deletions(-) diff --git a/src/libtomahawk/utils/MediaStream.cpp b/src/libtomahawk/utils/MediaStream.cpp index 6d304582c..8b98f4360 100644 --- a/src/libtomahawk/utils/MediaStream.cpp +++ b/src/libtomahawk/utils/MediaStream.cpp @@ -27,20 +27,31 @@ static QString s_aeInfoIdentifier = QString( "MEDIASTREAM" ); -MediaStream::MediaStream() - : m_type( Unknown ) +MediaStream::MediaStream( QObject* parent ) + : QObject( parent ) + , m_type( Unknown ) , 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; + } 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; @@ -49,11 +60,38 @@ MediaStream::MediaStream( const QUrl &url ) 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; 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 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(dts); Q_UNUSED(pts); Q_UNUSED(flags); MediaStream* that = static_cast < MediaStream * > ( data ); + qint64 bufsize = 0; + *bufferSize = 0; + +// tDebug() << Q_FUNC_INFO << " ---- type : " << that->m_type; if ( that->m_eos == true ) { return -1; } if ( that->m_type == Stream ) { - *bufferSize = that->needData(buffer); + bufsize = that->needData(buffer); } else if ( that->m_type == IODevice ) { - QByteArray data = that->m_ioDevice->read(BLOCK_SIZE); - *buffer = new char[data.size()]; - memcpy(*buffer, data.data(), data.size()); - *bufferSize = data.size(); + /* + *buffer = new char[BLOCK_SIZE]; + bufsize = that->m_ioDevice->read( (char*)*buffer, BLOCK_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; } @@ -146,9 +210,9 @@ MediaStream::readDoneCallback ( void *data, const char *cookie, size_t bufferSiz 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 - tDebug() << "buffer : " << buffer; +// tDebug() << "buffer : " << buffer; delete static_cast(buffer); } @@ -159,7 +223,7 @@ MediaStream::readDoneCallback ( void *data, const char *cookie, size_t bufferSiz int MediaStream::seekCallback ( void *data, const uint64_t pos ) { -// tDebug() << Q_FUNC_INFO; + tDebug() << Q_FUNC_INFO; MediaStream* that = static_cast < MediaStream * > ( data ); @@ -167,6 +231,7 @@ MediaStream::seekCallback ( void *data, const uint64_t pos ) return -1; } + that->m_started = false; that->m_pos = pos; if ( that->m_type == IODevice ) { that->m_ioDevice->seek(pos); diff --git a/src/libtomahawk/utils/MediaStream.h b/src/libtomahawk/utils/MediaStream.h index 66e5c2fdf..e1f7b0f9f 100644 --- a/src/libtomahawk/utils/MediaStream.h +++ b/src/libtomahawk/utils/MediaStream.h @@ -27,16 +27,19 @@ #include "utils/Logger.h" #include +#include #include #include -class DLLEXPORT MediaStream +class DLLEXPORT MediaStream : public QObject { + Q_OBJECT public: 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( QIODevice* device ); virtual ~MediaStream(); @@ -48,12 +51,15 @@ public: qint64 streamSize(); 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 readDoneCallback ( void *data, const char *cookie, size_t bufferSize, void *buffer ); static int seekCallback ( void *data, const uint64_t pos ); +public slots: + void bufferingFinished(); + protected: void endOfData(); @@ -61,9 +67,13 @@ protected: QUrl m_url; QIODevice* m_ioDevice; + bool m_started; + bool m_bufferingFinished; bool m_eos; qint64 m_pos; qint64 m_streamSize; + + char m_buffer[1048576]; }; #endif // MEDIASTREAM_H diff --git a/src/libtomahawk/utils/Qnr_IoDeviceStream.cpp b/src/libtomahawk/utils/Qnr_IoDeviceStream.cpp index 1199db759..fca6de94b 100644 --- a/src/libtomahawk/utils/Qnr_IoDeviceStream.cpp +++ b/src/libtomahawk/utils/Qnr_IoDeviceStream.cpp @@ -33,8 +33,7 @@ using namespace Tomahawk; #define BLOCK_SIZE 1048576 QNR_IODeviceStream::QNR_IODeviceStream( const QSharedPointer& reply, QObject* parent ) - : QObject( parent ) - , MediaStream() + : MediaStream( parent ) , m_networkReply( reply ) , m_timer( new QTimer( this ) ) { @@ -90,7 +89,7 @@ QNR_IODeviceStream::seekStream( qint64 offset ) } -size_t +qint64 QNR_IODeviceStream::needData ( void** buffer ) { // tDebug() << Q_FUNC_INFO; diff --git a/src/libtomahawk/utils/Qnr_IoDeviceStream.h b/src/libtomahawk/utils/Qnr_IoDeviceStream.h index e43a6437a..ef3b89263 100644 --- a/src/libtomahawk/utils/Qnr_IoDeviceStream.h +++ b/src/libtomahawk/utils/Qnr_IoDeviceStream.h @@ -38,15 +38,16 @@ class QTimer; namespace Tomahawk { -class DLLEXPORT QNR_IODeviceStream : public QObject, public MediaStream +class DLLEXPORT QNR_IODeviceStream : public MediaStream { Q_OBJECT + public: explicit QNR_IODeviceStream( const QSharedPointer& reply, QObject *parent = 0 ); ~QNR_IODeviceStream(); virtual void seekStream( qint64 offset ); - virtual size_t needData ( void** buffer ); + virtual qint64 needData ( void** buffer ); private slots: void readyRead();