ThreadX docsThreadX docs
ThreadX
ThreadX Modules
NetX Duo
FileX
GUIX
USBX
TraceX
LevelX
ThreadX
ThreadX Modules
NetX Duo
FileX
GUIX
USBX
TraceX
LevelX
  • NetX Duo Documentation

    • Understand NetX Duo
    • NetX Duo documentation
    • Chapter 1 - Introduction to NetX Duo
    • Chapter 2 - Installation and Use of NetX Duo
    • Chapter 3 - Functional Components of NetX Duo
    • Chapter 4 - Description of NetX Duo Services
    • Chapter 5 - NetX Duo Network Drivers
    • Appendix A - NetX Duo Services
    • Appendix B - NetX Duo Constants
    • Appendix C - NetX Duo Data Types
    • Appendix D - NetX Duo BSD-Compatible Socket API
    • Appendix E - NetX Duo ASCII Character Codes
    • Auto-IP

      • Chapter 1 - Introduction to NetX Duo AutoIP
      • Chapter 2 - Installation and use of NetX Duo AutoIP
      • Chapter 3 - Description of NetX Duo AutoIP services
    • BSD

      • Chapter 1 - Introduction to NetX Duo BSD
      • Chapter 2 - Installation and use of NetX Duo BSD
      • Chapter 3 - NetX Duo BSD Services
    • Crypto

      • Chapter 1 - Introduction to NetX Duo Crypto
      • Chapter 2 - Installation and use of NetX Duo Crypto
      • Chapter 3 - Functional description of NetX Duo Crypto
      • Chapter 4 - NetX Duo Crypto API description
      • Appendix - NetX Duo Crypto CAVS test
    • DHCP Client

      • Chapter 1 - Introduction to the NetX Duo DHCP Client
      • Chapter 2 - Installation and use of NetX Duo DHCP Client
      • Chapter 3 - Description of NetX Duo DHCP Client services
      • Appendix A - Description of the Restore state feature for NetX Duo DHCP Client services
    • DHCP Server

      • Chapter 1 - Introduction to NetX Duo DHCP Server
      • Chapter 2 - Installation and Use of the NetX Duo DHCP Server
      • Chapter 3 - Description of NetX Duo DHCP server services
    • DHCPv6 Client

      • Chapter 1 - Introduction to NetX Duo DHCPv6 Client
      • Chapter 2 - Installation and use of NetX Duo DHCPv6 Client
      • Chapter 3 - NetX Duo DHCPv6 configuration options
      • Chapter 4 - NetX Duo DHCPv6 Client services
      • Appendix A - Description of the Restore State Feature for NetX Duo DHCPv6 Client
    • DHCPv6 Server

      • Chapter 1 - Introduction to NetX Duo DHCPv6 server
      • Chapter 2 - Installation and use of NetX Duo DHCPv6 server
      • Chapter 3 - NetX Duo DHCPv6 server configuration options
      • Chapter 4 - NetX Duo DHCPv6 server services
      • Appendix A – NetX Duo DHCPv6 option codes
      • Appendix B - NetX Duo DHCPv6 server status codes
      • Appendix C - NetX Duo DHCPv6 unique identifiers (DUIDs)
      • Appendix D - NetX Duo Advanced DHCPv6 server example
    • DNS

      • Chapter 1 - Introduction to the NetX Duo DNS Client
      • Chapter 2 - Installation and Use of NetX Duo DNS Client
      • Chapter 3 - Description of NetX Duo DNS Client Services
    • FTP

      • Chapter 1 - Introduction to NetX Duo FTP
      • Chapter 2 - Installation and use of FTP
      • Chapter 3 - Description of FTP services
    • HTTP

      • Chapter 1 - Introduction to NetX Duo HTTP
      • Chapter 2 - Installation and Use of NetX Duo HTTP
      • Chapter 3 - Description of NetX Duo HTTP Services
    • iperf

      • Chapter 1 - Introduction to NetX Duo Iperf
      • Chapter 2 - Installing and using NetX Duo Iperf
      • Chapter 3 - Running the UDP Transmit Test
    • mDNS

      • Chapter 1 - Introduction to NetX Duo mDNS/DNS-SD
      • Chapter 2 - Installation and use of mDNS
      • Chapter 3 - Description of internal service cache
      • Chapter 4 - Description of mDNS services
    • mqtt

      • Chapter 1 - Introduction to NetX Duo MQTT
      • Chapter 2 - Installation and use of NetX Duo MQTT client
      • Chapter 3 - Description of NetX Duo MQTT Client Services
    • NAT

      • Chapter 1 - An introduction to Network Address Translation
      • Chapter 2 - Installation and use of NAT
      • Chapter 3 - NAT configuration options
      • Chapter 4 - Description of NAT services
    • POP3 Client

      • Chapter 1 - Introduction to NetX Duo POP3
      • Chapter 2 - Installation and use of NetX Duo POP3 Client
      • Chapter 3 - Description of POP3 Client services
    • PPP

      • Chapter 1 - Introduction to the NetX Duo Point-to-Point Protocol (PPP)
      • Chapter 2 - Installation and use of NetX Duo Point-to-Point Protocol (PPP)
      • Chapter 3 - Description of NetX Duo Point-to-Point Protocol (PPP) services
    • PTP Client

      • Chapter 1 - Introduction to NetX Duo PTP Client
      • Chapter 2 - Installation and Use of NetX Duo PTP Client
      • Chapter 3 - Description of NetX Duo PTP Client Services
    • rtp

      • Chapter 1 - Introduction to the NetX Duo RTP Sender
      • Chapter 2 - Installation and use of NetX Duo RTP Sender
      • Chapter 3 - Description of NetX Duo RTP Sender Services
    • rtsp

      • Chapter 1 - Introduction to the NetX Duo RTSP Server
      • Chapter 2 - Installation and use of NetX Duo RTSP Server
      • Chapter 3 - Description of NetX Duo RTSP Server Services
    • Secure DTLS

      • Chapter 1 - Introduction to NetX Duo Secure DTLS
      • Chapter 2 - Installation and use of NetX Duo Secure DTLS
      • Chapter 3 - Functional description of NetX Duo Secure DTLS
      • Chapter 4 - Description of NetX Duo Secure DTLS services
      • Appendix A - NetX Duo Secure DTLS return/error codes
    • Secure TLS

      • Chapter 1 - Introduction to NetX Duo Secure
      • Chapter 2 - Installation and use of NetX Duo Secure
      • Chapter 3 - Functional description of NetX Duo Secure
      • Chapter 4 - Description of NetX Duo Secure services
      • Appendix A - NetX Duo Secure return/error codes
    • SMTP Client

      • Chapter 1 - Introduction to NetX Duo SMTP client
      • Chapter 2 - Installation and use of NetX Duo SMTP client
      • Chapter 3 - Client description of SMTP Client services
    • snmp

      • Chapter 1 - Introduction to NetX Duo SNMP
      • Chapter 2 - Installation and use of the NetX Duo SNMP agent
      • Chapter 3 - Description of NetX Duo SNMP agent services
    • sntp client

      • Chapter 1 - Introduction to NetX Duo SNTP
      • Chapter 2 - Installation and Use of NetX Duo SNTP Client
      • Chapter 3 - Description of NetX Duo SNTP Client Services
      • Appendix A - NetX Duo SNTP Fatal Error Codes
    • telnet

      • Chapter 1 - Introduction to NetX Duo Telnet
      • Chapter 2 - Installation and use of NetX Duo Telnet
      • Chapter 3 - Description of NetX Duo Telnet services
    • TFTP

      • Chapter 1 - Introduction to NetX Duo TFTP
      • Chapter 2 - Installation and use of NetX Duo TFTP
      • Chapter 3 - Description of NetX Duo TFTP services
    • Web HTTP

      • Chapter 1 - Introduction to HTTP and HTTPS
      • Chapter 2 - Installation and use of HTTP and HTTPS
      • Chapter 3 - Description of HTTP services
    • About the NetX Duo User Guide

