发送端程序大意把文件转换为字节数组,每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