main.cpp 4.18 KB
//
// Created bxc on 2022/11/25.
//

#include "./sip/SipServer.h"
#include "./rtp/RTPReceiver.h"
#include"./rtp/RTPTcpReceiver.h"
#include"./rtp/RTPUdpReceiver.h"

#include <thread>
#include <chrono>
#include <iostream>


#ifdef __linux__
#include "arpa/inet.h"
#endif

#include "common_header.h"


#define MIN_RTP_PORT		10000
#define MAX_RTP_PORT		60000


using namespace std;


SipServer sipServer;

static void RTP_Stream_CallBack(void* userdata, int videoType, char* data, int len, int isKey, uint64_t pts, uint64_t localPts)
{
	LOG_INFO("len: {}", len);
    // FFGB28181Decoder* decoder = (FFGB28181Decoder*)userdata;
    // decoder->stream_callback(videoType, data, len, isKey, pts, localPts);
}

static void RTP_Stream_End_CallBack(void* userdata)
{
	LOG_INFO("finished.");
    // FFGB28181Decoder* decoder = (FFGB28181Decoder*)userdata;
    // decoder->stream_end_callback();
}

bool RequestStream(const char* deviceId, int rtp_port);

// ȡ MIN_RTP_PORT(10000)~MAX_RTP_PORT(60000)֮�������˿�(ż���������������˿ڿ���)
int allocRtpPort() {

	static int s_rtpPort = MIN_RTP_PORT;
	// if (MIN_RTP_PORT == s_rtpPort)
	// {
	// 	srand((unsigned int)time(NULL));
	// 	s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);
	// }

	// if (s_rtpPort % 2)
	// 	++s_rtpPort;

	while (true)
	{
		s_rtpPort = s_rtpPort >= MAX_RTP_PORT ? MIN_RTP_PORT : s_rtpPort;

		int i = 0;
		for (; i < 2; i++)
		{
			sockaddr_in sRecvAddr;
			int s = socket(AF_INET, SOCK_DGRAM, 0);

			sRecvAddr.sin_family = AF_INET;        
			sRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);    
			sRecvAddr.sin_port = htons(s_rtpPort + i); 

			int nResult = bind(s, (sockaddr *)&sRecvAddr, sizeof(sRecvAddr));
			if (nResult != 0)
			{
				break;
			}

			nResult = close(s);
			if (nResult != 0)
			{
				printf("closesocket failed:%d\n", nResult);
				break;
			}
		}

		if (i == 2)
			break;

		s_rtpPort += 2;
	}

	return s_rtpPort;
}

bool start_rtp(string deviceId, int m_port) {
	RTPReceiver* m_rtpPtr = nullptr;
	if(false){
        m_rtpPtr = new RTPTcpReceiver();
    }else{
        m_rtpPtr = new RTPUdpReceiver();
    }
    if(nullptr == m_rtpPtr){
        return false;
    }

	m_rtpPtr->SetRequestStreamCallback(RequestStream);



	m_rtpPtr->SetOutputCallback(RTP_Stream_CallBack, nullptr);
	m_rtpPtr->SetVodEndCallback(RTP_Stream_End_CallBack, nullptr);


    bool bRet = m_rtpPtr->Open(deviceId);
    if(bRet){
        // pthread_create(&m_post_decode_thread,0,
        //     [](void* arg)
        //     {
        //         FFGB28181Decoder* a=(FFGB28181Decoder*)arg;
        //         a->post_decode_thread();
        //         return (void*)0;
        //     }
        // ,this);
		LOG_INFO("rtp open successed.");
    }
    return bRet;
}


bool RequestStream(const char* deviceId, int rtp_port) {

	std::vector< DeviceInfo > vec_device = sipServer.GetDeviceList();
	if (vec_device.size() <= 0)
	{
		cout << "no device" << endl;
		return false;
	}

	int ret = sipServer.RequestInvite_TCP_a(vec_device[0].id.c_str(), rtp_port);
	if (ret > 0)
	{
		return true;
	}

	return false;
}

int main(int argc, char *argv[]) {

    ServerInfo info(
            "SY_Sip_Server",
            "12345678",
            "192.168.60.179",
            15060,
            "34020000002000000002",
            "3402000000",
            "sy123456",
            1800,
            3600);

    
    sipServer.Init(info);

	std::this_thread::sleep_for(std::chrono::seconds(5));

	sipServer.cacheCatalog();

	std::vector< DeviceInfo > vec_device;
	char oper = 'g';
	while (oper != 'q') {
		oper = getchar();
		switch (oper)
		{
		case 'g':
			sipServer.cacheCatalog();
			break;
		case 'i':
			vec_device = sipServer.GetDeviceList();
			if (vec_device.size() <= 0)
			{
				cout << "no device" << endl;
				break;
			}

			{
				int rtp_port = 30026;//allocRtpPort();
				start_rtp(vec_device[0].id, rtp_port);

				sipServer.RequestInvite_UDP(vec_device[0].id.c_str(), rtp_port);
			}
			break;
		case 'b':
			break;
		default:
			break;
		}
	}

    return 0;
}