summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndreas Eversberg <jolly@eversberg.eu>2021-03-13 17:09:20 +0100
committerAndreas Eversberg <jolly@eversberg.eu>2021-03-21 09:03:06 +0100
commit72b0d35e6c52a13f133e26b74b2414cf94fbc8e8 (patch)
tree6e3f9cb11fd0a748436b4e711979473d97508324 /src
Initial commit
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am21
-rw-r--r--src/telephone/Makefile.am30
-rw-r--r--src/telephone/main.c326
-rw-r--r--src/telephone/telephone.c849
-rw-r--r--src/telephone/telephone.h75
5 files changed, 1301 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..761af8d
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,21 @@
+AUTOMAKE_OPTIONS = foreign
+
+SUBDIRS = \
+ liboptions \
+ libdebug \
+ libsample \
+ libsamplerate \
+ libfilter \
+ libtimer \
+ libjitter \
+ libosmocc \
+ libg711
+
+if HAVE_ALSA
+SUBDIRS += \
+ libsound
+endif
+
+SUBDIRS += \
+ telephone
+
diff --git a/src/telephone/Makefile.am b/src/telephone/Makefile.am
new file mode 100644
index 0000000..e850614
--- /dev/null
+++ b/src/telephone/Makefile.am
@@ -0,0 +1,30 @@
+AM_CPPFLAGS = -Wall -Wextra -g $(all_includes)
+
+bin_PROGRAMS = \
+ osmo-cc-alsa-endpoint
+
+osmo_cc_alsa_endpoint_SOURCES = \
+ telephone.c \
+ main.c
+
+osmo_cc_alsa_endpoint_LDADD = \
+ $(COMMON_LA) \
+ ../libdebug/libdebug.a \
+ ../liboptions/liboptions.a \
+ ../libsample/libsample.a \
+ ../libsamplerate/libsamplerate.a \
+ ../libfilter/libfilter.a \
+ ../libtimer/libtimer.a \
+ ../libjitter/libjitter.a \
+ ../libosmocc/libosmocc.a \
+ ../libg711/libg711.a \
+ -lm
+
+if HAVE_ALSA
+AM_CPPFLAGS += -DHAVE_ALSA
+
+osmo_cc_alsa_endpoint_LDADD += \
+ ../libsound/libsound.a \
+ $(ALSA_LIBS)
+endif
+
diff --git a/src/telephone/main.c b/src/telephone/main.c
new file mode 100644
index 0000000..8bf604a
--- /dev/null
+++ b/src/telephone/main.c
@@ -0,0 +1,326 @@
+/* osmo-cc-alsa-endpoint main
+ *
+ * (C) 2020 by Andreas Eversberg <jolly@eversberg.eu>
+ * All Rights Reserved
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+#include <string.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <sched.h>
+#include "../libdebug/debug.h"
+#include "../liboptions/options.h"
+#include "../libg711/g711.h"
+#include "telephone.h"
+
+telephone_t *telephone_ep = NULL;
+int num_kanal = 1;
+
+static const char *name = "alsa";
+static const char *audiodev = NULL;
+static int samplerate = 48000;
+static int latency = 50;
+static int rt_prio = 1;
+static const char *caller_id = "";
+static int early_audio = 0;
+static int autoalert = 0, autoanswer = 1;
+#define MAX_CC_ARGS 1024
+static int cc_argc = 0;
+static const char *cc_argv[MAX_CC_ARGS];
+
+static void print_usage(const char *app)
+{
+ printf("Usage: %s -a hw:<card>,<device> [<options>] [dialing]\n", app);
+}
+
+static void print_help()
+{
+ /* - - */
+ printf(" -h --help\n");
+ printf(" This help\n");
+ printf(" --config [~/]<path to config file>\n");
+ printf(" Give a config file to use. If it starts with '~/', path is at home dir.\n");
+ printf(" Each line in config file is one option, '-' or '--' must not be given!\n");
+ printf(" -v --verbose <level> | <level>,<category>[,<category>[,...]] | list\n");
+ printf(" Use 'list' to get a list of all levels and categories\n");
+ printf(" Verbose level: digit of debug level (default = '%d')\n", debuglevel);
+ printf(" Verbose level+category: level digit followed by one or more categories\n");
+ printf(" -> If no category is specified, all categories are selected\n");
+ printf(" -n --name <interface name>\n");
+ printf(" Give name of this interface. It will be sent in each call towards\n");
+ printf(" -I --caller-id <caller id>\n");
+ printf(" What caller ID to send on calls made from terminal. (default = '%s')\n", caller_id);
+ printf(" -A --auto alerting | answer | off\n");
+ printf(" An incoming call can be responded automatically with an alerting or an\n");
+ printf(" answer, or not be responded automatically. (default = 'answer')\n");
+ printf(" -E --early-audio\n");
+ printf(" Send early audio when call is not yet connected. Must be used in\n");
+ printf(" conjunction with --auto\n");
+ printf(" -a --audio-device hw:<card>,<device>\n");
+ printf(" Sound card and device number (default = '%s')\n", audiodev);
+ printf(" -s --samplerate <rate>\n");
+ printf(" Sample rate of sound device (default = '%d')\n", samplerate);
+ printf(" -b --buffer <ms>\n");
+ printf(" How many milliseconds are processed in advance (default = '%d')\n", latency);
+ printf(" -r --realtime <prio>\n");
+ printf(" Set prio: 0 to disable, 99 for maximum (default = %d)\n", rt_prio);
+ printf(" -C --cc \"<osmo-cc arg>\" [--cc ...]\n");
+ printf(" Pass arguments to Osmo-CC endpoint. Use '-cc help' for description.\n");
+}
+
+static void add_options(void)
+{
+ option_add('h', "help", 0);
+ option_add('v', "verbose", 1);
+ option_add('n', "name", 1);
+ option_add('I', "caller-id", 1);
+ option_add('A', "auto", 1);
+ option_add('E', "early-audio", 0);
+ option_add('a', "audio-device", 1);
+ option_add('s', "samplerate", 1);
+ option_add('b', "buffer", 1);
+ option_add('r', "realtime", 1);
+ option_add('C', "cc", 1);
+}
+
+static int handle_options(int short_option, int argi, char **argv)
+{
+ int rc;
+
+ switch (short_option) {
+ case 'h':
+ print_usage(argv[0]);
+ print_help();
+ return 0;
+ case 'v':
+ if (!strcasecmp(argv[argi], "list")) {
+ debug_list_cat();
+ return 0;
+ }
+ rc = parse_debug_opt(argv[argi]);
+ if (rc < 0) {
+ fprintf(stderr, "Failed to parse debug option, please use -h for help.\n");
+ return rc;
+ }
+ break;
+ case 'n':
+ name = options_strdup(argv[argi]);
+ break;
+ case 'I':
+ caller_id = options_strdup(argv[argi]);
+ break;
+ case 'A':
+ if (!strcasecmp(argv[argi], "alerting")) {
+ autoalert = 1;
+ autoanswer = 0;
+ } else
+ if (!strcasecmp(argv[argi], "answer")) {
+ autoalert = 0;
+ autoanswer = 1;
+ } else
+ if (!strcasecmp(argv[argi], "off")) {
+ autoalert = 0;
+ autoanswer = 0;
+ } else {
+ fprintf(stderr, "Unknown parameter '%s', please use -h for help.\n", argv[argi]);
+ return -EINVAL;
+ }
+ break;
+ case 'E':
+ early_audio = 1;
+ break;
+ case 'a':
+ audiodev = options_strdup(argv[argi]);
+ break;
+ case 's':
+ samplerate = atoi(argv[argi]);
+ break;
+ case 'b':
+ latency = atoi(argv[argi]);
+ break;
+ case 'r':
+ rt_prio = atoi(argv[argi]);
+ break;
+ case 'C':
+ if (!strcasecmp(argv[argi], "help")) {
+ osmo_cc_help();
+ return 0;
+ }
+ if (cc_argc == MAX_CC_ARGS) {
+ fprintf(stderr, "Too many osmo-cc args!\n");
+ break;
+ }
+ cc_argv[cc_argc++] = options_strdup(argv[argi]);
+ break;
+ default:
+ return -EINVAL;
+ }
+ return 1;
+}
+
+static int quit = 0;
+void sighandler(int sigset)
+{
+ if (sigset == SIGHUP || sigset == SIGPIPE)
+ return;
+
+ fprintf(stderr, "\nSignal %d received.\n", sigset);
+
+ quit = 1;
+}
+
+static int get_char()
+{
+ struct timeval tv = {0, 0};
+ fd_set fds;
+ char c = 0;
+ int __attribute__((__unused__)) rc;
+
+ FD_ZERO(&fds);
+ FD_SET(0, &fds);
+ select(0+1, &fds, NULL, NULL, &tv);
+ if (FD_ISSET(0, &fds)) {
+ rc = read(0, &c, 1);
+ return c;
+ } else
+ return -1;
+}
+
+int main(int argc, char *argv[])
+{
+ int argi, rc;
+ const char *dialing = "";
+ struct termios term, term_orig;
+ char c;
+
+ g711_init();
+
+ telephone_ep = telephone_create();
+ if (!telephone_ep)
+ goto error;
+
+ cc_argv[cc_argc++] = options_strdup("remote auto");
+
+ /* handle options / config file */
+ add_options();
+ rc = options_config_file(argc, argv, "~/.osmocom/alsa/alsa.conf", handle_options);
+ if (rc < 0)
+ return 0;
+ argi = options_command_line(argc, argv, handle_options);
+ if (argi <= 0)
+ return argi;
+
+ if (argi < argc)
+ dialing = argv[argi];
+
+ rc = ui_init(dialing, autoalert, autoanswer);
+ if (rc) {
+ PDEBUG(DTEL, DEBUG_ERROR, "UI initializing failed!\n");
+ goto error;
+ }
+
+ rc = telephone_init(telephone_ep, name, caller_id, OSMO_CC_LOCATION_USER, early_audio, audiodev, samplerate, samplerate * latency / 1000);
+ if (rc) {
+ PDEBUG(DTEL, DEBUG_ERROR, "Endpoint initializing failed!\n");
+ goto error;
+ }
+
+ rc = osmo_cc_new(&telephone_ep->cc_ep, OSMO_CC_VERSION, name, OSMO_CC_LOCATION_USER, cc_message, NULL, telephone_ep, cc_argc, cc_argv);
+ if (rc < 0)
+ goto error;
+
+ /* real time priority */
+ if (rt_prio > 0) {
+ struct sched_param schedp;
+ int rc;
+
+ memset(&schedp, 0, sizeof(schedp));
+ schedp.sched_priority = rt_prio;
+ rc = sched_setscheduler(0, SCHED_RR, &schedp);
+ if (rc)
+ fprintf(stderr, "Error setting SCHED_RR with prio %d\n", rt_prio);
+ }
+
+ /* prepare terminal */
+ tcgetattr(0, &term_orig);
+ term = term_orig;
+ term.c_lflag &= ~(ISIG|ICANON|ECHO);
+ term.c_cc[VMIN]=1;
+ term.c_cc[VTIME]=2;
+ tcsetattr(0, TCSANOW, &term);
+
+ signal(SIGINT, sighandler);
+ signal(SIGHUP, sighandler);
+ signal(SIGTERM, sighandler);
+ signal(SIGPIPE, sighandler);
+
+ while (!quit) {
+ int w;
+ c = get_char();
+ if (c == 3) {
+ /* quit */
+ if (clear_console_text)
+ clear_console_text();
+ printf("CTRL+c received, quitting!\n");
+ quit = 1;
+ continue;
+ }
+ process_timer();
+ alsa_work(telephone_ep);
+ rtp_work(telephone_ep);
+ do {
+ w = 0;
+ w |= osmo_cc_handle();
+ w |= ui_work(telephone_ep, c);
+ c = 0;
+ } while (w);
+ usleep(1000);
+ }
+
+ signal(SIGINT, SIG_DFL);
+ signal(SIGTSTP, SIG_DFL);
+ signal(SIGHUP, SIG_DFL);
+ signal(SIGTERM, SIG_DFL);
+ signal(SIGPIPE, SIG_DFL);
+
+ /* reset terminal */
+ tcsetattr(0, TCSANOW, &term_orig);
+
+ /* reset real time prio */
+ if (rt_prio > 0) {
+ struct sched_param schedp;
+
+ memset(&schedp, 0, sizeof(schedp));
+ schedp.sched_priority = 0;
+ sched_setscheduler(0, SCHED_OTHER, &schedp);
+ }
+
+error:
+ if (telephone_ep) {
+ osmo_cc_delete(&telephone_ep->cc_ep);
+ telephone_destroy(telephone_ep);
+ }
+
+ options_free();
+
+ return 0;
+}
+
diff --git a/src/telephone/telephone.c b/src/telephone/telephone.c
new file mode 100644
index 0000000..343efad
--- /dev/null
+++ b/src/telephone/telephone.c
@@ -0,0 +1,849 @@
+/* console handling
+ *
+ * (C) 2020 by Andreas Eversberg <jolly@eversberg.eu>
+ * All Rights Reserved
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <stddef.h>
+#include "../libdebug/debug.h"
+#include "../libsample/sample.h"
+#include "../libg711/g711.h"
+#include "../libsound/sound.h"
+#include "telephone.h"
+#include "../libosmocc/helper.h"
+
+static struct osmo_cc_helper_audio_codecs codecs[] = {
+ { "PCMA", 8000, 1, g711_encode_alaw, g711_decode_alaw },
+ { "PCMU", 8000, 1, g711_encode_ulaw, g711_decode_ulaw },
+ { NULL, 0, 0, NULL, NULL},
+};
+
+const char *cause_name(int cause)
+{
+ static char cause_str[16];
+
+ switch (cause) {
+ case OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR:
+ return "hangup";
+ case OSMO_CC_ISDN_CAUSE_USER_BUSY:
+ return "busy";
+ case OSMO_CC_ISDN_CAUSE_USER_ALERTING_NA:
+ return "no-answer";
+ case OSMO_CC_ISDN_CAUSE_DEST_OOO:
+ case OSMO_CC_ISDN_CAUSE_NETWORK_OOO:
+ return "out-of-order";
+ case OSMO_CC_ISDN_CAUSE_INV_NR_FORMAT:
+ return "invalid-number";
+ case OSMO_CC_ISDN_CAUSE_NO_CIRCUIT_CHAN:
+ return "no-channel";
+ case OSMO_CC_ISDN_CAUSE_TEMP_FAILURE:
+ return "link-failure";
+ case OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL:
+ return "resource-unavail";
+ default:
+ sprintf(cause_str, "cause=%d", cause);
+ return cause_str;
+ }
+
+}
+
+static char ui_text[256];
+static char ui_clear[256];
+static int ui_autoalert;
+static int ui_autoanswer;
+static int ui_len = 0;
+#define UI_MAX_DIGITS 33
+static char ui_remote_id[256] = ""; /* what we dial or who called us */
+static char ui_remote_dialing[256] = ""; /* what remote called us with */
+static char ui_local_id[256] = ""; /* our ID */
+static const char ui_digits[] = "0123456789*#ABCDE";
+static uint8_t ui_cause = 0;
+
+static void append_string(char *string, size_t size, char c)
+{
+ size_t len = strlen(string);
+ int i;
+
+ /* string full */
+ if (len == size - 1)
+ return;
+
+ for (i = 0; i < (int)strlen(ui_digits); i++) {
+ if (c == ui_digits[i]) {
+ string[len] = c;
+ string[len + 1] = '\0';
+ break;
+ }
+ }
+}
+
+/*
+ * Endpoint instance
+ */
+
+/* create interface instance */
+telephone_t *telephone_create(void)
+{
+ telephone_t *telephone_ep;
+
+ telephone_ep = calloc(1, sizeof(*telephone_ep));
+ if (!telephone_ep) {
+ PDEBUG(DTEL, DEBUG_ERROR, "No memory!\n");
+ abort();
+ }
+
+ PDEBUG(DTEL, DEBUG_DEBUG, "Telephone instance created\n");
+
+ return telephone_ep;
+}
+
+static void telephone_close(telephone_t *telephone_ep);
+static void call_destroy(call_t *call);
+
+/* destroy interface instance and free all resource */
+void telephone_destroy(telephone_t *telephone_ep)
+{
+ /* remove stack instance */
+ telephone_close(telephone_ep);
+
+ /* destroy all calls */
+ while (telephone_ep->call_list)
+ call_destroy(telephone_ep->call_list);
+
+ free((char *)telephone_ep->name);
+
+ free(telephone_ep);
+
+ PDEBUG(DTEL, DEBUG_DEBUG, "Telephone instance destroyed\n");
+}
+
+/* initialization and configuration of interface instance */
+int telephone_init(telephone_t *telephone_ep, const char *name, const char *callerid, uint8_t serving_location, int early_audio, const char *audiodev, int samplerate, int __attribute__((unused)) latspl)
+{
+ telephone_ep->name = strdup(name);
+ telephone_ep->serving_location = serving_location;
+ telephone_ep->early_audio = early_audio;
+ telephone_ep->samplerate = samplerate;
+ telephone_ep->latspl = latspl;
+ telephone_ep->loopback = 0;
+ strcpy(ui_local_id, callerid);
+
+ if (audiodev) {
+#ifdef HAVE_ALSA
+ /* open sound device for call control */
+ /* use factor 1.4 of speech level for complete range of sound card */
+ telephone_ep->sound = sound_open(audiodev, NULL, NULL, NULL, 1, 0.0, samplerate, latspl, 1 / (SPEECH_LEVEL * 0.7079), 4000.0, 2.0);
+ if (!telephone_ep->sound) {
+ PDEBUG(DTEL, DEBUG_ERROR, "No sound device!\n");
+ return -EIO;
+ }
+ sound_start(telephone_ep->sound);
+#else
+ PDEBUG(DTEL, DEBUG_ERROR, "No sound card support compiled in!\n");
+ return -ENOTSUP;
+#endif
+ }
+
+ return 0;
+}
+
+static void telephone_close(telephone_t *telephone_ep)
+{
+ if (telephone_ep->sound) {
+ sound_close(telephone_ep->sound);
+ telephone_ep->sound = NULL;
+ }
+}
+
+/*
+ * call instance
+ */
+
+static const char *call_state_name[] = {
+ "on hook",
+ "incoming setup",
+ "outgoing setup",
+ "incoming overlap",
+ "outgoing overlap",
+ "incoming proceeding",
+ "outgoing proceeding",
+ "incoming alerting",
+ "outgoing alerting",
+ "connected",
+ "incoming disconect",
+ "outgoing disconect",
+};
+
+static void call_new_state(call_t *call, enum call_state state)
+{
+ PDEBUG(DTEL, DEBUG_DEBUG, "Call state '%s' -> '%s'\n", call_state_name[call->state], call_state_name[state]);
+ call->state = state;
+}
+
+static call_t *call_create(telephone_t *telephone_ep)
+{
+ call_t *call, **call_p;
+ int rc;
+
+ call = calloc(1, sizeof(*call));
+ if (!call) {
+ PDEBUG(DTEL, DEBUG_ERROR, "No memory!\n");
+ abort();
+ }
+
+ call_p = &telephone_ep->call_list;
+ while (*call_p)
+ call_p = &((*call_p)->next);
+ *call_p = call;
+
+ call->telephone_ep = telephone_ep;
+
+ /* init sample rate conversion */
+ rc = init_samplerate(&call->srstate, 8000.0, (double)telephone_ep->samplerate, 3300.0);
+ if (rc < 0)
+ abort();
+
+ /* allocate jitter buffer */
+ rc = jitter_create(&call->dejitter, telephone_ep->samplerate / 10); // FIXME: size
+ if (rc < 0)
+ abort();
+
+ PDEBUG(DTEL, DEBUG_DEBUG, "Created new call instance\n");
+
+ return call;
+}
+
+static void call_destroy(call_t *call)
+{
+ call_t **call_p;
+
+ /* free sdp */
+ free((char *)call->sdp);
+
+ /* free jitter buffer */
+ jitter_destroy(&call->dejitter);
+
+ /* free session description */
+ if (call->cc_session)
+ osmo_cc_free_session(call->cc_session);
+
+ /* detach */
+ call_p = &call->telephone_ep->call_list;
+ while (*call_p) {
+ if (*call_p == call)
+ break;
+ call_p = &((*call_p)->next);
+ }
+ *call_p = call->next;
+
+ free(call);
+
+ PDEBUG(DTEL, DEBUG_DEBUG, "destroyed call instance\n");
+}
+
+/*
+ * audio handling
+ */
+
+/* take audio from CC and store in jitter buffer */
+void down_audio(struct osmo_cc_session_codec *codec, uint16_t __attribute__((unused)) sequence_number, uint32_t __attribute__((unused)) timestamp, uint8_t *data, int len)
+{
+ call_t *call = codec->media->session->priv;
+ int count = len / 2;
+ sample_t samples[count];
+
+ if (call->telephone_ep->loopback != 3) {
+ sample_t up[(int)((double)count * call->srstate.factor + 0.5) + 10];
+ int16_to_samples(samples, (int16_t *)data, count);
+ count = samplerate_upsample(&call->srstate, samples, count, up);
+ jitter_save(&call->dejitter, up, count);
+ }
+}
+
+void alsa_work(telephone_t *telephone_ep)
+{
+ if (!telephone_ep->sound)
+ return;
+
+#ifdef HAVE_ALSA
+ /* handle audio, if sound device is used */
+ call_t *call;
+ sample_t samples[telephone_ep->latspl + 10], *samples_list[1];
+ uint8_t *power_list[1];
+ int count;
+ int rc;
+
+ /* hunt for call */
+ for (call = telephone_ep->call_list; call; call = call->next)
+ break; // just any call
+
+ count = sound_get_tosend(telephone_ep->sound, telephone_ep->latspl);
+ if (count < 0) {
+ PDEBUG(DTEL, DEBUG_ERROR, "Failed to get samples in buffer (rc = %d)!\n", count);
+ if (count == -EPIPE)
+ PDEBUG(DTEL, DEBUG_ERROR, "Trying to recover.\n");
+ return;
+ }
+ if (count > 0) {
+ if (call)
+ jitter_load(&call->dejitter, samples, count);
+ else
+ memset(samples, 0, sizeof(*samples) * count);
+ samples_list[0] = samples;
+ power_list[0] = NULL;
+ rc = sound_write(telephone_ep->sound, samples_list, power_list, count, NULL, NULL, 1);
+ if (rc < 0) {
+ PDEBUG(DTEL, DEBUG_ERROR, "Failed to write TX data to sound device (rc = %d)\n", rc);
+ if (rc == -EPIPE)
+ PDEBUG(DTEL, DEBUG_ERROR, "Trying to recover.\n");
+ return;
+ }
+ }
+ samples_list[0] = samples;
+ count = sound_read(telephone_ep->sound, samples_list, telephone_ep->latspl, 1, NULL);
+ if (count < 0) {
+ PDEBUG(DTEL, DEBUG_ERROR, "Failed to read from sound device (rc = %d)!\n", count);
+ if (count == -EPIPE)
+ PDEBUG(DTEL, DEBUG_ERROR, "Trying to recover.\n");
+ return;
+ }
+ if (call && count) {
+ int i;
+
+ if (telephone_ep->loopback == 3)
+ jitter_save(&call->dejitter, samples, count);
+ count = samplerate_downsample(&call->srstate, samples, count);
+ /* put samples into ring buffer */
+ for (i = 0; i < count; i++) {
+ call->tx_buffer[call->tx_buffer_pos] = samples[i];
+ /* if ring buffer wrapps, deliver data down to call process */
+ if (++call->tx_buffer_pos == 160) {
+ call->tx_buffer_pos = 0;
+ /* only if we have a call */
+ if (call->cc_callref && call->codec) {
+ int16_t data[160];
+ samples_to_int16(data, call->tx_buffer, 160);
+ osmo_cc_rtp_send(call->codec, (uint8_t *)data, 160 * 2, 1, 160);
+ }
+ }
+ }
+ }
+
+//printf("%p, %p\n", call, call ? call->codec : NULL);
+// if (call && call->codec)
+// while (osmo_cc_rtp_receive(call->codec->media) >= 0);
+#endif
+}
+
+void rtp_work(telephone_t *telephone_ep)
+{
+ call_t *call;
+
+ call = telephone_ep->call_list;
+ while (call) {
+ if (call->cc_session)
+ osmo_cc_session_handle(call->cc_session);
+ call = call->next;
+ }
+}
+
+
+/*
+ * handle message from CC
+ */
+
+static void release_reject_ind(call_t *call, uint8_t msg_type, uint8_t isdn_cause)
+{
+ osmo_cc_msg_t *new_msg;
+
+ /* create osmo-cc message */
+ new_msg = osmo_cc_new_msg(msg_type);
+ /* cause */
+ osmo_cc_add_ie_cause(new_msg, call->telephone_ep->serving_location, isdn_cause, 0, 0);
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_IDLE);
+ /* destroy call */
+ call_destroy(call);
+}
+
+void cc_message(osmo_cc_endpoint_t *ep, uint32_t callref, osmo_cc_msg_t *msg)
+{
+ telephone_t *telephone_ep = ep->priv;
+ call_t *call, *other;
+ osmo_cc_msg_t *new_msg;
+ uint8_t type, plan, present, screen;
+ char callerid[128], dialing[128];
+ uint8_t coding, location, progress, socket_cause;
+ uint16_t sip_cause;
+ int rc;
+ int i;
+
+ /* hunt for callref */
+ call = telephone_ep->call_list;
+ while (call) {
+ if (call->cc_callref == callref)
+ break;
+ call = call->next;
+ }
+
+ /* process SETUP */
+ if (!call) {
+ if (msg->type != OSMO_CC_MSG_SETUP_REQ) {
+ PDEBUG(DTEL, DEBUG_ERROR, "received message without call instance, please fix!\n");
+ goto done;
+ }
+ /* creating call instance */
+ call = call_create(telephone_ep);
+ if (!call) {
+ PDEBUG(DTEL, DEBUG_ERROR, "Cannot create calll instance.\n");
+ abort();
+ }
+ /* link with cc */
+ call->cc_callref = callref;
+ }
+
+ switch (msg->type) {
+ case OSMO_CC_MSG_SETUP_REQ: /* dial-out command received from epoint */
+ /* hunt for call */
+ for (other = telephone_ep->call_list; other; other = other->next) {
+ if (other != call)
+ break;
+ }
+ if (other) {
+ release_reject_ind(call, OSMO_CC_MSG_REJ_IND, OSMO_CC_ISDN_CAUSE_USER_BUSY);
+ break;
+ }
+ /* calling */
+ rc = osmo_cc_get_ie_calling(msg, 0, &type, &plan, &present, &screen, callerid, sizeof(callerid));
+ if (rc >= 0)
+ strncpy(ui_remote_id, callerid, sizeof(ui_remote_id) - 1);
+ /* called */
+ rc = osmo_cc_get_ie_called(msg, 0, &type, &plan, dialing, sizeof(dialing));
+ if (rc >= 0)
+ strncpy(ui_remote_dialing, dialing, sizeof(ui_remote_dialing) - 1);
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call from '%s' to '%s'\n", ui_remote_id, ui_remote_dialing);
+ /* sdp accept */
+ call->sdp = osmo_cc_helper_audio_accept(call, codecs, down_audio, msg, &call->cc_session, &call->codec, 0);
+ if (!call->sdp) {
+ release_reject_ind(call, OSMO_CC_MSG_REJ_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
+ break;
+ }
+ call->sdp = strdup(call->sdp);
+ /* change state */
+ call_new_state(call, CALL_STATE_IN_SETUP);
+ if (ui_autoanswer) {
+ /* create osmo-cc message */
+ new_msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_CNF);
+ /* sdp */
+ osmo_cc_add_ie_sdp(new_msg, call->sdp);
+ free((char *)call->sdp);
+ call->sdp = NULL;
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_CONNECT);
+ } else
+ if (ui_autoalert) {
+ /* create osmo-cc message */
+ new_msg = osmo_cc_new_msg(OSMO_CC_MSG_ALERT_IND);
+ /* sdp */
+ osmo_cc_add_ie_sdp(new_msg, call->sdp);
+ free((char *)call->sdp);
+ call->sdp = NULL;
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_IN_ALERTING);
+ }
+ break;
+ case OSMO_CC_MSG_SETUP_ACK_REQ: /* more information is needed */
+ rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
+ if (rc < 0) {
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
+ break;
+ }
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call acknowledged\n");
+ /* change state */
+ call_new_state(call, CALL_STATE_OUT_OVERLAP);
+ break;
+ case OSMO_CC_MSG_PROC_REQ: /* call of endpoint is proceeding */
+ rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
+ if (rc < 0) {
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
+ break;
+ }
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call proceeding\n");
+ /* change state */
+ call_new_state(call, CALL_STATE_OUT_PROCEEDING);
+ break;
+ case OSMO_CC_MSG_ALERT_REQ: /* call of endpoint is ringing */
+ rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
+ if (rc < 0) {
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
+ break;
+ }
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call alerting\n");
+ /* change state */
+ call_new_state(call, CALL_STATE_OUT_ALERTING);
+ break;
+ case OSMO_CC_MSG_SETUP_RSP: /* call of endpoint is connected */
+ rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
+ if (rc < 0) {
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
+ break;
+ }
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call acknowledged\n");
+ /* create osmo-cc message */
+ new_msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_COMP_IND);
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_CONNECT);
+ break;
+ case OSMO_CC_MSG_SETUP_COMP_REQ: /* call of endpoint is connected */
+ break;
+ case OSMO_CC_MSG_INFO_REQ: /* overlap dialing */
+ rc = osmo_cc_get_ie_called(msg, 0, &type, &plan, dialing, sizeof(dialing));
+ if (rc < 0)
+ dialing[0] = '\0';
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call received additional dialing '%s'\n", dialing);
+ for (i = 0; dialing[i]; i++) {
+ /* add to dial string */
+ append_string(ui_remote_dialing, sizeof(ui_remote_dialing), dialing[i]);
+ }
+ break;
+ case OSMO_CC_MSG_PROGRESS_REQ: /* progress */
+ rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
+ if (rc < 0) {
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
+ break;
+ }
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call received progress\n");
+ break;
+ case OSMO_CC_MSG_NOTIFY_REQ: /* display and notifications */
+ break;
+ case OSMO_CC_MSG_REJ_REQ: /* call has been rejected */
+ /* get cause */
+ rc = osmo_cc_get_ie_cause(msg, 0, &location, &ui_cause, &sip_cause, &socket_cause);
+ if (rc < 0)
+ ui_cause = OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR;
+ else
+ PDEBUG(DTEL, DEBUG_INFO, "Incoming call rejected: ISDN cause = %d, SIP cause = %d, socket cause = %d\n", ui_cause, sip_cause, socket_cause);
+ /* change state */
+ call_new_state(call, CALL_STATE_IDLE);
+ /* destroy call */
+ call_destroy(call);
+ break;
+ case OSMO_CC_MSG_DISC_REQ: /* call has been disconnected */
+ rc = osmo_cc_helper_audio_negotiate(msg, &call->cc_session, &call->codec);
+ if (rc < 0) {
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_RESOURCE_UNAVAIL);
+ break;
+ }
+ /* get cause */
+ rc = osmo_cc_get_ie_cause(msg, 0, &location, &ui_cause, &sip_cause, &socket_cause);
+ if (rc < 0)
+ ui_cause = OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR;
+ else
+ PDEBUG(DTEL, DEBUG_INFO, "Disconnect by remote: ISDN cause = %d, SIP cause = %d, socket cause = %d\n", ui_cause, sip_cause, socket_cause);
+ /* change state */
+ call_new_state(call, CALL_STATE_IN_DISCONNECT);
+ /* progress indicator */
+ rc = osmo_cc_get_ie_progress(msg, 0, &coding, &location, &progress);
+ if (rc < 0 || coding != OSMO_CC_CODING_ITU_T || !(progress == 1 || progress == 8)) {
+ PDEBUG(DTEL, DEBUG_INFO, "no audio after disconnect, releasing!\n");
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, ui_cause);
+ }
+
+ break;
+ case OSMO_CC_MSG_REL_REQ: /* release call */
+ /* get cause */
+ rc = osmo_cc_get_ie_cause(msg, 0, &location, &ui_cause, &sip_cause, &socket_cause);
+ if (rc < 0)
+ ui_cause = OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR;
+ else
+ PDEBUG(DTEL, DEBUG_INFO, "Disconnect by remote: ISDN cause = %d, SIP cause = %d, socket cause = %d\n", ui_cause, sip_cause, socket_cause);
+ /* create osmo-cc message */
+ new_msg = osmo_cc_new_msg(OSMO_CC_MSG_REL_CNF);
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, new_msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_IDLE);
+ /* destroy call */
+ call_destroy(call);
+ break;
+ default:
+ PDEBUG(DTEL, DEBUG_ERROR, "received an unsupported CC message: %d\n", msg->type);
+ }
+
+done:
+ osmo_cc_free_msg(msg);
+}
+
+/*
+ * user interface
+ */
+
+
+static void _clear_ui_text(void)
+{
+ if (!ui_len)
+ return;
+
+ fwrite(ui_clear, ui_len, 1, stdout);
+ // note: fflused by user of this function
+ ui_len = 0;
+}
+
+static void _print_ui_text(void)
+{
+ if (!ui_len)
+ return;
+
+ printf("\033[1;37m");
+ fwrite(ui_text, ui_len, 1, stdout);
+ printf("\033[0;39m");
+}
+
+int ui_init(const char *remote_id, int autoalert, int autoanswer)
+{
+ clear_console_text = _clear_ui_text;
+ print_console_text = _print_ui_text;
+ ui_autoalert = autoalert;
+ ui_autoanswer = autoanswer;
+
+ strncpy(ui_remote_id, remote_id, sizeof(ui_remote_id) - 1);
+ ui_remote_id[sizeof(ui_remote_id) - 1] = '\0';
+
+ return 0;
+}
+
+int ui_work(telephone_t *telephone_ep, int c)
+{
+ int work = (c > 0) ? 1 : 0;
+ call_t *call;
+ osmo_cc_msg_t *msg;
+ char text[1024] = "";
+ char display[UI_MAX_DIGITS + 1];
+ int len;
+
+ /* hunt for call */
+ for (call = telephone_ep->call_list; call; call = call->next)
+ break; // just any call
+ if (!call) {
+ if (c > 0) {
+ append_string(ui_remote_id, sizeof(ui_remote_id), c);
+ if ((c == 8 || c == 127) && strlen(ui_remote_id))
+ ui_remote_id[strlen(ui_remote_id) - 1] = '\0';
+dial_after_hangup:
+ if (c == 'd') {
+ PDEBUG(DTEL, DEBUG_INFO, "Outgoing call from '%s' to '%s'\n", ui_local_id, ui_remote_id);
+ ui_remote_dialing[0] = '\0';
+ /* creating call instance */
+ call = call_create(telephone_ep);
+ if (!call) {
+ PDEBUG(DTEL, DEBUG_ERROR, "Cannot create calll instance.\n");
+ abort();
+ }
+
+ /* setup message */
+ msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_IND);
+ /* network type */
+ osmo_cc_add_ie_calling_network(msg, OSMO_CC_NETWORK_ALSA_NONE, "");
+ /* calling number */
+ osmo_cc_add_ie_calling(msg, OSMO_CC_TYPE_UNKNOWN, OSMO_CC_PLAN_TELEPHONY, OSMO_CC_PRESENT_ALLOWED, OSMO_CC_SCREEN_USER_UNSCREENED, ui_local_id);
+ /* called number */
+ osmo_cc_add_ie_called(msg, OSMO_CC_TYPE_UNKNOWN, OSMO_CC_PLAN_TELEPHONY, ui_remote_id);
+ /* bearer capability */
+ osmo_cc_add_ie_bearer(msg, OSMO_CC_CODING_ITU_T, OSMO_CC_CAPABILITY_AUDIO, OSMO_CC_MODE_CIRCUIT);
+ /* sdp offer */
+ call->cc_session = osmo_cc_helper_audio_offer(call, codecs, down_audio, msg, 1);
+ if (!call->cc_session) {
+ PDEBUG(DTEL, DEBUG_NOTICE, "Failed to offer audio, call aborted.\n");
+ osmo_cc_free_msg(msg);
+ call_destroy(call);
+ } else {
+ /* create new call */
+ osmo_cc_call_t *cc_call = osmo_cc_call_new(&call->telephone_ep->cc_ep);
+ call->cc_callref = cc_call->callref;
+ /* send message to CC */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_OUT_SETUP);
+ }
+ }
+ }
+ memset(display, '.', UI_MAX_DIGITS);
+ memcpy(display, ui_remote_id, strlen(ui_remote_id));
+ display[UI_MAX_DIGITS] = '\0';
+ sprintf(text, "%s: %s (press digits 0..9 or d=dial)\r", call_state_name[(call) ? call->state : CALL_STATE_IDLE], display);
+ goto done;
+ }
+
+ if (c == 'h' || (c == 'd' && call->state == CALL_STATE_IN_DISCONNECT)) {
+ PDEBUG(DTEL, DEBUG_INFO, "Call hangup\n");
+ /* create osmo-cc message */
+ if (call->state == CALL_STATE_IN_SETUP)
+ release_reject_ind(call, OSMO_CC_MSG_REJ_IND, OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR);
+ else
+ release_reject_ind(call, OSMO_CC_MSG_REL_IND, OSMO_CC_ISDN_CAUSE_NORM_CALL_CLEAR);
+ /* dial new number */
+ if (c == 'd') {
+ call = NULL;
+ goto dial_after_hangup;
+ }
+ goto done;
+ }
+ if (c == 'o' && call->state == CALL_STATE_IN_SETUP) {
+ PDEBUG(DTEL, DEBUG_INFO, "Acknowledge incoming call\n");
+ /* create osmo-cc message */
+ msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_ACK_IND);
+ if (call->telephone_ep->early_audio && call->sdp) {
+ /* progress */
+ osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
+ /* sdp */
+ osmo_cc_add_ie_sdp(msg, call->sdp);
+ free((char *)call->sdp);
+ call->sdp = NULL;
+ }
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_IN_OVERLAP);
+ }
+ if (c == 'p' && (call->state == CALL_STATE_IN_SETUP || call->state == CALL_STATE_IN_OVERLAP)) {
+ PDEBUG(DTEL, DEBUG_INFO, "Proceeding incoming call\n");
+ /* create osmo-cc message */
+ msg = osmo_cc_new_msg(OSMO_CC_MSG_PROC_IND);
+ if (call->telephone_ep->early_audio && call->sdp) {
+ /* progress */
+ osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
+ /* sdp */
+ osmo_cc_add_ie_sdp(msg, call->sdp);
+ free((char *)call->sdp);
+ call->sdp = NULL;
+ }
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_IN_PROCEEDING);
+ }
+ if (c == 'a' && (call->state == CALL_STATE_IN_SETUP || call->state == CALL_STATE_IN_OVERLAP || call->state == CALL_STATE_IN_PROCEEDING)) {
+ PDEBUG(DTEL, DEBUG_INFO, "Alerting incoming call\n");
+ /* create osmo-cc message */
+ msg = osmo_cc_new_msg(OSMO_CC_MSG_ALERT_IND);
+ if (call->telephone_ep->early_audio && call->sdp) {
+ /* progress */
+ osmo_cc_add_ie_progress(msg, OSMO_CC_CODING_ITU_T, call->telephone_ep->serving_location, OSMO_CC_PROGRESS_INBAND_INFO_AVAILABLE);
+ /* sdp */
+ osmo_cc_add_ie_sdp(msg, call->sdp);
+ free((char *)call->sdp);
+ call->sdp = NULL;
+ }
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_IN_ALERTING);
+ }
+ if (c == 'c' && (call->state == CALL_STATE_IN_SETUP || call->state == CALL_STATE_IN_OVERLAP || call->state == CALL_STATE_IN_PROCEEDING || call->state == CALL_STATE_IN_ALERTING)) {
+ PDEBUG(DTEL, DEBUG_INFO, "Answer incoming call\n");
+ /* create osmo-cc message */
+ msg = osmo_cc_new_msg(OSMO_CC_MSG_SETUP_CNF);
+ /* sdp */
+ if (call->sdp) {
+ osmo_cc_add_ie_sdp(msg, call->sdp);
+ free((char *)call->sdp);
+ call->sdp = NULL;
+ }
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
+ /* change state */
+ call_new_state(call, CALL_STATE_CONNECT);
+ }
+ if (c > 0 && call->state == CALL_STATE_OUT_OVERLAP && strchr(ui_digits, c)) {
+ PDEBUG(DTEL, DEBUG_INFO, "Send digit %c\n", c);
+ char called[] = { c, '\0' };
+ /* create osmo-cc message */
+ msg = osmo_cc_new_msg(OSMO_CC_MSG_INFO_IND);
+ /* add dialing */
+ osmo_cc_add_ie_called(msg, OSMO_CC_TYPE_UNKNOWN, OSMO_CC_PLAN_TELEPHONY, called);
+ /* send message to osmo-cc */
+ osmo_cc_ll_msg(&call->telephone_ep->cc_ep, call->cc_callref, msg);
+ /* add to dial string */
+ append_string(ui_remote_id, sizeof(ui_remote_id), c);
+ }
+
+ /* NOTE: the state is from console view: a call towards CC is OUT and from CC is IN */
+ switch (call->state) {
+ case CALL_STATE_OUT_SETUP:
+ case CALL_STATE_OUT_PROCEEDING:
+ case CALL_STATE_OUT_ALERTING:
+ sprintf(text, "%s: %s (press h=hangup)\r", call_state_name[call->state], ui_remote_id);
+ break;
+ case CALL_STATE_OUT_OVERLAP:
+ sprintf(text, "%s: %s (press digits 0..9 h=hangup)\r", call_state_name[call->state], ui_remote_id);
+ break;
+ case CALL_STATE_CONNECT:
+ if (ui_remote_dialing[0])
+ sprintf(text, "%s: %s->%s (press h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
+ else
+ sprintf(text, "%s: %s (press h=hangup)\r", call_state_name[call->state], ui_remote_id);
+ break;
+ case CALL_STATE_IN_DISCONNECT:
+ sprintf(text, "%s: %s (press h=hangup d=redial)\r", call_state_name[call->state], cause_name(ui_cause));
+ break;
+ case CALL_STATE_IN_SETUP:
+ sprintf(text, "%s: %s->%s (press o=overlap p=proceeding a=alerting c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
+ break;
+ case CALL_STATE_IN_OVERLAP:
+ sprintf(text, "%s: %s->%s (press p=proceeding a=alerting c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
+ break;
+ case CALL_STATE_IN_PROCEEDING:
+ sprintf(text, "%s: %s->%s (press a=alerting c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
+ break;
+ case CALL_STATE_IN_ALERTING:
+ sprintf(text, "%s: %s->%s (press c=connect h=hangup)\r", call_state_name[call->state], ui_remote_id, ui_remote_dialing);
+ break;
+ default:
+ break;
+ }
+
+done:
+ /* skip if nothing has changed */
+ len = strlen(text);
+ if (ui_len == len && !memcmp(ui_text, text, len))
+ return work;
+ clear_console_text();
+ ui_len = len;
+ memcpy(ui_text, text, len);
+ memset(ui_clear, ' ', len - 1);
+ ui_clear[len - 1] = '\r';
+ print_console_text();
+ fflush(stdout);
+
+ return work;
+}
+
diff --git a/src/telephone/telephone.h b/src/telephone/telephone.h
new file mode 100644
index 0000000..4c7dfa0
--- /dev/null
+++ b/src/telephone/telephone.h
@@ -0,0 +1,75 @@
+
+#include "../libtimer/timer.h"
+#include "../libosmocc/endpoint.h"
+#include "../libsample/sample.h"
+#include "../libsamplerate/samplerate.h"
+#include "../libjitter/jitter.h"
+
+enum call_state {
+ CALL_STATE_IDLE = 0, /* no call */
+ CALL_STATE_IN_SETUP, /* incoming connection */
+ CALL_STATE_OUT_SETUP, /* outgoing connection */
+ CALL_STATE_IN_OVERLAP, /* more informatiopn needed */
+ CALL_STATE_OUT_OVERLAP, /* more informatiopn needed */
+ CALL_STATE_IN_PROCEEDING,/* call is proceeding */
+ CALL_STATE_OUT_PROCEEDING,/* call is proceeding */
+ CALL_STATE_IN_ALERTING, /* call is ringing */
+ CALL_STATE_OUT_ALERTING,/* call is ringing */
+ CALL_STATE_CONNECT, /* call is connected and transmission is enabled */
+ CALL_STATE_IN_DISCONNECT,/* incoming disconnected */
+ CALL_STATE_OUT_DISCONNECT,/* outgoing disconnected */
+};
+
+struct telephone;
+
+struct call_list;
+
+typedef struct telephone {
+ osmo_cc_endpoint_t cc_ep;
+ struct call_list *call_list;
+ const char *name;
+
+ /* settings */
+ int serving_location; /* who we serve when sending causes towards interface */
+ int early_audio;
+
+ /* sound */
+ int loopback;
+ int samplerate; /* sample rate of headphone interface */
+ int latspl;
+ void *sound; /* headphone interface */
+
+} telephone_t;
+
+typedef struct call_list {
+ struct call_list *next;
+ telephone_t *telephone_ep;
+
+ /* alsa states */
+
+ /* osmo-cc states */
+ uint32_t cc_callref;
+ const char *sdp;
+ osmo_cc_session_t *cc_session;
+ osmo_cc_session_codec_t *codec;
+ int codec_negotiated;
+
+ /* call states */
+ enum call_state state;
+
+ /* audio states */
+ jitter_t dejitter;
+ samplerate_t srstate;
+ sample_t tx_buffer[160]; /* transmit audio buffer */
+ int tx_buffer_pos; /* current position in transmit audio buffer */
+} call_t;
+
+void telephone_destroy(telephone_t *telephone_ep);
+telephone_t *telephone_create(void);
+int telephone_init(telephone_t *telephone_ep, const char *name, const char *callerid, uint8_t serving_location, int early_audio, const char *audiodev, int samplerate, int __attribute__((unused)) latspl);
+void cc_message(osmo_cc_endpoint_t *ep, uint32_t callref, osmo_cc_msg_t *msg);
+int ui_init(const char *remote_id, int autoalert, int autoanswer);
+int ui_work(telephone_t *telephone_ep, int c);
+void alsa_work(telephone_t *telephone_ep);
+void rtp_work(telephone_t *telephone_ep);
+