#include <ptlib.h>
#include <gst/gst.h>


class PSoundChannelGSTREAMER: public PSoundChannel
{
public:
    PSoundChannelGSTREAMER();
    void Construct();
    PSoundChannelGSTREAMER(const PString &device,
                           PSoundChannel::Directions dir,
                           unsigned numChannels,
                           unsigned sampleRate,
                           unsigned bitsPerSample);
    ~PSoundChannelGSTREAMER();
    static PStringArray GetDeviceNames(PSoundChannel::Directions);
    static PString GetDefaultDevice(PSoundChannel::Directions);
    BOOL Open(const PString & _device,
              Directions _dir,
              unsigned _numChannels,
              unsigned _sampleRate,
              unsigned _bitsPerSample);
    BOOL Setup();
    BOOL Close();
    BOOL Write(const void * buf, PINDEX len);
    BOOL Read(void * buf, PINDEX len);
    void OnWriteHandoff (GstElement *elt,
                         GstBuffer *buffer,
                         GstPad *pad);
    void OnReadHandoff (GstElement *elt,
                        GstBuffer *buffer,
                        GstPad *pad);
    BOOL SetFormat(unsigned numChannels,
                   unsigned sampleRate,
                   unsigned bitsPerSample);
    unsigned GetChannels() const;
    unsigned GetSampleRate() const;
    unsigned GetSampleSize() const;
    BOOL SetBuffers(PINDEX size, PINDEX count);
    BOOL GetBuffers(PINDEX & size, PINDEX & count);
    BOOL PlaySound(const PSound & sound, BOOL wait);
    BOOL PlayFile(const PFilePath & filename, BOOL wait);
    BOOL HasPlayCompleted();
    BOOL WaitForPlayCompletion();
    BOOL RecordSound(PSound & sound);
    BOOL RecordFile(const PFilePath & filename);
    BOOL StartRecording();
    BOOL IsRecordBufferFull();
    BOOL AreAllRecordBuffersFull();
    BOOL WaitForRecordBufferFull();
    BOOL WaitForAllRecordBuffersFull();
    BOOL Abort();
    BOOL SetVolume (unsigned);
    BOOL GetVolume (unsigned &);
    BOOL IsOpen() const;

private:

    PSoundChannel::Directions direction;
    PString device;
    unsigned mNumChannels;
    unsigned mSampleRate;
    unsigned mBitsPerSample;
    BOOL isInitialised;

    char *dataBuf;
    BOOL dataBufReady;

    PMutex bufferMutex;

    GstElement *pipeline;
};
