/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */


#ifndef _NMM_FFMPEGAUDIOENCODENODE_HPP_
#define _NMM_FFMPEGAUDIOENCODENODE_HPP_ _NMM_FFMPEGAUDIOENCODENODE_HPP_

#include "nmm/nmm_prefix.hpp"

#include <sys/time.h>

#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif

#ifndef UINT8
#define UINT8 uint8_t
#endif

#include "nmm/multimedia/GenericProcessorNode.hpp"
#include "nmm/multimedia/interfaces/ITrackImpl.hpp"
#include "nmm/plugins/interfaces/IEncodeImpl.hpp"

#include <ffmpeg/avcodec.h>
#include <ffmpeg/avformat.h>


namespace NMM {

  class FFMpegAudioEncodeNode :
    public GenericProcessorNode,
    public ITrackImpl,
    public IEncodeImpl
  {
  private:
    class FFMpegHandle;

    FFMpegHandle* ffmpeg;    ///< class containing all avcodec relevant stuff

    bool first_timestamp;    ///< check to see if we have timestamp 
    Timestamp timestamp;     ///< The timestamp to put in the next outgoing buffer.
    Timestamp in_buf_ts;
/*
    long int my_delay;        //! The duration of the next output buffer at audio sink
    long int my_delay_num;    //! The duration of the next output buffer at audio sink (numerator)
    long int my_delay_den;    //! The duration of the next output buffer at audio sink (denominator)
    long int num_samples;     //! number of samples in frame
    */

    //unsigned char* last_buffer; ///< pointer to temp buffer (2 pass)
    Buffer* out_buffer;      ///< pointer to outgoing buffer
    Buffer* work_buffer;     ///< Working buffer. It is filled up with incoming frames if not enough incoming data is  available
    UINT8* work_buf;
    UINT8* work_buf_ptr;
    UINT8* inbuf;
    UINT8* inbuf_ptr;	     ///< pointer to use for reading from input buffer
    int sample_rate;         ///< sample rate
    int channels;            ///< channels
    int bitrate;             /// bitrate
    int size; 		     ///< number of input bytes left to read
    int byte_per_sample;
    int frame_size;
    unsigned int input_bytes_needed;  ///< needed bytes of input to encode a frame
    int enc_buf_size;
    string compressor;
    
    ThreadMutex ffmpeg_mutex;

    typedef std::map<std::string, int> CodecMap;
    typedef CodecMap::iterator CodecIterator;
    typedef CodecMap::const_iterator CodecConstIterator;

    CodecMap codecs;       ///< FourCC codes of all available codecs, mapped to codec IDs.
    //std::string default_codec; ///< FourCC code of default codec

    void setWOPInternal();
  public:
    FFMpegAudioEncodeNode(const char* name = "FFMpegAudioEncodeNode",
		    StreamQueue::Mode qmode = StreamQueue::MODE_SUSPEND,
		    int qsize = StreamQueue::DEFAULT_SIZE);
    ~FFMpegAudioEncodeNode();

  protected:
    /* GenericNode */
    virtual Result doInit();
    virtual Result doInitOutput();
    virtual Result doActivate();
    virtual Result doDeactivate();
    virtual Result doFlush();
    virtual Message* processBuffer(Buffer*);

    /* ITrackImpl */
    virtual Result startTrack(const string&);
    virtual Result endTrack();
    virtual Result finished();
    
    /* IEncodeImpl */
    virtual void setQuality(const nmm_float&);
    virtual void setBitrate(const nmm_int32&);
    virtual nmm_float getQuality();
    virtual nmm_int32 getBitrate();
    virtual Result started(const NMM::Interval&) { return SUCCESS; }

        

  private:
    void detectAllCodecs() throw();
    void addCodec(const std::string& fourcc, int id);
    /**
     * struct FFMpegHandle: This is used to handle all ffmpeg relevant stuff
     */
   struct FFMpegHandle {
     AVCodec* ffAVCodec;
     AVCodecContext* ffavcc;
     FFMpegHandle();
   };

  };

}

#include "nmm/nmm_suffix.hpp"

#endif //_NMM_FFMPEGAUDIODECODENODE_HPP_

