/* 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_WAVREADNODE_HPP
#define NMM_WAVREADNODE_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/multimedia/GenericSourceNode.hpp"
#include <fstream>

#include "nmm/plugins/interfaces/IBufferSizeImpl.hpp"
#include "nmm/plugins/interfaces/IProgressImpl.hpp"
#include "nmm/plugins/file/interfaces/ISourceFileHandlerImpl.hpp"
#include "nmm/plugins/file/interfaces/ISeekableImpl.hpp"
#include "nmm/plugins/file/interfaces/IHeaderImpl.hpp"

#include "visibility.hpp"

namespace NMM {

  /** Node to read wav files.
   */
  class WavReadNode :
    public GenericSourceNode,
    public ISourceFileHandlerImpl,
    public ISeekableImpl,
    public IBufferSizeImpl,
    public IHeaderImpl
  {
  public:
    Format* out_format;
    
    /** Node constructor.
     *
     * @param name node name
     * @param queue_mode stream queue mode
     * @param size stream queue size (in buffers)
     */
    WavReadNode(const char * name = "WavReadNode",
		StreamQueue::Mode queue_mode = StreamQueue::MODE_SUSPEND,
		int size = StreamQueue::DEFAULT_SIZE);

    /** Destructor.
     */
    virtual ~WavReadNode();

  private:
    bool buffersize_changed; ///< true if buffer size changed
    
  protected:
    Timestamp current_ts; ///< current timestamp used
    
    enum localstates { START_FLUSH, START, DATA, DATA_STOPPED, TRACK_ENDED, FINISHED }; ///< possible states of node 
    
    long int buffer_size; ///< current buffer size

    string filename; ///< name of file currently opened
    fstream in; ///< file stream to read wav file

    long int channels; ///< channels of current file
    long int bytes_per_sample; ///< bytes per sample of current file
    long int bytes_per_second; ///< bytes per second of current file

    // seeking & file position
    u_int64_t         filesize; ///< size of current file
    u_int64_t         filepos;  ///< byte position in current file
    int64_t           seekto;   ///< byte position to seek to 

    Interval          track_duration; ///< the duration of the current track

    localstates state; ///< current state of node

    bool   send_format; ///< true if we have to send setFormat event
    bool   send_header; ///< true if we have to send header
    
    /** The doInit method from Node.
     *
     * The doInit method stores all formats in the input and output property which are
     * supported by this node. These properties are used to register a Node by a registry.
     *
     * @return SUCCESS
     */
    virtual Result doInit();

    /** The doDeinit method from Node.
     *
     * Resets variables and erases output property.
     *
     * @return SUCCESS
     */
    virtual Result doDeinit();

    /** The doInitOutput method from Node.
     *
     * Opens wav file and sets working output property correspondingly.
     *
     * @return SUCCESS if opening of file was successful, FAILURE otherwise
     */
    virtual Result doInitOutput();

    /** The doDeinitOutput method from Node.
     *
     * Closes wav file and erases working output property.
     *
     * @return SUCCESS
     */
    virtual Result doDeinitOutput();

    /** The doStop method from Node.
     *
     * Sets state to DATA_STOPPED.
     *
     * @return SUCCESS
     */
    virtual Result doStop();
    
    /** The processBuffer method from GenericNode.
     *
     * Processes data from wav file depending on state machine.
     *
     * @param in_buffer pointer to incoming buffer (alsways NULL for this node)
     * @return SUCCESS
     */
    virtual Message* processBuffer(Buffer* in_buffer);
    
    //IHeader
    /** Enables sending of file header.
     *
     * @param value  True if sending of file header is enabled, false otherwise.
     */
    virtual void sendHeader(const nmm_bool& value) {send_header = value;}

    /** Event which contains file header as string.
     *
     * @param header File Header 
     */
    virtual void FileHeader(const string& header);
    
    // IFileHandler interface
    /** Sets name of file to be opened.
     *
     * @param name Name of the file to be read.
     */
    virtual void setFilename(const string& name);
    
     // ISourceFileHandler interface
    /** Returns file size of currently opened file.
     *
     * @return File size of currently opened file.
     */
    virtual nmm_uint64 getFilesize();
    
    // IBufferSize interface
    /** Sets buffer size.
     *
     *  Sets buffer size to be used by the node.
     *  
     *  @param size Buffer size to be used.
     */
    virtual void setBufferSize(const nmm_int32& size);

    /** Requests buffer size node is currently using.
     *  
     *  @return Buffer size used by node.
     */
    virtual nmm_int32 getBufferSize();
    
    // ISeekable interface
    /** Tests if node suports seeking by percent.
     *
     * This method can be used to test if this interface supports
     * seeking by percentage.
     * @return true iff percantage seeking is supported.
     */
    virtual nmm_bool hasPercentSeek() const;

    /** Seeks relative by percent.
     *
     * This method seeks relative from the current position in
     * percent.
     *
     * @param percent The offset to seek in a Rational from -1 to 1.
     */
    virtual void seekPercent(const Rational& percent);
    
    /** Seeks absolute by percent.
     *
     * This method seeks absolute to the given position in percent.
     *
     * @param percent The position to seek to in a Rational from 0 to 1.
     */
    virtual void seekPercentTo(const Rational& percent);

    /** Tests if node supports seeking by bytes.
     *
     * This method can be used to test if this interface supports
     * seeking by bytes.
     *
     * @return true iff bytewise seeking is supported.
     */
    virtual nmm_bool hasBytesSeek() const;

    /** Seeks relative by bytes.
     *
     * This method seeks relative from the current position in
     * bytes.
     *
     * @param offset The offset to seek.
     */
    virtual void seekBytes(const nmm_int64& offset);

    /** Seeks absolute by bytes.
     *
     * This method seeks absolute to the given position in percent.
     *
     * @param bytes The position to seek to.
     */
    virtual void seekBytesTo(const nmm_uint64& bytes);

    /** Tests if node supports seeking by time.
     *
     * This method can be used to test if this interface supports
     * seeking by Time.
     *
     * @return true iff seeking by Time is supported.
     */
    virtual nmm_bool hasTimeSeek() const;

    /** Seeks relative by time.
     *
     * This method seeks relative from the current position in time.
     *
     * @param interval The Interval to seek.
     */
    virtual void seekTime(const Interval& interval);
    
    /** Seeks absolute by time.
     *
     * This method seeks absolute to the given position in Time.
     *
     * @param time The Time to seek to.
     */
    virtual void seekTimeTo(const Time& time);
  };

  // IMPLEMENTATION

  inline void WavReadNode::FileHeader(const string&) {}
  
}

#include "nmm/nmm_suffix.hpp"

#endif /* _NMM_READWAVEVENTNODE_HPP_ */
