![]() 如果你也在Android端做RTP發(fā)送數(shù)據(jù)的話,通過網(wǎng)上查找資料,相信你不難發(fā)現(xiàn),在使用RTP/RTCP協(xié)議發(fā)送數(shù)據(jù)是有現(xiàn)成的庫進(jìn)行調(diào)用的,Jlibrtp這個(gè)庫就是Java實(shí)現(xiàn)的,但是這個(gè)庫是沒有說明文檔的,比較摳腳,而且百度谷歌找到例子又很少,基本上都沒什么卵用;一般搜到都是Jrtplib庫,這是C實(shí)現(xiàn)庫,對(duì)我們沒多大用,除非你愿意先研究這個(gè)庫的RTP實(shí)現(xiàn),在用JNI調(diào)用,明顯更蛋疼。這個(gè)沒文檔就只能自己瞎倒騰一下,還好勉強(qiáng)倒騰出來了。 首先,你必須下一個(gè)Jlibrtp庫:http://download.csdn.net/detail/ericfantastic/9206693 里面包含Jlibrtp的實(shí)現(xiàn)和java調(diào)用的Demo。 下完后,新建一個(gè)Android工程,將Jlibrtp-0.2.2中的jlibrtp文件夾中的所有.java文件導(dǎo)入工程,全丟到一個(gè)新建的jlibrtp包里面,準(zhǔn)備工作算是完成,在需要地方直接import這個(gè)包,就可以使用了。 簡單整理一下Jlibrtp實(shí)現(xiàn)RTP的過程: 1、首先建立收發(fā)端的會(huì)話,調(diào)用Jlibrtp庫的實(shí)時(shí)傳輸會(huì)話類RTPSession,該類可以創(chuàng)建一個(gè)RTP會(huì)話,并設(shè)置傳輸?shù)腞TP端口和RTCP端口,以及與RTP包的相關(guān)的時(shí)間戳數(shù)據(jù)等。 2、然后通過RTPSessionRegister方法用于添加RTP會(huì)話的參與者,同時(shí)開啟接收包的AppCallerThread線程類,其run方法調(diào)用回調(diào)函數(shù)receiveData,才開始接收RTP包,receiveData函數(shù)會(huì)去掉RTP包頭,直接將RTP負(fù)載存入緩存,之后再進(jìn)行分包的判斷。
具體實(shí)現(xiàn)過程如下: 1、InitSession.java類初始化會(huì)話基礎(chǔ)參數(shù),設(shè)置目標(biāo)IP及RTP端口號(hào)和RTCP端口號(hào)等;
package com.eric.androidrtpsenddata; import java.net.DatagramSocket; import java.net.InetSocketAddress; import jlibrtp.*; /* *@author Eric *@2015-10-23下午3:28:20 */ public class InitSession implements RTPAppIntf{ public RTPSession rtpSession = null; public InitSession() { DatagramSocket rtpSocket = null; DatagramSocket rtcpSocket = null; try { rtpSocket = new DatagramSocket(8002); rtcpSocket = new DatagramSocket(8003); } catch (Exception e) { System.out.println("發(fā)送創(chuàng)建會(huì)話異常拋出:"+e); } //建立會(huì)話 rtpSession = new RTPSession(rtpSocket, rtcpSocket); rtpSession.RTPSessionRegister(this,null,null); //設(shè)置參與者(目標(biāo)IP地址,RTP端口,RTCP端口) Participant p = new Participant("192.168.226.116", 8004, 8005); rtpSession.addParticipant(p); } public void receiveData(DataFrame frame, Participant p){ String s = new String(frame.getConcatenatedData()); System.out.println("接收到數(shù)據(jù): "+s+" , 參與者CNAME: " +p.getCNAME()+"同步源標(biāo)識(shí)符("+p.getSSRC()+")"); } public void userEvent(int type, Participant[] participant) { // TODO Auto-generated method stub } public int frameSize(int payloadType) { return 1; } }
2、MainActivity.java中發(fā)送數(shù)據(jù);
package com.eric.androidrtpsenddata; import android.app.Activity; import android.os.Bundle; /* *@author Eric *@2015-10-12上午8:31:54 */ public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*Thread receiveThread = new Thread(new Runnable() { @Override public void run() { try { receiveData(); } catch (Exception e) { e.printStackTrace(); System.out.println("RTP接收數(shù)據(jù)異常:"+e); } } }); receiveThread.start();*/ Thread sendThread = new Thread(new Runnable() { @Override public void run() { try { openSession(); } catch (Exception e) { e.printStackTrace(); System.out.println("RTP發(fā)送數(shù)據(jù)異常:"+e); } } }); sendThread.start(); } //Jlibrtp開啟會(huì)話發(fā)送數(shù)據(jù) public void openSession(){ InitSession test = new InitSession(); long teststart = System.currentTimeMillis(); String str = "abce abcd abce abce abce abcd abcd abce " + "abcd abce abcd abce abcd abce abcd abce abcd abce " + "abcd abce abcd abce abcd abce abcd abce abcd abce abcd " + "abce abcd abce abcd abce abcd abce abcd abce abcd abce " + "abcd abce abcd abce abcd abce abcd abce abcd abce abcd " + "abce abcd abce abcd abce abcd abce abcd abce abcd abce " + "abcd abce abcd abce abcd abce abcd abce abcd abce abcd " + "abce abcd abce abcd abce abcd abce abcd abce abcd abce " + "abcd abce abcd abce abcd abce abcd abce abcd abce abcd " + "abce abcd abce abcd abce abcd abce abcd abce abcd abce " + "abcd abce abcd abce abcd abce abcd abce abcd abce abcd " + "abce abcd abce abcd abce abcd abce abcd "; byte[] data = str.getBytes(); System.out.println(data.length); int i=0; while(i<data.length) { System.out.println("已發(fā)送第"+i+"個(gè)字節(jié)數(shù)組:"+data); test.rtpSession.sendData(data); i++; } long testend = System.currentTimeMillis(); System.out.println("發(fā)送用時(shí):" + (testend - teststart)); System.out.println("結(jié)束時(shí)間:" + testend); System.out.println("開始時(shí)間:" + teststart); } public void receiveData(){ ReceiveData receive = new ReceiveData(); } }Demo運(yùn)行結(jié)果:
附上Demo地址:http://download.csdn.net/detail/ericfantastic/9216101
|
|