/*
 * dvbapi.h: Interface to the DVB driver
 *
 * Copyright (C) 2000 Klaus Schmidinger
 *
 * Modified by Motama GmbH,
 * Copyright of modifications (C) 2006-2007 Motama GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 *
 * The author can be reached at kls@cadsoft.de
 *
 * The project's page is at http://www.cadsoft.de/people/kls/vdr
 *
 */

#ifndef __DVBAPI_H
#define __DVBAPI_H
#include <stdlib.h> // FIXME: this is apparently necessary for the ost/... header files
                    // FIXME: shouldn't every header file include ALL the other header
                    // FIXME: files it depends on? The sequence in which header files
                    // FIXME: are included here should not matter - and it should NOT
                    // FIXME: be necessary to include <stdlib.h> here!
#include <linux/videodev.h>
#include <ost/dmx.h>
//#include <ost/sec.h>
#include <ost/frontend.h>
#include <ost/video.h>
#include <ost/audio.h>
#include <ost/osd.h>
#include <stdio.h>
//#include "dvbosd.h"
#include "eit.h"
#include "thread.h"

#include "../DVBReadNode.hpp"

#define EBUFFEROVERFLOW 769

namespace VDR {
/*
// Overlay facilities
#define MAXCLIPRECTS 100
typedef struct CRect {
  signed short x, y, width, height;
  };

#define MenuLines   13 // XXX originally 15, but since driver version 2001-05-25 there is less OSD memory :-(
#define MenuColumns 40
*/

typedef int dvb_pid_t;
const char *IndexToHMSF(int Index, bool WithFrame = false);
      // Converts the given index to a string, optionally containing the frame number.
int HMSFToIndex(const char *HMSF);
      // Converts the given string (format: "hh:mm:ss.ff") to an index.

class cChannel;

class cRecordBuffer;
class cReplayBuffer;
class cTransferBuffer;
class cCuttingBuffer;

class cVideoCutter {
private:
  static cCuttingBuffer *cuttingBuffer;
public:
  static bool Start(const char *FileName);
  static void Stop(void);
  static bool Active(void);
  };

class cDvbApi {
  friend class cRecordBuffer;
  friend class cReplayBuffer;
  friend class cTransferBuffer;
private:
  int videoDev;
  int fd_osd, fd_qpskfe, fd_qamfe, fd_sec, fd_dvr, fd_audio, fd_video, fd_demuxa, fd_demuxv, fd_demuxt, fd_frontend;
  int vPid, aPid;
  bool SetPid(int fd, dmx_pes_type_t PesType, int Pid, dmx_output_t Output);
  bool SetVpid(int Vpid, dmx_output_t Output) { return SetPid(fd_demuxv, DMX_PES_VIDEO,    Vpid, Output); }
  bool SetApid(int Apid, dmx_output_t Output) { return SetPid(fd_demuxa, DMX_PES_AUDIO,    Apid, Output); }
  bool SetTpid(int Tpid, dmx_output_t Output) { return SetPid(fd_demuxt, DMX_PES_TELETEXT, Tpid, Output); }
  bool SetPids(bool ForRecording);
  cDvbApi(int n);
public:
  ~cDvbApi();

#define MAXDVBAPI 4
  static int NumDvbApis;
private:
  static cDvbApi *dvbApi[MAXDVBAPI];
  static int useDvbApi;
public:
  static cDvbApi *PrimaryDvbApi;
  static void SetUseDvbApi(int n);
         // Sets the 'useDvbApi' flag of the given DVB device.
         // If this function is not called before Init(), all DVB devices
         // will be used.
  static bool SetPrimaryDvbApi(int n);
         // Sets the primary DVB device to 'n' (which must be in the range
         // 1...NumDvbApis) and returns true if this was possible.
  static cDvbApi *GetDvbApi(int Ca, int Priority);
         // Selects a free DVB device, starting with the highest device number
         // (but avoiding, if possible, the PrimaryDvbApi).
         // If Ca is not 0, the device with the given number will be returned.
         // If all DVB devices are currently recording, the one recording the
         // lowest priority timer (if any) that is lower than the given Priority
         // will be returned.
         // The caller must check whether the returned DVB device is actually
         // recording and stop recording if necessary.
  int Index(void);
         // Returns the index of this DvbApi.
  static bool Probe(const char *FileName);
         // Probes for existing DVB devices.
  static bool Init(void);
         // Initializes the DVB API.
         // Must be called before accessing any DVB functions.
  static void Cleanup(void);
         // Closes down all DVB devices.
         // Must be called at the end of the program.
  static int muteAudio(bool);


  // EIT facilities

private:
  cSIProcessor *siProcessor;
public:
  const cSchedules *Schedules(cThreadLock *ThreadLock) const;
         // Caller must provide a cThreadLock which has to survive the entire
         // time the returned cSchedules is accessed. Once the cSchedules is no
         // longer used, the cThreadLock must be destroyed.
  void SetUseTSTime(bool On) { if (siProcessor) siProcessor->SetUseTSTime(On); }

