p2p audio and video solution

P2P audio and video transmission technology is designed to save server bandwidth. The HYP2P SDK developed by Haoyou Technology is a set of general-purpose p2p sdk libraries that have nothing to do with business. It is easy to access and supports various scenarios developed with tcp or udp, such as: rtmp,rtstp protocol, udp file transfer, etc. At present, it is mainly used in Internet of Things related industries such as network cameras, video door locks, pet machines, sweeping machines, and building intercoms. Support Ankai AK, Hisilicon HI, Ingenic, Ambarella, Rockchip and other linux, Android, IOS and windows platforms. There are sample codes for reference, and you can download and test them for free.

Introduction to HYP2P SDK

First, through a simple example, establish a p2p connection, and send a string "hello world" to illustrate the use of sdk.

This example is divided into two programs for demonstration, one as client and the other as device, simulating the scenario where a user connects to a certain device through p2p. The following sample code is compiled and executed with vc2008 or above under windows. First look at the client code, all codes are less than 100 lines.

#include "stdafx.h"
#include "p2papi.h"

int DEVICE_ID   = 1000;
int g_nP2PSetUp = 0;

class CP2PHoleSink : public IP2PSessionSink
{
public:
    CP2PHoleSink()
    {
    }
    ~CP2PHoleSink()
    {
    }

    virtual void onServerConnected(int session, int state)
    {
        p2p_login_server(session, DEVICE_ID);
    }

    virtual void  onLoginServer(int session, int state)
    {
        //13 is the device ID to connect
        p2p_connect_peer(session,13);
    }

    virtual void onHoleState(int session, int state)
    {        
        g_nP2PSetUp =state;
        if (state > 0 )
        {
            char str[32] = {"hello world"};
            p2p_send(session,10,str,strlen(str));
        }
    }

    virtual void onRecvServerData(int cmd, int subcmd, const unsigned char * pDataBuffer, int wDataSize)
    {
    }

    virtual void onRecvP2PData(int session,int channel, const unsigned char * pDataBuffer, int wDataSize)
    {
    }

    virtual void onChannelState(int session,int state)
    {
    }

    virtual bool OnSocketCommand(int wMain, int sub, const void* pData, int nLen)
    {
        return true;
    }

    virtual void onProxyStarted(int session, int port)
    {
    }
};

int main(int argc, char* argv[])
{
    if (argc >= 2)
        DEVICE_ID = atoi(argv[1]);
    
    printf("my deviceid = %d\n", DEVICE_ID);

    p2p_engine_init("43.142.138.68",20000,"d:/p2p.log");//"192.168.3.166"
    
    int sessionID = p2p_get_free_session();

    CP2PHoleSink sink;
    p2p_set_sink(sessionID,&sink);

    int64_t p2puid = DEVICE_ID | 0x1000000000000000;
    p2p_connect_server(sessionID, p2puid, "");

    while (1)
    {
        SleepEx(10,TRUE);
    }

    p2p_engine_destroy();
    return 0;
}

The entire client code, the main function is less than 30 lines of code, in front of it is an implementation class CP2PHoleSink of the IP2PSessionSink callback interface.

The steps to establish a p2p connection are as follows:

  1. Call p2p_engine_init("43.142.138.68",20000,"d:/p2p.log"); set the ip (domain name) and port of the p2p server.

  1. Get an idle session: An app can establish p2p connections with multiple devices at the same time, and each connection is called a session.

  1. Set the session callback interface CP2PHoleSink.

  1. Call p2p_connect_server(sessionID, p2puid, ""); to connect to the p2p server. p2puid is the id of this device.

  1. If the connection to the p2p server is successful, CP2PHoleSink::onServerConnected will be called back, and then call p2p_login_server(session, DEVICE_ID); to log in to the p2p server.

  1. If the login to the p2p server is successful, CP2PHoleSink::onLoginServer will be called back, and p2p_connect_peer(session,13) will be called at this time; the connection represents another program of the device, and establishes a p2p connection with the device 13.

  1. When the p2p connection is successfully established, CP2PHoleSink::onHoleState will be called back. At this point, you can call p2p_send(session,10,str,strlen(str)); to send any data.

The complete code of another program device is as follows:

#include "stdafx.h"
#include "p2papi.h"

int DEVICE_ID   = 13;
int g_nP2PSetUp = 0;

class CP2PHoleSink : public IP2PSessionSink
{
public:
    CP2PHoleSink()
    {

    }
    ~CP2PHoleSink()
    {

    }

    virtual void onServerConnected(int session, int state)
    {
        p2p_login_server(session, DEVICE_ID);
    }

    virtual void  onLoginServer(int session, int state)
    {
    }

    virtual void onHoleState(int session, int state)
    {        
        g_nP2PSetUp=state;
        printf("onHoleState g_nP2PSetUp=%d\n",g_nP2PSetUp);
    }

    virtual void onRecvServerData(int cmd, int subcmd, const unsigned char * pDataBuffer, int wDataSize)
    {
    }

    virtual void onRecvP2PData(int session,int channel, const unsigned char * pDataBuffer, int wDataSize)
    {
        printf("recv p2p data, %d bytes:%s\n",wDataSize,pDataBuffer);
    }

    virtual void onChannelState(int session,int state)
    {
    }

    virtual void onProxyStarted(int session, int port)
    {
    }
};


int main(int argc, char* argv[])
{
    if (argc >= 2)
        DEVICE_ID = atoi(argv[1]);

    p2p_engine_init("43.142.138.68",20000,"d:/p2pserver.log");//"192.168.3.166"

    printf("my deviceid = %d\n", DEVICE_ID );
    int sessionID = p2p_get_free_session();

    CP2PHoleSink sink;
    p2p_set_sink(sessionID,&sink);
    
    //Device authorization ID and key: 4294967390,49uozeazd9
    //This authorization ID is different from the device ID in p2p_login_server
    p2p_connect_server(sessionID, 4294967390, "49uozeazd9");
    
    while (1)
    {
//        checkReadWrite( );        
        SleepEx(10,TRUE);
    }
    p2p_engine_destroy();
    return 0;
}

Most of the device code is the same as that of the client, except that the device id is changed to 13, and CP2PHoleSink::onRecvP2PData prints out the received p2p data.

The above shows the simple use of p2p through two console programs. If you have any questions, please contact me on qq: 390090739.

In the next article, I will introduce the use of HYP2P SDK in the rtsp protocol, and show an example of p2p transmission of real-time video.

Complete sample code download

Tags: p2p rtc

Posted by giraffemedia on Fri, 10 Feb 2023 20:51:56 +0530