JRTP实时音视频传输(2)-使用TCP通信的案例

发布时间:2024年01月17日

1.创建自己的demo

先将example1拷贝为myclienttcp.cpp和myservertcp.cpp
cp example1.cpp myclienttcp.cpp
cp example1.cpp myservertcp.cpp

改写jrtplib/JRTPLIB/examples/CMakeLists.txt,添加myclienttcp和myservertcp编译
image.png
重新生成Makefile并编译

sudo cmake CMakeLists.txt
sudo make

可以看到成功编译了myclienttcp和myservertcp源文件
image.png
编译通过,这里就去实现demo就行

2.demo源码-客户端

#include <iostream>
#include <arpa/inet.h>
#include "rtptcpaddress.h"
#include "rtpsession.h"
#include "rtpsessionparams.h"
#include "rtptcptransmitter.h"
#include "rtpipv4address.h"
#include "rtptimeutilities.h"
#include "rtppacket.h"
#include "rtpabortdescriptors.h"

using namespace jrtplib;

#define SERVER_IP    "127.0.0.1"
#define SERVER_PORT  58008

int main()
{
    RTPSession session;
    RTPAbortDescriptors m_descriptors;

    RTPSessionParams sessionparams;
    sessionparams.SetAcceptOwnPackets(true);
    sessionparams.SetOwnTimestampUnit(1.0/10.0);

    m_descriptors.Init();

    RTPTCPTransmissionParams transparams;
    transparams.SetCreatedAbortDescriptors(&m_descriptors);
    int status = session.Create(sessionparams,&transparams,RTPTransmitter::TCPProto);

    if (status < 0)
    {
        printf("my client session create failed\n");
        return -1;
    }

    //初始化socket
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    sockaddr_in addrSrv;
    addrSrv.sin_addr.s_addr = inet_addr(SERVER_IP);
    addrSrv.sin_family = AF_INET;
    addrSrv.sin_port = htons(SERVER_PORT);

    printf("my client prepare to connect\n");

    //连接服务器
    connect( sock, (sockaddr*)&addrSrv, sizeof(sockaddr));

    RTPTCPAddress addr(sock);

    status = session.AddDestination(addr);
    if (status < 0)
    {
        printf("my client session add destination failed\n");
        return -1;
    }

    session.SetDefaultPayloadType(96);
    session.SetDefaultMark(false);
    session.SetDefaultTimestampIncrement(160);

    for (int i = 0; i < 50 ; i++)
        {
            std::string str("123456");
            //发送数据
            session.SendPacket((void *)str.c_str(), str.length(),0,false,10);

            printf("my client send packet:%s, len:%d, idx:%d\n", str.c_str(), str.length(), i);
            RTPTime::Wait(RTPTime(10, 0));
        }

    RTPTime delay(0.020);
    session.BYEDestroy(delay,"Client End",9);
}

3.demo源码-服务端

/*
   Here's a small IPv4 example: it asks for a portbase and a destination and 
   starts sending packets to that destination.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include "rtppacket.h"
#include "rtptcpaddress.h"
#include "rtptcptransmitter.h"
#include "rtpsession.h"
#include "rtpudpv4transmitter.h"
#include "rtpipv4address.h"
#include "rtpsessionparams.h"
#include "rtperrors.h"
#include "rtplibraryversion.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>

using namespace jrtplib;

#define SERVER_PORT  58008

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

int main(void)
{
    int   nListener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (nListener == -1)
	{
		return  -1;
	}
 
	sockaddr_in   serverAddr;
	memset(&serverAddr, 0, sizeof(sockaddr_in));
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_addr.s_addr = INADDR_ANY;
	serverAddr.sin_port = htons(SERVER_PORT);
	int  nRet = bind(nListener, (sockaddr*)&serverAddr, sizeof(serverAddr));
	if (nRet == -1)
	{
		return  -1;
	}
	if (listen(nListener, 1) == -1)
	{
		return  -1;
	}
 
	printf("my server is listen ready, wait for connect\n");

	sockaddr_in   clientAddr;
	int  nLen = sizeof(sockaddr_in);
	int   nServer = -1;
	while (true)
	{
		nServer = accept(nListener, (sockaddr*)&clientAddr, (socklen_t *)&nLen);
		if (nServer == -1)
		{
			continue;
		}
		else
		{
			break;
		}
	}
	printf("my server connect new client\n");

	int status = -1;
	int  nPackSize = 45678;
	RTPSessionParams   sessparams;
	RTPSession m_RTPTCPSession;

	sessparams.SetProbationType(RTPSources::NoProbation);
	sessparams.SetOwnTimestampUnit(90000.0 / 25.0);
	sessparams.SetMaximumPacketSize(nPackSize + 64);

	RTPTCPTransmitter    *pTransparams =  new RTPTCPTransmitter(NULL);
	status = pTransparams->Init(false);
	if (status < 0)
	{
		printf("my server trans param init failed, reason:%s\n", RTPGetErrorString(status).c_str());
        return -1;		
	}
	status = pTransparams->Create(65535, NULL);
	if (status < 0)
	{
        printf("my server trans param create failed, reason:%s\n", RTPGetErrorString(status).c_str());
        return -1;		
	}

	status = m_RTPTCPSession.Create(sessparams, pTransparams);
	if (status < 0)
	{
        printf("my server trans session create failed, reason:%s\n", RTPGetErrorString(status).c_str());
        return -1;		
	}
	status = m_RTPTCPSession.AddDestination(RTPTCPAddress(nServer));
	if (status < 0)
	{
        printf("my server trans session add failed, reason:%s\n", RTPGetErrorString(status).c_str());
        return -1;		
	}

	while (1)
	{	
		m_RTPTCPSession.BeginDataAccess();
		
		// check incoming packets
		if (m_RTPTCPSession.GotoFirstSourceWithData())
		{
			do
			{
				RTPPacket *pack;
				
				while ((pack = m_RTPTCPSession.GetNextPacket()) != NULL)
				{
					// You can examine the data here
					printf("myserver recv packet buf:%s, len:%d\n", pack->GetPayloadData(), pack->GetPayloadLength());
					
					// we don't longer need the packet, so
					// we'll delete it
					m_RTPTCPSession.DeletePacket(pack);
				}
			} while (m_RTPTCPSession.GotoNextSourceWithData());
		}
		
		m_RTPTCPSession.EndDataAccess();

#ifndef RTP_SUPPORT_THREAD
		status = m_RTPTCPSession.Poll();
		checkerror(status);
#endif // RTP_SUPPORT_THREAD
		
		RTPTime::Wait(RTPTime(1,0));
	}
	
	m_RTPTCPSession.BYEDestroy(RTPTime(10,0),0,0);

	return 0;
}


4.demo运行测试

分别运行client和server ,可以看到数据正常传输到server端
image.png
用netstat查看连接端口信息,也能看到该端口目前的状态,属于TCP连接,实验成功
image.png

对环境搭建不清楚的可以看这篇博客~
JRTP实时音视频传输(1)-必做的环境搭建与demo测试

5.源码下载

哈喽~我是Embedded-Xin,沪漂嵌入式开发工程师一枚,立志成为嵌入式全栈开发工程师,成为优秀博客创作者,共同学习进步。
以上代码全部放在我私人的github地址,其中有许多自己辛苦敲的例程源码,供大家参考、批评指正,有兴趣还可以直接提patch修改我的仓库~:
https://github.com/Xuzhangxin/study_linux_project.git
觉得不错的话可以点个收藏和star~

文章来源:https://blog.csdn.net/qq_43603125/article/details/135658672
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。