/* 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_AVCOLLECTOR_HPP_
#define _NMM_AVCOLLECTOR_HPP_ _NMM_AVCOLLECTOR_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/plugins/interfaces/IAVMuxImpl.hpp"
#include "nmm/multimedia/interfaces/ITrackImpl.hpp"
#include "nmm/multimedia/GenericMultiplexerNode.hpp"
#include "nmm/utils/thread/MutexGuard.hpp"

namespace NMM {

  /**
   * Audio/Video multiplexer.
   * This nodes puts events and buffers from two streams called "audio" and "video" together to
   * a single stream. Events and buffers receive labels, so that successor nodes can tell from
   * which input stream they came.
   */
  class AVMuxNode :
    public GenericMultiplexerNode,
    public ITrackImpl,
    public IAVMuxImpl
  {
  private:
    ThreadMutex mutex;

    int buf_count;
    int track_start_count;
    bool send_event;
    Buffer* last_buffer;
    
    bool audio_end_track;
    bool video_end_track;

    /** various members for discard mode */
    bool first_audio_buffer;
    bool first_video_buffer;
    bool audio_too_late;
    bool set_audio_too_late;
    Time first_audio_buffer_sync_time;
    Time first_video_buffer_sync_time;
    bool discard_mode;
    bool discard_mode_running;

    /** synchronized mode. synchronize audio and video */
    bool sync_mode;
    
    /** send all buffers remembered for synchronization */
    bool sync_flush;
    
    /** last video frame */
    Buffer* last_audio;
    
    /** last audio frame */
    Buffer* last_video;
    
    /** send an endTrack_event after sending remembered buffers */
    bool sync_end_track;
    
  protected:
    virtual Result doInit();
    virtual Result doInitOutput();
    
    virtual Result started(const Interval& overall_duration);
    virtual Result startTrack(const string& filename);
    virtual Result endTrack();
    virtual Result finished();
    
    /* IAVMux interface */
    virtual void setSyncMode(const nmm_bool& value);
    
    Message* processBuffer(Buffer*);
    
    Result processCEvent(CEvent*);
    
  public:

    AVMuxNode(const char *name = "AVMuxNode", StreamQueue::Mode = StreamQueue::MODE_SUSPEND, int = 20);
    ~AVMuxNode();
  };

}

#include "nmm/nmm_suffix.hpp"

#endif

