1
0
mirror of https://github.com/rofafor/vdr-plugin-iptv.git synced 2023-10-10 13:37:03 +02:00

Minor refactoring.

This commit is contained in:
Rolf Ahrenberg 2007-09-13 16:58:22 +00:00
parent aa57cc6fa2
commit 5d6d68d566
3 changed files with 146 additions and 162 deletions

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * See the README file for copyright information and how to reach the author.
* *
* $Id: device.c,v 1.4 2007/09/12 21:21:55 rahrenbe Exp $ * $Id: device.c,v 1.5 2007/09/13 16:58:22 rahrenbe Exp $
*/ */
#include "common.h" #include "common.h"
@ -140,7 +140,7 @@ bool cIptvDevice::OpenDvr(void)
isPacketDelivered = false; isPacketDelivered = false;
tsBuffer->Clear(); tsBuffer->Clear();
mutex.Unlock(); mutex.Unlock();
pIptvStreamer->Activate(); pIptvStreamer->OpenStream();
isOpenDvr = true; isOpenDvr = true;
return true; return true;
} }
@ -148,7 +148,7 @@ bool cIptvDevice::OpenDvr(void)
void cIptvDevice::CloseDvr(void) void cIptvDevice::CloseDvr(void)
{ {
debug("cIptvDevice::CloseDvr(%d)\n", deviceIndex); debug("cIptvDevice::CloseDvr(%d)\n", deviceIndex);
pIptvStreamer->Deactivate(); pIptvStreamer->CloseStream();
isOpenDvr = false; isOpenDvr = false;
} }

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * See the README file for copyright information and how to reach the author.
* *
* $Id: streamer.c,v 1.9 2007/09/13 14:10:37 ajhseppa Exp $ * $Id: streamer.c,v 1.10 2007/09/13 16:58:22 rahrenbe Exp $
*/ */
#include <sys/types.h> #include <sys/types.h>
@ -21,20 +21,18 @@
cIptvStreamer::cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex) cIptvStreamer::cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex)
: cThread("IPTV streamer"), : cThread("IPTV streamer"),
dataPort(1234), streamPort(1234),
socketDesc(-1),
pRingBuffer(Buffer), pRingBuffer(Buffer),
bufferSize(TS_SIZE * 7), bufferSize(TS_SIZE * 7),
mutex(Mutex), mutex(Mutex),
socketActive(false),
mcastActive(false) mcastActive(false)
{ {
debug("cIptvStreamer::cIptvStreamer()\n"); debug("cIptvStreamer::cIptvStreamer()\n");
memset(&stream, '\0', strlen(stream)); streamAddr = strdup("");
// Create the socket // Create the socket
CheckAndCreateSocket(dataPort); OpenSocket(streamPort);
// Allocate receive buffer
pReceiveBuffer = MALLOC(unsigned char, bufferSize); pReceiveBuffer = MALLOC(unsigned char, bufferSize);
if (!pReceiveBuffer) if (!pReceiveBuffer)
error("ERROR: MALLOC(pReceiveBuffer) failed"); error("ERROR: MALLOC(pReceiveBuffer) failed");
@ -43,69 +41,65 @@ cIptvStreamer::cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex)
cIptvStreamer::~cIptvStreamer() cIptvStreamer::~cIptvStreamer()
{ {
debug("cIptvStreamer::~cIptvStreamer()\n"); debug("cIptvStreamer::~cIptvStreamer()\n");
// Drop the multicast group
Deactivate(); DropMulticast();
// Close the stream and socket
if (pReceiveBuffer) CloseStream();
free(pReceiveBuffer);
// Close the socket
CloseSocket(); CloseSocket();
// Free allocated memory
free(streamAddr);
free(pReceiveBuffer);
} }
void cIptvStreamer::Action() void cIptvStreamer::Action(void)
{ {
debug("cIptvStreamer::Action(): Entering\n"); debug("cIptvStreamer::Action(): Entering\n");
// Create files necessary for selecting I/O from socket
// Create files necessary for selecting I/O from socket.
fd_set rfds; fd_set rfds;
FD_ZERO(&rfds); FD_ZERO(&rfds);
FD_SET(socketDesc, &rfds); FD_SET(socketDesc, &rfds);
// Do the thread loop
while (Running()) { while (Running()) {
socklen_t addrlen = sizeof(sa); if (pRingBuffer && mutex && pReceiveBuffer && (socketDesc >= 0)) {
struct timeval tv; struct timeval tv;
socklen_t addrlen = sizeof(sockAddr);
// Wait for data
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 500000; tv.tv_usec = 500000;
// Wait for data
int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv); int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv);
if (retval < 0) { if (retval < 0) {
char tmp[64]; char tmp[64];
error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp)));
} else if (retval) { } else if (retval) {
// Read data from socket // Read data from socket
int length = recvfrom(socketDesc, pReceiveBuffer, bufferSize, int length = recvfrom(socketDesc, pReceiveBuffer, bufferSize, MSG_DONTWAIT,
MSG_DONTWAIT, (struct sockaddr *)&sa, &addrlen); (struct sockaddr *)&sockAddr, &addrlen);
mutex->Lock(); mutex->Lock();
int p = pRingBuffer->Put(pReceiveBuffer, length); int p = pRingBuffer->Put(pReceiveBuffer, length);
if (p != length && Running()) { if (p != length && Running())
pRingBuffer->ReportOverflow(length - p); pRingBuffer->ReportOverflow(length - p);
}
mutex->Unlock(); mutex->Unlock();
} else
} else { debug("cIptvStreamer::Action(): Timeout\n");
debug("Timeout waiting for data\n");
} }
else
cCondWait::SleepMs(100); // avoid busy loop
} }
debug("cIptvStreamer::Action(): Exiting\n"); debug("cIptvStreamer::Action(): Exiting\n");
} }
bool cIptvStreamer::CheckAndCreateSocket(const int port) bool cIptvStreamer::OpenSocket(const int port)
{ {
debug("cIptvStreamer::OpenSocket()\n");
// If socket is there already and it is bound to a different port, it must // If socket is there already and it is bound to a different port, it must
// be closed first // be closed first
if (socketActive && port != dataPort) { if (port != streamPort) {
debug("Full tear-down of active socket\n"); debug("cIptvStreamer::OpenSocket(): Socket tear-down\n");
CloseSocket(); CloseSocket();
} }
// Bind to the socket if it is not active already // Bind to the socket if it is not active already
if (!socketActive) { if (socketDesc < 0) {
int yes = 1;
// Create socket // Create socket
socketDesc = socket(PF_INET, SOCK_DGRAM, 0); socketDesc = socket(PF_INET, SOCK_DGRAM, 0);
if (socketDesc < 0) { if (socketDesc < 0) {
@ -113,79 +107,60 @@ bool cIptvStreamer::CheckAndCreateSocket(const int port)
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false; return false;
} }
// Make it use non-blocking I/O to avoid stuck read calls
// Make it use non-blocking I/O to avoid stuck read -calls.
if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) { if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) {
char tmp[64]; char tmp[64];
error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp)));
close(socketDesc); CloseSocket();
return false; return false;
} }
int yes = 1;
// Allow multiple sockets to use the same PORT number // Allow multiple sockets to use the same PORT number
if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0) { sizeof(yes)) < 0) {
char tmp[64]; char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
close(socketDesc); CloseSocket();
return false; return false;
} }
// Bind socket
memset(&sa, '\0', sizeof(sa)); memset(&sockAddr, '\0', sizeof(sockAddr));
sa.sin_family = AF_INET; sockAddr.sin_family = AF_INET;
sa.sin_port = htons(port); sockAddr.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_ANY); sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr));
int err = bind(socketDesc, (struct sockaddr *)&sa, sizeof(sa));
if (err < 0) { if (err < 0) {
char tmp[64]; char tmp[64];
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
close(socketDesc); CloseSocket();
return false; return false;
} }
// Update stream port
dataPort = port; streamPort = port;
socketActive = true;
} }
return true;
}
void cIptvStreamer::CloseSocket()
{
if (socketActive) {
close(socketDesc);
socketActive = false;
mcastActive = false;
}
}
bool cIptvStreamer::Activate()
{
struct ip_mreq mreq;
debug("cIptvStreamer::Activate(): stream = %s\n", stream);
if (!stream) {
error("No stream set yet, not activating\n");
return false;
}
if (mcastActive) {
debug("cIptvStreamer::Activate(): Already active\n");
return true; return true;
} }
void cIptvStreamer::CloseSocket(void)
{
debug("cIptvStreamer::CloseSocket()\n");
// Check if socket exists
if (socketDesc >= 0) {
close(socketDesc);
socketDesc = -1;
}
}
bool cIptvStreamer::JoinMulticast(void)
{
debug("cIptvStreamer::JoinMulticast()\n");
// Check that stream address is valid
if (!mcastActive && !isempty(streamAddr)) {
// Ensure that socket is valid // Ensure that socket is valid
CheckAndCreateSocket(dataPort); OpenSocket(streamPort);
// Join a new multicast group // Join a new multicast group
mreq.imr_multiaddr.s_addr = inet_addr(stream); struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(streamAddr);
mreq.imr_interface.s_addr = htonl(INADDR_ANY); mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)); sizeof(mreq));
if (err < 0) { if (err < 0) {
@ -193,29 +168,23 @@ bool cIptvStreamer::Activate()
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false; return false;
} }
// Update multicasting flag
// Start thread
if (!Running())
Start();
mcastActive = true; mcastActive = true;
}
return true; return true;
} }
bool cIptvStreamer::Deactivate() bool cIptvStreamer::DropMulticast(void)
{ {
debug("cIptvStreamer::Deactivate(): stream = %s\n", stream); debug("cIptvStreamer::DropMulticast()\n");
// Check that stream address is valid
// Stop thread if (mcastActive && !isempty(streamAddr)) {
if (Running()) // Ensure that socket is valid
Cancel(3); OpenSocket(streamPort);
// Drop the multicast group
if (stream && mcastActive) {
struct ip_mreq mreq; struct ip_mreq mreq;
debug("cIptvStreamer::Deactivate(): Deactivating\n"); mreq.imr_multiaddr.s_addr = inet_addr(streamAddr);
mreq.imr_multiaddr.s_addr = inet_addr(stream);
mreq.imr_interface.s_addr = htonl(INADDR_ANY); mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq,
sizeof(mreq)); sizeof(mreq));
if (err < 0) { if (err < 0) {
@ -223,32 +192,45 @@ bool cIptvStreamer::Deactivate()
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false; return false;
} }
// Update multicasting flag
// Not active any more
mcastActive = false; mcastActive = false;
} }
return true;
}
bool cIptvStreamer::OpenStream(void)
{
debug("cIptvStreamer::OpenStream(): streamAddr = %s\n", streamAddr);
// Join a new multicast group
JoinMulticast();
// Start thread
Start();
return true;
}
bool cIptvStreamer::CloseStream(void)
{
debug("cIptvStreamer::CloseStream(): streamAddr = %s\n", streamAddr);
// Stop thread
if (Running())
Cancel(3);
// Drop the multicast group
DropMulticast();
return true; return true;
} }
bool cIptvStreamer::SetStream(const char* address, const int port, const char* protocol) bool cIptvStreamer::SetStream(const char* address, const int port, const char* protocol)
{ {
debug("cIptvStreamer::SetStream(): %s://%s:%d\n", protocol, address, port); debug("cIptvStreamer::SetStream(): %s://%s:%d\n", protocol, address, port);
if (!isempty(address)) {
// Deactivate the reception if it is running currently. Otherwise the // Drop the multicast group
// reception stream is overwritten and cannot be un-set after this DropMulticast();
Deactivate(); // Update stream address and port
streamAddr = strcpyrealloc(streamAddr, address);
// Ensure that the socket is valid streamPort = port;
CheckAndCreateSocket(port); // Join a new multicast group
JoinMulticast();
// Check if the address fits into the buffer
if (strlen(address) > sizeof(stream)) {
error("ERROR: Address too big\n");
return false;
} }
strn0cpy(stream, address, sizeof(stream));
return true; return true;
} }

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * See the README file for copyright information and how to reach the author.
* *
* $Id: streamer.h,v 1.4 2007/09/12 21:55:57 rahrenbe Exp $ * $Id: streamer.h,v 1.5 2007/09/13 16:58:22 rahrenbe Exp $
*/ */
#ifndef __IPTV_STREAMER_H #ifndef __IPTV_STREAMER_H
@ -16,28 +16,30 @@
class cIptvStreamer : public cThread { class cIptvStreamer : public cThread {
private: private:
char stream[256]; char* streamAddr;
int streamPort;
int socketDesc; int socketDesc;
int dataPort; struct sockaddr_in sockAddr;
struct sockaddr_in sa;
cRingBufferLinear* pRingBuffer; cRingBufferLinear* pRingBuffer;
unsigned char* pReceiveBuffer; unsigned char* pReceiveBuffer;
unsigned int bufferSize; unsigned int bufferSize;
cMutex* mutex; cMutex* mutex;
bool socketActive;
bool mcastActive; bool mcastActive;
bool CheckAndCreateSocket(const int port); private:
void CloseSocket(); bool OpenSocket(const int port);
void CloseSocket(void);
bool JoinMulticast(void);
bool DropMulticast(void);
public: public:
cIptvStreamer(); cIptvStreamer();
cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex); cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex);
virtual ~cIptvStreamer(); virtual ~cIptvStreamer();
virtual void Action(); virtual void Action(void);
bool SetStream(const char* address, const int port, const char* protocol); bool SetStream(const char* address, const int port, const char* protocol);
bool Activate(); bool OpenStream(void);
bool Deactivate(); bool CloseStream(void);
}; };
#endif // __IPTV_STREAMER_H #endif // __IPTV_STREAMER_H