/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Trivial application to playback a sound file
 * 
 * Copyright (C) 1999, Mark Spencer
 *
 * Mark Spencer <markster@linux-support.net>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#include <asterisk/lock.h>
#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <asterisk/translate.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

static char *tdesc = "Trivial Playback Application";

static char *app = "Playback1";

static char *synopsis = "Play a file";

static char *descrip = 
"  Playback(filename[|option]):  Plays  back  a  given  filename (do not put\n"
"extension). Options may also be  included following a pipe symbol. The only\n"
"defined option at this time is 'skip',  which  causes  the  playback of the\n"
"message to  be  skipped  if  the  channel is not in the 'up' state (i.e. it\n"
"hasn't been  answered  yet. If 'skip' is specified, the application will\n"
"return immediately should the channel not be off hook.  Otherwise, unless\n"
"'noanswer' is specified, the channel channel will be answered before the sound\n"
"is played. Not all channels support playing messages while on hook. Returns -1\n"
"if the channel was hung up, or if the file does not exist. Returns 0 otherwise.\n";

STANDARD_LOCAL_USER;

LOCAL_USER_DECL;

static int playback_exec(struct ast_channel *chan, void *data)
{
	int res = 0;
	struct localuser *u;
	char tmp[256];
	char *options;
	LOCAL_USER_ADD(u);
	ast_set_write_format(chan,ast_best_codec(chan->nativeformats));
	if (chan->_state != AST_STATE_UP) {
		ast_log(LOG_WARNING,"Channel not up\n");
		res = ast_answer(chan);
	}
	if (chan->_state == AST_STATE_UP)
		ast_log(LOG_WARNING,"channel isup now \n");
	strcpy(tmp,"/home/rohan/asterisk/myapps/test");
	ast_log(LOG_WARNING,"%s %d Playing my file %s\n",__FILE__,__LINE__,tmp);
	//ast_log(LOG_WARNING,"%s %d Native format =  %d\n",__FILE__,__LINE__,chan->nativeformats);
	struct ast_frame fr;
	unsigned char digit[] = {0x1e,0x0b,0x0b,0x1e,0x9e,0x8b,0x8b,0x9e} ;
	unsigned char buff[160];
	int i,j;
	int count = 0;
	int whennext = 0;
	int fd;
	//fd = open(tmp,O_RDONLY,0777);
	//if (fd < 0) 
		//ast_log(LOG_WARNING,"failed to open file\n");
	while(!whennext) {
		memset(&fr,0,sizeof(fr));
		fr.data = buff;
		fr.frametype = AST_FRAME_VOICE;
		fr.subclass = AST_FORMAT_ULAW;
		fr.offset = AST_FRIENDLY_OFFSET;
		fr.datalen = 160;
		fr.mallocd = 0;
		fr.samples = 160;
		fr.src = "app_playback";
		for (i = 0; i<160; i++) {
			buff[i] = digit[j++];
			j &= 7;
		}
		res = ast_write(chan,&fr);
		ast_log(LOG_WARNING, "Sent frame for writing\n");
		if (res) {
			ast_log(LOG_WARNING, "Failed to write frame\n");
			//chan->streamid = -1;
			break;
		}
		res = ast_safe_sleep(chan,250);

	}
	/*if (!res) {
		ast_log(LOG_WARNING,"%s %d Playing my file %s\n",__FILE__,__LINE__,tmp);
		ast_log(LOG_WARNING,"AST_FORMAT_ULAW = %d\n",__FILE__,__LINE__,AST_FORMAT_ULAW);
		ast_log(LOG_WARNING,"channel state = %d\n",chan->_state);
		ast_log(LOG_WARNING,"Hangup = %d\n",chan->_softhangup);
		ast_stopstream(chan);
		if (chan->_softhangup) {
			ast_log(LOG_WARNING,"The user has hung up\n");
			LOCAL_USER_REMOVE(u);
			return res;
		}
		else
			ast_log(LOG_WARNING,"The user did not hangup\n");
		res = ast_streamfile(chan, tmp, chan->language);
		ast_log(LOG_WARNING, "%d\n",res);
		ast_log(LOG_WARNING, "%d\n",chan->_state);
		if (!res) {
			ast_log(LOG_WARNING,"Started file\n");
			res = ast_waitstream(chan,"");
		}
		else {
			ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
			res = 0;
		}
		ast_stopstream(chan);
	}*/
	ast_softhangup(chan,1);
	ast_log(LOG_WARNING, "Hangup = %d\n",chan->_softhangup);
	LOCAL_USER_REMOVE(u);
	return res;
}

int unload_module(void)
{
	STANDARD_HANGUP_LOCALUSERS;
	return ast_unregister_application(app);
}

int load_module(void)
{
	return ast_register_application(app, playback_exec, synopsis, descrip);
}

char *description(void)
{
	return tdesc;
}

int usecount(void)
{
	int res;
	STANDARD_USECOUNT(res);
	return res;
}

char *key()
{
	return ASTERISK_GPL_KEY;
}

//struct ast_filestream *fs;
/*struct ast_frame *fr;
  char buff[160];
  int count = 0;
  int whennext = 0;
  int fd;
  fs = ast_openstream(chan,tmp,chan->language);
  fd = open(tmp,O_RDONLY,0777);
  while(!whennext) {
  memset(buff,'\0',160);
  count = read(fd,buff,160);
  if (!count) whennext = 1;
  fr = (struct ast_frame*)malloc(sizeof(struct ast_frame));
  fr->data = buff;
  fr->datalen = count;
  fr->frametype = AST_FRAME_VOICE;
  fr->mallocd = 0;
//fr->subclass = 4;
//fr->samples = count;
if (fr != NULL && count != 0) {
if (ast_write(chan,fr)) {
ast_log(LOG_DEBUG, "Failed to write frame");
chan->streamid = -1;
} else
chan->streamid = -1;
}
free(fr);
}*/

