/* 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_SYNCHRONIZER_HPP_
#define _NMM_SYNCHRONIZER_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/comm/StdNamedObject.hpp"
#include "nmm/utils/time/TimedElement.hpp"
#include "nmm/multimedia/sync/interfaces/ISourceSynchronizerImpl.hpp"
#include "nmm/multimedia/sync/interfaces/ISinkSynchronizerImpl.hpp"
#include "nmm/multimedia/ReferenceCount.hpp"

namespace NMM {
  /** A synchronizer.
   * A synchronizer is an object which performs synchronization between a set of nodes in a flow graph.
   * Each synchronized node has a controller object which communicates with the synchronizer.
   * This is the abstract base class of all synchronizer classes. Different synchronizers exist for
   * synchronizing source and sink nodes, audio and video sources and sinks, and different numbers
   * of synchronized nodes.
   *
   * The synchronizer can be created directly by the application and may run on any host, as long as
   * a connection with every controller is possible.
   */
  class Synchronizer :
    public StdNamedObject,
    public TimedElement,
    public ReferenceCount,
    virtual public ISynchronizerImpl
  {
  protected:
    /** Pause the synchronizer.
     * This method pauses all sinks synchronized by the synchronizer.
     *
     * @return @c SUCCESS if the operation succeeded, @c FAILURE otherwise
     */
    virtual Result pause() = 0;

    /** Wake up the synchronizer after pause.
     * This method wakes up all sinks synchronized by the synchronizer after pause.
     *
     * @return @c SUCCESS if the operation succeeded, @c FAILURE otherwise
     */
    virtual Result wakeup() = 0;

    /** Reset the synchronizer.
     * This method resets all sinks synchronized by the synchronizer.
     * It is called by a sink which received a syncReset event. Only all other sinks are reset
     * explicitly by the synchronizer.
     *
     * @param sink_id Sink ID of sink which received the initial syncReset
     */
    virtual Result reset(const nmm_int32& sink) = 0;
  public:
    /** Create a new synchronizer.
     *
     * @param name Object name of the synchronizer
     */
    Synchronizer(const char* name = "Synchronizer") : StdNamedObject(name) { }

    /** Virtual destructor.
     */
    virtual ~Synchronizer() { }
  };

  /** A sink synchronizer.
   * A sink synchronizer is an object which performs synchronization between a set of sink nodes in a flow graph.
   * Sink synchronizers communicate with sink controllers, which are attached to sink nodes.
   * This is the abstract base class of all sink synchronizer classes. Different synchronizers exist for
   * synchronizing audio and video sinks and different numbers of synchronized nodes.
   *
   * The synchronizer can be created directly by the application and may run on any host, as long as
   * a connection with every controller is possible.
   */
  class SinkSynchronizer : 
    public Synchronizer, 
    virtual public ISinkSynchronizerImpl 
  {
  public:
    /** Create a new synchronizer.
     *
     * @param name Object name of the synchronizer
     */
    SinkSynchronizer(const char* name = "SinkSynchronizer") : Synchronizer(name) { }

    /** Virtual destructor.
     */
    virtual ~SinkSynchronizer() { }

  protected:

    /**
     * Set average latency.
     * This method is used by the sink controllers and should not be called 
     * directly.
     * With this method, the sink controllers tell the
     * synchronizer the average latency of their incoming buffers.
     * @param average_latency The average_latency of incoming buffers computed
     *                        by the Controller.
     * @param sink sink ID
     */
    virtual void setAverageLatency(const Interval& value, const nmm_int32& sink) = 0;
  };

  /** A source synchronizer.
   * A source synchronizer is an object which performs synchronization between a set of source nodes in a flow graph.
   * Source synchronizers communicate with source controllers, which are attached to source nodes.
   * This is the abstract base class of all source synchronizer classes. Different synchronizers exist for
   * synchronizing audio and video sources and different numbers of synchronized nodes.
   *
   * The synchronizer can be created directly by the application and may run on any host, as long as
   * a connection with every controller is possible.
   */
  class SourceSynchronizer : 
    public Synchronizer, 
    virtual public ISourceSynchronizerImpl 
  {
  public:
    /** Create a new synchronizer.
     *
     * @param name Object name of the synchronizer
     */
    SourceSynchronizer(const char* name = "SourceSynchronizer") : Synchronizer(name) { }

    /** Virtual destructor.
     */
    virtual ~SourceSynchronizer() { }

  protected:

    /**
     * Sets a skew between audio and video output.
     * The video output is presented later as the audio output.
     */
    virtual Result setMasterSkew(const Interval& skew) = 0;
  };

}

#include "nmm/nmm_suffix.hpp"

#endif // _NMM_SYNCHRONIZER_HPP_