Chapter 2 - Installation and use of NetX Duo RTP Sender

This chapter contains a description of various issues related to installation, setup, and usage of the NetX Duo RTP Sender services.

Product Distribution

NetX Duo RTP Sender is available at https://github.com/eclipse-threadx/netxduo. The package includes one source file and one header file, as follows:

  • nx_rtp_sender.h Header file for NetX Duo RTP Sender
  • nx_rtp_sender.c C Source file for NetX Duo RTP Sender
  • demo_rtsp_over_rtp.h Header file for NetX Duo RTP Sender demo with RTSP server
  • demo_rtsp_over_rtp.c C Source file for NetX Duo RTP Sender demo with RTSP server

NetX Duo RTP Sender Installation

In order to use the NetX Duo RTP Sender API, the entire distribution mentioned previously should be copied to the same directory where NetX Duo is installed. For example, if NetX Duo is installed in the directory "\threadx\arm7\green" then the nx_rtp_sender.h and nx_rtp_sender.c files should be copied into this directory for RTP Sender applications.

Using NetX Duo RTP Sender

Using the NetX Duo RTP Sender is easy. Basically, the application code must include nx_rtp_sender.h after it includes tx_api.h, and nx_api.h, in order to use ThreadX and NetX Duo respectively. The build project must include the RTP source code nx_rtp_sender.c and the host application file, and of course the ThreadX and NetX library files. This is all that is required to use NetX Duo RTP Sender.

