用的網路協議都是RTP/RTCP,網絡上找了不少資料,本來想整個現成的例子看看,下了不少發現都不滿意,只好自己找到那個很牛的老外Jori寫的開源的RTP庫jrtplib研究一下自己編譯一下,學學了。
首先從他的個人網站上http://research.edm.uhasselt.be/~jori/page/index.php?n=CS.Jrtplib下載最新的jrtplib-3.9.0,以及最新的jthread-1.3.0(他的頁面上也有下載),解壓完之後,看了看ReadMe發現需要Cmake編譯,oh!mygod!慢慢來吧。
使用Cmake_gui 2.8版本,首先編譯生成jthread.lib。
添加完路徑之後,很順利的就可以得到vs2005下的工程文件,編譯之後可以在C:/Program Files/jthread/include(默認的路徑)下看到jthread的頭文件以及在C:/Program Files/jthread/lib下的lib文件(一般吧debug和release都編譯一下)。
然後就是jrtplib的編譯了。
jrtplib的cmake編譯需要jthread.lib(jthread_d.lib)編譯,cmake不會用,開始時出現錯誤,後來自己摸索着試了試,成功了,就是在編譯幾個example時發現有warning,也沒什麼關係。設置如圖所示
這樣再generate就可以了(不知道以後有沒有問題,反正現在沒問題)。
然後就是vs2005中的生成lib庫了。
首先需要將所有jrtplib中src下的頭文件中包含jmutex.h和jthread.h的include全部修改爲include "jmutex."和include"jthread.h",然後進行下一步
打開工程文件編譯之後安裝,可以在C:\Program Files\jrtplib下面看到兩個文件夾include和lib分別放着頭文件和庫文件。
打開example1,進行相應的設置之後run,發現可以運行了!
這樣應該就可以在工程中使用jrtplib庫了---------還沒有試,期待ing。。。。。
另:在Jori的主頁上有關於這兩個庫jrtplib和jthread的使用手冊的pdf下載可以參考。
NOTE:開始時沒有將jthread和jmutex的include修改,雖然編譯成功了,運行example也可以但是自己編的程序就出問題,後來參考了一下http://blog.csdn.net/aaronalan/article/details/5153604 重新編譯的才行了。
附上一個在網上找到的例子:
- /*
- Here's a small IPv4 example: it asks for a portbase and a destination and
- starts sending packets to that destination.
- */
- #include "rtpsession.h"
- #include "rtpudpv4transmitter.h"
- #include "rtpipv4address.h"
- #include "rtpsessionparams.h"
- #include "rtperrors.h"
- #include "rtppacket.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;
- #pragma comment(lib,"jrtplib_d.lib")
- #pragma comment(lib,"jthread_d.lib")
- #pragma comment(lib,"ws2_32.lib")
- //
- // This function checks if there was a RTP error. If so, it displays an error
- // message and exists.
- //
- void checkerror(int rtperr)
- {
- if (rtperr < 0)
- {
- std::cout << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
- exit(-1);
- }
- }
- //
- // The main routine
- //
- int main(void)
- {
- #ifdef WIN32
- WSADATA dat;
- WSAStartup(MAKEWORD(2,2),&dat);
- #endif // WIN32
- RTPSession sess;
- uint16_t portbase,destport;
- uint32_t destip;
- std::string ipstr;
- int status,i,num;
- BYTE *pBuffer;
- BYTE *pfBuffer;
- // 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
- 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.
- 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)
- 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("\nSending 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");
- std::cout << "Got packet with "
- << "extended sequence number "
- << pack->GetExtendedSequenceNumber()
- << " from SSRC " << pack->GetSSRC()
- << std::endl;
- int dataLength = pack->GetPayloadLength();
- pfBuffer =(unsigned char*)pack->GetPayloadData();
- pBuffer = new BYTE[dataLength + 1];
- memcpy(pBuffer, pfBuffer, dataLength);
- pBuffer[dataLength] = 0;
- std::cout << pBuffer << std::endl;
- delete []pBuffer;
- pBuffer = NULL;
- // 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;
- }