Skip to content

Commit

Permalink
split engineshoutcast from sidechainworker
Browse files Browse the repository at this point in the history
  • Loading branch information
daschuer committed Sep 25, 2015
1 parent a5db63c commit 29a4814
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/engine/sidechain/enginenetworkstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ qint64 EngineNetworkStream::getNetworkTimeUs() {
#endif
}

void EngineNetworkStream::addWorker(QSharedPointer<SideChainWorker> pWorker) {
void EngineNetworkStream::addWorker(QSharedPointer<NetworkStreamWorker> pWorker) {
m_pWorker = pWorker;
m_pWorker->setOutputFifo(m_pOutputFifo);
}
6 changes: 3 additions & 3 deletions src/engine/sidechain/enginenetworkstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "util/types.h"
#include "util/fifo.h"
#include "engine/sidechain/sidechainworker.h"
#include "engine/sidechain/networkstreamworker.h"

class EngineNetworkStream {
public:
Expand Down Expand Up @@ -34,7 +34,7 @@ class EngineNetworkStream {

static qint64 getNetworkTimeUs();

void addWorker(QSharedPointer<SideChainWorker> pWorker);
void addWorker(QSharedPointer<NetworkStreamWorker> pWorker);

private:
void scheduleWorker();
Expand All @@ -47,7 +47,7 @@ class EngineNetworkStream {
qint64 m_streamStartTimeUs;
qint64 m_streamFramesWritten;
qint64 m_streamFramesRead;
QSharedPointer<SideChainWorker> m_pWorker;
QSharedPointer<NetworkStreamWorker> m_pWorker;
int m_writeOverflowCount;
};

Expand Down
44 changes: 22 additions & 22 deletions src/engine/sidechain/engineshoutcast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ EngineShoutcast::EngineShoutcast(ConfigObject<ConfigValue>* _config)
signal(SIGPIPE, SIG_IGN);
#endif

m_pShoutcastStatus->set(SIDECHAINWORKER_STATE_DISCONNECTED);
m_pShoutcastStatus->set(NETWORKSTREAMWORKER_STATE_DISCONNECTED);
m_pShoutcastNeedUpdateFromPrefs = new ControlObject(
ConfigKey(SHOUTCAST_PREF_KEY,"update_from_prefs"));

setState(SIDECHAINWORKER_STATE_INIT);
setState(NETWORKSTREAMWORKER_STATE_INIT);

// Initialize libshout
shout_init();
Expand Down Expand Up @@ -114,7 +114,7 @@ EngineShoutcast::~EngineShoutcast() {
bool EngineShoutcast::serverDisconnect() {
m_bThreadQuit = true;
wait();
setState(SIDECHAINWORKER_STATE_DISCONNECTED);
setState(NETWORKSTREAMWORKER_STATE_DISCONNECTED);
return false; // if no connection has been established, nothing can be disconnected
}

Expand All @@ -137,7 +137,7 @@ QByteArray EngineShoutcast::encodeString(const QString& string) {
void EngineShoutcast::updateFromPreferences() {
qDebug() << "EngineShoutcast: updating from preferences";

setState(SIDECHAINWORKER_STATE_BUSY);
setState(NETWORKSTREAMWORKER_STATE_BUSY);
m_pShoutcastNeedUpdateFromPrefs->set(0.0);

m_format_is_mp3 = false;
Expand Down Expand Up @@ -357,20 +357,20 @@ void EngineShoutcast::updateFromPreferences() {
delete m_encoder;
m_encoder = NULL;
}
setState(SIDECHAINWORKER_STATE_READY);
setState(NETWORKSTREAMWORKER_STATE_READY);
}

bool EngineShoutcast::serverConnect() {
start();
setState(SICECHAINWORKER_STATE_CONNECTING);
setState(NETWORKSTREAMWORKER_STATE_CONNECTING);
return true;
}

bool EngineShoutcast::processConnect() {
// set to busy in case another thread calls one of the other
// EngineShoutcast calls
m_iShoutStatus = SHOUTERR_BUSY;
m_pShoutcastStatus->set(SICECHAINWORKER_STATE_CONNECTING);
m_pShoutcastStatus->set(NETWORKSTREAMWORKER_STATE_CONNECTING);
// reset the number of failures to zero
m_iShoutFailures = 0;
// set to a high number to automatically update the metadata
Expand Down Expand Up @@ -399,7 +399,7 @@ bool EngineShoutcast::processConnect() {
m_iShoutStatus = shout_open(m_pShout);
if (m_iShoutStatus == SHOUTERR_SUCCESS) {
m_iShoutStatus = SHOUTERR_CONNECTED;
setState(SICECHAINWORKER_STATE_CONNECTED);
setState(NETWORKSTREAMWORKER_STATE_CONNECTED);
}

if ((m_iShoutStatus == SHOUTERR_BUSY) ||
Expand All @@ -415,25 +415,25 @@ bool EngineShoutcast::processConnect() {
if (m_pShout) {
shout_close(m_pShout);
}
setState(SIDECHAINWORKER_STATE_ERROR);
setState(NETWORKSTREAMWORKER_STATE_ERROR);
m_pConfig->set(ConfigKey(SHOUTCAST_PREF_KEY,"enabled"),ConfigValue("0"));
m_pShoutcastStatus->set(SIDECHAINWORKER_STATE_DISCONNECTED);
m_pShoutcastStatus->set(NETWORKSTREAMWORKER_STATE_DISCONNECTED);
return false;
}

m_iShoutFailures = 0;
int timeout = 0;
while (m_iShoutStatus == SHOUTERR_BUSY && timeout < TIMEOUT) {
setState(SIDECHAINWORKER_STATE_WAITING);
setState(NETWORKSTREAMWORKER_STATE_WAITING);
qDebug() << "Connection pending. Sleeping...";
sleep(1);
m_iShoutStatus = shout_get_connected(m_pShout);
++ timeout;
}
if (m_iShoutStatus == SHOUTERR_CONNECTED) {
setState(SIDECHAINWORKER_STATE_READY);
setState(NETWORKSTREAMWORKER_STATE_READY);
qDebug() << "***********Connected to streaming server...";
m_pShoutcastStatus->set(SICECHAINWORKER_STATE_CONNECTED);
m_pShoutcastStatus->set(NETWORKSTREAMWORKER_STATE_CONNECTED);

// Signal user also that we are connected
infoDialog(tr("Mixxx has successfully connected to the streaming server"), "");
Expand All @@ -443,22 +443,22 @@ bool EngineShoutcast::processConnect() {

return true;
}
setState(SIDECHAINWORKER_STATE_ERROR);
setState(NETWORKSTREAMWORKER_STATE_ERROR);
//otherwise disable shoutcast in preferences
m_pConfig->set(ConfigKey(SHOUTCAST_PREF_KEY,"enabled"),ConfigValue("0"));
if (m_pShout) {
shout_close(m_pShout);
//errorDialog(tr("Mixxx could not connect to the server"), tr("Please check your connection to the Internet and verify that your username and password are correct."));
}
m_pShoutcastStatus->set(SIDECHAINWORKER_STATE_DISCONNECTED);
m_pShoutcastStatus->set(NETWORKSTREAMWORKER_STATE_DISCONNECTED);
return false;
}

void EngineShoutcast::processDisconnect() {
if (isConnected()) {
// We are conneced but shoutcast is disabled. Disconnect.
shout_close(m_pShout);
m_pShoutcastStatus->set(SIDECHAINWORKER_STATE_DISCONNECTED);
m_pShoutcastStatus->set(NETWORKSTREAMWORKER_STATE_DISCONNECTED);
infoDialog(tr("Mixxx has successfully disconnected from the streaming server"), "");
}

Expand Down Expand Up @@ -525,7 +525,7 @@ void EngineShoutcast::write(unsigned char *header, unsigned char *body,

void EngineShoutcast::process(const CSAMPLE* pBuffer, const int iBufferSize) {

setState(SIDECHAINWORKER_STATE_BUSY);
setState(NETWORKSTREAMWORKER_STATE_BUSY);
// If we are here then the user wants to be connected (shoutcast is enabled
// in the preferences).

Expand Down Expand Up @@ -559,7 +559,7 @@ void EngineShoutcast::process(const CSAMPLE* pBuffer, const int iBufferSize) {
if (metaDataHasChanged()) {
updateMetaData();
}
setState(SIDECHAINWORKER_STATE_READY);
setState(NETWORKSTREAMWORKER_STATE_READY);
}

bool EngineShoutcast::metaDataHasChanged() {
Expand Down Expand Up @@ -700,7 +700,7 @@ void EngineShoutcast::errorDialog(QString text, QString detailedError) {
props->setDefaultButton(QMessageBox::Close);
props->setModal(false);
ErrorDialogHandler::instance()->requestErrorDialog(props);
setState(SIDECHAINWORKER_STATE_ERROR);
setState(NETWORKSTREAMWORKER_STATE_ERROR);
}

void EngineShoutcast::infoDialog(QString text, QString detailedInfo) {
Expand Down Expand Up @@ -730,18 +730,18 @@ void EngineShoutcast::run() {
QThread::currentThread()->setObjectName(QString("EngineShoutcast %1").arg(++id));
qDebug() << "EngineShoutcast::run: starting thread";

setState(SIDECHAINWORKER_STATE_BUSY);
setState(NETWORKSTREAMWORKER_STATE_BUSY);
processConnect();

setState(SIDECHAINWORKER_STATE_WAITING);
setState(NETWORKSTREAMWORKER_STATE_WAITING);
DEBUG_ASSERT_AND_HANDLE(m_pOutputFifo) {
return;
}
if (m_pOutputFifo->readAvailable()) {
m_pOutputFifo->flushReadData(m_pOutputFifo->readAvailable());
}
m_threadWaiting = true;
setState(SIDECHAINWORKER_STATE_READY);
setState(NETWORKSTREAMWORKER_STATE_READY);
for(;;) {
m_readSema.acquire();
// Check to see if Shoutcast is enabled, and pass the samples off to be
Expand Down
5 changes: 3 additions & 2 deletions src/engine/sidechain/engineshoutcast.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "controlobjectthread.h"
#include "controlobjectslave.h"
#include "encoder/encodercallback.h"
#include "engine/sidechain/sidechainworker.h"
#include "engine/sidechain/networkstreamworker.h"
#include "errordialoghandler.h"
#include "trackinfoobject.h"
#include "util/fifo.h"
Expand All @@ -45,7 +45,8 @@ typedef struct shout shout_t;
struct _util_dict;
typedef struct _util_dict shout_metadata_t;

class EngineShoutcast : public QThread, public EncoderCallback, public SideChainWorker {
class EngineShoutcast :
public QThread, public EncoderCallback, public NetworkStreamWorker {
Q_OBJECT
public:
EngineShoutcast(ConfigObject<ConfigValue>* _config);
Expand Down
56 changes: 1 addition & 55 deletions src/engine/sidechain/sidechainworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,13 @@
#define SIDECHAINWORKER_H

#include "util/types.h"
#include "util/fifo.h"

/*
* States:
* Error Something errornous has happenned and can't go on
* New First state before init
* Init Initing state don't feed anything in this state
* Waiting Waiting something not ready yet
* Busy Is busy doing something can't process anything new
* Ready Functioning ok
* Reading Reading something and can't do anything else
* Writing Writing something and can't do anything else
* Connected Is connected to storage or server
* Connecting Trying to connect storate or server
* Disconnected Ain't connected to storage or server
*
* First state should be SIDECHAINWORKER_STATE_UNKNOWN and
* if state handling ain't supported by SideChainWorker-class
* then 'SIDECHAINWORKER_STATE_NEW' should be treated as
* SIDECHAINWORKER_STATE_READY. Newly written SideChainWorker-class
* should support state handling at leas this SIDECHAINWORKER_STATE_READY state.
*/

enum SidechaingStates {
SIDECHAINWORKER_STATE_ERROR = -1,
SIDECHAINWORKER_STATE_NEW,
SIDECHAINWORKER_STATE_INIT,
SIDECHAINWORKER_STATE_WAITING,
SIDECHAINWORKER_STATE_BUSY,
SIDECHAINWORKER_STATE_READY,
SIDECHAINWORKER_STATE_READING,
SIDECHAINWORKER_STATE_WRITING,
SICECHAINWORKER_STATE_CONNECTED,
SICECHAINWORKER_STATE_CONNECTING,
SIDECHAINWORKER_STATE_DISCONNECTED
};

class SideChainWorker {
public:
SideChainWorker() :
m_iSideChainWorkerState(SIDECHAINWORKER_STATE_NEW) { }
SideChainWorker() { }
virtual ~SideChainWorker() { }
virtual void process(const CSAMPLE* pBuffer, const int iBufferSize) = 0;
virtual void shutdown() = 0;
virtual void outputAvailable() {
};
virtual void setOutputFifo(FIFO<CSAMPLE>* pOutputFifo) {
Q_UNUSED(pOutputFifo);
};
virtual bool threadWaiting() {
return false;
}
virtual int getState() {
return m_iSideChainWorkerState;
}
protected:
virtual void setState(int state) {
m_iSideChainWorkerState = state;
}
private:
int m_iSideChainWorkerState;
};

#endif /* SIDECHAINWORKER_H */

0 comments on commit 29a4814

Please sign in to comment.