/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 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_AUDIOMUXNODE_HPP_
#define _NMM_AUDIOMUXNODE_HPP_ _NMM_AUDIOMUXNODE_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/multimedia/GenericMultiplexerNode.hpp"
#include "nmm/multimedia/ISyncResetImpl.hpp"
#include "nmm/utils/interpolator/LinearInterpolator.hpp"
#include "nmm/utils/thread/ThreadMutex.hpp"
#include "IAudioMuxImpl.hpp"

namespace NMM {
    
  class AudioMuxNode :
    public GenericMultiplexerNode,
    public ISyncResetImpl,
    public IAudioMuxImpl
  {
  private:
    Buffer* in_buffer1; ///< stores buffer from input1
    Buffer* in_buffer2; ///< stores buffer from input2

    int32_t volume1; ///< volume of stream from input1, simply between 0 and 100 %
    int32_t volume2; ///< volume of stream from input1, simply between 0 and 100 %
    
    bool received_first_buffer; ///< false if no buffer with valid and set timestamp was received

    Timestamp current_timestamp; ///< stores the latest set and valid timestamp of incoming stream from input1

    ThreadMutex mutex; ///< mutex used to guard processBuffer from all method from IAudioMux

    LinearInterpolator volume_interpolator1; ///< controls how fade in/fade out of audio is done for input1
    LinearInterpolator volume_interpolator2; ///< controls how fade in/fade out of audio is done for input2

  protected:

    /** filter out syncReset instream events */
    virtual Result syncReset();

    /** state transition */
    virtual Result doInit();
    /** state transition */
    virtual Result doInitOutput();

    /** IAudioMux */
    virtual Time getCurrentSyncTime();
    /** IAudioMux */
    virtual void setVolumeControl(const string& input_jack_tag, const Time& start_time, const Time& end_time, const nmm_int32& start_value, const nmm_int32& end_value);
    /** IAudioMux */
    virtual nmm_int32 setVolume(const string& input_jack_tag, const nmm_int32& volume);
    /** IAudioMux */
    virtual nmm_int32 getVolume(const string& input_jack_tag);

    /** implements functionality of node */
    Message* processBuffer(Buffer*);
      
  public:
      
    AudioMuxNode(const char *name = "AudioMuxNode", StreamQueue::Mode = StreamQueue::MODE_SUSPEND, int = 10);
    ~AudioMuxNode();
  };
}
#include "nmm/nmm_suffix.hpp"

#endif
