RTP over TCP example in jrtplib

昨天写了一段rtp over tcp, 还没来得及调试,突然想起来jrtplib里会不会有现成的,进去一顿翻,靠,真有一个,

jrtplib-3.11.1/tests/tcptest.cpp

#include "rtpconfig.h"
#include "rtpsocketutil.h"
#include "rtpsocketutilinternal.h"
#include "rtpsession.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#include "rtpsourcedata.h"
#include "rtptcpaddress.h"
#include "rtptcptransmitter.h"
#include "rtppacket.h"
#include <string.h>
#include 
#include 
#include 

using namespace std;
using namespace jrtplib;

inline void checkerror(int rtperr)
{
    if (rtperr < 0)
    {
        cerr << "ERROR: " << RTPGetErrorString(rtperr) << std::endl;
        exit(-1);
    }
}

class MyRTPSession : public RTPSession
{
public:
    MyRTPSession() : RTPSession() { }
    ~MyRTPSession() { }
protected:
    void OnValidatedRTPPacket(RTPSourceData *srcdat, RTPPacket *rtppack, bool isonprobation, bool *ispackethandled)
    {
        printf("SSRC %x Got packet (%d bytes) in OnValidatedRTPPacket from source 0x%04x!\n", GetLocalSSRC(), 
               (int)rtppack->GetPayloadLength(), srcdat->GetSSRC());
        DeletePacket(rtppack);
        *ispackethandled = true;
    }

    void OnRTCPSDESItem(RTPSourceData *srcdat, RTCPSDESPacket::ItemType t, const void *itemdata, size_t itemlength)
    {
        char msg[1024];

        memset(msg, 0, sizeof(msg));
        if (itemlength >= sizeof(msg))
            itemlength = sizeof(msg)-1;

        memcpy(msg, itemdata, itemlength);
        printf("SSRC %x Received SDES item (%d): %s from SSRC %x\n", GetLocalSSRC(), (int)t, msg, srcdat->GetSSRC());
    }
};

class MyTCPTransmitter : public RTPTCPTransmitter
{
public:
    MyTCPTransmitter(const string &name) : RTPTCPTransmitter(0), m_name(name) { }

    void OnSendError(SocketType sock)
    {
        cout << m_name << ": Error sending over socket " << sock << ", removing destination" << endl;
        DeleteDestination(RTPTCPAddress(sock));
    }
    
    void OnReceiveError(SocketType sock)
    {
        cout << m_name << ": Error receiving from socket " << sock << ", removing destination" << endl;
        DeleteDestination(RTPTCPAddress(sock));
    }
private:
    string m_name;
};

