rtprecv.c

 /*
  The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack..
  Copyright (C) 2001  Simon MORLAT [email protected]

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#include
#include
#include
#ifndef _WIN32
#include
#include
#include
#endif
#include
#include
#include
#include
#define LENGTH 1    /* 存储秒数 */
#define RATE 8000   /* 采样频率 */
#define SIZE 8      /* 量化位数 */
#define CHANNELS 1 /* 声道数目 */

// 用于保存数字音频数据的内存    recvs[MAXSIZE]缓冲区  8000bytes
unsigned char sound_buf[LENGTH*RATE*SIZE*CHANNELS/400];

int cond=1;

void stop_handler(int signum)
{
    cond=0;
}

void ssrc_cb(RtpSession *session)
{
    printf("hey, the ssrc has changed !\n");
}

static char *help="usage: rtprecv loc_port [--format format] [--soundcard] [--noadapt] [--with-jitter ]\n";

#define MULAW 0
#define ALAW 1
#if 0
#if defined(__hpux) && HAVE_SYS_AUDIO_H

#include

int sound_init(int format)
{
    int fd;
    fd=open("/dev/audio",O_WRONLY);
    if (fd<0){
        perror("Can't open /dev/audio");
        return -1;
    }
    ioctl(fd,AUDIO_RESET,0);
    ioctl(fd,AUDIO_SET_SAMPLE_RATE,8000);
    ioctl(fd,AUDIO_SET_CHANNELS,1);
    if (format==MULAW)
        ioctl(fd,AUDIO_SET_DATA_FORMAT,AUDIO_FORMAT_ULAW);
    else ioctl(fd,AUDIO_SET_DATA_FORMAT,AUDIO_FORMAT_ALAW);
    return fd;    
}
#else
int sound_init(int format)
{
    return -1;
}
#endif
#endif
int main(int argc, char*argv[])
{
    RtpSession *session;
    unsigned char buffer[160];
    int err;
    uint32_t ts=0;
    int stream_received=0;
    FILE *outfile;
    int local_port;
    int have_more;
    int i;
    int format=0;
    int soundcard=0;
    int sound_fd=0;
    int jittcomp=40;
    bool_t adapt=TRUE;
    int     ret;
    /* init the lib */
    if (argc<2){
        printf(help);
        return -1;
    }
    local_port=atoi(argv[1]);
    if (local_port<=0) {
        printf(help);
        return -1;
    }
    for (i=2;i     {
        if (strcmp(argv[i],"--noadapt")==0) adapt=FALSE;
        if (strcmp(argv[i],"--format")==0){
            i++;
            if (i                 if (strcmp(argv[i],"mulaw")==0){
                    format=MULAW;
                }else
                if (strcmp(argv[i],"alaw")==0){
                    format=ALAW;
                }else{
                    printf("Unsupported format %s\n",argv[i]);
                    return -1;
                }
            }
        }
        else if (strcmp(argv[i],"--soundcard")==0){
            soundcard=1;
        }
        else if (strcmp(argv[i],"--with-jitter")==0){
            i++;
            if (i                 jittcomp=atoi(argv[i]);
                printf("Using a jitter buffer of %i milliseconds.\n",jittcomp);
            }
        }
    }
    #if 0
    outfile=fopen(argv[1],"wb");
    if (outfile==NULL) {
        perror("Cannot open file for writing");
        return -1;
    }
    #endif
    
    if (soundcard){
        sound_fd= get_sound();
    }
    
    ortp_init();
    ortp_scheduler_init();
    ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
    signal(SIGINT,stop_handler);
    session=rtp_session_new(RTP_SESSION_RECVONLY);    
    rtp_session_set_scheduling_mode(session,1);
    rtp_session_set_blocking_mode(session,1);
    rtp_session_set_local_addr(session,"192.168.0.42",atoi(argv[1]));
    rtp_session_set_connected_mode(session,TRUE);
    rtp_session_set_symmetric_rtp(session,TRUE);
    rtp_session_enable_adaptive_jitter_compensation(session,adapt);
    rtp_session_set_jitter_compensation(session,jittcomp);
    rtp_session_set_payload_type(session,0);
    rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)ssrc_cb,0);
    rtp_session_signal_connect(session,"ssrc_changed",(RtpCallback)rtp_session_reset,0);
    
    while(cond)
    {
        have_more=1;
        while (have_more){
            err=rtp_session_recv_with_ts(session,buffer,160,ts,&have_more);
            if (err>0) //stream_received=1;
            /* this is to avoid to write to disk some silence before the first RTP packet is returned*/
            #if 0    
            if ((stream_received) && (err>0)) {
                size_t ret = fwrite(buffer,1,err,outfile);
            #endif
                //if (sound_fd>0)
            ret = write(sound_fd,buffer,err);
            /*if (ret != err)
            perror("wrote wrong number of bytes");    */    
            //}
        }
        ts+=160;
        //ortp_message("Receiving packet.");
    }
    
    rtp_session_destroy(session);
    ortp_exit();
    
    ortp_global_stats_display();
    
    return 0;
}
int get_sound(void)
{
    int fd; /* 声音设备的文件描述符 */
    int arg; /* 用于ioctl调用的参数 */
    int status;   /* 系统调用的返回值 */
    
    /* 打开声音设备 */
    fd = open("/dev/dsp", O_RDWR);
    if (fd < 0) {
        perror("open of /dev/dsp failed");
        exit(1);
    }
 
    /* 设置采样时的量化位数 */
    arg = SIZE;
    status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg);
    if (status == -1)
        perror("SOUND_PCM_WRITE_BITS ioctl failed");
    if (arg != SIZE)
        perror("unable to set sample size");

    /* 设置采样时的声道数目 */
    arg = CHANNELS; 
    status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg);
    if (status == -1)
        perror("SOUND_PCM_WRITE_CHANNELS ioctl failed");
    if (arg != CHANNELS)
        perror("unable to set number of channels");

    /* 设置采样时的采样频率 */
    arg = RATE;
    status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
    if (status == -1)
        perror("SOUND_PCM_WRITE_WRITE ioctl failed");
    
    return(fd);
}

你可能感兴趣的:(默认分类)