/* 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_AVIWRITENODE_HPP
#define NMM_AVIWRITENODE_HPP NMM_AVIWRITENODE_HPP

#include "nmm/nmm_prefix.hpp"

#include <stdio.h>

#include "nmm/multimedia/GenericSinkNode.hpp"
#include "nmm/plugins/file/interfaces/ISinkFileHandlerImpl.hpp"
#include "nmm/plugins/interfaces/ISetBufferLabelImpl.hpp"
#include "nmm/plugins/interfaces/IKeyframeImpl.hpp"
#include "nmm/plugins/file/avi/IFrameCountImpl.hpp"

// do forward declaration of needed structs from libmpdemux/muxer.h
struct muxer_t;
struct muxer_stream_t;

namespace NMM {

  class AVIWriteNode :
    public GenericSinkNode,
    public ISinkFileHandlerImpl,
    public ISetBufferLabelImpl,
    public ITrackImpl,
    public IKeyframeImpl,
    public IFrameCountImpl
  {

  private:
    muxer_t* avimux;
    muxer_stream_t* mux_a;
    muxer_stream_t* mux_v;
    int out_file_format;
    FILE* avifile;
    long int bytes_written;
    long int frames_written;
    string filename;
    string __file_extension;
    string __working_directory;
    bool closed;
    bool is_key_frame;
    unsigned int labelnum_video;
    unsigned int labelnum_audio;
    ThreadMutex avw_mutex;
    
    bool first_audio;
    bool first_video;
    Timestamp first_audio_ts;
    Timestamp first_video_ts;
    
  protected:
    // Node interface
    virtual Result doInit();
    virtual Result doDeinit();
    virtual Result doActivate();
    virtual Result doDeactivate();
    
    // ITrack interface
    virtual Result started(const Interval& overall_duration);
    virtual Result startTrack(const string& filename);
    virtual Result endTrack();
    virtual Result finished();
    
    // IKeyframe interface
    virtual Result setKeyframe();
    
    // ISetBufferLabel interface
    virtual Result setBufferLabel(const string& stream, const nmm_int32& label);
    
    // ISinkFileHandler interface
    virtual void setWorkingDirectory(const string& directory);
    virtual string getWorkingDirectory() ;
    virtual void setFileExtension(const string& extension) ;
    virtual string getFileExtension();
    
    // IFileHandler interface
    virtual void setFilename(const string& filename);

    // IFrameCount interface
    virtual nmm_int32 getFrameCount() const;

    virtual Message* processBuffer(Buffer *in_buffer); 
    
  public: 
    AVIWriteNode(const char *name = "AVIWriteNode", 
		 const StreamQueue::Mode queue_mode = StreamQueue::MODE_SUSPEND, 
		 const int queue_size = 1);
    ~AVIWriteNode();
  };
  
} // class AVIWriteNode

#include "nmm/nmm_suffix.hpp"

#endif // NMM_AVIWRITENODE_HPP