void runTest(int sock1, int sock2)
{
    const int packSize = 1500;
    RTPSessionParams sessParams;
    MyTCPTransmitter trans1("Transmitter1"), trans2("Transmitter2");
    MyRTPSession sess1, sess2;

    sessParams.SetProbationType(RTPSources::NoProbation);
    sessParams.SetOwnTimestampUnit(1.0/packSize);
    sessParams.SetMaximumPacketSize(packSize + 64); // some extra room for rtp header

    bool threadsafe = false;
#ifdef RTP_SUPPORT_THREAD
    threadsafe = true;
#endif // RTP_SUPPORT_THREAD

    checkerror(trans1.Init(threadsafe));
    checkerror(trans2.Init(threadsafe));
    checkerror(trans1.Create(65535, 0));
    checkerror(trans2.Create(65535, 0));

    checkerror(sess1.Create(sessParams, &trans1));
    cout << "Session 1 created " << endl;
    checkerror(sess2.Create(sessParams, &trans2));
    cout << "Session 2 created " << endl;

    checkerror(sess1.AddDestination(RTPTCPAddress(sock1)));
    checkerror(sess2.AddDestination(RTPTCPAddress(sock2)));

    vector pack(packSize);

    int num = 10;
    for (int i = 1 ; i <= num ; i++)
    {
        printf("\nSending packet %d/%d\n",i,num);
        
        // send a packet, alternating between a large packet and a zero length one
        //int len = (i%2 == 0)?pack.size():0;
        int len = 1500;
        checkerror(sess1.SendPacket((void *)&pack[0],len,0,false,10));

        //if (i == 10)
        //    RTPCLOSE(sock1); // Induce an error when sending/receiving

        // Either the background thread or the poll function itself will
        // cause the OnValidatedRTPPacket and OnRTCPSDESItem functions to
        // be called, so in this loop there's not much left to do. 

        sess2.BeginDataAccess();
        
        // check incoming packets
        if (sess2.GotoFirstSourceWithData())
        {
            do
            {
                RTPPacket *pack;
                
                while ((pack = sess2.GetNextPacket()) != NULL)
                {
                    // You can examine the data here
                    printf("Got packet !\n");
                    
                    // we don't longer need the packet, so
                    // we'll delete it
                    sess2.DeletePacket(pack);
                }
            } while (sess2.GotoNextSourceWithData());
        }
        
        sess2.EndDataAccess();
        
#ifndef RTP_SUPPORT_THREAD
        checkerror(sess1.Poll());
        checkerror(sess2.Poll());
#endif // RTP_SUPPORT_THREAD
        
        //RTPTime::Wait(RTPTime(1,0));
    }
    sess1.BYEDestroy(RTPTime(10,0),0,0);
    sess2.BYEDestroy(RTPTime(10,0),0,0);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " portnumber" << endl;
        return -1;
    }

    // Create a listener socket and listen on it
    SocketType listener = socket(AF_INET, SOCK_STREAM, 0);
    if (listener == RTPSOCKERR)
    {
        cerr << "Can't create listener socket" << endl;
        return -1;
    }

    struct sockaddr_in servAddr;

    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = AF_INET;
    servAddr.sin_port = htons(atoi(argv[1]));

    if (bind(listener, (struct sockaddr *)&servAddr, sizeof(servAddr)) != 0)
    {
        cerr << "Can't bind listener socket" << endl;
        return -1;
    }

    listen(listener, 1);

    // Create a client socket and connect to the listener
    SocketType client = socket(AF_INET, SOCK_STREAM, 0);
    if (client == RTPSOCKERR)
    {
        cerr << "Can't create client socket" << endl;
        return -1;
    }

    servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if (connect(client, (struct sockaddr *)&servAddr, sizeof(servAddr)) != 0)
    {
        cerr << "Can't connect to the listener socket" << endl;
        return -1;
    }

    SocketType server = accept(listener, 0, 0);
    if (server == RTPSOCKERR)
    {
        cerr << "Can't accept incoming connection" << endl;
        return -1;
    }
    RTPCLOSE(listener);

    cout << "Got connected socket pair" << endl;

    runTest(server, client);

    cout << "Done." << endl;
    RTPCLOSE(server);
    RTPCLOSE(client);

    return 0;
}

 

build.sh

export LD_LIBRARY_PATH=$(pwd)/_install/lib:$LD_LIBRARY_PATH

rm ./example1 ./tcptest

g++ example1.cpp -I./_install/include -I./_install/include/jrtplib3 -L./_install/lib -g -o example1 -lpthread -ljrtp -lm
g++ tcptest.cpp -I./_install/include -I./_install/include/jrtplib3 -L./_install/lib -g -o tcptest -lpthread -ljrtp -lm

 

dong@ubuntu:~/jrtplib_example$ ./tcptest 15000
Got connected socket pair
Session 1 created
Session 2 created

Sending packet 1/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 2/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 3/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 4/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 5/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 6/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 7/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 8/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 9/10
SSRC fed93305 Got packet (1500 bytes) in OnValidatedRTPPacket from source 0xd244e47a!

Sending packet 10/10
Done.
dong@ubuntu:~/jrtplib_example$

 

jrtplib注意事项:

https://www.cnblogs.com/dong1/p/10492229.html

 

你可能感兴趣的:(RTP over TCP example in jrtplib)