rtpsend.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 3    /* 存储秒数 */
#define RATE 8000   /* 采样频率 */
#define SIZE 8      /* 量化位数 */
#define CHANNELS 1 /* 声道数目 */
unsigned char sound_buf[LENGTH*RATE*SIZE*CHANNELS/400];
int runcond=1;
int get_sound(void);
void stophandler(int signum)
{
    runcond=0;
}

static char *help="usage: rtpsend dest_ip4addr dest_port [ --with-clockslide ] [ --with-jitter ]\n";

int main(int argc, char *argv[])
{
    RtpSession *session;
    unsigned char buffer[160];
    int i;
    FILE *infile;
    char *ssrc;
    uint32_t user_ts=0;
    int clockslide=0;
    int jitter=0;
    int    sound_fd;
    int     state;
    if (argc<3){
        printf(help);
        return -1;
    }
    for(i=3;i         if (strcmp(argv[i],"--with-clockslide")==0){
            i++;
            if (i>=argc) {
                printf(help);
                return -1;
            }
            clockslide=atoi(argv[i]);
            ortp_message("Using clockslide of %i milisecond every 50 packets.",clockslide);
        }else if (strcmp(argv[i],"--with-jitter")==0){
            ortp_message("Jitter will be added to outgoing stream.");
            i++;
            if (i>=argc) {
                printf(help);
                return -1;
            }
            jitter=atoi(argv[i]);
        }
    }
    ortp_init();
    ortp_scheduler_init();
    ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
    session=rtp_session_new(RTP_SESSION_SENDONLY);    
    
    rtp_session_set_scheduling_mode(session,1);
    rtp_session_set_blocking_mode(session,1);
    rtp_session_set_connected_mode(session,TRUE);
    rtp_session_set_remote_addr(session,argv[1],atoi(argv[2]));
    rtp_session_set_payload_type(session,0);
    
    ssrc=getenv("SSRC");
    if (ssrc!=NULL) {
        printf("using SSRC=%i.\n",atoi(ssrc));
        rtp_session_set_ssrc(session,atoi(ssrc));
    }
    #if 0    
    #ifndef _WIN32
    infile=fopen(argv[1],"r");
    #else
    infile=fopen(argv[1],"rb");
    #endif

    if (infile==NULL) {
        perror("Cannot open file");
        return -1;
    }
    signal(SIGINT,stophandler);
#endif
    sound_fd=get_sound();
    while(1)//((i=fread(buffer,1,160,infile))>0) && (runcond) )
    {    state = read(sound_fd, sound_buf, sizeof(sound_buf)); /* 录音 */
        if (state != sizeof(sound_buf))
            perror("read wrong number of bytes");
        rtp_session_send_with_ts(session,sound_buf,i,user_ts);
        user_ts+=160;
        if (clockslide!=0 && user_ts%(160*50)==0){
            ortp_message("Clock sliding of %i miliseconds now",clockslide);
            rtp_session_make_time_distorsion(session,clockslide);
        }
        /*this will simulate a burst of late packets */
        if (jitter && (user_ts%(8000)==0)) {
            struct timespec pausetime, remtime;
            ortp_message("Simulating late packets now (%i milliseconds)",jitter);
            pausetime.tv_sec=jitter/1000;
            pausetime.tv_nsec=(jitter%1000)*1000000;
            while(nanosleep(&pausetime,&remtime)==-1 && errno==EINTR){
                pausetime=remtime;
            }
        }
    }
     close(sound_fd);
    fclose(infile);
    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);

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