Exemple #1
0
RtpSession* rtp_listen(const char *remote_addr, unsigned short remote_port) {
RtpSession *rtpSession;

    recv_ts=0;
    send_ts=0;
    rtpSession=rtp_session_new(RTP_SESSION_SENDRECV);
    rtp_session_set_scheduling_mode(rtpSession,TRUE);
    rtp_session_set_blocking_mode(rtpSession,FALSE);

#ifdef HAVE_RTCP_ORTP
    rtp_session_set_local_addr(rtpSession,INADDR_DSPSERVER,LOCAL_RTP_PORT,LOCAL_RTCP_PORT);
#else
    rtp_session_set_local_addr(rtpSession,INADDR_DSPSERVER,LOCAL_RTP_PORT);
#endif
    rtp_session_set_remote_addr(rtpSession, remote_addr, remote_port );

    rtp_session_set_connected_mode(rtpSession,TRUE);
    rtp_session_set_symmetric_rtp(rtpSession,TRUE);

    rtp_session_enable_adaptive_jitter_compensation(rtpSession,adapt);
    rtp_session_set_jitter_compensation(rtpSession,jittcomp);
    rtp_session_set_payload_type(rtpSession,0);

    rtp_session_set_time_jump_limit	(rtpSession, timestamp_jump_limit);
    //rtp_session_signal_connect(rtpSession,"ssrc_changed",(RtpCallback)ssrc_cb,0);
    rtp_session_signal_connect(rtpSession,"ssrc_changed",(RtpCallback)rtp_session_reset,0);
    rtp_session_signal_connect(rtpSession,"timestamp_jump",(RtpCallback)rtp_session_resync,0);

    rtp_listening = 1;
    rtp_connected = 1;
    return rtpSession;
}
int main(int argc, char *argv[])
{
	RtpSession *session;
	unsigned char buffer[160];
	int err;
	FILE *outfile;
	uint32_t ts=0;
	int have_more;

	if (argc<3){
		printf("%s",help);
		return -1;
	}

	ortp_init();
	ortp_scheduler_init();
	
	/* set the telephony event payload type to 96 in the av profile.*/
	rtp_profile_set_payload(&av_profile,96,&payload_type_telephone_event);
	
	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,"0.0.0.0",atoi(argv[2]));
	rtp_session_set_payload_type(session,0);
	
	/* register for telephony events */
	rtp_session_signal_connect(session,"telephone-event",(RtpCallback)recv_tev_cb,0);
		
	outfile=fopen(argv[1],"wb");
	if (outfile==NULL) {
		perror("Cannot open file");
		return -1;
	}
	signal(SIGINT,stophandler);
	while(runcond)
	{
		have_more=1;
		while (have_more){
			err=rtp_session_recv_with_ts(session,buffer,160,ts,&have_more);
			if (err>0) {
				size_t ret = fwrite(buffer,1,err,outfile);
				assert( ret == err );
			}
		}
		ts+=160;
		//ortp_message("Receiving packet.");
	}
	fclose(outfile);
	rtp_session_destroy(session);
	ortp_exit();
	ortp_global_stats_display();
	printf("Total dtmf events received: %i\n",dtmf_count);
	return 0;
}
RtpSession * create_duplex_rtpsession( int locport, bool_t ipv6){
	RtpSession *rtpr;
	rtpr=rtp_session_new(RTP_SESSION_SENDRECV);
	rtp_session_set_recv_buf_size(rtpr,MAX_RTP_SIZE);
	rtp_session_set_scheduling_mode(rtpr,0);
	rtp_session_set_blocking_mode(rtpr,0);
	rtp_session_enable_adaptive_jitter_compensation(rtpr,TRUE);
	rtp_session_set_symmetric_rtp(rtpr,TRUE);
	rtp_session_set_local_addr(rtpr,ipv6 ? "::" : "0.0.0.0",locport);
	rtp_session_signal_connect(rtpr,"timestamp_jump",(RtpCallback)rtp_session_resync,(long)NULL);
	rtp_session_signal_connect(rtpr,"ssrc_changed",(RtpCallback)rtp_session_resync,(long)NULL);
	return rtpr;
}
RtpSession * create_duplex_rtpsession(int loc_rtp_port, int loc_rtcp_port, bool_t ipv6) {
	RtpSession *rtpr;

	rtpr = rtp_session_new(RTP_SESSION_SENDRECV);
	rtp_session_set_recv_buf_size(rtpr, MAX(ms_get_mtu() , MS_MINIMAL_MTU));
	rtp_session_set_scheduling_mode(rtpr, 0);
	rtp_session_set_blocking_mode(rtpr, 0);
	rtp_session_enable_adaptive_jitter_compensation(rtpr, TRUE);
	rtp_session_set_symmetric_rtp(rtpr, TRUE);
	rtp_session_set_local_addr(rtpr, ipv6 ? "::" : "0.0.0.0", loc_rtp_port, loc_rtcp_port);
	rtp_session_signal_connect(rtpr, "timestamp_jump", (RtpCallback)rtp_session_resync, (long)NULL);
	rtp_session_signal_connect(rtpr, "ssrc_changed", (RtpCallback)rtp_session_resync, (long)NULL);
	rtp_session_set_ssrc_changed_threshold(rtpr, 0);
	disable_checksums(rtp_session_get_rtp_socket(rtpr));
	return rtpr;
}
Exemple #5
0
static void parse_cmd_line(int argc, char **argv, RtpSession* session)
{
    char* local_address = NULL;
    int local_port = DEFAULT_RTP_PORT;
    int ch;


    // Parse the options/switches
    while ((ch = getopt(argc, argv, "h?")) != -1)
        switch (ch) {
        // case 'T': FIXME: timeout?
        case '?':
        case 'h':
        default:
            usage();
        }


    // Parse the ip address and port
    if (argc > optind) {
        local_address = argv[optind];
        optind++;

        // Look for port in the address
        char* portstr = strchr(local_address, '/');
        if (portstr && strlen(portstr)>1) {
            *portstr = 0;
            portstr++;
            local_port = atoi(portstr);
        }

    } else {
        MAST_ERROR("missing address/port to receive from");
        usage();
    }

    // Make sure the port number is even
    if (local_port%2 == 1) local_port--;

    // Set the remote address/port
    if (rtp_session_set_local_addr( session, local_address, local_port )) {
        MAST_FATAL("Failed to set receive address/port (%s/%u)", local_address, local_port);
    } else {
        printf( "Receive address: %s/%u\n", local_address,  local_port );
    }

}
Exemple #6
0
static int rtp_setup( struct rtp_spook_input *conf )
{
	// ??RTP??
    conf->session=rtp_session_new(RTP_SESSION_RECVONLY);
    rtp_session_set_scheduling_mode(conf->session,1);
	rtp_session_set_blocking_mode(conf->session,1);
	rtp_session_set_local_addr(conf->session,"0.0.0.0", conf->localport);
	rtp_session_set_connected_mode(conf->session,TRUE);
	rtp_session_set_symmetric_rtp(conf->session,TRUE);
    rtp_session_enable_adaptive_jitter_compensation(conf->session,TRUE);
	rtp_session_set_jitter_compensation(conf->session,40);
	rtp_session_set_payload_type(conf->session,0);
	rtp_session_signal_connect(conf->session,"ssrc_changed",(RtpCallback)ssrc_cb,0);
	rtp_session_signal_connect(conf->session,"ssrc_changed",(RtpCallback)rtp_session_reset,0);

	return 0;
}
Exemple #7
0
RtpSession * ms_create_duplex_rtp_session(const char* local_ip, int loc_rtp_port, int loc_rtcp_port, int mtu) {
	RtpSession *rtpr;

	rtpr = rtp_session_new(RTP_SESSION_SENDRECV);
	rtp_session_set_recv_buf_size(rtpr, MAX(mtu , MS_MINIMAL_MTU));
	rtp_session_set_scheduling_mode(rtpr, 0);
	rtp_session_set_blocking_mode(rtpr, 0);
	rtp_session_enable_adaptive_jitter_compensation(rtpr, TRUE);
	rtp_session_set_symmetric_rtp(rtpr, TRUE);
	rtp_session_set_local_addr(rtpr, local_ip, loc_rtp_port, loc_rtcp_port);
	rtp_session_signal_connect(rtpr, "timestamp_jump", (RtpCallback)rtp_session_resync, NULL);
	rtp_session_signal_connect(rtpr, "ssrc_changed", (RtpCallback)rtp_session_resync, NULL);
	rtp_session_set_ssrc_changed_threshold(rtpr, 0);
	rtp_session_set_rtcp_report_interval(rtpr, 2500);	/* At the beginning of the session send more reports. */
	rtp_session_set_multicast_loopback(rtpr,TRUE); /*very useful, specially for testing purposes*/
	disable_checksums(rtp_session_get_rtp_socket(rtpr));
	return rtpr;
}
Exemple #8
0
void MastTool::set_session_address( const char* in_addr )
{
	char* address = strdup( in_addr );
	char* portstr = NULL;
	int port = DEFAULT_RTP_PORT;

	// Look for port in the address
	portstr = strchr(address, '/');
	if (portstr && strlen(portstr)>1) {
		*portstr = 0;
		portstr++;
		port = atoi(portstr);
	}

	
	// Make sure the port number is even
	if (port%2 == 1) port--;
	
	
	// Send of recieve address?
	if (this->session->mode == RTP_SESSION_RECVONLY) {
	
		// Set the local address/port
		if (rtp_session_set_local_addr( session, address, port )) {
			MAST_FATAL("Failed to set local address/port (%s/%d)", address, port);
		} else {
			MAST_INFO( "Local address: %s/%d", address,  port );
		}
		
	} else if (this->session->mode == RTP_SESSION_SENDONLY) {
	
		// Set the remote address/port
		if (rtp_session_set_remote_addr( session, address, port )) {
			MAST_FATAL("Failed to set remote address/port (%s/%d)", address, port);
		} else {
			MAST_INFO( "Remote address: %s/%d", address,  port );
		}
		
	} else {
		MAST_FATAL("Mode unsupported by MastTool: %d", this->session->mode);
	}
	
	free( address );
}
Exemple #9
0
MSFilter *set_MSFilter(EndPoint *endpoint, int type, FlowDirections *fdir){
	MSFilter *filter;
	RtpSession *rtps;
	switch(endpoint->protocol){
		case MEDIA_RTP:
			rtps = rtp_session_new(RTP_SESSION_RECVONLY);
			rtp_session_set_local_addr(rtps,"0.0.0.0",8000);
			rtp_session_set_scheduling_mode(rtps,0);
			rtp_session_set_blocking_mode(rtps,0);
			
			if(type == 1){
				filter = ms_rtp_recv_new();
				ms_rtp_recv_set_session(MS_RTP_RECV(filter), rtps);
				fdir->rtpSessions = g_list_append(fdir->rtpSessions, rtps);
				return filter;
			}
			else{
				//ms_rtp_send_new
			}
		case MEDIA_OSS:
			if(type == 1){
				filter = ms_oss_read_new();
				ms_sound_read_set_device(MS_SOUND_READ(filter),0);
				return filter;
			}
			else{
				filter = ms_oss_write_new();
				ms_sound_write_set_device(MS_SOUND_WRITE(filter),0);
				return filter;
			}
		case MEDIA_FILE:
			if(type == 1){
				filter = ms_read_new(endpoint->file);
				return filter;
			}
			if(type == 0){
				filter = ms_write_new(endpoint->file);
				return filter;
			}

	}
}
int yy_init( int port) {

	RtpSession *session;
	ortp_init();
	ortp_scheduler_init();
	ortp_set_log_level_mask(
			ORTP_DEBUG | ORTP_MESSAGE | ORTP_WARNING | ORTP_ERROR);
	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, "0.0.0.0", port);
	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);

	return (int) session;
}
Exemple #11
0
void VodWnd::vod(const char *ip, int rtp_port, int rtcp_port)
{
    server_ip_ = ip;
    server_rtp_port_ = rtp_port;
    server_rtcp_port_ = rtcp_port;

    rtp_ = rtp_session_new(RTP_SESSION_RECVONLY);
    rtp_session_set_payload_type(rtp_, 100);
    rtp_session_set_local_addr(rtp_, util_get_myip(), 0, 0);
    rtp_session_set_remote_addr_and_port(rtp_, ip, rtp_port, rtcp_port);

    JBParameters jb;
    jb.adaptive = 1;
    jb.max_packets = 3000;
    jb.max_size = -1;
    jb.min_size = jb.nom_size = 300;
    rtp_session_set_jitter_buffer_params(rtp_, &jb);

    rtp_session_enable_jitter_buffer(rtp_, 0);

    evq_ = ortp_ev_queue_new();
    rtp_session_register_event_queue(rtp_, evq_);

    ticker_ = ms_ticker_new();

    filter_rtp_ = ms_filter_new(MS_RTP_RECV_ID);
    ms_filter_call_method(filter_rtp_, MS_RTP_RECV_SET_SESSION, rtp_);

    filter_decoder_ = ms_filter_new(MS_H264_DEC_ID);

    ZonekeyYUVSinkCallbackParam cbp;
    cbp.ctx = this;
    cbp.push = cb_yuv;
    filter_sink_ = ms_filter_new_from_name("ZonekeyYUVSink");
    ms_filter_call_method(filter_sink_, ZONEKEY_METHOD_YUV_SINK_SET_CALLBACK_PARAM, &cbp);

    ms_filter_link(filter_rtp_, 0, filter_decoder_, 0);
    ms_filter_link(filter_decoder_, 0, filter_sink_, 0);

    ms_ticker_attach(ticker_, filter_rtp_);
}
bool myAudioStream::init_stream()
{
    /** Init stream **/
    stream = (AudioStream *)ms_new0(AudioStream,1);
    if (stream == 0) {
        ms_error("Failed to create new stream");
        return false;
    }

    /** Configure stream **/
    stream->play_dtmfs = false;
    stream->use_gc = false;
    stream->use_agc = false;
    stream->use_ng = false;

    /** Init RTP session **/
    stream->session = rtp_session_new(RTP_SESSION_RECVONLY);
    if (stream->session == 0) {
        ms_error("Failed to create new RTP session");
        return false;
    }

    /** Configure RTP session **/
    /* Create profile to use in session */
    RtpProfile *rtp_profile = rtp_profile_new("My profile");
    if (rtp_profile == 0) {
        ms_error("Failed to create new RTP profile");
        return false;
    }
    rtp_session_set_profile(stream->session, rtp_profile);

    /* Define some payloads */
    rtp_profile_set_payload(rtp_profile,110,&payload_type_speex_nb);
    rtp_profile_set_payload(rtp_profile,111,&payload_type_speex_wb);
    rtp_profile_set_payload(rtp_profile,112,&payload_type_speex_uwb);

    /* Set local address and port */
    rtp_session_set_local_addr(stream->session, "0.0.0.0", 1337);

    return true;
}
int os_sound_start(jcall_t *ca, int port)
{
  int p,cond;
  int bits = 16;
  int stereo = 0; /* 0 is mono */
  int rate = 8000;
  int blocksize = 512;

  if (port == 0)
    return -1;

  fd=open(AUDIO_DEVICE, O_RDWR|O_NONBLOCK);
  if (fd<0) return -EWOULDBLOCK;
  fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)&~O_NONBLOCK);

  ioctl(fd, SNDCTL_DSP_RESET, 0);
  
  p =  bits;  /* 16 bits */
  ioctl(fd, SNDCTL_DSP_SAMPLESIZE, &p);
  
  p =  stereo;  /* number of channels */
  ioctl(fd, SNDCTL_DSP_CHANNELS, &p);
  
