/*
 * transfer.h: Transfer mode
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: transfer.h 1.11 2006/01/29 17:24:43 kls Exp $
 */

#ifndef __TRANSFER_H
#define __TRANSFER_H

#include "player.h"
#include "receiver.h"
#include "remux.h"
#include "ringbuffer.h"
#include "thread.h"
#include "recorder.h"

class cTransfer : public cReceiver, public cPlayer, public cThread {
private:
  cRingBufferLinear *ringBuffer;
  cRemux *remux;
protected:
  virtual void Activate(bool On);
  virtual void Receive(uchar *Data, int Length);
  virtual void Action(void);
public:
  cTransfer(int VPid, const int *APids, const int *DPids, const int *SPids);
  virtual ~cTransfer();
  };

class cReplayControl;

#define MAXLIVERECORDERS 16

class cLiveRecorder {
friend class cLiveRecorderControl;
private:
  struct PIDs
  {
    int VPid;
    int APids[MAXAPIDS+1];
    int DPids[MAXDPIDS+1];
    int SPids[MAXSPIDS+1];
  } Pids;
  static cLiveRecorder *ActiveLiveRecorder;
  time_t start;
  bool New;
  bool Rew;
  char text[256];
  tChannelID channelID; 
  cReplayControl *replay;
  cDevice *receiverDevice;
  char FileName[256];
  cRecorder* Recorder;
  cLiveRecorder(cDevice *ReceiverDevice, const cChannel *Channel);
  bool PIDsChanged(const cChannel *Channel);
  void Text(char *Name);
public:
  ~cLiveRecorder();
  void rew(bool On = true);
  bool IsRew() { return Rew; }
  const char* GetFileName(void) {return FileName;}
  bool IsAlreadyRecording(tChannelID ChannelID);
  cReplayControl* GetReplayControl() { return replay; }
  void Resume();
  void ChangePriority(int prio) { Recorder->priority = prio; }
};

class cLiveRecorderControl : public cThread {
friend class cLiveRecorder;
private:
  bool wantadd,add;
  static cLiveRecorderControl *LiveThread; 
  static cLiveRecorder *liveRecorder[MAXLIVERECORDERS];
  cDevice *ReceiverDevice;
  const cChannel *Channel;
  cLiveRecorderControl();
  void ClearBufferDirectory(void);
  virtual void Action(void);
public:
  static void Add(cDevice *ReceiverDevice, const cChannel *Channel);
  static void Shutdown();
  static cLiveRecorder* GetLiveRecorder(const cChannel *Channel = NULL);
  static void UpdateTimes(void);
  static bool HasProgramme(void) { return (LiveThread && LiveThread->add) || cControl::Control() && cControl::Control()->IsLiveRecording(); }
  static bool InLiveBuffer(cTimer *timer);
  static int MatchesEvent(const cEvent *event);
};  

class cTransferControl : public cControl {
friend class cLiveRecorderControl;
friend class cReplayControl;
private:
  cTransfer *transfer;
  static cDevice *receiverDevice;
public:
  cTransferControl(cDevice *ReceiverDevice, int VPid, const int *APids, const int *DPids, const int *SPids);
  ~cTransferControl();
  virtual void Hide(void) {}
  static cDevice *ReceiverDevice(void) { return receiverDevice; }
  };

#endif //__TRANSFER_H
