
/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: pcm/include/ALSAIOStream.hh
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René Rebe
 * 
 * This program 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; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */

/* Short Description:
 *   ALSA IOStream implementation.
 */

#ifndef LOWLEVEL__ALSAIOSTREAM_HH__
#define LOWLEVEL__ALSAIOSTREAM_HH__

#include "ALSAHelper.hh"    // our ALSA C++ wrappings
#include "ALSAConverter.hh" // the alsa data converter templates

#include "BasicIOStream.hh"

#include <string>

namespace GSMP {

  extern GSMP::Logger <DefaultLogging_ALSA> ALSAIOStreamLog;
  
  // the ALSA implementation
  class ALSAIOStream : public BasicIOStream
  {
  public:
    ALSAIOStream ();
    virtual ~ALSAIOStream ();
    
    virtual bool Init ();
    
    virtual const std::vector<std::string>& Interfaces ();
    virtual const std::string& Interface ();
    virtual bool SetInterface (const std::string& n_interface);
        
    virtual int PlaybackPeriodSize ();
    virtual int CapturePeriodSize ();
    
    virtual int PlaybackPeriods ();
    virtual int CapturePeriods ();
    
    virtual int PlaybackXRuns ();
    virtual int CaptureXRuns ();
    
    virtual void SetPlayback (bool state);
    virtual void SetCapture (bool state);
    
    virtual bool Playback ();
    virtual bool Capture ();
    
    virtual bool StartIO ();
    virtual bool StopIO ();
    
    virtual bool IORunning ();
    
    virtual global_index CurrentPos ();
    
    /* Operations */
    virtual local_index Write (BasicStream* src, local_index start_index = 0);
    virtual local_index Read (BasicStream* det);
    
  protected:
    
    bool reget_card_params ();
    
    // TODO: obsolete by ostream operator
    void dump_alsa_params_info (ALSAPCM* card);
    bool internal_config_params (ALSAPCM* card,
				 bool is_pl, bool overwrite_params);
    void handle_xrun (ALSAPCM* card);
    
    ALSAPCM* playback_card;
    ALSAPCM* capture_card;


    BasicALSAConverter* read_converter;
    BasicALSAConverter* write_converter;
    
    bool initialized;
    bool io_running;
    bool cap_running; // idicating if already started
    
    int pl_period_size;
    int ca_period_size;
    
    int pl_periods;
    int ca_periods;
    
    bool stop_requested;
    bool m_is_first_loop;
    bool m_worked_in_last_loop;
    bool m_using_realtime_sched;
    
    /* frame counting */
    unsigned int cur_playback_period;
    unsigned int cur_capture_period;
    
    int pl_xruns;
    int ca_xruns;
    
    // config
    std::string cname;
    
    bool do_playback;
    bool do_capture;
  };
  
} // end namespace GSMP

#endif // LOWLEVEL__ALSAIOSTREAM_HH__
