IP Phones
In this lab you will have the opportunity to write a client/server ip phone. To get started, you should look at how to use the audio in Linux.
One way is to use rec (record audio to any sound file format) and play (play any sound file to audio device). Play and rec are programs that allow you to play and record different types of sound files. It is a front-end to the more general sox package.
You can make a FIFO file to use for record and play, this way you can start recording the file and read it at the same time. A FIFO special file (a named pipe) is similar to a pipe, except that it is accessed as part of the file system. It can be opened by multiple processes for reading or writing. You can create a FIFO file by using mkfifo (command to make a FIFO file).
Here is an example recPlay.cc:
This program creates a FIFO file (called temp.ub), then starts 2 threads, one to constantly record audio and write to temp.ub and another that play what is on temp.ub. In order to compile the code in Linux:
g++ recPlay.cc
–pthread
#include
<pthread.h>
#include
<stdlib.h>
#include
<stdio.h>
//g++
recPlay.cc -o myaudio -pthread
//this
thread calls a system call (with the arg as paramether)
void
*RecAndPlay(void *arg)
{
char *command;
command = (char *)arg;
system (command);
return 0;
}
int
main(){
pthread_t threadId;
pthread_attr_t attr;
int status = 0;
system ("mkfifo temp.ub"); //make a
FIFO file called temp.ub
pthread_attr_init(&attr);
status =
pthread_create(&threadId,&attr, RecAndPlay, (void *)"rec -r
44100 --volume=1000 temp.ub");
if(status)printf("***ERROR***,
pthread_create failed\n");
status =
pthread_create(&threadId,&attr, RecAndPlay, (void *)"play -r 44100 --volume=1000 temp.ub");
if(status)printf("***ERROR***,
pthread_create failed\n");
return 0;
}
You can read more about rec and play in the manual. You can read more about rec and sox and make
changes to the buffer size for more efficiency.