/* 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_GENERIC_SYNC_SOURCE_NODE_HPP_
#define _NMM_GENERIC_SYNC_SOURCE_NODE_HPP_ _NMM_GENERIC_SYNC_SOURCE_NODE_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/utils/time/Interval.hpp"
#include "nmm/multimedia/GenericSourceNode.hpp"
#include "nmm/multimedia/sync/interfaces/ISynchronizedSourceImpl.hpp"

#include "visibility.hpp"

namespace NMM {

  class SourceController;

  //! This class is a superclass for synchronized source nodes.
  /*!
   *  Synchronization can be switched on and off by the setSynchronized -
   *  method. By default the switch is turned off, so an application 
   *  that don't uses synchronization should not care about it.
   *  In synchronized mode, be careful that incoming buffers are 
   *  timestamped: The GenericSyncSourceNode tries to present them according
   *  to their sync_time in the timestamps. 
   */
  class DLLEXPORT GenericSyncSourceNode : public GenericSourceNode,
				public ISynchronizedSourceImpl {
  private:
    bool synchronized;           //!< Internal flag.
                                 //!< (True if the node should be synchronized.)
    bool sync_master;            //!< Internal flag.
                                 //!< (True if the node is the master for synchronization)
    bool const_rate_producer;    //!< Internale flag.
                                 //!< (True if the node produces its data at a constant rate)
    Buffer* last_buffer;          //!< A pointer to the last buffer (needed to double a buffer)

    u_int64_t valid_bytes_send;   ///< The number of bytes ever send
    int buf_count;                ///< The number of buffers produced
    Interval time_skew;           ///< The difference between the sound card's clock
                                  ///< and the system clock
    ThreadMutex reset_mutex;
  protected:

    SourceController* controller; //!< The controller belonging to this node

    virtual Message* produceBuffer(Buffer*) = 0;
    
    virtual Time getLocalTime(u_int64_t /*valid_read_bytes*/) = 0;

  public:
    //! The main constructor.
    GenericSyncSourceNode(const char* name,
			const StreamQueue::Mode queue_mode 
			 = StreamQueue::DEFAULT_MODE,
			const int size = StreamQueue::DEFAULT_SIZE);

    //! The main destructor.
    virtual ~GenericSyncSourceNode();

    Message* processBuffer(Buffer *);

    /**
     * Implementation of ISynchronizedSource interface.
     */
    Result setSynchronized(const nmm_bool&);
    nmm_bool isSynchronized();
    void setSyncMaster(const nmm_bool&);
    nmm_bool isSyncMaster();
    void setConstantRateProducer(const nmm_bool&);
    nmm_bool isConstantRateProducer();
    ISourceController* getController();

    /**
     * Implementation of ISynchronized interface.
     */
    Result syncReset();
    Result syncEnable();
    Result syncDisable();

    ////////////////////////////////////////////////////////
    //////////// Internal Methods, used to  ////////////////
    /////// communicate between Node and Controller ////////
    ////////////////////////////////////////////////////////

    void resetTime();
  };
}
#include "nmm/nmm_suffix.hpp"

#endif // _NMM_GENERIC_SYNC_SOURCE_NODE_HPP_
