Use the jrtplib library to transmit video files-the sender Program

Source: Internet
Author: User
Use the jrtplib library to transmit video files-the sender Program

(18:09:11)

Reprinted token
Tags:

Jrtplib

 

RTP

 

RTCP

 

It
Category: Other technical articles

 // Jrtplib sender on ADSP-BF537
 // Wangsu820@163.com2008-06-30

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <unistd. h>
# Include <iostream>

# Include <sys/socket. h>
 
# Include "rtpsession. H"
# Include "rtpsessionparams. H"
# Include "rtpudpv4transmitter. H"
# Include "rtp00004address. H"
# Include "rtperrors. H"

Using namespace STD;
// Debug information
# Define debug

Void checkerror (INT errorcode)
{
  If (errorcode <0)
  {
    STD: cout <"error:" <rtpgterrorstring (errorcode) <STD: Endl;
    Exit (-1 );
  }
}

Void usage (void)
{
  STD: cout <"Usage:./bf537_send destinationipaddress destinationport filepath" <STD: Endl;
}

Long getfilelen (File * FP)
{
  Long cur_pos;
  Long Len;
  
  If (null = FP)
  {
    Printf ("file FP is null \ n ");
    Exit (-1 );
  }
  Cur_pos = ftell (FP );
  Fseek (FP, 0, seek_end );
  Len = ftell (FP );
  Fseek (FP, cur_pos, seek_set );
  Return Len;
}

Int main (INT argc, char ** argv)
{
  Rtpsession rt1_ss;
  Rtpsessionparams sessionparams;
  Rtpudpv4transmissionparaMS transparams;
  Unsigned long destip;
  Int destport, srcport = 8000;
  Char * filepath;
  File * FP;
  Long filelength, templength, maxpacket, sendsize;
  Unsigned char * MEM;
  Int status;
  
  // Command Line Parameter Parsing
  If (argc! = 4)
  {
    Usage ();
    Return-1;
  }
  
  Destip = inet_addr (argv [1]);
  If (inaddr_none = destip)
  {
    Cerr <"Bad IP Address" <Endl;
    Return-1;
  }
  Destip = ntohl (destip );
  Cout <"Destination IP Address :"
       <Argv [1]
       <Endl;

  Destport = atoi (argv [2]);
  Cout <"Destination Port :"
       <Destport
       <Endl;
  
  Strcpy (filepath, argv [3]);
  Cout <"filepath :"
       <Filepath
       <Endl;
    
  // Create a session
  Sessionparams. setowntimestampunit (1.0/8000.0 );
  Transparams. setportbase (srcport );
  Status = rtdomainss. Create (sessionparams, & transparams );
# Ifdef debug
  Cout <"check if create session error" <Endl;
# Endif 
  Checkerror (Status );
  
  // Add the target address to the sending Address List
  Rtp00004address ADDR (destip, destport );
  Status = rt1_ss. adddestination (ADDR );
# Ifdef debug
  Cout <"check if adddestination error" <Endl;
# Endif  
  Checkerror (Status );
  
  // Set the default parameters of the RTP package
  Rtpsess. setdefapaypayloadtype (0 );
  Rtpsess. setdefamark mark (false );
  Rt1_ss. setdefatimetimestampincresMent (10 );
  
  // Open the file
  Fp = fopen (filepath, "rb ");
  If (null = FP)
  {
    Perror ("fopen :");
    Exit (-1 );
  }

  // Get the file size
  Filelength = getfilelen (FP );
# Ifdef debug
  Printf ("the size of % s is % ldkb \ n", filepath, (filelength/1024 ));
# Endif 

  // Obtain the maximum allowed Package Size
  Maxpacket = sessionparams. getmaximumpacketsize ();
# Ifdef debug
  Printf ("Maximum packet size is % ldkb \ n", (maxpacket/1024 ));
# Endif
  Maxpacket-= 12;
# Ifdef debug
  Printf ("maximum payload size per packet is % ldkb (% LDB) \ n", (maxpacket/1024), maxpacket );
# Endif 

  // Allocate memory space to store the entire file
  Mem = (unsigned char *) malloc (filelength );
# Ifdef debug
  Printf ("memory buffer is 0x % 8x \ n", (INT) MEm );
# Endif  

  // Read the file content to the memory
  Templength = fread (MEm, 1, filelength, FP );
  If (templength! = Filelength)
  {
     Printf ("fread error: templength = % lD, filelength = % LD \ n", templength, filelength );
     Exit (-1 );
  }
# Ifdef debug
  Printf ("have fread % ldkb \ n", (templength/1024 ));
# Endif 

  // Close the file
  Fclose (FP );
 
  
  Sendsize = filelength;

  // Send a file
  If (filelength <= maxpacket)
  {
# Ifdef debug
    Printf ("we will send data in a single packet \ n ");
# Endif   
    Status = rtcancss. sendpacket (MEm, filelength );
    Checkerror (Status );
    Printf ("sent % s... [% ldkb/% ldkb]", filepath, (filelength/1024), (filelength/1024 ));
  }
  Else // filelength> the maxpacket file is larger than the maximum package size and must be sent through subcontracting.
  {
# Ifdef debug
     Printf ("we will divide data into serval packets to send \ n ");
# Endif
     While (1)
     {
       If (sendsize <= maxpacket)
       {
         Status = rt1_ss. sendpacket (MEm, sendsize );
         Checkerror (Status );
         Printf ("sent % s... [% ldkb/% ldkb] \ n", filepath,
           (Filelength/1024), (filelength/1024 ));
         Break;
       }
       Status = rtdeskss. sendpacket (void *) MEm, maxpacket );
       Checkerror (Status );
       Sendsize-= maxpacket;
       Printf ("sent % s... [% ldkb/% ldkb] \ n", filepath,
         (Filelength-sendsize)/1024), (filelength/1024 ));
       Mem + = maxpacket;
       Rtptime: Wait (rtptime (0,200 ));
     } 
  } 
  
   // Exit the session
  Rt1_ss. byedestroy (rtptime (10, 0), 0, 0 );
# Ifdef debug
  Cout <"Goodbye RTP session" <Endl;
# Endif   
  Return 0;
}

Reproduced in: http://blog.sina.com.cn/s/blog_4e87ac6e01009y26.html

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.