/*
 * ringbuffer.h: A threaded ring buffer
 *
 * Copyright (C) 2000 Klaus Schmidinger
 * Copyright of modifications (C) 2006-2007 Motama GmbH
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 *
 * The author can be reached at kls@cadsoft.de
 *
 * The project's page is at http://www.cadsoft.de/people/kls/vdr
 *
 */

#ifndef __RINGBUFFER_H
#define __RINGBUFFER_H

#include "thread.h"

namespace VDR {

typedef unsigned char uchar;

class cRingBufferInputThread;
class cRingBufferOutputThread;

class cRingBuffer {
  friend class cRingBufferInputThread;
  friend class cRingBufferOutputThread;
private:
  cRingBufferInputThread *inputThread;
  cRingBufferOutputThread *outputThread;
  cMutex mutex;
  int size, head, tail;
  uchar *buffer;
  int maxFill;
  bool busy;
  bool statistics;
protected:
  void Lock(void) { mutex.Lock(); }
  void Unlock(void) { mutex.Unlock(); }
  int Available(void);
  bool Busy(void) { return busy; }
  void Clear(void);
    // Immediately clears the ring buffer.
  int Put(const uchar *Data, int Count);
    // Puts at most Count bytes of Data into the ring buffer.
    // Returns the number of bytes actually stored.
  int Get(uchar *Data, int Count);
    // Gets at most Count bytes of Data from the ring buffer.
    // Returns the number of bytes actually retrieved.
  virtual void Input(void) = 0;
    // Runs as a separate thread and shall continuously read data from
    // a source and call Put() to store the data in the ring buffer.
  virtual void Output(void) = 0;
    // Runs as a separate thread and shall continuously call Get() to
    // retrieve data from the ring buffer and write it to a destination.
public:
  cRingBuffer(int Size, bool Statistics = false);
  virtual ~cRingBuffer();
  bool Start(void);
  bool Active(void);
  void Stop(void);
  };

}
#endif // __RINGBUFFER_H
