/*
 * mcast.c: A Multicast plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <vdr/plugin.h>
#include <vdr/status.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "mcastreceiver.h"

static const char *VERSION        = "0.0.1";
static const char *DESCRIPTION    = "mcast plugin";
static const char *MAINMENUENTRY  = NULL;

// ---

class cStatusMon : public cStatus {
private:
  cMCastReceiver *m_Receiver;
  int m_ChannelNumber;
  in_addr_t mcastip;
  unsigned int mcastport;
  in_addr_t mcastintf;
  
protected:
  virtual void ChannelSwitch(const cDevice *Device, int ChannelNumber);
public:
	cStatusMon(const char *CfgDir);
	~cStatusMon(void);
  };

cStatusMon::cStatusMon(const char *CfgDir)
:cStatus()
{
  Dprintf("%s()\n", __PRETTY_FUNCTION__);
  m_Receiver = NULL;
  m_ChannelNumber = -1;
  
  mcastip = inet_addr("224.0.0.1");
  mcastintf = inet_addr("192.168.0.1");
  mcastport = 1234;

  cString cname=AddDirectory(CfgDir,"mcast");
  FILE * conf;
  int retval;
  char tmp[1024];
  
  conf = fopen(cname, "r");
  if (conf != NULL)
  {
	retval = fscanf(conf, "%s\n", tmp);
	printf("==>%s<==\n", tmp);
	mcastip = inet_addr(tmp);
	retval = fscanf(conf, "%d\n", &mcastport);
	printf("==>%d<==\n", mcastport);
	retval = fscanf(conf, "%s\n", tmp);
	printf("==>%s<==\n", tmp);
	mcastintf = inet_addr(tmp);
	fclose(conf);
  }
}

cStatusMon::~cStatusMon(void)
{
  Dprintf("%s()\n", __PRETTY_FUNCTION__);
  if (m_Receiver)
     delete m_Receiver;
}
		
void cStatusMon::ChannelSwitch(const cDevice *Device, int ChannelNumber)
{
  Dprintf("%s()\n", __PRETTY_FUNCTION__);

  if (!(ChannelNumber != 0 && ChannelNumber != m_ChannelNumber))
     return;

	m_ChannelNumber = ChannelNumber;
  
     if (m_Receiver)
        delete m_Receiver;

	cChannel *channel = Channels.GetByNumber(cDevice::CurrentChannel());
	m_Receiver = new cMCastReceiver(channel->Ca(), channel->Vpid(), channel->Apids(), channel->Dpids(), mcastip, mcastport, mcastintf);
	cDevice::ActualDevice()->AttachReceiver(m_Receiver);
}
// ---

class cPluginMCast : public cPlugin {
private:
  // Add any member variables or functions you may need here.
  cStatusMon *statusMon;
  
public:
  cPluginMCast(void);
  virtual ~cPluginMCast();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return DESCRIPTION; }
  virtual const char *CommandLineHelp(void);
  virtual bool ProcessArgs(int argc, char *argv[]);
  virtual bool Start(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  };

cPluginMCast::cPluginMCast(void)
{
  // Initialize any member variables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
  statusMon = NULL;
}

cPluginMCast::~cPluginMCast()
{
  // Clean up after yourself!
  delete statusMon;
}

const char *cPluginMCast::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return NULL;
}

bool cPluginMCast::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  return true;
}

bool cPluginMCast::Start(void)
{
  // Start any background activities the plugin shall perform.

  statusMon = new cStatusMon(ConfigDirectory());
  return true;
}

void cPluginMCast::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

cOsdObject *cPluginMCast::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  return NULL;
}

cMenuSetupPage *cPluginMCast::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return NULL;
}

bool cPluginMCast::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  return false;
}

VDRPLUGINCREATOR(cPluginMCast); // Don't touch this!
