1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-13 17:43:59 +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" );
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<char *>(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);

View File

@@ -27,16 +27,19 @@
#include "utils/Logger.h"
#include <stdint.h>
#include <QObject>
#include <QUrl>
#include <QIODevice>
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

View File

@@ -33,8 +33,7 @@ using namespace Tomahawk;
#define BLOCK_SIZE 1048576
QNR_IODeviceStream::QNR_IODeviceStream( const QSharedPointer<QNetworkReply>& 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;

View File

@@ -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<QNetworkReply>& 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();