Skip to content

Commit

Permalink
Convert the DVB Stream Read thread to QThread
Browse files Browse the repository at this point in the history
Pursuant to #5501, I have converted the DVB Stream Read thread from pthreads
to QThread.  The startup synchronization here was a bit excessive, and has been
simplified.

This has been successfully recording from my DVB capture devices (ATSC to be
exact) for most oF a week.
  • Loading branch information
Beirdo committed Feb 26, 2011
1 parent ebe0ba8 commit f14252b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 38 deletions.
41 changes: 10 additions & 31 deletions mythtv/libs/libmythtv/dvbstreamhandler.cpp
@@ -1,7 +1,6 @@
// -*- Mode: c++ -*-

// POSIX headers
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/select.h>
Expand Down Expand Up @@ -95,8 +94,6 @@ DVBStreamHandler::DVBStreamHandler(const QString &dvb_device) :
_allow_retune(false),

_start_stop_lock(QMutex::Recursive),
_running(false),
_reader_thread(pthread_t()),
_using_section_reader(false),

_device_read_buffer(NULL),
Expand Down Expand Up @@ -188,11 +185,12 @@ void DVBStreamHandler::RemoveListener(MPEGStreamData *data)
VERBOSE(VB_RECORD, LOC + "RemoveListener("<<data<<") -- end");
}

void *run_dvb_stream_handler_thunk(void *param)
void DVBReadThread::run(void)
{
DVBStreamHandler *mon = (DVBStreamHandler*) param;
mon->Run();
return NULL;
if (!m_parent)
return;

m_parent->Run();
}

void DVBStreamHandler::Start(void)
Expand All @@ -209,22 +207,14 @@ void DVBStreamHandler::Start(void)

if (!IsRunning())
{
QMutex is_running_lock;
int rval = pthread_create(&_reader_thread, NULL,
run_dvb_stream_handler_thunk, this);
_reader_thread.SetParent(this);
_reader_thread.start();

if (0 != rval)
if (_reader_thread.isRunning())
{
VERBOSE(VB_IMPORTANT, LOC_ERR +
"Start: Failed to create thread." + ENO);
VERBOSE(VB_IMPORTANT, LOC_ERR + "Start: Failed to create thread.");
return;
}

is_running_lock.lock();
while (!IsRunning())
{
_running_state_changed.wait(&is_running_lock, 100);
}
}
}

Expand All @@ -236,15 +226,12 @@ void DVBStreamHandler::Stop(void)
{
if (_device_read_buffer)
_device_read_buffer->Stop();
SetRunning(false);
pthread_join(_reader_thread, NULL);
_reader_thread.wait();
}
}

void DVBStreamHandler::Run(void)
{
SetRunning(true);

_using_section_reader = !SupportsTSMonitoring() && _allow_section_reader;

if (_using_section_reader)
Expand Down Expand Up @@ -407,8 +394,6 @@ void DVBStreamHandler::RunTS(void)
delete[] buffer;

VERBOSE(VB_RECORD, LOC + "RunTS(): " + "end");

SetRunning(false);
}

/** \fn DVBStreamHandler::RunSR(void)
Expand Down Expand Up @@ -990,12 +975,6 @@ bool PIDInfo::Close(const QString &dvb_dev)
return true;
}

void DVBStreamHandler::SetRunning(bool is_running)
{
_running = is_running;
_running_state_changed.wakeAll();
}

PIDPriority DVBStreamHandler::GetPIDPriority(uint pid) const
{
QMutexLocker reading_locker(&_listener_lock);
Expand Down
22 changes: 15 additions & 7 deletions mythtv/libs/libmythtv/dvbstreamhandler.h
Expand Up @@ -8,6 +8,7 @@ using namespace std;

#include <QMap>
#include <QMutex>
#include <QThread>

#include "util.h"
#include "DeviceReadBuffer.h"
Expand Down Expand Up @@ -45,9 +46,20 @@ class PIDInfo
};
typedef QMap<uint,PIDInfo*> PIDInfoMap;

class DVBReadThread : public QThread
{
Q_OBJECT
public:
DVBReadThread() : m_parent(NULL) {}
void SetParent(DVBStreamHandler *parent) { m_parent = parent; }
void run(void);
private:
DVBStreamHandler *m_parent;
};

class DVBStreamHandler : public ReaderPausedCB
{
friend void *run_dvb_stream_handler_thunk(void *param);
friend class DVBReadThread;

public:
static DVBStreamHandler *Get(const QString &dvb_device);
Expand All @@ -60,7 +72,7 @@ class DVBStreamHandler : public ReaderPausedCB

void RetuneMonitor(void);

bool IsRunning(void) const { return _running; }
bool IsRunning(void) const { return _reader_thread.isRunning(); }
bool IsRetuneAllowed(void) const { return _allow_retune; }

void SetRetuneAllowed(bool allow,
Expand Down Expand Up @@ -88,8 +100,6 @@ class DVBStreamHandler : public ReaderPausedCB
bool RemoveAllPIDFilters(void);
void CycleFiltersByPriority(void);

void SetRunning(bool);

PIDPriority GetPIDPriority(uint pid) const;
bool SupportsTSMonitoring(void);

Expand All @@ -101,9 +111,7 @@ class DVBStreamHandler : public ReaderPausedCB
bool _allow_retune;

mutable QMutex _start_stop_lock;
bool _running;
QWaitCondition _running_state_changed;
pthread_t _reader_thread;
DVBReadThread _reader_thread;
bool _using_section_reader;
DeviceReadBuffer *_device_read_buffer;
DTVSignalMonitor *_sigmon;
Expand Down

0 comments on commit f14252b

Please sign in to comment.