Note: Since RTP utilizes NetX Duo UDP services, UDP must be enabled with the nx_udp_enable call prior to using RTP.

Small Example System of NetX Duo RTP Sender

An example of how to use NetX Duo RTP Sender is described in the header file and the source file below. For simplicity, the return codes are assumed to be successful, therefore no further error checking is done.

Caution: This is provided for demonstration purposes only and is not guaranteed to compile and run as is.

Please refer to the NetX Duo RTP Sender release code distribution for demo source code file(s) that will properly build in the native Eclipse ThreadX environment. Also be aware that these demos are intentionally kept very simple as they are intended to introduce NetX Duo RTP Sender application to new users.

/* This is a small demo of NetX Duo RTP Sender on the high-performance NetX TCP/IP stack. This demo relies on ThreadX and NetX Duo to show a simple rtp audio (AAC format) and video (H.264) data transfer to the client.
*/

#include "tx_api.h"
#include "nx_api.h"
#include "nx_rtp_sender.h"

/* Define demo macros.  */

#define DEMO_RTP_SERVER_ADDRESS         IP_ADDRESS(1,2,3,4)
#define DEMO_RTP_CLIENT_ADDRESS         IP_ADDRESS(1,2,3,5)
#define DEMO_RTP_CLIENT_RTP_PORT        6002
#define DEMO_RTP_CLIENT_RTCP_PORT       6003
#define DEMO_RTP_PAYLOAD_TYPE_VIDEO     96
#define DEMO_RTP_PAYLOAD_TYPE_AUDIO     97
#define DEMO_CNAME                      "someone@example.com"

/* Define demo data. */

#define DEMO_RTP_TIMESTAMP              1234
#define DEMO_NTP_MSW                    123
#define DEMO_NTP_LSW                    456

static UCHAR demo_rtp_audio_packet_data[] = "Demo rtp audio packet data";
static UCHAR demo_rtp_video_packet_data[] = "Demo rtp video packet data";

/* Define the ThreadX object control blocks...  */

static TX_THREAD                   thread_0;
static NX_PACKET_POOL              pool_0;
static NX_IP                       ip_0;

/* Define the rtp sender control block.  */
static NX_RTP_SENDER               rtp_0;
static NX_RTP_SESSION              rtp_session_0;
static NX_RTP_SESSION              rtp_session_1;

