您当前的位置: 首页 >  udp

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

异步udpserver接收rtp转html5(一)

qianbo_insist 发布时间:2022-03-04 17:52:12 ,浏览量:0

第二篇在这里 第二篇 rtsp rtp gb28181转html5播放

udpserver

假定服务端有一个端口接收rtp over udp,接收到数据以后转成html5能展示的,这种效果有作用否?

我们知道ffmpeg是可以直接发送rtp流的,gb28181 也是可以发送rtp流的,那么我们制作一个简洁的服务器,在udp端口接收到以后转成tcp,让后在网页上展示。

ffmpeg发送命令ffmpeg \

ffmpeg -re -i video.mp4 -an -c:v copy -f rtp -sdp_file video.sdp “rtp://192.168.1.109:5004” 使用ffmpeg来发送文件,去除音频,当然也可以接收自己写的程序发送rtp,下面,我们使用asio来制作一个异步的服务器,来接收udp流。

code
#pragma once

#include 
#include 
#include 
#include 
#include 
#include 
#include "c_rtp.h"
using boost::asio::ip::udp;

using namespace boost;

#include "c_hub.h"
#include "c_rtp.h"

#define DEFINE_EC \
boost::system::error_code ec;

class c_flvserver;

class c_udpserver:public std::enable_shared_from_this
{
	//asio::strand v_strand;
	asio::io_context &v_context;

	asio::ip::udp::socket v_socket;
	c_flvserver *v_flvserver = NULL;
	unordered_map v_ctxs;
	//s_rtp_h264_context v_rtp;
	s_rtp_h264_context * getctx(uint32_t ssrc)
	{
		auto it = v_ctxs.find(ssrc);
		if (it != v_ctxs.end())
			return it->second;
		else
		{
			s_rtp_h264_context *sc = new s_rtp_h264_context();
			v_ctxs[ssrc] = sc;
			return sc;
		}
	}
	void data_call(s_rtp_h264_context *ctx);
	s_flvhub * func_context(uint32_t ssrc);

	int live_rtp_unpack_h264(s_rtp_h264_context *ctx, uint8_t *data, int inlen);
	//int live_rtp_unpack_h265(s_rtp_h264_context *ctx, uint8_t *data, int inlen);
	//int live_rtp_unpack_aac(s_rtp_h264_context *ctx, uint8_t *data, int inlen);
	//int live_rtp_unpack_opus(s_rtp_h264_context *ctx, uint8_t *data, int inlen);
	//int live_rtp_unpack(s_rtp_h264_context *ctx, uint8_t *data, int inlen);

public:
	c_udpserver(asio::io_context& io_context, short port, c_flvserver *flvserver):
		//v_strand(io_context.get_executor()),
		v_context(io_context), v_flvserver(flvserver),
		v_socket(v_context)
		 //v_socket(v_context, udp::endpoint(udp::v4(), port))
	{
		//boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("192.168.1.206"),port);
		udp::endpoint ep(udp::v4(), port);
		v_socket.open(ep.protocol());
		v_socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
		boost::asio::socket_base::receive_buffer_size recv_option(1*1024*1024);
		v_socket.set_option(recv_option);
		v_socket.bind(ep);
	}
	~c_udpserver()
	{
		//v_context_maps.clear();
	}

	
	//to flv server
	void do_receive();

#ifdef _WEBRTC_
	void do_receive_webrtc()
	{
		//std::cout v_buffer + ctx->v_len, buffer + jump, len);
				ctx->v_len += len;
			}
			else//分片丢失,RTP丢包
			{
				ctx->reset();
				//ctx->v_current_FU = 0; 
				return -1;
			}
		}
		else if (!(header & 0x80) && (header & 0x40)) {
			/* Last part of fragmented packet (E bit set) */
			if (ctx->v_current_FU) {
				ctx->v_current_FU = 0;
				memcpy(ctx->v_buffer + ctx->v_len, buffer + jump, len);
				ctx->v_len += len;
				//data_call(ctx);
				//ctx->reset();
				//return 0;
				//printf("end of m:%d", m);
				//cout             
关注
打赏
1663161521
查看更多评论
0.1201s