/* 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_MPEGREADNODE_HPP
#define NMM_MPEGREADNODE_HPP NMM_MPEGREADNODE_HPP

#include "nmm/nmm_prefix.hpp"

#include "MPEGCheckFormat.hpp"

#include "nmm/multimedia/GenericSourceNode.hpp"

#include "nmm/multimedia/sync/StreamTimer.hpp"

#include "nmm/plugins/interfaces/IFileHandlerImpl.hpp"
#include "nmm/plugins/file/interfaces/ISeekableImpl.hpp"
#include "nmm/plugins/interfaces/IProgressImpl.hpp"
#include "nmm/plugins/interfaces/IBufferSizeImpl.hpp"
#include <fstream>

namespace NMM {

  class MPEGReadNode :
    public GenericSourceNode,
    public MPEGCheckFormat,
    public IProgressImpl,
    public IFileHandlerImpl,
    public ISeekableImpl,
    public IBufferSizeImpl
  {
    
  private:
    string           filename;
    bool             send_start;
    bool             send_progress;
    bool             send_format;
    mpeg_file_format file_format;

    // seeking & file position
    u_int64_t        filesize;
    u_int64_t        filepos;
    int64_t          seekto;

    // progress
    int              buffers_progress;
    int              buffers_before_progress;
    StreamTimer stream_timer;
    
    // need for CheckFormat
    int MPEG_Read_Buffer( unsigned char *buf, int size );
    void MPEG_Seek_Begin();
    
  protected:
    ifstream stream;
    int buffer_size;
    
    virtual Result doInit();
    virtual Result doInitOutput();
    virtual Result doDeinitOutput();
    
    virtual Message* processBuffer(Buffer *);

    // IFileHandler interface
    virtual void setFilename(const string& name);
    
    // ISeekable interface
    virtual nmm_bool hasPercentSeek() const;
    virtual void seekPercent(const Rational& percent);
    virtual void seekPercentTo(const Rational& percent);
    virtual nmm_bool hasBytesSeek() const;
    virtual void seekBytes(const nmm_int64& offset);
    virtual void seekBytesTo(const nmm_uint64& bytes);
    virtual nmm_bool hasTimeSeek() const;
    virtual void seekTime(const Interval& interval);
    virtual void seekTimeTo(const Time& time);
    
    // IProgress interface
    virtual void sendProgressInformation(const nmm_bool& in);
    virtual void setProgressInterval(const nmm_int32& buffers);

    // IBufferSize interface
    virtual void setBufferSize(const nmm_int32& size);
    virtual nmm_int32 getBufferSize();
        
  public:
    MPEGReadNode(const char *name = "MPEGReadNode",
		 StreamQueue::Mode = StreamQueue::MODE_SUSPEND,
		 int = StreamQueue::DEFAULT_SIZE);
    
    virtual ~MPEGReadNode();
  };

}

#include "nmm/nmm_suffix.hpp"

#endif //NMM_MPEGREADNODE_HPP
