1. 程式人生 > >基於jrtplib實現的最簡單服務端和客戶端

基於jrtplib實現的最簡單服務端和客戶端

使用Jrtplib庫實現服務端和客戶端通訊

程式碼都是直接從jrtplib自帶例子中摳出來的,就是為了方便研究,話不多說直接上菜

服務端程式碼:
/*
   Here's a small IPv4 example for receiver
*/

/*
#include "rtpsession.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#include "rtplibraryversion.h"
*/ #include <jrtplib3/rtpsession.h> #include <jrtplib3/rtpudpv4transmitter.h> #include <jrtplib3/rtpipv4address.h> #include <jrtplib3/rtpsessionparams.h> #include <jrtplib3/rtperrors.h> #include <jrtplib3/rtplibraryversion.h> #include <jrtplib3/rtppacket.h> #include
<stdlib.h> #include <stdio.h> #include <iostream> #include <string> using namespace jrtplib; using namespace std; // // 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 RTP_SOCKETTYPE_WINSOCK WSADATA dat; WSAStartup(MAKEWORD(2,2),&dat); #endif // RTP_SOCKETTYPE_WINSOCK RTPSession sess; uint16_t portbase; int status,i,num; cout << "Using version " << RTPLibraryVersion::GetVersion().GetVersionString() << endl; cout << "Enter local portbase:" << endl; cin >> portbase; cout << endl; // 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); while (1) { sess.BeginDataAccess(); // check incoming packets if (sess.GotoFirstSourceWithData()) { do { RTPPacket *pack; while ((pack = sess.GetNextPacket()) != NULL) { // You can examine the data here cout << "Got packet !" << endl; cout << pack->GetPayloadData() << endl; // 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 RTP_SOCKETTYPE_WINSOCK WSACleanup(); #endif // RTP_SOCKETTYPE_WINSOCK return 0; }
客戶端程式碼:
/*
   Here's a small IPv4 example for client
*/

/*
#include "rtpsession.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#include "rtplibraryversion.h"
*/

#include <jrtplib3/rtpsession.h>
#include <jrtplib3/rtpudpv4transmitter.h>
#include <jrtplib3/rtpipv4address.h>
#include <jrtplib3/rtpsessionparams.h>
#include <jrtplib3/rtperrors.h>
#include <jrtplib3/rtplibraryversion.h>
#include <jrtplib3/rtppacket.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace jrtplib;
using namespace std;

//
// 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 RTP_SOCKETTYPE_WINSOCK
    WSADATA dat;
    WSAStartup(MAKEWORD(2,2),&dat);
#endif // RTP_SOCKETTYPE_WINSOCK

    RTPSession sess;
    uint16_t portbase,destport;
    uint32_t destip;
    string ipstr;
    int status,i,num;

    cout << "Using version " << RTPLibraryVersion::GetVersion().GetVersionString() << endl;

    // First, we'll ask for the necessary information
    cout << "Enter local portbase:" << endl;
    cin >> portbase;
    cout << endl;
    cout << "Enter the destination IP address" << endl;
    cin >> ipstr;

    destip = inet_addr(ipstr.c_str());

    if (destip == INADDR_NONE)
    {
        cerr << "Bad IP address specified" << 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);
    cout << "Enter the destination port" << endl;
    cin >> destport;
    cout << endl;
    cout << "Number of packets you wish to be sent:" << endl;
    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);

#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 RTP_SOCKETTYPE_WINSOCK
    WSACleanup();
#endif // RTP_SOCKETTYPE_WINSOCK

    return 0;
}
程式碼已經很全,如有需要可以 下載