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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
2007-10-21 15:31:21 +02:00
|
|
|
#include "socket.h"
|
2007-09-14 17:44:25 +02:00
|
|
|
|
|
|
|
cIptvProtocolUdp::cIptvProtocolUdp()
|
|
|
|
{
|
2007-09-28 18:44:59 +02:00
|
|
|
debug("cIptvProtocolUdp::cIptvProtocolUdp()\n");
|
2007-09-14 17:44:25 +02:00
|
|
|
streamAddr = strdup("");
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvProtocolUdp::~cIptvProtocolUdp()
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolUdp::~cIptvProtocolUdp()\n");
|
|
|
|
// Drop the multicast group and close the socket
|
2008-01-05 00:36:37 +01:00
|
|
|
cIptvProtocolUdp::Close();
|
2007-09-14 17:44:25 +02:00
|
|
|
// Free allocated memory
|
|
|
|
free(streamAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2010-07-02 17:28:49 +02:00
|
|
|
OpenSocket(inet_addr(streamAddr), socketPort);
|
2007-09-14 17:44:25 +02:00
|
|
|
// 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));
|
2007-10-21 01:25:14 +02:00
|
|
|
ERROR_IF_RET(err < 0, "setsockopt()", return false);
|
2007-09-14 17:44:25 +02:00
|
|
|
// 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
|
2010-07-02 17:28:49 +02:00
|
|
|
OpenSocket(inet_addr(streamAddr), socketPort);
|
2007-09-14 17:44:25 +02:00
|
|
|
// 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));
|
2007-10-21 01:25:14 +02:00
|
|
|
ERROR_IF_RET(err < 0, "setsockopt()", return false);
|
2007-09-14 17:44:25 +02:00
|
|
|
// Update multicasting flag
|
2007-09-26 21:49:35 +02:00
|
|
|
isActive = false;
|
2007-09-14 17:44:25 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-26 15:04:12 +01:00
|
|
|
int cIptvProtocolUdp::Read(unsigned char* BufferAddr, unsigned int BufferLen)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
2009-02-26 15:04:12 +01:00
|
|
|
return cIptvUdpSocket::Read(BufferAddr, BufferLen);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2007-10-21 15:31:21 +02:00
|
|
|
socketPort = 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()");
|
2007-10-21 15:31:21 +02:00
|
|
|
return cString::sprintf("udp://%s:%d", streamAddr, socketPort);
|
2007-10-08 00:54:09 +02:00
|
|
|
}
|