  // Image Grab facilities

  //bool GrabImage(const char *FileName, bool Jpeg = true, int Quality = -1, int SizeX = -1, int SizeY = -1);

  // Channel facilities

private:
  int currentChannel;
public:
  bool SetChannel(int ChannelNumber, int FrequencyMHz, char Polarization, int Diseqc, int Srate, int Vpid, int Apid, int Tpid, int Ca, int Pnr);
  static int CurrentChannel(void) { return PrimaryDvbApi ? PrimaryDvbApi->currentChannel : 0; }
  int Channel(void) { return currentChannel; }

  // Transfer facilities

private:
  cTransferBuffer *transferBuffer;
  cDvbApi *transferringFromDvbApi;
public:
  bool Transferring(void);
       // Returns true if we are currently transferring video data.
private:
  cDvbApi *StartTransfer(int TransferToVideoDev);
       // Starts transferring video data from this DVB device to TransferToVideoDev.
  void StopTransfer(void);
       // Stops transferring video data (in case a transfer is currently active).

  // Record/Replay facilities

private:
  cRecordBuffer *recordBuffer;
  cReplayBuffer *replayBuffer;
  int ca;
  int priority;
  int  Ca(void) { return ca; }
       // Returns the ca of the current recording session (0..MAXDVBAPI).
  int  Priority(void) { return priority; }
       // Returns the priority of the current recording session (0..MAXPRIORITY),
       // or -1 if no recording is currently active.
  int  SetModeRecord(void);
       // Initiates recording mode and returns the file handle to read from.
  void SetModeReplay(void);
  void SetModeNormal(bool FromRecording);
public:
  int  SecondsToFrames(int Seconds);
       // Returns the number of frames corresponding to the given number of seconds.
  bool Recording(void);
       // Returns true if we are currently recording.

  bool collectingBuffers(void);  //> Returns true if we are currently collecting NMM-buffers

  bool Replaying(void);
       // Returns true if we are currently replaying.

  bool StartRecord(int Ca, int Priority); //> Start collecting NMM-Buffers without saving a file to disk

  bool StartRecord(const char *FileName, int Ca, int Priority);
       // Starts recording the current channel into the given file, with
       // the given ca and priority.
       // In order to be able to record longer movies,
       // a numerical suffix will be appended to the file name. The inital
       // value of that suffix will be larger than any existing file under
       // the given name, thus allowing an interrupted recording to continue
       // gracefully.
       // Returns true if recording was started successfully.
       // If there is already a recording session active, false will be
       // returned.
  void StopRecord(void);
       // Stops the current recording session (if any).
  bool StartReplay(const char *FileName);
       // Starts replaying the given file.
       // If there is already a replay session active, it will be stopped
       // and the new file will be played back.
  void StopReplay(void);
       // Stops the current replay session (if any).
  void Pause(void);
       // Pauses the current replay session, or resumes a paused session.
  void Play(void);
       // Resumes normal replay mode.
  void Forward(void);
       // Runs the current replay session forward at a higher speed.
  void Backward(void);
       // Runs the current replay session backwards at a higher speed.
  void SkipSeconds(int Seconds);
       // Skips the given number of seconds in the current replay session.
       // The sign of 'Seconds' determines the direction in which to skip.
       // Use a very large negative value to go all the way back to the
       // beginning of the recording.
  int  SkipFrames(int Frames);
       // Returns the new index into the current replay session after skipping
       // the given number of frames (no actual repositioning is done!).
       // The sign of 'Frames' determines the direction in which to skip.
  bool GetIndex(int &Current, int &Total, bool SnapToIFrame = false);
       // Returns the current and total frame index, optionally snapped to the
       // nearest I-frame.
  void Goto(int Index, bool Still = false);
       // Positions to the given index and displays that frame as a still picture
       // if Still is true.
//----------------------------------NMM related stuff

private:
  NMM::DVBReadNode* parent_node;
  bool node_is_set;
  bool collecting_buffers; //! True if MPEG2-PES Buffers should be sent

public:
  void setParentNode(NMM::DVBReadNode* x) {
    parent_node = x;
    node_is_set = true;
  }
  void enableBufferCollecting() {
    collecting_buffers = true;
  }
  void disableBufferCollecting() {
    collecting_buffers = false;
  }
};


class cEITScanner {
private:
  enum { ActivityTimeout = 60,
         ScanTimeout = 20
       };
  time_t lastScan, lastActivity;
  int currentChannel, lastChannel;
  int numTransponders, *transponders;
  bool TransponderScanned(cChannel *Channel);
public:
  cEITScanner(void);
  ~cEITScanner();
  bool Active(void) { return currentChannel; }
  void Activity(void);
  void Process(void);
  };

}
#endif //__DVBAPI_H
