天天看點

jrtplib3.9.1 example5

*轉載時請注明出處。
 
 
  http://blog.csdn.net/str999_cn/article/details/28173559

 
 
  

 
 
  

 
 
      此代碼執行時,如果隻發送1個資料包,發送和接收完畢,會自動退出。沒問題。但一旦發送的資料包等會或超過2個時,則程式執行時會無休無止地配置設定和釋放,還沒來得及去搞清為什麼是這樣的。
 
 
  

 
 
  

 
 
      執行圖檔如下:
 
 
  
        
jrtplib3.9.1 example5
jrtplib3.9.1 example5
/* This is a modified version of example1.cpp to illustrate the use of a memory manager. 這是對示例1的一個修改版本,用以展示記憶體管理的使用 */ #include "rtpsession.h" #include "rtppacket.h" #include "rtpudpv4transmitter.h" #include "rtpipv4address.h" #include "rtpsessionparams.h" #include "rtperrors.h" #include "rtpmemorymanager.h" #ifndef WIN32 #include <netinet/in.h> #include <arpa/inet.h> #else #include <winsock2.h> #endif // WIN32 #include <stdlib.h> #include <stdio.h> #include <iostream> #include <string> using namespace jrtplib ; // // This function checks if there was a RTP error. If so, it displays an error // message and exists. // 本函數檢查是否有RTP錯誤。如果有,本函數顯示錯誤資訊,然後退出。 void checkerror ( int rtperr ) { if ( rtperr < 0 ) { std :: cout << "ERROR: " << RTPGetErrorString ( rtperr ) << std :: endl ; exit ( - 1 ); } } #ifdef RTP_SUPPORT_THREAD //此處定義了RTP_SUPPORT_THREAD,會用到jthread這個單獨的庫。是以多定義了一個JMutex mutex;在進行記憶體配置設定和釋放的時候,需要加鎖和解鎖。 using namespace jthread ; class MyMemoryManager : public RTPMemoryManager { public: MyMemoryManager () { mutex . Init (); alloccount = 0 ; freecount = 0 ; } ~ MyMemoryManager () { std :: cout << "alloc: " << alloccount << " free: " << freecount << std :: endl ; } void * AllocateBuffer ( size_t numbytes , int memtype ) { mutex . Lock (); void * buf = malloc ( numbytes ); std :: cout << "Allocated " << numbytes << " bytes at location " << buf  << " (memtype = " << memtype << ")" << std :: endl ; alloccount ++ ; mutex . Unlock (); return buf ; } void FreeBuffer ( void * p ) { mutex . Lock (); std :: cout << "Freeing block " << p << std :: endl ; freecount ++ ; free ( p ); mutex . Unlock (); } private: int alloccount , freecount ; JMutex mutex ; }; #else class MyMemoryManager : public RTPMemoryManager { public: MyMemoryManager () { alloccount = 0 ; freecount = 0 ; } ~ MyMemoryManager () { std :: cout << "alloc: " << alloccount << " free: " << freecount << std :: endl ; } void * AllocateBuffer ( size_t numbytes , int memtype ) { void * buf = malloc ( numbytes ); std :: cout << "Allocated " << numbytes << " bytes at location " << buf  << " (memtype = " << memtype << ")" << std :: endl ; alloccount ++ ; return buf ; } void FreeBuffer ( void * p ) { std :: cout << "Freeing block " << p << std :: endl ; freecount ++ ; free ( p ); } private: int alloccount , freecount ; }; #endif // RTP_SUPPORT_THREAD int main ( void ) { #ifdef WIN32 WSADATA dat ; WSAStartup ( MAKEWORD ( 2 , 2 ), & dat ); #endif // WIN32 MyMemoryManager mgr ; RTPSession sess ( 0 , & mgr ); uint16_t portbase , destport ; uint32_t destip ; std :: string ipstr ; int status , i , num ;          // First, we'll ask for the necessary information // 首先,讓使用者輸入一些必要的資訊 std :: cout << "Enter local portbase:" << std :: endl ; std :: cin >> portbase ; std :: cout << std :: endl ; std :: cout << "Enter the destination IP address" << std :: endl ; std :: cin >> ipstr ; destip = inet_addr ( ipstr . c_str ()); if ( destip == INADDR_NONE ) { std :: cerr << "Bad IP address specified" << std :: endl ; return - 1 ; } // The inet_addr function returns a value in network byte order, but // we need the IP address in host byte order, so we use a call to // ntohl // 函數inet_addr 傳回網絡位元組序的值,但我們需要的IP位址是要主機位元組序,是以我們調用ntohl函數 destip = ntohl ( destip ); std :: cout << "Enter the destination port" << std :: endl ; std :: cin >> destport ; std :: cout << std :: endl ; std :: cout << "Number of packets you wish to be sent:" << std :: endl ; std :: cin >> num ; // Now, we'll create a RTP session, set the destination, send some // packets and poll for incoming data. // 現在,我們将建立一個RTP會話,設定目的端點,發送一些資料包,然後輪詢等待發進來的資料 RTPUDPv4TransmissionParams transparams ; RTPSessionParams sessparams ; // IMPORTANT: The local timestamp unit MUST be set, otherwise // RTCP Sender Report info will be calculated wrong // In this case, we'll be sending 10 samples each second, so we'll // put the timestamp unit to (1.0/10.0) // 重要資訊:本地的時間戳機關必須要設定,否則RTCP發送報告資訊将會計算錯誤 // 在本例,我們将每秒發送10個樣本,是以我們将時間戳機關設定為(1.0/10.0) sessparams . SetOwnTimestampUnit ( 1.0 / 10.0 ); sessparams . SetAcceptOwnPackets ( true ); transparams . SetPortbase ( portbase ); status = sess . Create ( sessparams , & transparams ); checkerror ( status ); RTPIPv4Address addr ( destip , destport ); status = sess . AddDestination ( addr ); checkerror ( status ); for ( i = 1 ; i <= num ; i ++ ) { printf ( " \n Sending packet %d/%d \n " , i , num ); // send the packet 發送資料包 status = sess . SendPacket (( void * ) "1234567890" , 10 , 0 , false , 10 ); checkerror ( status ); sess . BeginDataAccess (); // check incoming packets檢查發送進來的資料包 if ( sess . GotoFirstSourceWithData ()) { do { RTPPacket * pack ; while (( pack = sess . GetNextPacket ()) != NULL ) { // You can examine the data here 你可以在此處檢查(收到的)資料 printf ( "Got packet ! \n " ); // we don't longer need the packet, so // we'll delete it // 我們不再需要這個資料包,是以我們删除它 sess . DeletePacket ( pack ); } } while ( sess . GotoNextSourceWithData ()); } sess . EndDataAccess (); #ifndef RTP_SUPPORT_THREAD status = sess . Poll (); checkerror ( status ); #endif // RTP_SUPPORT_THREAD RTPTime :: Wait ( RTPTime ( 1 , 0 )); } sess . BYEDestroy ( RTPTime ( 10 , 0 ), 0 , 0 ); #ifdef WIN32 WSACleanup (); #endif // WIN32 return 0 ; }