Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:47 2009

## Asterisk developer's documentation

# app_echo.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (C) 1999 - 2005, Digium, Inc.
    00005  *
    00006  * Mark Spencer <markster@digium.com>
    00007  *
    00008  * See http://www.asterisk.org for more information about
    00009  * the Asterisk project. Please do not directly contact
    00010  * any of the maintainers of this project for assistance;
    00011  * the project provides a web site, mailing lists and IRC
    00012  * channels for your use.
    00013  *
    00014  * This program is free software, distributed under the terms of
    00015  * the GNU General Public License Version 2. See the LICENSE file
    00016  * at the top of the source tree.
    00017  */
    00018
    00019 /*! \file
    00020  *
    00021  * \brief Echo application -- play back what you hear to evaluate latency
    00022  *
    00023  * \ingroup applications
    00024  */
    00025
    00026 #include <stdlib.h>
    00027 #include <stdio.h>
    00028 #include <unistd.h>
    00029 #include <string.h>
    00030
    00031 #include "asterisk.h"
    00032
    00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00034
    00035 #include "asterisk/lock.h"
    00036 #include "asterisk/file.h"
    00037 #include "asterisk/logger.h"
    00038 #include "asterisk/channel.h"
    00039 #include "asterisk/pbx.h"
    00040 #include "asterisk/module.h"
    00041
    00042 static char *tdesc = "Simple Echo Application";
    00043
    00044 static char *app = "Echo";
    00045
    00046 static char *synopsis = "Echo audio read back to the user";
    00047
    00048 static char *descrip =
    00049 "  Echo():  Echo audio read from channel back to the channel. \n"
    00050 "User can exit the application by either pressing the '#' key, \n"
    00051 "or hanging up.\n";
    00052
    00053 STANDARD_LOCAL_USER;
    00054
    00055 LOCAL_USER_DECL;
    00056
    00057 static int echo_exec(struct ast_channel *chan, void *data)
    00058 {
    00059    int res=-1;
    00060    struct localuser *u;
    00061    struct ast_frame *f;
    00062    LOCAL_USER_ADD(u);
    00063    ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
    00064    ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
    00065    /* Do our thing here */
    00066    while(ast_waitfor(chan, -1) > -1) {
    00067       f = ast_read(chan);
    00068       if (!f)
    00069          break;
    00070       f->delivery.tv_sec = 0;
    00071       f->delivery.tv_usec = 0;
    00072       if (f->frametype == AST_FRAME_VOICE) {
    00073          if (ast_write(chan, f))
    00074             break;
    00075       } else if (f->frametype == AST_FRAME_VIDEO) {
    00076          if (ast_write(chan, f))
    00077             break;
    00078       } else if (f->frametype == AST_FRAME_DTMF) {
    00079          if (f->subclass == '#') {
    00080             res = 0;
    00081             break;
    00082          } else
    00083             if (ast_write(chan, f))
    00084                break;
    00085       }
    00086       ast_frfree(f);
    00087    }
    00088    LOCAL_USER_REMOVE(u);
    00089    return res;
    00090 }
    00091
    00092 int unload_module(void)
    00093 {
    00094    int res;
    00095
    00096    res = ast_unregister_application(app);
    00097
    00098    STANDARD_HANGUP_LOCALUSERS;
    00099
    00100    return res;
    00101 }
    00102
    00103 int load_module(void)
    00104 {
    00105    return ast_register_application(app, echo_exec, synopsis, descrip);
    00106 }
    00107
    00108 char *description(void)
    00109 {
    00110    return tdesc;
    00111 }
    00112
    00113 int usecount(void)
    00114 {
    00115    int res;
    00116    STANDARD_USECOUNT(res);
    00117    return res;
    00118 }
    00119
    00120 char *key()
    00121 {
    00122    return ASTERISK_GPL_KEY;
    00123 }