两端之间使用RTP互相进行传输文件

本文介绍了一种通过RTP协议在两端之间进行文件传输的方法。程序将文件转换为字节数组,每隔100毫秒发送960字节。这种方法不仅限于文件,还可用于传输其他数据。由于Maven仓库缺少相关依赖,需要从GitHub下载Efflux项目,编译成jar并引入到项目中。
摘要由CSDN通过智能技术生成

发送端程序大意把文件转换为字节数组,每100毫秒传输960字节,其实发送端也是接收端,按照这个思路也传输其他数据,不单单只是文件,亲测可行

因为maven仓库上没有相关的依赖,所以我们

首先下载rtp相关的代码https://github.com/jonbo372/efflux

然后打成jar包,导入你 的项目里,就可以使用相关的类,如下图

package com.example.demo.controller;

import com.biasedbit.efflux.packet.DataPacket;
import com.biasedbit.efflux.participant.RtpParticipant;
import com.biasedbit.efflux.participant.RtpParticipantInfo;
import com.biasedbit.efflux.session.SingleParticipantSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author lhj
 * @Date 2019/9/10 15:13
 * @Description sdk的接收和发送
 */
@Component
public class RtpSdkSendAndReceive {

	private int n=1;
	private int m=1;

    public void recevie(){
    	//创建会话
    	SingleParticipantSession session;
    	System.out.println("sdk receive listening......");
        RtpParticipant local = RtpParticipant.createReceiver(new RtpParticipantInfo(1), "0.0.0.0", 6000, 6001);
        RtpParticipant remote = RtpParticipant.createReceiver(new RtpParticipantInfo(2), "127.0.0.1", 7000, 7001);
        session = new SingleParticipantSession("Session2", 8, local, remote);
        session.init();
        session.addDataListener((session1, participant, packet) -> {
            session.getRemoteParticipant().setLastSequenceNumber(-1);
            byte[] byte_name = packet.getExtensionData();
            try {
                byte[] send = packet.getDataAsArray();
                String fileName = new String(byte_name, "utf-8");
                if (fileName.contains("start")) {
					System.out.println("第"+n+"次接收");
				}else {
					System.out.println("第"+n+"次接收");
				}
                n++;
                if (fileName.contains("end")) {
					n=1;
				}
                String[] arr = fileName.split("\\.");
                String num = (arr[0].split("_")[1]).split("#")[1];
                String type = (arr[0].split("_")[1]).split("#")[0];
                int nVoiceCallType = type.equals("single") ? 0 : 1;
                int number = 0;
               
                System.err.println("Session 1 received packet: " + packet + "(session: " + session1.getId() + ")");
                System.out.println("number="+num);
            }catch (Exception e){
                e.printStackTrace();
            }

        });
    }

    public void send(String filePath){
    	SingleParticipantSession sessi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值