#ifdef USE_PCM
  p = AFMT_S16_NE; /* choose LE or BE (endian) */
  ioctl(fd, SNDCTL_DSP_SETFMT, &p);
#else
  if (ca->payload==0)
    p =  AFMT_MU_LAW;
  else if (ca->payload==8)
    p = AFMT_A_LAW;
  else if (ca->payload==110||ca->payload==111)
    p = AFMT_S16_NE; /* choose LE or BE (endian) */
  ioctl(fd, SNDCTL_DSP_SETFMT, &p);
#endif

  p =  rate;  /* rate in khz*/
  ioctl(fd, SNDCTL_DSP_SPEED, &p);
  
  ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &min_size);
  if (min_size>blocksize)
    {
      cond=1;
      p=min_size/blocksize;
      while(cond)
	{
	  int i=ioctl(fd, SNDCTL_DSP_SUBDIVIDE, &p);
	  /* printf("SUB_DIVIDE said error=%i,errno=%i\n",i,errno); */
	  if ((i==0) || (p==1)) cond=0;
	  else p=p/2;
	}
    }
  ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &min_size);
  if (min_size>blocksize)
    {
      printf("dsp block size set to %i.",min_size);
      exit(0);
    }else{
      /* no need to access the card with less latency than needed*/
      min_size=blocksize;
    }

  printf("blocksize = %i\n", min_size);
  