/* Define error counter. */
static ULONG                       error_counter;

/* Define user registered rtcp callback functions.  */
static UINT demo_rtcp_receiver_report_callback(NX_RTP_SESSION *session, NX_RTCP_RECEIVER_REPORT *report)
{
    /*
        TODO: Add user implementation code..

        Note!: since this callback is invoked from the IP thread, the application should not block in this callback.

        Tip: in this callback, we can obtain and record below information:
            1) report -> receiver_ssrc: the ssrc of the receiver who sends the rr report
            2) report -> fraction_loss: the fraction lost of the receiver
            3) report -> packet_loss: the cumulative number of packets lost of the receiver
            4) report -> extended_max: the extended highest sequence number received of the receiver
            5) report -> jitter: the inter-arrival jitter of the receiver
            6) report -> last_sr: the last SR timestamp of the receiver
            7) report -> delay: the delay since last SR timestamp of the receiver.
    */

    return(NX_SUCCESS);
}

static UINT demo_rtcp_sdes_callback(NX_RTCP_SDES_INFO *sdes_info)
{
    /*
        TODO: Add user implementation code..

        Note!: since this callback is invoked from the IP thread, the application should not block in this callback.

        Tip: in this callback, we can obtain and record below information:
            1) sdes_info -> ssrc: the ssrc of the receiver who sends the sdes packet
            2) sdes_info -> cname_length: the length of the cname field
            3) sdes_info -> cname: the cname field
    */

    return(NX_SUCCESS);
}

/* Define what the initial system looks like.  */
void sample_entry(NX_IP *ip_ptr, NX_PACKET_POOL *pool_ptr, VOID *dns_ptr,
                  UINT (*unix_time_callback)(ULONG *unix_time))
{

UINT        status;
NXD_ADDRESS client_ip_address;
NX_PACKET  *send_packet;


    /* Create RTP sender.  */
    status = nx_rtp_sender_create(&rtp_0, ip_ptr, pool_ptr, DEMO_CNAME, sizeof(DEMO_CNAME) - 1);
    if (status)
        error_count++;

    /* Register RR report callback function. */
    status = nx_rtp_sender_rtcp_receiver_report_callback_set(&rtp_0, demo_rtcp_receiver_report_callback);
    if (status)
        error_count++;

    /* Register SDES report callback function. */
    status = nx_rtp_sender_rtcp_sdes_callback_set(&rtp_0, demo_rtcp_sdes_callback);
    if (status)
        error_count++;

    /* Setup rtp sender session for video data send.  */
    client_ip_address.nxd_ip_version = NX_IP_VERSION_V4;
    client_ip_address.nxd_ip_address.v4 = RTP_CLIENT_ADDRESS;
    status = nx_rtp_sender_session_create(&rtp_0, &rtp_session_0, RTP_PAYLOAD_TYPE_VIDEO,
                                          0, &client_ip_address,
                                          RTP_CLIENT_RTP_PORT, RTP_CLIENT_RTCP_PORT);
    if (status)
        error_count++;

    /* Setup rtp sender session for audio data send.  */
    status = nx_rtp_sender_session_create(&rtp_0, &rtp_session_1, RTP_PAYLOAD_TYPE_AUDIO,
                                          0, &client_ip_address,
                                          RTP_CLIENT_RTP_PORT, RTP_CLIENT_RTCP_PORT);
    if (status)
        error_count++;

    /* Use h264 and aac api to send video and audio data */
    status = nx_rtp_sender_session_h264_send(&rtp_session_0, (void*)demo_rtp_video_packet_data,
                                             sizeof(demo_rtp_video_packet_data),
                                             DEMO_RTP_TIMESTAMP, DEMO_NTP_MSW, DEMO_NTP_LSW, NX_TRUE);
    if (status)
        error_count++;

    status = nx_rtp_sender_session_aac_send(&rtp_session_1, (void*)demo_rtp_audio_packet_data,
                                            sizeof(demo_rtp_audio_packet_data),
                                            DEMO_RTP_TIMESTAMP, DEMO_NTP_MSW, DEMO_NTP_LSW, NX_TRUE);
    if (status)
        error_count++;

    /* Note!: for PCM audio payload, below typical procedure could be applied to send an RTP packet. */
    //
    // /* Allocate a packet. */
    // status = nx_rtp_sender_session_packet_allocate(&rtp_session_0, &send_packet, 5 * NX_IP_PERIODIC_RATE);
    // if (status)
    //     error_count++;
    //
    // /* Copy payload data into the packet. */
    // status = nx_packet_data_append(send_packet, (void*)demo_rtp_audio_packet_data,
    //                                sizeof(demo_rtp_audio_packet_data),
    //                                rtp_0.nx_rtp_sender_ip_ptr -> nx_ip_default_packet_pool,
    //                                5 * NX_IP_PERIODIC_RATE);
    // if (status)
    //     error_count++;
    //
    // /* Send RTP packet data. */
    // status = nx_rtp_sender_session_packet_send(&rtp_session_0, send_packet, DEMO_RTP_TIMESTAMP,
    //                                            DEMO_NTP_MSW, DEMO_NTP_LSW, NX_TRUE);
    // if (status)
    //     error_count++;
}

