main.cpp.28181 3.72 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;


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() {

	int s_rtpPort = MIN_RTP_PORT;

	srand((unsigned int)time(NULL));
	s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);

	if (s_rtpPort % 2)
		++s_rtpPort;

	int count = 0;

	while (true)
	{
		if (s_rtpPort >= MAX_RTP_PORT) {
			s_rtpPort = MIN_RTP_PORT;
			count ++;
			if (count > 1) {
				printf("10000 到 60000 没有可用的port");
			}
		}

		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 : {}", 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) {
	SipServer* pServer = SipServer::getInstance();
	int ret = pServer->RequestInvite_UDP(deviceId, rtp_port);
	if (ret > 0)
	{
		return true;
	}

	return false;
}

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

	if (argc <= 1)
	{
		printf("请输入待获取的摄像头的视频通道编码ID。");
		return -1;
	}

	SipServer* pServer = SipServer::getInstance();
    pServer->Init(nullptr);

	// const char* sipid = "34020000001310000001";
	const char* sipid = argv[1];
	char oper = 'g';
	while (oper != 'q') {
		oper = getchar();
		switch (oper)
		{
		case 'g':
			break;
		case 'i':
			{
				int rtp_port = allocRtpPort();
				start_rtp(sipid, rtp_port);

				pServer->RequestInvite_UDP(sipid, rtp_port);
			}
			break;
		case 'b':
			break;
		default:
			break;
		}
	}

    return 0;
}