vdr-plugin-iptv/streamer.c

255 lines
6.0 KiB
C
Raw Normal View History

2007-09-12 19:28:59 +02:00
/*
* streamer.c: IPTV plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
2007-09-13 16:10:37 +02:00
* $Id: streamer.c,v 1.9 2007/09/13 14:10:37 ajhseppa Exp $
2007-09-12 19:28:59 +02:00
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <vdr/device.h>
#include <vdr/thread.h>
#include <vdr/ringbuffer.h>
#include "common.h"
#include "streamer.h"
2007-09-12 23:55:57 +02:00
cIptvStreamer::cIptvStreamer(cRingBufferLinear* Buffer, cMutex* Mutex)
2007-09-12 19:28:59 +02:00
: cThread("IPTV streamer"),
dataPort(1234),
2007-09-12 23:55:57 +02:00
pRingBuffer(Buffer),
2007-09-12 19:28:59 +02:00
bufferSize(TS_SIZE * 7),
mutex(Mutex),
socketActive(false),
mcastActive(false)
{
debug("cIptvStreamer::cIptvStreamer()\n");
memset(&stream, '\0', strlen(stream));
// Create the socket
CheckAndCreateSocket(dataPort);
2007-09-12 19:28:59 +02:00
pReceiveBuffer = MALLOC(unsigned char, bufferSize);
if (!pReceiveBuffer)
error("ERROR: MALLOC(pReceiveBuffer) failed");
}
cIptvStreamer::~cIptvStreamer()
{
debug("cIptvStreamer::~cIptvStreamer()\n");
2007-09-12 20:30:50 +02:00
Deactivate();
2007-09-12 19:28:59 +02:00
if (pReceiveBuffer)
free(pReceiveBuffer);
// Close the socket
CloseSocket();
2007-09-12 19:28:59 +02:00
}
void cIptvStreamer::Action()
{
debug("cIptvStreamer::Action(): Entering\n");
2007-09-13 16:10:37 +02:00
// Create files necessary for selecting I/O from socket.
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(socketDesc, &rfds);
2007-09-12 19:28:59 +02:00
while (Running()) {
socklen_t addrlen = sizeof(sa);
2007-09-13 16:10:37 +02:00
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000;
// Wait for data
int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv);
if (retval < 0) {
char tmp[64];
error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp)));
} else if(retval) {
// Read data from socket
int length = recvfrom(socketDesc, pReceiveBuffer, bufferSize,
MSG_DONTWAIT, (struct sockaddr *)&sa, &addrlen);
mutex->Lock();
int p = pRingBuffer->Put(pReceiveBuffer, length);
if (p != length && Running()) {
pRingBuffer->ReportOverflow(length - p);
}
mutex->Unlock();
} else {
debug("Timeout waiting for data\n");
2007-09-12 19:28:59 +02:00
}
2007-09-13 16:10:37 +02:00
}
2007-09-12 19:28:59 +02:00
debug("cIptvStreamer::Action(): Exiting\n");
}
bool cIptvStreamer::CheckAndCreateSocket(const int port)
{
// If socket is there already and it is bound to a different port, it must
// be closed first
if (socketActive && port != dataPort) {
debug("Full tear-down of active socket\n");
CloseSocket();
}
// Bind to the socket if it is not active already
if (!socketActive) {
// Create socket
socketDesc = socket(PF_INET, SOCK_DGRAM, 0);
if (socketDesc < 0) {
char tmp[64];
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
2007-09-12 23:55:57 +02:00
return false;
}
2007-09-13 16:10:37 +02:00
// Make it use non-blocking I/O to avoid stuck read -calls.
if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) {
char tmp[64];
error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp)));
close(socketDesc);
return false;
}
int yes = 1;
// Allow multiple sockets to use the same PORT number
if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0) {
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
2007-09-12 23:55:57 +02:00
close(socketDesc);
return false;
}
memset(&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
int err = bind(socketDesc, (struct sockaddr *)&sa, sizeof(sa));
if (err < 0) {
char tmp[64];
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
2007-09-12 23:55:57 +02:00
close(socketDesc);
return false;
}
dataPort = port;
socketActive = true;
}
return true;
}
void cIptvStreamer::CloseSocket()
{
if (socketActive) {
close(socketDesc);
socketActive = false;
2007-09-13 16:10:37 +02:00
mcastActive = false;
}
}
2007-09-12 19:28:59 +02:00
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) {
2007-09-12 23:55:57 +02:00
debug("cIptvStreamer::Activate(): Already active\n");
2007-09-12 19:28:59 +02:00
return true;
}
// Ensure that socket is valid
CheckAndCreateSocket(dataPort);
2007-09-12 19:28:59 +02:00
// Join a new multicast group
mreq.imr_multiaddr.s_addr = inet_addr(stream);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
2007-09-12 23:55:57 +02:00
int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq));
2007-09-12 19:28:59 +02:00
if (err < 0) {
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
2007-09-12 23:55:57 +02:00
// Start thread
if (!Running())
Start();
2007-09-12 19:28:59 +02:00
mcastActive = true;
return true;
}
bool cIptvStreamer::Deactivate()
{
2007-09-12 23:55:57 +02:00
debug("cIptvStreamer::Deactivate(): stream = %s\n", stream);
// Stop thread
if (Running())
Cancel(3);
2007-09-12 19:28:59 +02:00
2007-09-12 23:55:57 +02:00
if (stream && mcastActive) {
2007-09-12 19:28:59 +02:00
struct ip_mreq mreq;
2007-09-12 23:55:57 +02:00
debug("cIptvStreamer::Deactivate(): Deactivating\n");
2007-09-12 19:28:59 +02:00
mreq.imr_multiaddr.s_addr = inet_addr(stream);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
2007-09-12 23:55:57 +02:00
int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq,
sizeof(mreq));
2007-09-12 19:28:59 +02:00
if (err < 0) {
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Not active any more
mcastActive = false;
}
return true;
}
2007-09-12 23:14:51 +02:00
bool cIptvStreamer::SetStream(const char* address, const int port, const char* protocol)
2007-09-12 19:28:59 +02:00
{
2007-09-12 23:55:57 +02:00
debug("cIptvStreamer::SetStream(): %s://%s:%d\n", protocol, address, port);
2007-09-12 19:28:59 +02:00
2007-09-12 23:55:57 +02:00
// Deactivate the reception if it is running currently. Otherwise the
2007-09-12 20:05:58 +02:00
// reception stream is overwritten and cannot be un-set after this
Deactivate();
// Ensure that the socket is valid
CheckAndCreateSocket(port);
2007-09-12 19:28:59 +02:00
// 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;
}