/*
    GDAM - Geoff & Dave's Audio Mixer
    Copyright (C) 1999    Dave Benson, Geoff Matters.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

    Contact:
        daveb@ffem.org <Dave Benson>
        geoff@ugcs.caltech.edu <Geoff Matters>
*/
#ifndef __GDAS_EXTERNAL_H_
#define __GDAS_EXTERNAL_H_

typedef struct _GdasExternal GdasExternal;
typedef struct _GdasExternalClass GdasExternalClass;

#include "gdassource.h"
#include <gdam/common/gdamsampleconverter.h>

GtkType gdas_external_get_type();
#define GDAS_TYPE_EXTERNAL   gdas_external_get_type()
#define GDAS_EXTERNAL(ptr) ((GdasExternal*) gtk_type_check_object_cast ((GtkTypeObject*) (ptr), GDAS_TYPE_EXTERNAL))
#define GDAS_EXTERNAL_CLASS(klass)      (GTK_CHECK_CLASS_CAST ((klass), GDAS_TYPE_EXTERNAL, GdasExternalClass))

struct _GdasExternalClass { 
	GdasSourceClass		source_class;
        void                  (*do_exec)(GdasExternal *external,
                                         char        **command,
					 int           fd);
	guint                   do_dup2 : 1;
};

struct _GdasExternal {
	GdasSource		base;

        int                     stdin_redirect_fd;

	/* The process id of the thread we started. */
	int			process_id;

	/* The file descriptor we're reading from. */
	int                     fd;

	GdamFdPoll             *fd_poll;

	/* Data buffered from the external source. */
	GdamBuffer		buffer;

	/* Maximum of data to buffer from source. */
	int			max_buffer_size;

	/* Data we must discard b/c we were behind. */
	int			skipped;

	int			num_channels;
	int			bytes_per_sample;
	GdamSampleConverter	sample_converter;
	GdamSampleConverter	channel_converter;

	/*
	 * The next sample we are about to output.
	 * (So this is incremented entirely by `read' and `miss_data').
	 * (And zeroed by run_command and kill_command.)
	 */
	GdamTime		samples_output;

	/* Number of samples remaining to output, or -1 for no maximum. */
	GdamTime		samples_remaining;

	/* How many silent samples to output. */
	int			zero_time;

	/* Set this for weird things that should not be closed on
	 * destruction (eg full duplex soundcards of which this
	 * is the input half). */
	gboolean		dont_close;

	unsigned		has_been_ready : 1,
				ignore_miss_data : 1;
};


GdasExternal*  gdas_external_new          (const char*   sh_command);

/* The command you pass is held by the external. */
gboolean       gdas_external_run_command  (GdasExternal *external, 
                                           char        **command);
void           gdas_external_has_opened   (GdasExternal *external,
                                           int           fd);

gboolean       gdas_external_kill_command (GdasExternal *external,
                                           int          *exit_status);
void           gdas_external_set_num_channels
                                          (GdasExternal *external,
                                           int           num_channels);

int gdas_external_get_default_buffer_size();
void gdas_external_set_default_buffer_size(int buffer_size);

#endif
