vdr-plugin-iptv/streamer.c

237 lines
6.7 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 18:58:22 +02:00
* $Id: streamer.c,v 1.10 2007/09/13 16:58:22 rahrenbe 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"),
2007-09-13 18:58:22 +02:00
streamPort(1234),
socketDesc(-1),
2007-09-12 23:55:57 +02:00
pRingBuffer(Buffer),
2007-09-12 19:28:59 +02:00
bufferSize(TS_SIZE * 7),
mutex(Mutex),
mcastActive(false)
{
debug("cIptvStreamer::cIptvStreamer()\n");
2007-09-13 18:58:22 +02:00
streamAddr = strdup("");
// Create the socket
2007-09-13 18:58:22 +02:00
OpenSocket(streamPort);
// Allocate receive buffer
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-13 18:58:22 +02:00
// Drop the multicast group
DropMulticast();
// Close the stream and socket
CloseStream();
CloseSocket();
2007-09-13 18:58:22 +02:00
// Free allocated memory
free(streamAddr);
free(pReceiveBuffer);
2007-09-12 19:28:59 +02:00
}
2007-09-13 18:58:22 +02:00
void cIptvStreamer::Action(void)
2007-09-12 19:28:59 +02:00
{
debug("cIptvStreamer::Action(): Entering\n");
2007-09-13 18:58:22 +02:00
// Create files necessary for selecting I/O from socket
2007-09-13 16:10:37 +02:00
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(socketDesc, &rfds);
2007-09-13 18:58:22 +02:00
// Do the thread loop
2007-09-12 19:28:59 +02:00
while (Running()) {
2007-09-13 18:58:22 +02:00
if (pRingBuffer && mutex && pReceiveBuffer && (socketDesc >= 0)) {
struct timeval tv;
socklen_t addrlen = sizeof(sockAddr);
// Wait for data
tv.tv_sec = 0;
tv.tv_usec = 500000;
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 *)&sockAddr, &addrlen);
mutex->Lock();
int p = pRingBuffer->Put(pReceiveBuffer, length);
if (p != length && Running())
pRingBuffer->ReportOverflow(length - p);
mutex->Unlock();
} else
debug("cIptvStreamer::Action(): Timeout\n");
}
else
cCondWait::SleepMs(100); // avoid busy loop
2007-09-12 19:28:59 +02:00
}
debug("cIptvStreamer::Action(): Exiting\n");
}
2007-09-13 18:58:22 +02:00
bool cIptvStreamer::OpenSocket(const int port)
{
2007-09-13 18:58:22 +02:00
debug("cIptvStreamer::OpenSocket()\n");
// If socket is there already and it is bound to a different port, it must
// be closed first
2007-09-13 18:58:22 +02:00
if (port != streamPort) {
debug("cIptvStreamer::OpenSocket(): Socket tear-down\n");
CloseSocket();
}
// Bind to the socket if it is not active already
2007-09-13 18:58:22 +02:00
if (socketDesc < 0) {
int yes = 1;
// 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 18:58:22 +02:00
// Make it use non-blocking I/O to avoid stuck read calls
2007-09-13 16:10:37 +02:00
if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) {
char tmp[64];
error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp)));
2007-09-13 18:58:22 +02:00
CloseSocket();
2007-09-13 16:10:37 +02:00
return false;
}
// 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-13 18:58:22 +02:00
CloseSocket();
2007-09-12 23:55:57 +02:00
return false;
}
2007-09-13 18:58:22 +02:00
// Bind socket
memset(&sockAddr, '\0', sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(port);
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr));
if (err < 0) {
char tmp[64];
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
2007-09-13 18:58:22 +02:00
CloseSocket();
return false;
}
2007-09-13 18:58:22 +02:00
// Update stream port
streamPort = port;
}
return true;
}
2007-09-13 18:58:22 +02:00
void cIptvStreamer::CloseSocket(void)
{
2007-09-13 18:58:22 +02:00
debug("cIptvStreamer::CloseSocket()\n");
// Check if socket exists
if (socketDesc >= 0) {
close(socketDesc);
socketDesc = -1;
2007-09-12 19:28:59 +02:00
}
}
2007-09-13 18:58:22 +02:00
bool cIptvStreamer::JoinMulticast(void)
2007-09-12 19:28:59 +02:00
{
2007-09-13 18:58:22 +02:00
debug("cIptvStreamer::JoinMulticast()\n");
// Check that stream address is valid
if (!mcastActive && !isempty(streamAddr)) {
// Ensure that socket is valid
OpenSocket(streamPort);
// Join a new multicast group
2007-09-12 19:28:59 +02:00
struct ip_mreq mreq;
2007-09-13 18:58:22 +02:00
mreq.imr_multiaddr.s_addr = inet_addr(streamAddr);
2007-09-12 19:28:59 +02:00
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
2007-09-13 18:58:22 +02:00
int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
2007-09-12 23:55:57 +02:00
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-13 18:58:22 +02:00
// Update multicasting flag
mcastActive = true;
}
return true;
}
2007-09-12 19:28:59 +02:00
2007-09-13 18:58:22 +02:00
bool cIptvStreamer::DropMulticast(void)
{
debug("cIptvStreamer::DropMulticast()\n");
// Check that stream address is valid
if (mcastActive && !isempty(streamAddr)) {
// Ensure that socket is valid
OpenSocket(streamPort);
// Drop the multicast group
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(streamAddr);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq,
sizeof(mreq));
if (err < 0) {
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Update multicasting flag
mcastActive = false;
2007-09-12 19:28:59 +02:00
}
2007-09-13 18:58:22 +02:00
return true;
}
2007-09-12 19:28:59 +02:00
2007-09-13 18:58:22 +02:00
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();
2007-09-12 19:28:59 +02:00
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-13 18:58:22 +02:00
if (!isempty(address)) {
// Drop the multicast group
DropMulticast();
// Update stream address and port
streamAddr = strcpyrealloc(streamAddr, address);
streamPort = port;
// Join a new multicast group
JoinMulticast();
}
2007-09-12 19:28:59 +02:00
return true;
}