2007-09-14 17:44:25 +02:00
|
|
|
/*
|
2007-09-26 21:49:35 +02:00
|
|
|
* protocoludp.c: IPTV plugin for the Video Disk Recorder
|
2007-09-14 17:44:25 +02:00
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
2007-10-20 00:18:55 +02:00
|
|
|
* $Id: protocoludp.c,v 1.13 2007/10/19 22:18:55 rahrenbe Exp $
|
2007-09-14 17:44:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-09-15 22:33:15 +02:00
|
|
|
#include <vdr/device.h>
|
|
|
|
|
2007-09-14 17:44:25 +02:00
|
|
|
#include "common.h"
|
2007-09-16 15:38:20 +02:00
|
|
|
#include "config.h"
|
2007-09-14 17:44:25 +02:00
|
|
|
#include "protocoludp.h"
|
|
|
|
|
|
|
|
cIptvProtocolUdp::cIptvProtocolUdp()
|
|
|
|
: streamPort(1234),
|
|
|
|
socketDesc(-1),
|
2007-09-28 18:44:59 +02:00
|
|
|
readBufferLen(TS_SIZE * IptvConfig.GetReadBufferTsCount()),
|
2007-09-26 21:49:35 +02:00
|
|
|
isActive(false)
|
2007-09-14 17:44:25 +02:00
|
|
|
{
|
2007-09-28 18:44:59 +02:00
|
|
|
debug("cIptvProtocolUdp::cIptvProtocolUdp()\n");
|
2007-09-14 17:44:25 +02:00
|
|
|
streamAddr = strdup("");
|
2007-09-15 22:33:15 +02:00
|
|
|
// Allocate receive buffer
|
2007-09-28 18:44:59 +02:00
|
|
|
readBuffer = MALLOC(unsigned char, readBufferLen);
|
2007-09-15 22:33:15 +02:00
|
|
|
if (!readBuffer)
|
|
|
|
error("ERROR: MALLOC() failed in ProtocolUdp()");
|
2007-09-14 17:44:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvProtocolUdp::~cIptvProtocolUdp()
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolUdp::~cIptvProtocolUdp()\n");
|
|
|
|
// Drop the multicast group and close the socket
|
|
|
|
Close();
|
|
|
|
// Free allocated memory
|
|
|
|
free(streamAddr);
|
2007-09-15 22:33:15 +02:00
|
|
|
free(readBuffer);
|
2007-09-14 17:44:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolUdp::OpenSocket(const int Port)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolUdp::OpenSocket()\n");
|
|
|
|
// If socket is there already and it is bound to a different port, it must
|
|
|
|
// be closed first
|
|
|
|
if (Port != streamPort) {
|
|
|
|
debug("cIptvProtocolUdp::OpenSocket(): Socket tear-down\n");
|
|
|
|
CloseSocket();
|
|
|
|
}
|
|
|
|
// Bind to the socket if it is not active already
|
|
|
|
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)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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)));
|
|
|
|
CloseSocket();
|
|
|
|
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)));
|
|
|
|
CloseSocket();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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)));
|
|
|
|
CloseSocket();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Update stream port
|
|
|
|
streamPort = Port;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvProtocolUdp::CloseSocket(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolUdp::CloseSocket()\n");
|
|
|
|
// Check if socket exists
|
|
|
|
if (socketDesc >= 0) {
|
|
|
|
close(socketDesc);
|
|
|
|
socketDesc = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolUdp::JoinMulticast(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolUdp::JoinMulticast()\n");
|
|
|
|
// Check that stream address is valid
|
2007-09-26 21:49:35 +02:00
|
|
|
if (!isActive && !isempty(streamAddr)) {
|
2007-09-14 17:44:25 +02:00
|
|
|
// Ensure that socket is valid
|
|
|
|
OpenSocket(streamPort);
|
|
|
|
// Join a new 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_ADD_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
|
2007-09-26 21:49:35 +02:00
|
|
|
isActive = true;
|
2007-09-14 17:44:25 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolUdp::DropMulticast(void)
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolUdp::DropMulticast()\n");
|
|
|
|
// Check that stream address is valid
|
2007-09-26 21:49:35 +02:00
|
|
|
if (isActive && !isempty(streamAddr)) {
|
2007-09-14 17:44:25 +02:00
|
|
|
// 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
|
2007-09-26 21:49:35 +02:00
|
|
|
isActive = false;
|
2007-09-14 17:44:25 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-15 23:27:00 +02:00
|
|
|
int cIptvProtocolUdp::Read(unsigned char* *BufferAddr)
|
2007-09-14 17:44:25 +02:00
|
|
|
{
|
|
|
|
//debug("cIptvProtocolUdp::Read()\n");
|
|
|
|
socklen_t addrlen = sizeof(sockAddr);
|
2007-09-16 01:58:23 +02:00
|
|
|
// Set argument point to read buffer
|
|
|
|
*BufferAddr = readBuffer;
|
2007-09-14 17:44:25 +02:00
|
|
|
// Wait for data
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 500000;
|
|
|
|
// Use select
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(socketDesc, &rfds);
|
|
|
|
int retval = select(socketDesc + 1, &rfds, NULL, NULL, &tv);
|
|
|
|
// Check if error
|
|
|
|
if (retval < 0) {
|
|
|
|
char tmp[64];
|
|
|
|
error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp)));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Check if data available
|
|
|
|
else if (retval) {
|
|
|
|
// Read data from socket
|
2007-09-28 18:44:59 +02:00
|
|
|
int len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *)&sockAddr, &addrlen);
|
|
|
|
if ((len > 0) && (readBuffer[0] == 0x47)) {
|
|
|
|
// Set argument point to read buffer
|
|
|
|
*BufferAddr = &readBuffer[0];
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
else if (len > 3) {
|
|
|
|
// http://www.networksorcery.com/enp/rfc/rfc2250.txt
|
2007-09-28 20:48:07 +02:00
|
|
|
// version
|
2007-09-28 18:44:59 +02:00
|
|
|
unsigned int v = (readBuffer[0] >> 6) & 0x03;
|
2007-09-28 20:48:07 +02:00
|
|
|
// extension bit
|
2007-09-28 18:44:59 +02:00
|
|
|
unsigned int x = (readBuffer[0] >> 4) & 0x01;
|
2007-09-28 20:48:07 +02:00
|
|
|
// cscr count
|
2007-09-28 18:44:59 +02:00
|
|
|
unsigned int cc = readBuffer[0] & 0x0F;
|
2007-09-28 20:48:07 +02:00
|
|
|
// payload type
|
2007-09-28 18:44:59 +02:00
|
|
|
unsigned int pt = readBuffer[1] & 0x7F;
|
2007-09-28 20:48:07 +02:00
|
|
|
// header lenght
|
2007-09-28 18:44:59 +02:00
|
|
|
unsigned int headerlen = (3 + cc) * sizeof(uint32_t);
|
2007-09-28 20:48:07 +02:00
|
|
|
// check if extension
|
|
|
|
if (x) {
|
|
|
|
// extension header length
|
|
|
|
unsigned int ehl = (((readBuffer[headerlen + 2] & 0xFF) << 8) | (readBuffer[headerlen + 3] & 0xFF));
|
|
|
|
// update header length
|
|
|
|
headerlen += (ehl + 1) * sizeof(uint32_t);
|
|
|
|
}
|
|
|
|
// Check that rtp is version 2, payload type is MPEG2 TS
|
|
|
|
// and payload contains multiple of TS packet data
|
2007-09-28 18:44:59 +02:00
|
|
|
if ((v == 2) && (pt == 33) && (((len - headerlen) % TS_SIZE) == 0)) {
|
|
|
|
// Set argument point to payload in read buffer
|
|
|
|
*BufferAddr = &readBuffer[headerlen];
|
|
|
|
return (len - headerlen);
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 17:44:25 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolUdp::Open(void)
|
|
|
|
{
|
2007-10-19 23:36:27 +02:00
|
|
|
debug("cIptvProtocolUdp::Open()\n");
|
2007-09-14 17:44:25 +02:00
|
|
|
// Join a new multicast group
|
|
|
|
JoinMulticast();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolUdp::Close(void)
|
|
|
|
{
|
2007-10-19 23:36:27 +02:00
|
|
|
debug("cIptvProtocolUdp::Close()\n");
|
2007-09-14 17:44:25 +02:00
|
|
|
// Drop the multicast group
|
|
|
|
DropMulticast();
|
|
|
|
// Close the socket
|
|
|
|
CloseSocket();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-20 00:18:55 +02:00
|
|
|
bool cIptvProtocolUdp::Set(const char* Location, const int Parameter, const int Index)
|
2007-09-14 17:44:25 +02:00
|
|
|
{
|
2007-10-20 00:18:55 +02:00
|
|
|
debug("cIptvProtocolUdp::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index);
|
2007-10-19 23:36:27 +02:00
|
|
|
if (!isempty(Location)) {
|
2007-09-14 17:44:25 +02:00
|
|
|
// Drop the multicast group
|
|
|
|
DropMulticast();
|
|
|
|
// Update stream address and port
|
2007-10-19 23:36:27 +02:00
|
|
|
streamAddr = strcpyrealloc(streamAddr, Location);
|
|
|
|
streamPort = Parameter;
|
2007-09-14 17:44:25 +02:00
|
|
|
// Join a new multicast group
|
|
|
|
JoinMulticast();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-10-08 00:54:09 +02:00
|
|
|
|
|
|
|
cString cIptvProtocolUdp::GetInformation(void)
|
|
|
|
{
|
|
|
|
//debug("cIptvProtocolUdp::GetInformation()");
|
|
|
|
return cString::sprintf("udp://%s:%d", streamAddr, streamPort);
|
|
|
|
}
|