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

120 lines
3.1 KiB
C
Raw Permalink Normal View History

2007-09-14 17:44:25 +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.
*
* $Id: protocoludp.c,v 1.21 2007/10/21 19:32:15 ajhseppa 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>
#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"
#include "socket.h"
2007-09-14 17:44:25 +02:00
cIptvProtocolUdp::cIptvProtocolUdp()
{
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
Close();
// Free allocated memory
free(streamAddr);
}
bool cIptvProtocolUdp::JoinMulticast(void)
{
debug("cIptvProtocolUdp::JoinMulticast()\n");
// Check that stream address is valid
if (!isActive && !isempty(streamAddr)) {
2007-09-14 17:44:25 +02:00
// Ensure that socket is valid
OpenSocket(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));
ERROR_IF_RET(err < 0, "setsockopt()", return false);
2007-09-14 17:44:25 +02:00
// Update multicasting flag
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
if (isActive && !isempty(streamAddr)) {
2007-09-14 17:44:25 +02:00
// Ensure that socket is valid
OpenSocket(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));
ERROR_IF_RET(err < 0, "setsockopt()", return false);
2007-09-14 17:44:25 +02:00
// Update multicasting flag
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;
}
int cIptvProtocolUdp::Read(unsigned char* *BufferAddr)
{
return cIptvUdpSocket::Read(BufferAddr);
}
bool cIptvProtocolUdp::Set(const char* Location, const int Parameter, const int Index)
2007-09-14 17:44:25 +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);
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()");
return cString::sprintf("udp://%s:%d", streamAddr, socketPort);
2007-10-08 00:54:09 +02:00
}