/* 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_RECORDNODE_HPP_
#define _NMM_RECORDNODE_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/multimedia/sync/GenericSyncSourceNode.hpp"

#include "nmm/plugins/interfaces/IBufferSizeImpl.hpp"
#include "nmm/multimedia/IDeviceImpl.hpp"
#include "IFragmentSizeImpl.hpp"

#include "nmm/multimedia/TDevicePlugin.hpp"
//#include "nmm/base/registry/DevicePlugin.hpp"

namespace NMM {
   class StreamTimer;
  /**
   * This class records an audio stream from a given input device.
   * So far, the formats are hardwired and we already know what the
   * sound device is capable of.
   */
  class RecordNode :
    public GenericSyncSourceNode,
    public IDeviceImpl,
    public IBufferSizeImpl,
    public IFragmentSizeImpl
  {
  private:
    StreamTimer* timer;
    int sample_rate;        ///< The sample rate to record with
    int num_channels;       ///< The number of channels to record with
    int bits_per_sample;    ///< The number of bits per sample to record with
    
    string dev;             ///< The name of the sound device.
    int audio_fd;           ///< File descriptor number of the sound device.
    
    int buffer_size;         ///< Size of the buffers.

    int fragment_argument;
    
  protected:
    /**
     * Opens the recording device.
     */
    virtual Result doInit();
    virtual Result doDeinit();
    
    /**
     * Setup working output property.
     */
    virtual Result doInitOutput();
    
    /**
     * Extracts format.
     */
    virtual Result doActivate();
    
    /**
     * The produceBuffer method from GenericSyncSourceNode.
     */
    virtual Message* produceBuffer(Buffer*);

    // virtual functions inherited from GenericSyncSourceNode
    virtual Time getLocalTime(u_int64_t);
    
  public:
    
    /**
     * The main constructor. Can take a BufferManager to use instead
     * of creating its own Allocator.
     * \param dev the device to open.
     * \param manager the buffer manager to use.
     */
    RecordNode(const char* name = "RecordNode",
	       StreamQueue::Mode = StreamQueue::MODE_SUSPEND,
	       int = StreamQueue::DEFAULT_SIZE);
    virtual ~RecordNode();
    
    /**
     * IDevice interface.
     */
    virtual void setDevice(const string& value)
      throw(const NMM::DeviceNotFoundException&);
    virtual string getDevice() const;
    
    /**
     * IBufferSize interface.
     */
    virtual void setBufferSize(const nmm_int32& size);
    virtual nmm_int32 getBufferSize();

    /**
     * IFragmentSize interface.
     */
    virtual nmm_int32 getFragmentSize();
    virtual void setFragmentSize(const nmm_int32& value); 
  };
  
  class RecordNodePlugin
    : public TDevicePlugin<RecordNode>
  {
  public:
    RecordNodePlugin(const string& node_name, int max_instance = -1);
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif //_NMM_RECORDNODE_HPP_