#ifdef SPEEX_SUPPORT
  {
    float vbr_qual;
    int value;
    int quality;
    ca->speex_enc  = speex_encoder_init(&speex_nb_mode); /* 8kHz */
    /* 16kHz speex_enc = speex_encoder_init(&speex_wb_mode);   */
    /* 32kHz speex_enc = speex_encoder_init(&speex_uwb_mode);  */
    ca->speex_dec  = speex_decoder_init(&speex_nb_mode);
    value = 1;
    speex_decoder_ctl(ca->speex_dec, SPEEX_SET_ENH, &value);
    quality = 8; /* 15kb */
    speex_encoder_ctl(ca->speex_enc, SPEEX_SET_QUALITY, &quality);
    /* ou bien le bit rate:
       value = 15000; // 15kb
       speex_encoder_ctl(ca->speex_enc, SPEEX_SET_BITRATE, &value);
    */
    /* silence suppression (VAD)
       value = 1; // 15kb
       speex_encoder_ctl(ca->speex_enc, SPEEX_SET_VAD, &value);
       Discontinuous transmission (DTX)
       value = 1; // 15kb
       speex_encoder_ctl(ca->speex_enc, SPEEX_SET_DTX, &value);
       
       Variable Bit Rate (VBR)
       value = 1; // 15kb
       speex_encoder_ctl(ca->speex_enc, SPEEX_SET_VBR, &value);
       vbr_qual = 5,0; // between 0 and 10
       speex_encoder_ctl(ca->speex_enc, SPEEX_SET_VBR_QUALITY, &vbr_qual);
       
       Average bit rate: (ABR)
       value = 15000; // 15kb
       speex_encoder_ctl(ca->speex_enc, SPEEX_SET_ABR, &value);
    */
    speex_encoder_ctl(ca->speex_enc, SPEEX_GET_FRAME_SIZE,
		      &ca->speex_fsize);
    
    ca->speex_nb_packet = 1;
    speex_bits_init(&(ca->speex_bits));
    speex_bits_init(&(ca->dec_speex_bits));
  }
#endif
  
  ca->rtp_session = rtp_session_new(RTP_SESSION_SENDRECV);
  rtp_session_set_scheduling_mode(ca->rtp_session, 1); /* yes */
  rtp_session_set_blocking_mode(ca->rtp_session, 1);
  
  rtp_session_set_profile(ca->rtp_session, &av_profile);
  rtp_session_set_jitter_compensation(ca->rtp_session, 60);
  rtp_session_set_local_addr(ca->rtp_session, _localip, port);
  rtp_session_set_remote_addr(ca->rtp_session,
			      ca->remote_sdp_audio_ip,
			      ca->remote_sdp_audio_port);
  rtp_session_set_payload_type(ca->rtp_session, ca->payload);
  rtp_session_signal_connect(ca->rtp_session, "telephone-event",
			     (RtpCallback)rcv_telephone_event, ca);
  
  /* enter a loop (thread?) to send AUDIO data with
     rtp_session_send_with_ts(ca->rtp_session, data, data_length, timestamp);
  */
  ca->audio_thread = osip_thread_create(20000,
					os_sound_start_thread, ca);
  ca->out_audio_thread = osip_thread_create(20000,
					    os_sound_start_out_thread, ca);
  return 0;
}
Exemple #14
0
int main(int argc, char*argv[])
{
	RtpSession *session;
	unsigned char buffer[32];
	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;
	
	/* init the lib */
	if (argc<3){
		printf("%s",help);
		return -1;
	}
	local_port=atoi(argv[2]);
	if (local_port<=0) {
		printf("%s",help);
		return -1;
	}
	for (i=3;i<argc;i++)
	{
		if (strcmp(argv[i],"--noadapt")==0) adapt=FALSE;
		if (strcmp(argv[i],"--format")==0){
			i++;
			if (i<argc){
				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<argc){
				jittcomp=atoi(argv[i]);
				printf("Using a jitter buffer of %i milliseconds.\n",jittcomp);
			}
		}
	}
	
	outfile=fopen(argv[1],"wb");
	if (outfile==NULL) {
		perror("Cannot open file for writing");
		return -1;
	}
	
	
	if (soundcard){
		sound_fd=sound_init(format);
	}
	
	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,"0.0.0.0",atoi(argv[2]));
	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);
	

	fwrite("#!AMR\n",1,6,outfile);
//	write(sound_fd,"#!AMR\n",err);
	while(cond)
	{
		have_more=1;
		while (have_more){
			err=rtp_session_recv_with_ts(session,buffer,32,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 ((stream_received) && (err>0)) {
				size_t ret = fwrite(buffer,1,err,outfile);
				int ii;
				for(ii =0 ; ii < 32 ; ii ++)
					printf("%x " ,buffer[ii]);
				printf("\n");
//				while(1);
				if (sound_fd>0){
					ret = write(sound_fd,buffer,err);
					if (ret==-1){
						fprintf(stderr,"write to sound card failed (%s)",strerror(errno));
					}
				}
			}
		}
		ts+=160;
		//ortp_message("Receiving packet.");
	}
	
	rtp_session_destroy(session);
	ortp_exit();
	
	ortp_global_stats_display();
	
	return 0;
}
Exemple #15
0
int main(int argc, char **argv)
{
	ortp_init();
	ms_init();
	zk_xmpp_uac_init();
	ortp_set_log_level_mask(ORTP_MESSAGE);

	if (argc < 2) {
		fprintf(stderr, "usage: %s <zqpkt src url> [s]\n", argv[0]);
		return -1;
	}

	bool stream_mode = false;
	if (argc == 3 && argv[2][0] == 's')
		stream_mode = true;

	_stream_mode = stream_mode;

	if (stream_mode)
		fprintf(stdout, "=== STREAMING MODE ===\n\n");
	else
		fprintf(stdout, "=== SOURCING MODE ===\n\n");

	_url = argv[1];
	_env = CreateEvent(0, 0, 0, 0);

	fprintf(stdout, "%s: using zqpkt src '%s', just wait mcu .....\n", argv[0], argv[1]);

	// 使用 normaluser 登录
	cb_xmpp_uac cbs = { 0, 0, 0, 0, cb_connect_notify };
	_uac = zk_xmpp_uac_log_in(get_user_jid(), "ddkk1212", &cbs, 0);

	WaitForSingleObject(_env, 10000);

	if (_sid == -1) {
		fprintf(stderr, ":( somthing err, exit!\n");
	}
	else {
		SetConsoleCtrlHandler(signal_ctrl_c, 1);
		
		const char *src_url = argv[1];
		const char *target_ip = _ip.c_str();
		int target_port = _rtp_port;
		int target_port2 = _rtcp_port;

		//fprintf(stdout, "target ip=%s\ntarget port=%d\n\n", target_ip, target_port);

		// only support h264
		rtp_profile_set_payload(&av_profile,100, &payload_type_h264);

		/// 使用 zonekey.h264.source filter
		zonekey_h264_source_register();
		MSFilterDesc *desc = ms_filter_lookup_by_name("ZonekeyH264Source");
		MSFilter *source = ms_filter_new_from_desc(desc);

		if (_stream_mode)
			zonekey_yuv_sink_register();

		// 获取 writer_params
		ZonekeyH264SourceWriterParam writer_param;
		ms_filter_call_method(source, ZONEKEY_METHOD_H264_SOURCE_GET_WRITER_PARAM, &writer_param);

		// RTP Session
		RtpSession *rtpsess = rtp_session_new(RTP_SESSION_SENDRECV);	// 
		rtp_session_set_local_addr(rtpsess, "0.0.0.0", -1, -1);	// 随机端口
		rtp_session_set_remote_addr_and_port(rtpsess, target_ip, target_port, target_port2);
		rtp_session_set_payload_type(rtpsess, 100);	// h264

		JBParameters jb;
		jb.adaptive = 1;
		jb.max_packets = 3000;
		jb.max_size = -1;
		jb.min_size = jb.nom_size = 300;
		rtp_session_set_jitter_buffer_params(rtpsess, &jb);
	
		// disable video jitter control
		rtp_session_enable_jitter_buffer(rtpsess, 0);

		/// rtp sender
		MSFilter *rtp_sender = ms_filter_new(MS_RTP_SEND_ID);
		ms_filter_call_method(rtp_sender, MS_RTP_SEND_SET_SESSION, rtpsess);

		// connect source --> rtp sender
		ms_filter_link(source, 0, rtp_sender, 0);

		// MSTicker
		MSTicker *ticker = ms_ticker_new();

		// attach ticker
		ms_ticker_attach(ticker, source);

		if (_stream_mode) {
			// FIXME: recv, but ....
			MSFilter *rtp_recver = ms_filter_new(MS_RTP_RECV_ID);
			ms_filter_call_method(rtp_recver, MS_RTP_RECV_SET_SESSION, rtpsess);

			MSFilter *decoder = ms_filter_new(MS_H264_DEC_ID);
			MSFilter *sink = ms_filter_new_from_name("ZonekeyYUVSink");

			ms_filter_link(rtp_recver, 0, decoder, 0);
			ms_filter_link(decoder, 0, sink, 0);

			MSTicker *tk = ms_ticker_new();
			//ms_ticker_attach(tk, rtp_recver);
		}

		// 利用 libzqpkt 接收 h264 数据,并且调用 zonekey h264 source 的 writer() 
		void *zqp = 0;
		if (zqpsrc_open(&zqp, src_url) < 0) {
			fprintf(stderr, "to open src err\n");
			return -1;
		}

		while (!_quit) {
			zq_pkt *pkt = zqpsrc_getpkt(zqp);
			if (pkt) {
				if (pkt->type == 1) {
					// h264
					writer_param.write(writer_param.ctx, pkt->ptr, pkt->len, pkt->pts / 45000.0);
				}

				zqpsrc_freepkt(zqp, pkt);
			}
			else
				break;
		}

		// 发送删除 sid 的命令
		char options[128], *cmd="test.fc.del_source";
		if (_stream_mode) {
			snprintf(options, sizeof(options), "streamid=%d", _sid);
			cmd = "test.dc.del_stream";
		}
		else
			snprintf(options, sizeof(options), "sid=%d", _sid);

		zk_xmpp_uac_send_cmd(_uac, get_mcu_jid(), cmd, options, 0, cb_response);
		fprintf(stderr, "\n\nen. to del sid=%d\n\n", _sid);

		zqpsrc_close(zqp);
		fprintf(stderr, "END!\n");

		WaitForSingleObject(_env, 3000);	// 等待 test.fc.de_source 发送成功
	}

	return 0;
}
int main(int argc, char *argv[])
{
	RtpSession *session[STREAMS_COUNT];
	gint i;
	gint filefd[STREAMS_COUNT];
	gint port;
	guint32 user_ts=0;
	gint channels;
	SessionSet *set;
	gchar *filename;

	if (argc<4){
		printf(help);
		return -1;
	}
	
	channels=atoi(argv[3]);
	if (channels==0){
		printf(help);
		return -1;
	}
	
	ortp_init();
	ortp_scheduler_init();
	ortp_set_debug_file("oRTP",NULL);
	
        /* set the telephony event payload type to 96 in the av profile.*/
        rtp_profile_set_payload(&av_profile,96,&payload_type_telephone_event);

	port=atoi(argv[2]);
	p_channel_id = (int *)g_malloc(channels*sizeof(int));
	for (i=0;i<channels;i++){
		session[i]=rtp_session_new(RTP_SESSION_RECVONLY);	
		rtp_session_set_scheduling_mode(session[i],1);
		rtp_session_set_blocking_mode(session[i],0);

		rtp_session_set_local_addr(session[i],"0.0.0.0",port);
		rtp_session_set_recv_payload_type(session[i],0);
		rtp_session_set_recv_buf_size(session[i],256);

		p_channel_id[i] = i;
		/* register for telephony events */
		rtp_session_signal_connect(session[i],"telephone-event",(RtpCallback)recv_tev_cb,&p_channel_id[i]);

		port+=2;
	}
		
	filename=g_malloc(strlen(argv[1])+8);
	for (i=0;i<channels;i++){
		sprintf(filename,"%s%4.4d.dat",argv[1],i);
		#ifndef _WIN32
		filefd[i]=open(filename,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
		#else
		filefd[i]=open(filename,_O_BINARY | O_WRONLY | O_CREAT | O_TRUNC);
		#endif
		if (filefd[i]<0) g_error("Could not open %s for writing: %s",filename,strerror(errno));
	}
	signal(SIGINT,stophandler);
	/* create a set */
	set=session_set_new();
	while(runcond)
	{
		int k;
		
		for (k=0;k<channels;k++){
			/* add the session to the set */
			session_set_set(set,session[k]);
			
		}
		/* and then suspend the process by selecting() */
		session_set_select(set,NULL,NULL);
		for (k=0;k<channels;k++){
			if (session_set_is_set(set,session[k])){
				rtp2disk(session[k],user_ts,filefd[k]);
			}
		}
		user_ts+=160;
	}
	for (i=0;i<channels;i++){
		close(filefd[i]);
		rtp_session_destroy(session[i]);
	}
	session_set_destroy(set);
	g_free(p_channel_id);
	g_free(filename);
	ortp_exit();
	ortp_global_stats_display();
	return 0;
}
Exemple #17
0
int main(int argc, char *argv[])
{
	RtpSession *session[STREAMS_COUNT];
	int i;
	int filefd[STREAMS_COUNT];
	int port;
	uint32_t user_ts=0;
	int channels;
	SessionSet *set;
	char *filename;
	
	if (argc<4){
		printf("%s",help);
		return -1;
	}
	
	channels=atoi(argv[3]);
	if (channels==0){
		printf("%s",help);
		return -1;
	}
	
	ortp_init();
	ortp_scheduler_init();
	
	port=atoi(argv[2]);
	recvbuf=ortp_malloc(160);
	
	for (i=0;i<channels;i++){

		session[i]=rtp_session_new(RTP_SESSION_RECVONLY);	
		rtp_session_set_scheduling_mode(session[i],1);
		rtp_session_set_blocking_mode(session[i],0);
		rtp_session_set_local_addr(session[i],"0.0.0.0",port,port+1);
		rtp_session_set_payload_type(session[i],0);
		rtp_session_enable_adaptive_jitter_compensation(session[i], TRUE);
		rtp_session_set_recv_buf_size(session[i],256);
		port+=2;
	}
		
	filename=ortp_malloc(strlen(argv[1])+15);
	for (i=0;i<channels;i++){
		sprintf(filename,"%s%4.4d.dat",argv[1],i);
		#ifndef _WIN32
		filefd[i]=open(filename,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
		#else
		filefd[i]=open(filename,_O_BINARY | O_WRONLY | O_CREAT | O_TRUNC);
		#endif
		if (filefd[i]<0) ortp_error("Could not open %s for writing: %s",filename,strerror(errno));
	}
	signal(SIGINT,stophandler);
	/* create a set */
	set=session_set_new();
	while(runcond)
	{
		int k;
		
		for (k=0;k<channels;k++){
			/* add the session to the set */
			session_set_set(set,session[k]);
			//printf("session_set_set %d\n", k);
		}
		/* and then suspend the process by selecting() */
		k=session_set_select(set,NULL,NULL);
		if (k==0) printf("warning: session_set_select() is returning 0...\n");
		for (k=0;k<channels;k++){
			if (session_set_is_set(set,session[k])){
				rtp2disk(session[k],user_ts,filefd[k]);
				//printf("session_set_is_set %d\n", k);
			} else {
				//printf("warning: session %i is not set !\n",k);
			}
		}
		user_ts+=160;
	}
	printf("Exiting\n");
	for (i=0;i<channels;i++){
		close(filefd[i]);
		rtp_session_destroy(session[i]);
	}
	session_set_destroy(set);
	ortp_free(filename);
	ortp_exit();
	ortp_global_stats_display();
	ortp_free(recvbuf);
	return 0;
}
Exemple #18
0
void CameraStream::init()
{
	/** 获取第一帧图像,初始化 sws_,创建 h264 encoder ....
	 */
	IplImage *img = cvQueryFrame(cap_);
	// FIXME: 未必是 rgb24 吧???
	sws_ = sws_getContext(img->width, img->height, PIX_FMT_RGB24, WIDTH, HEIGHT, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, 0, 0, 0);

	x264_param_t param;
	x264_param_default_preset(&param, "veryfast", "zerolatency");
	param.i_threads = 0;
	param.i_width = WIDTH;
	param.i_height = HEIGHT;
	param.i_keyint_max = FPS * 2;
	param.i_fps_den = 1;
	param.i_fps_num = FPS;
	param.i_slice_max_size = 1300;
	param.b_repeat_headers = 1;
	param.b_annexb = 1;
	param.rc.i_rc_method = X264_RC_ABR;
	param.rc.i_bitrate = KBPS;
	param.rc.i_vbv_max_bitrate = KBPS*1.1;
		
	encoder_ = x264_encoder_open(&param);

	avpicture_alloc(&pic_, PIX_FMT_YUV420P, WIDTH, HEIGHT);

	rtp_ = rtp_session_new(RTP_SESSION_SENDRECV);
	rtp_session_set_payload_type(rtp_, 100);
	rtp_session_set_remote_addr_and_port(rtp_, server_ip_.c_str(), server_rtp_port_, server_rtcp_port_);
	rtp_session_set_local_addr(rtp_, util_get_myip(), 0, 0);
	JBParameters jb;
	jb.adaptive = 1;
	jb.max_packets = 500;
	jb.max_size = -1;
	jb.min_size = jb.nom_size = 300;
	rtp_session_set_jitter_buffer_params(rtp_, &jb);

	filter_rtp_sender_ = ms_filter_new(MS_RTP_SEND_ID);
	ms_filter_call_method(filter_rtp_sender_, MS_RTP_SEND_SET_SESSION, rtp_);

	filter_h264_sender_ = ms_filter_new_from_name("ZonekeyH264Source");
	ms_filter_call_method(filter_h264_sender_, ZONEKEY_METHOD_H264_SOURCE_GET_WRITER_PARAM, &sender_params_);

	filter_rtp_recver_ = ms_filter_new(MS_RTP_RECV_ID);
	ms_filter_call_method(filter_rtp_recver_, MS_RTP_RECV_SET_SESSION, rtp_);

	filter_decoder_ = ms_filter_new(MS_H264_DEC_ID);

	filter_yuv_sink_ = ms_filter_new_from_name("ZonekeyYUVSink");
	// TODO: 显示 ...

	ms_filter_link(filter_rtp_recver_, 0, filter_decoder_, 0);
	ms_filter_link(filter_decoder_, 0, filter_yuv_sink_, 0);

	ticker_recver_ = ms_ticker_new();
	ms_ticker_attach(ticker_recver_, filter_rtp_recver_);

	ms_filter_link(filter_h264_sender_, 0, filter_rtp_sender_, 0);

	ticker_sender_ = ms_ticker_new();
	ms_ticker_attach(ticker_sender_, filter_h264_sender_);
}
Exemple #19
0
int main(int argc, char*argv[])
{
	RtpSession *session;
#ifndef SBUS
	unsigned char buffer[160];
#else
	unsigned char buffer[SBUS_FRAME_SIZE];
#endif
	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;
#ifdef SBUS
	RtpProfile prof;
#endif
	
	/* init the lib */
	if (argc<3){
		printf("%s",help);
		return -1;
	}
	local_port=atoi(argv[2]);
	if (local_port<=0) {
		printf("%s",help);
		return -1;
	}
	for (i=3;i<argc;i++)
	{
		if (strcmp(argv[i],"--noadapt")==0) adapt=FALSE;
		if (strcmp(argv[i],"--format")==0){
			i++;
			if (i<argc){
				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<argc){
				jittcomp=atoi(argv[i]);
				printf("Using a jitter buffer of %i milliseconds.\n",jittcomp);
			}
		}
	}
	
	outfile=fopen(argv[1],"wb");
	if (outfile==NULL) {
		perror("Cannot open file for writing");
		return -1;
	}
#ifdef SBUS
	setvbuf(outfile, NULL, _IONBF, 0);
#endif
	
	
	if (soundcard){
		sound_fd=sound_init(format);
	}
	
	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,"0.0.0.0",atoi(argv[2]),-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);
#ifndef SBUS
	rtp_session_set_payload_type(session,0);
#else
	rtp_profile_clear_all(&prof);
	//rtp_profile_set_name(&prof, "SBUS");
	rtp_profile_set_payload(&prof, 71, &payload_type_sbus);
	rtp_session_set_profile(session, &prof);
	rtp_session_set_payload_type(session, 71);
#endif
	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){
#ifndef SBUS
			err=rtp_session_recv_with_ts(session,buffer,160,ts,&have_more);
#else
			err=rtp_session_recv_with_ts(session,buffer,SBUS_FRAME_SIZE,ts,&have_more);
#endif
			if (err>0) stream_received=1;
			/* this is to avoid to write to disk some silence before the first RTP packet is returned*/	
			if ((stream_received) && (err>0)) {
#ifdef SBUS
				clock_gettime(CLOCK_REALTIME, &tsnew);
				printf("%09ld\n", tsnew.tv_nsec);
#endif
				size_t ret = fwrite(buffer,1,err,outfile);
				if (sound_fd>0){
					ret = write(sound_fd,buffer,err);
					if (ret==-1){
						fprintf(stderr,"write to sound card failed (%s)",strerror(errno));
					}
				}
			}
		}
#ifndef SBUS
		ts+=160;
#else
		ts+=70;
#endif
		//ortp_message("Receiving packet.");
	}
	
	rtp_session_destroy(session);
	ortp_exit();
	
	ortp_global_stats_display();
	
	return 0;
}
int ph_media_start(phcall_t *ca, int port, void (*dtmfCallback)(phcall_t *ca, int event), const char * deviceId)
{
  int					format = WAVE_FORMAT_PCM;
  phmstream_t	*stream;
#if USE_CODECS
  phcodec_t		*codec;
#endif /* !USE_CODECS */
#if DO_ECHO_CAN
  int					taps=256;
#endif /* !DO_ECHO_CAN */

#if USE_CODECS
  codec = ph_media_lookup_codec(ca->payload);
  if (!codec)
	  return 0;
#endif /* !USE_CODECS */

  stream = open_sndcard(format, codec, deviceId);
  if (!stream)
    return -1;

  stream->payload = ca->payload;

  stream->rtp_session = rtp_session_new(RTP_SESSION_SENDRECV);
  rtp_session_set_scheduling_mode(stream->rtp_session, 0); /* yes */
  rtp_session_set_blocking_mode(stream->rtp_session, 0);
  
  rtp_session_set_profile(stream->rtp_session, &av_profile);
  rtp_session_set_jitter_compensation(stream->rtp_session, 60);
  rtp_session_set_local_addr(stream->rtp_session, "0.0.0.0", port);
  rtp_session_set_remote_addr(stream->rtp_session,
			      ca->remote_sdp_audio_ip,
			      ca->remote_sdp_audio_port);
  rtp_session_set_payload_type(stream->rtp_session, stream->payload);
  rtp_session_signal_connect(stream->rtp_session, "telephone-event",
			     (RtpCallback)ph_telephone_event, ca);
  

  ca->hasaudio = 1;
  stream->running = 1;
  ca->phstream = stream;
  stream->dtmfCallback = dtmfCallback;
# if DO_ECHO_CAN
#  if AEC_BIS
  create_AEC();
#  else /* !AEC_BIS */
  stream->ec = echo_can_create(taps, 0);
  if(stream->ec == 0){
    fprintf(stderr, "Echo CAN creating failed\n");
  }
  if(stream->ec)
  {
#  endif /* !AEC_BIS */
    stream->echocancel = taps;
    stream->pcm_rd = stream->pcm_wr = 0;
    stream->pcm_sent = malloc(PCM_TRACE_LEN);
	stream->sent_cnt = stream->recv_cnt = 0;
    if(stream->pcm_sent == 0)
      fprintf(stderr, "No memory for EC  %d\n", stream->pcm_sent);
#if !AEC_BIS
  }
#endif /* AEC_BIS */
  stream->aec_mutex = g_mutex_new();
  stream->synclock = g_mutex_new();
  stream->sync_cond = g_cond_new();
  stream->pcm_need_resync = 1;
  stream->bytes_to_throw = 0;
# endif /* !DO_ECHO_CAN */
  stream->dtmfg_lock = g_mutex_new();
  stream->dtmfq_cnt = 0;
  stream->dtmfg_phase = DTMF_IDLE;

 stream->audio_in_thread = osip_thread_create(20000,
					ph_audio_in_thread, stream);
 stream->audio_out_thread = osip_thread_create(20000,
				ph_audio_out_thread, stream);


  return 0;
}