Demo System of NetX Duo RTSP Server over RTP Sender

The above example is simple. More detailed information about how to use RTP sender to send RTP packets to the client can be found inside NetX Duo rtsp folder, with a demo module called demo_rtsp_over_rtp. The demo system is described below.

Caution: This is provided for demonstration purposes only and is not guaranteed to compile and run as is.

Please refer to the NetX Duo RTP Sender release code distribution for demo source code file(s) that will properly build in the native Eclipse ThreadX environment.  Also be aware that these demos are intentionally kept very simple as they are intended to introduce NetX Duo RTP Sender application to new users.

In this demo, the RTP sender is created and started in the sample_entry. The RTP sender is created with the nx_rtp_sender_create API. Then, the test_rtcp_receiver_report_callback and test_rtcp_sdes_callback are registered by calling API functions nx_rtp_sender_rtcp_receiver_report_callback_set and nx_rtp_sender_rtcp_sdes_callback_set separately.

In the rtsp_setup_callback, the transport information between RTSP server and client is set back to the transport_ptr parameter. Therefore, firstly, the nx_rtp_sender_port_get is called to get bound RTP/RTCP port pair in the specific RTP sender, and the bound port pair are recorded into transport_ptr. An RTP session for transferring specific payload type data requested by the RTSP client is created by calling nx_rtp_sender_session_create. After the session created, the session ssrc can be read by calling nx_rtp_sender_session_ssrc_get and recorded into transport_ptr as well. All recorded RTP information are set to the SETUP response to the RTSP client. If sample-based encoding payload format such as PCM is applied, the sample factor can be configured by calling nx_rtp_sender_session_sample_factor_set. The sample factor is computed by the audio sample size (DEMO_AUDIO_SAMPLE_SIZE) and audio channel number (DEMO_AUDIO_CHANNEL_NUM), with example as follows:

  1) sample bits:  8, channel number: 1, factor = 1 * (8/8) = 1
  2) sample bits: 16, channel number: 1, factor = 1 * (16/8) = 2
  3) sample bits: 16, channel number: 2, factor = 2 * (16/8) = 4

In the rtsp_play_callback, the current sequence number in RTP session is read by calling nx_rtp_sender_session_sequence_number_get and set into RTSP response to the RTSP client.

In the rtsp_teardown_callback, since there is no need to maintain the RTP session(s) with the specific receiver when this callback is invokes, it is fine to delete the session by calling nx_rtp_sender_session_delete.

