////////////////////////////////////////////////////////////////////////////////
// StreamRecorder.h
// Mario Chirinos
// 2014-02-12
//------------------------------------------------------------------------------
// Notes:
//	Streaming recorder with GStreamer
//
////////////////////////////////////////////////////////////////////////////////

#ifndef STREAMRECORDER_H
#define STREAMRECORDER_H

// your public header include
//------------------------------------------------------------------------------
#include <gst/gst.h>
#include <jmorecfg.h>
//------------------------------------------------------------------------------
#define STREAMRECORDER_SAMPLERATE 44100
#define READSIZE 1152 //For MPEG1, frame_size = 1152 samples/frame
#define STREAMRECORDER_BYTESPERSAMPLE 2
// the declaration of your class...
//------------------------------------------------------------------------------
class StreamRecorder
{
    private:

        unsigned int nBytes;
		unsigned int bufferSize;
		unsigned char* audioBuffer;
		unsigned char* audioBufferPosition;
     	int recordTime;
        bool isConnectionLost;
    
        char * pluginUri;
        //char* sourceName;
     	GstElement* streamSrc;
     	GstElement* audioConvert;
     	//GstElement* audioResample;
     	GstElement* filterCaps;
     	GstElement* queue0;
     	GstElement* queue1;
     	GstElement* filter;
     	GstElement* fakeSink;
     	//GstElement* audioSink;
     	GstElement* mainPipeline;
     	//GstElement* tempBin;
    
		int createMainPipeline();
		int connect(const char *uri);
		int disconnect();
    
        // callbacks
        static void srcNewPad_callback(GstElement *element, GstPad *pad, void *data);
        static int bus_callback(GstBus *bus, GstMessage *message, void *data);
        static int filter_handoff_callback(GstElement* filter, GstBuffer* buffer, void* user_data);
		
        // add data to buffer
        int addToBuffer(unsigned char* data, int length);
		int compressBuffer();
    
        // Save information when connection fails
		static void savePartialBuffer(void *user_data);
	    
        // Restart the pipeline
		static gboolean reconnectURIStream(void *data);
    
    public:
	    
	    StreamRecorder(const char* source, int time);
};
//------------------------------------------------------------------------------
#endif
