⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 voudpd.cpp

📁 voudp - VoIP for NetBSD
💻 CPP
字号:
//// Copyright 2004 Alan Post//// This file is part of voudp.//// voudp 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 2 of the License, or (at your option) any later// version.//// voudp 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// voudp; if not, write to the Free Software Foundation, Inc., 59 Temple Place,// Suite 330, Boston, MA  02111-1307  USA//#include "constants.h"#include "receiver.h"#include "player.h"#include "sender.h"#include "recorder.h"#include "voudpconfigd.h"#include "die.h"#include <fcntl.h>#include <errno.h>#include <string.h>#include <sys/audioio.h>#include <sys/ioctl.h>#include <sys/mman.h>#include <unistd.h>#include <stdio.h>#include <err.h>#include <signal.h>#include <pwd.h>static int my_open( const char *path, int flags ){    int fd = open( path, flags, 0 );    if ( fd < 0 ) err( EXIT_FAILURE, "Can't open %s", path );    return fd;}static void get_info( int afd, audio_info_t *info ){    AUDIO_INITINFO( info );    if ( ioctl( afd, AUDIO_GETINFO, info ) < 0 )        die( "failed to get audio info" );}static void set_info( int afd, audio_info_t *info ){    if ( ioctl( afd, AUDIO_SETINFO, info ) < 0 )        die( "failed to set audio info" );}static void *setup_audio( int afd, audio_info_t &info ){    {        int is_full_duplex = 1;        if ( -1 == ioctl( afd, AUDIO_SETFD, &is_full_duplex ))            die( "ioctl" );    }    AUDIO_INITINFO( &info );    //    // Ask for a blocksize equal to one speex frame.  We may not get it, but we    // can at least ask.    //    info.blocksize = samples_per_frame * sizeof( hw_sample_t );    info.play.channels = hw_channels;    info.play.sample_rate = samples_per_sec;    info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;    info.play.precision = 8 * sizeof( hw_sample_t ) / hw_channels;    info.record.channels = hw_channels;    info.record.sample_rate = samples_per_sec;    info.record.encoding = AUDIO_ENCODING_SLINEAR_LE;    info.record.precision = 8 * sizeof( hw_sample_t ) / hw_channels;    info.mode = AUMODE_PLAY | AUMODE_RECORD | AUMODE_PLAY_ALL;    set_info( afd, &info );    get_info( afd, &info );    void *pb = mmap( 0, info.play.buffer_size + info.record.buffer_size,                     PROT_READ|PROT_WRITE, MAP_SHARED, afd, 0 );    if ( pb == MAP_FAILED ) die( "mmap" );    return pb;}static bool done = false;void set_done( int /* sig */ ) { done = true; }static void daemonize(){    static const char *chroot_dir = "/var/chroot/voudpd";    if ( -1 == close( STDERR_FILENO )) die( "close" );    if ( -1 == open( "/var/log/voudpd", O_WRONLY | O_APPEND | O_CREAT, 0644 ))    {        //        // We print to stdout, here, because stderr is closed at the moment        //        printf( "Can't open /var/log/voudpd: %s\n", strerror( errno ));        exit( EXIT_FAILURE );    }    if ( -1 == close( STDIN_FILENO )) die( "close" );    if ( -1 == close( STDOUT_FILENO )) die( "close" );    if ( -1 == daemon( 0, 1 )) die( "daemon" );    //    // Do the getpwnam *before* chrooting -- otherwise, we can't see the passwd    // database.    //    passwd *entry = getpwnam( "voudpd" );    if ( entry == NULL ) die( "getpwnam" );    if ( -1 == chroot( chroot_dir )) die( "chroot" );    if ( -1 == setgid( entry->pw_gid )) die( "setgid" );    if ( -1 == setuid( entry->pw_uid )) die( "setuid" );}int main( int argc, char **argv ){    setbuf( stdout, NULL );    setbuf( stderr, NULL );    //    // If we are killed, make sure we exit in an orderly fashion, so we can    // clean up after ourselves.    //    signal( SIGTERM, &set_done );    signal( SIGINT, &set_done );    voudp_configd_t configd;    int listen_fd = configd.get_listen_fd();    Sender sender;    int afd = my_open( "/dev/audio", O_RDWR );    audio_info_t info;    void *pb = setup_audio( afd, info );    Receiver receiver(                 atoi( argv[1]),        (size_t) (  max( samples_per_frame, info.blocksize / sizeof( hw_sample_t ))                  * 1000 * 3 / 2 / samples_per_sec ),        (size_t) atol( argv[2]));    int recv_fd = receiver.get_sock_fd();    Player player( (u_int8_t*)pb, afd, info.blocksize, info.play.buffer_size );    Recorder recorder( ((u_int8_t*)pb) + info.play.buffer_size, afd,                       info.blocksize, info.record.buffer_size );    daemonize();    fprintf( stderr, "voudpd started\n" );    fd_set readables;    FD_ZERO( &readables );    while ( ! done )    {        FD_SET( recv_fd, &readables );        FD_SET( listen_fd, &readables );        struct timeval wait = { 0, 1000 };        int nready = select( 1 + max( recv_fd, listen_fd ),                             &readables, NULL, NULL, &wait );        if ( nready == -1 )        {            if ( errno == EINTR ) continue;            die( "select" );        }        if ( FD_ISSET( recv_fd, &readables )) receiver.recv_packet();        if ( FD_ISSET( listen_fd, &readables ))            configd.do_config_req( sender, receiver );        //        // If we actually record a block, then we will do some speex encoding,        // which is slow.  Check with the player both before *and* after the        // recorder, to make sure it gets a chance.        //        // This is necessary on my P2 Celeron 433 when using a 20 ms blocksize.        //        player.play_if_needed( receiver );        recorder.record_if_needed( sender );        player.play_if_needed( receiver );    }    fprintf( stderr, "voudpd exiting\n" );    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -