Ortp analysis example

Source: Internet
Author: User

I. Main content of rtpsend. c

This example mainly shows how to send audio files in PCMU format or audio streams obtained directly from the sound card.

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;
If (argc <4 ){
Printf ("% s", help );
Return-1;
}
For (I = 4; I if (strcmp (argv [I], "-- with-clockslide") = 0 ){
I ++;
If (I> = argc ){
Printf ("% s", 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 ("% s", help );
Return-1;
}
Jitter = atoi (argv [I]);
}
}
// Initialize the ortp library, which should be used before using the ortp API
Ortp_init ();
// Set the scheduler. scheduler can manage multiple sessions and receive multiple sessions through select at the receiving end. It is common that audio and video are transmitted in two ways. This is combined with the latter, and the latter is basically set for the session, for example, setting different payload for the two sessions.
Ortp_scheduler_init ();
// Set the record level
Ortp_set_log_level_mask (ortp_message | ortp_warning | ortp_error );
// Create a new RTP session. If the session can send data (rtp_session_sendonly or rtp_session_sendrecv), the output stream is assigned a random number of SSRC instances.
Session = rtp_session_new (rtp_session_sendonly );
Rtp_session_set_scheduling_mode (Session, 1 );
/* This function implicitly starts the schedule mode when yesno (the second parameter) is true. It defines the behaviors of rtp_session_recv_with_ts () and rtp_session_send_with_ts. If this parameter is set to true, rtp_session_recv_with_ts () will be blocked before the next receiving packet arrives. After this event, the function returns. For rtp_session_send_with_ts (), it will be blocked before the packet sending time. If it is false, these two functions will return immediately.
Rtp_session_set_blocking_mode (Session, 1 );
/* If it is true, a connect () system call will be used on the socket sent to the destination. If the session uses transport Ric RTP (see rtp_session_set_transport ric_rtp (), it is mainly used to traverse the firewall ), A connect () will be used for the source address after receiving the first package. Connecting to a socket will result in rejecting all packets not sent from the address specified in connect. It also causes the application to detect moral ICMP errors.
*/
Rtp_session_set_connected_mode (Session, true );
// Set the remote address
Rtp_session_set_remote_addr (Session, argv [2], atoi (argv [3]);
// Set the desired package and the load type of the package to be sent. If the load type in the receiving package is different from the Expected One, The payload_type_changed signal is sent. You can mount the function to process this signal.

// The value 0 indicates that payload type is PCMU. In ortp, this setting not only sets numbers, but also the payload structure defined in avprofile, which reports unrecognized types.
Rtp_session_set_payload_type (Session, 0 );
SSRC = getenv ("SSRC ");
If (SSRC! = NULL ){
Printf ("using SSRC = % I./N", atoi (SSRC ));
// Set the SSRC of the output stream. If this step is not performed, a random value will be given.
Rtp_session_set_ssrc (Session, atoi (SSRC ));
}
# 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 );
While (I = fread (buffer, 1,160, infile)> 0) & (runcond ))
{
/* Send an RTP datagram, set the destination through rtp_session_set_remote_addr (), and send data with a timestamp. This is a high-level function that uses rtp_session_create_packet () and rtp_session_sendm_with_ts () to send data.
*/
/* Send an RTP datagram, set the destination through rtp_session_set_remote_addr (), and send data with a timestamp. For audio data, the timestamp is the sequence number of the first sampling result. Packet () is released immediately after being sent. */
Rtp_session_send_with_ts (Session, buffer, I, user_ts );
User_ts + = 160;
If (clockslide! = 0 & user_ts % (160*50) = 0 ){
Ortp_message ("clock sliding of % I miliseconds now", clockslide );
/* Set the time offset.
Void rtp_session_make_time_distorsion (rtpsession * Session, int milisec)
{
Session-> RTP. snd_time_offset + = milisec;
}
*/
Rtp_session_make_time_distorsion (Session, clockslide );
}
/* The following will simulate some burst delay packages */
/* 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;
}
}
}

Fclose (infile );
Rtp_session_destroy (session );
Ortp_exit ();
Ortp_global_stats_display ();

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.