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

100 lines
2.6 KiB
C

/*
* protocoludp.c: IPTV plugin for the Video Disk Recorder
*
* 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>
#include <vdr/device.h>
#include "common.h"
#include "config.h"
#include "protocoludp.h"
#include "socket.h"
cIptvProtocolUdp::cIptvProtocolUdp()
: streamAddr(strdup("")),
sourceAddr(strdup(""))
{
debug("cIptvProtocolUdp::cIptvProtocolUdp()\n");
}
cIptvProtocolUdp::~cIptvProtocolUdp()
{
debug("cIptvProtocolUdp::~cIptvProtocolUdp()\n");
// Drop the multicast group and close the socket
cIptvProtocolUdp::Close();
// Free allocated memory
free(streamAddr);
free(sourceAddr);
}
bool cIptvProtocolUdp::Open(void)
{
debug("cIptvProtocolUdp::Open()\n");
if (!isempty(streamAddr)) {
// Join a new multicast group
OpenSocket(socketPort, isempty(sourceAddr) ? INADDR_ANY : inet_addr(sourceAddr));
JoinMulticast(inet_addr(streamAddr));
}
return true;
}
bool cIptvProtocolUdp::Close(void)
{
debug("cIptvProtocolUdp::Close()\n");
if (!isempty(streamAddr)) {
// Drop the multicast group
OpenSocket(socketPort, isempty(sourceAddr) ? INADDR_ANY : inet_addr(sourceAddr));
DropMulticast(inet_addr(streamAddr));
}
// Close the socket
CloseSocket();
return true;
}
int cIptvProtocolUdp::Read(unsigned char* BufferAddr, unsigned int BufferLen)
{
return cIptvUdpSocket::Read(BufferAddr, BufferLen);
}
bool cIptvProtocolUdp::Set(const char* Location, const int Parameter, const int Index)
{
debug("cIptvProtocolUdp::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index);
if (!isempty(Location)) {
// Drop the multicast group
if (!isempty(streamAddr)) {
OpenSocket(socketPort, isempty(sourceAddr) ? INADDR_ANY : inet_addr(sourceAddr));
DropMulticast(inet_addr(streamAddr));
}
// Update stream address and port
streamAddr = strcpyrealloc(streamAddr, Location);
char *p = strstr(streamAddr, ";");
if (p) {
sourceAddr = strcpyrealloc(sourceAddr, p + 1);
*p = 0;
}
else
sourceAddr = strcpyrealloc(sourceAddr, "");
socketPort = Parameter;
// Join a new multicast group
if (!isempty(streamAddr)) {
OpenSocket(socketPort, isempty(sourceAddr) ? INADDR_ANY : inet_addr(sourceAddr));
JoinMulticast(inet_addr(streamAddr));
}
}
return true;
}
cString cIptvProtocolUdp::GetInformation(void)
{
//debug("cIptvProtocolUdp::GetInformation()");
return cString::sprintf("udp://%s:%d", streamAddr, socketPort);
}