RTP payload data sending is mainly implemented in the test_server_entry. Both unicast and multicast RTP payload data sending are supported. By default, unicast is applied and multicast can replace unicast by defining the macro DEMO_MULTICAST_ENABLED. This demo shows 2 ways how RTP payload data sending is triggered. By default, users can trigger audio data transfer or video data transfer by calling tx_event_flags_set(&demo_test_events, DEMO_AUDIO_DATA_READY_EVENT, TX_OR) or tx_event_flags_set(&demo_test_events, DEMO_VIDEO_DATA_READY_EVENT, TX_OR) separately. Users can also define the macro DEMO_PLAY_BY_TIMER to enable software timer triggered RTP payload data sending. When this macro is defined, a software timer is automatically enabled with default 10-millisecond period defined by the macro DEMO_PLAY_TIMER_INTERVAL. Users can define the value of macros DEMO_AUDIO_FRAME_PER_SECOND and DEMO_VIDEO_FRAME_PER_SECOND to control the audio and video data sending rate. The default value of these 2 macros are 30 and 43, respectively. The sending rate shall be the same as the real audio or video FPS to guarantee media playing normally. This demo also shows how different RTP payload data to send, with 2 video types and 2 audio types. By default, the demo shows H.264 and AAC data sending. Users can replace video type with MJPEG by re-defining the macro DEMO_AUDIO_FORMAT with DEMO_VIDEO_FORMAT_MJPEG; and replace audio type with PCM by re-defining the macro DEMO_AUDIO_FORMAT with DEMO_AUDIO_FORMAT_PCM.

No matter which configuration combination is applied as described above, users need to implement the following callback function(s) to provide payload data and also define following macro(s):

/* Define this callback function if users want to execute any code after RTSP PLAY command is received; otherwise, left this macro to be its default value. For example, in this function, it is suitable for users to execute camera initialization or audio initialization corresponding codes. */

#define DEMO_MEDIA_DATA_INIT  demo_media_data_init_callback

VOID (*demo_media_data_init_callback)(VOID)
{
    /* User implementation here to initialize media data.  */
}
/* Define this callback function if users want to run the demo code for RTP audio data transfer; otherwise, left this macro to be its default value.  */
#define DEMO_AUDIO_DATA_READ  demo_audio_data_read_callback

UINT (*demo_audio_data_read_callback)(UCHAR **data_ptr, ULONG *data_size)
{
    /* User implementation here to read audio data.  */
}
/* Define this callback function if users want to run the demo code for RTP video data transfer; otherwise, left this macro to be its default value.  */
#define DEMO_VIDEO_DATA_READ  demo_video_data_read_callback

UINT (*demo_video_data_read_callback)(UCHAR **data_ptr, ULONG *data_size)
{
    /* User implementation here to read video data.  */
}

Configuration Options

There are several configuration options for building NetX Duo RTP sender. The default values are listed but can be redefined prior to inclusion of nx_rtp_sender.h. The following list describes each in detail:

  • NX_RTP_SENDER_TYPE_OF_SERVICE The type of service for RTP UDP requests. By default, this value is defined as NX_IP_NORMAL.
  • NX_RTP_SENDER_FRAGMENT_OPTION The fragment option for RTP UDP requests. By default, this value is defined as NX_FRAGMENT_OKAY.
  • NX_RTP_SENDER_TIME_TO_LIVE The TTL for RTP UDP requests. By default, this value is defined as 0x80.
  • NX_RTP_SENDER_QUEUE_DEPTH The maximum depth of receive queue for RTSP UDP requests. By default, this value is defined as 5.
  • NX_RTP_SENDER_PACKET_TIMEOUT The suspension option for packet data operations. By default, this value is defined as NX_IP_PERIODIC_RATE.
  • NX_RTCP_INTERVAL The period for automatic RTCP packet sending. By default, this value is defined as 5.
Prev
Chapter 1 - Introduction to the NetX Duo RTP Sender
Next
Chapter 3 - Description of NetX Duo RTP Sender Services