/*
 * analogradio - A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 */

#ifndef AR__DEVICE_H
#define AR__DEVICE_H

#include <vdr/device.h>

enum e_audio_mode {
  AUDIO_MODE_OSS  = 0,
  AUDIO_MODE_RAW  = 1,
  AUDIO_MODE_ALSA = 2,
};

struct analogRadioSetup {
  int initVolume;
  int muteTuner;
};

//-----cAnalogRadioDevice------------------------------------------------------------

class cAnalogRadioDevice : public cDevice {
  private:
    int source;
    int apid;
    int frequency;
    int freqfact;
    bool use_stereo;    //Stereo (radio device)
    bool v4l2;          //V4L2 compatible device

    //Only for ADR mode
    bool use_adr;       //ADR mode
    bool adr_h;         //Horizontal polarisation

    //Audio
    int audio_rate;     //Sampling rate
    int audio_stereo;   //Stereo (audio device)
    int audio_encoding; //8/16 Bit, signed/unsigned, little/big endian

    //Devices
    int fd;             //radio
    int audio_fd;       //audio

    //Command line options
    char * after;       //command after using this plugin
    char * before;      //command before using this plugin
    char * device;      //radio path
    char * audio_device; //audio path
    e_audio_mode audio_mode; //ALSA, OSS, RAW?

    //Plugin Setup
    analogRadioSetup * settings;

    uchar buf[TS_SIZE]; //Buffer for one TS-Packet
    int byte_count;     //Bytes transferred so far (per PES-Packet)
    int ts_count;       //TS-Packets transferred (for TS-Header)
    float timestamp;    //timestamp

    bool setModeOSS();  //Set up the capturing device with OSS
    bool setModeRAW();  //Set up the capturing device for RAW input (48 kHz, 16 Bit LE, Stereo)
    bool setModeALSA(); //Set up the capturing device with ALSA
  public:
    cAnalogRadioDevice(char * sDevice, char * sAudioDevice, e_audio_mode eAudioMode, char * sAfter, char * sBefore, analogRadioSetup * arsSettings);
    virtual ~cAnalogRadioDevice();
    virtual bool ProvidesSource(int Source) const;
    virtual bool ProvidesChannel(const cChannel *Channel, int Priority = -1, bool *NeedsDetachReceivers = NULL) const;
    virtual bool SetChannelDevice(const cChannel *Channel, bool LiveView);

    virtual bool SetPid(cPidHandle *Handle, int Type, bool On) {return true;};
    virtual bool OpenDvr(void);
    virtual void CloseDvr(void);
    virtual bool GetTSPacket(uchar *&Data);

    bool mute();
    bool unmute();
    bool setFrequency();
};

#endif // AR__DEVICE_H
