使用jmf开发RTSP流媒体客户端,接收得到音频,但是很不清晰。。。求解
使用jmf开发RTSP流媒体客户端,接收得到音频,但是很不清晰。。。求解
代码如下:
package test3;
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Format;
import javax.media.Manager;
import javax.media.Player;
import javax.media.control.BufferControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.DataSource;
import javax.media.protocol.PullSourceStream;
import javax.media.rtp.Participant;
import javax.media.rtp.RTPControl;
import javax.media.rtp.RTPManager;
import javax.media.rtp.ReceiveStream;
import javax.media.rtp.ReceiveStreamListener;
import javax.media.rtp.SessionAddress;
import javax.media.rtp.SessionListener;
import javax.media.rtp.event.NewReceiveStreamEvent;
import javax.media.rtp.event.ReceiveStreamEvent;
import javax.media.rtp.event.SessionEvent;
import javax.media.rtp.rtcp.SourceDescription;
import com.sun.media.format.WavAudioFormat;
import com.sun.media.parser.audio.WavParser;
import com.sun.media.vfw.WaveFormatEx;
public class RTSPClient implements ReceiveStreamListener,SessionListener,ControllerListener{
private static final String VERSION = " RTSP/1.0\r\n";
private static final String RTSP_OK = "RTSP/1.0 200 OK";
/** *//** 远程地址 */
private InetSocketAddress remoteAddress;
/** *//** * 本地地址 */
private InetSocketAddress localAddress;
private enum Status {
init, options, describe, setup, play, pause, teardown
}
private Status sysState = Status.init;
private String address = null;
private Socket socket;
private PrintWriter out;
private BufferedInputStream bis;
private AtomicBoolean shutdown;
private int seq = 2;
private String trackInfo;
private String response = "FAlSE";
private String serverport;
private String clientport = "1934";
private String sessionid;
private String payload = "96";//有效负载
public RTSPClient(InetSocketAddress remoteAddress,
InetSocketAddress localAddress, String address) throws Exception {
this.remoteAddress = remoteAddress;
this.localAddress = localAddress;
this.address = address;
socket = new Socket();
socket.bind(localAddress);
socket.connect(remoteAddress);
out = new PrintWriter(socket.getOutputStream());
bis = new BufferedInputStream(socket.getInputStream());
shutdown = new AtomicBoolean(false);
}
public void start() throws Exception {
new ReadThread().start();
doOption();//option();
Thread.sleep(2000);
doDescribe();
Thread.sleep(2000);
doSetup();
Thread.sleep(2000);
}
private void Option1() { //这个事为什么呢!!!!!!
String sb1 = "OPTIONS "+this.address+VERSION+"Cseq: "+(seq++)+"\r\n\r\n";
String sb2 = "OPTIONS "+this.address+VERSION+"CSeq: "+(seq++)+"\r\n\r\n";
System.out.println(sb1.equals(sb2));//为什么sb1不等于sb2
out.print(sb1);
out.flush();
System.out.println(sb1);
}
private void doOption(){
String sb = "OPTIONS "+this.address+VERSION+"CSeq: "+(seq++)+"\r\n\r\n";
out.print(sb);
out.flush();
System.out.println(sb);
}
private void doDescribe(){
String sb = "DESCRIBE "+this.address+VERSION+"CSeq: "+(seq++)+"\r\nUser-Agent: XIANGHAO\r\nAccept: application/sdp\r\n\r\n";
out.print(sb);
out.flush();
System.out.println(sb);
}
private void doSetup(){
String sb = "SETUP "+this.address+"/"+trackInfo+VERSION+"CSeq: "+(seq++)+"\r\nUser-Agent: XIANGHAO\r\nTransport: RTP/AVP;unicast;client_port=1934-1935\r\n\r\n";
out.print(sb);
out.flush();
System.out.println(sb);
}
private void doPlay(){
String sb = "PLAY "+this.address+VERSION+"CSeq: "+(seq++)+"\r\nScale: 1\r\nUser-Agent: XIANGHAO\r\nSession: "+sessionid+"\r\nRange: npt=0.000-\r\n\r\n";
out.print(sb);
out.flush();
System.out.println(sb);
}
private void initRtpManager() throws Exception{
RTPManager rtp = RTPManager.newInstance();
Format format = new AudioFormat(AudioFormat.ULAW_RTP, 44100.0, 16, 2);
rtp.addFormat(format, Integer.valueOf(payload));
SessionAddress local = new SessionAddress(localAddress.getAddress(),Integer.valueOf(clientport));
rtp.initialize(local);
rtp.addReceiveStreamListener(this);
rtp.addSessionListener(this);
SessionAddress remote = new SessionAddress(this.remoteAddress.getAddress(),Integer.valueOf(serverport));
rtp.addTarget(remote);
BufferControl bc = (BufferControl)rtp.getControl("javax.media.control.BufferControl");
bc.setBufferLength(300);
}
public void play(){
try {
initRtpManager();
System.out.println("complement initRtpManager");
doPlay();
} catch (Exception e) {
e.printStackTrace();
}
}
private class ReadThread extends Thread {
@Override
public void run() {
while (!shutdown.get()) {
try {
byte[] bytes = new byte[1024];
int len = bis.read(bytes);
if(len > 0){
String mes = new String(bytes, 0, len);
if(mes.contains(RTSP_OK)){
System.out.println("正确接受协议数据");
response = "OK";
if (mes.contains("control:")) {
trackInfo = mes.substring(mes.lastIndexOf("control:")+"control:".length(),mes.lastIndexOf("\r\n"));
}
if(mes.contains("server_port")){
String temp = mes.substring(mes.lastIndexOf("server_port"));
//System.out.println(temp);
serverport = temp.substring(temp.indexOf('=')+1,temp.indexOf('-'));
System.out.println(serverport);
}
if (mes.contains("Session: ")) {
String temp = mes.substring(mes.lastIndexOf("Session: "));
sessionid = temp.substring(
temp.lastIndexOf("Session: ") + "Session: ".length(),
temp.indexOf("\r\n"));
}
if(mes.contains("m=audio 0 RTP/AVP ")){
String temp = mes.substring(mes.lastIndexOf("m=audio 0 RTP/AVP "));
payload = temp.substring(temp.lastIndexOf("m=audio 0 RTP/AVP ") + "m=audio 0 RTP/AVP ".length(), temp.indexOf("\r\n"));
}//特定音频流
System.out.println(mes);
}else if(mes.contains("RTSP/1.0")){
response = "False";
System.out.println("错误请求!!");
System.out.println(mes);
System.exit(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
public void controllerUpdate(ControllerEvent arg0) {
// TODO Auto-generated method stub
System.out.println("controll-->"+arg0);
}
@Override
public void update(SessionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("session"+arg0);
}
@Override
public void update(ReceiveStreamEvent event) {
// TODO Auto-generated method stub
System.out.println("ReceiveStreamEvent--->"+event);
RTPManager manager = (RTPManager) event.getSource();
Participant sender = event.getParticipant();
ReceiveStream rs = event.getReceiveStream();
if(event instanceof NewReceiveStreamEvent){
try {
rs = ((NewReceiveStreamEvent)event).getReceiveStream();
DataSource ds = rs.getDataSource();
System.out.println("content type--->"+ds.getContentType());
//System.out.println(""+ds.);
RTPControl rtpctl = (RTPControl)ds.getControl("javax.media.rtp.RTPControl");
if(rtpctl != null){
//rtpctl.addFormat(new AudioFormat(AudioFormat.ULAW_RTP, 44100.0, 16, 2), 10);
System.out.println("new receive"+rtpctl.getFormat());
}else{
System.out.println("no format");
}
if(sender == null){
System.out.println("need to analsy");
}else{
System.out.println("ok to form "+sender.getCNAME());
SourceDescription sd = ((SourceDescription)sender.getSourceDescription().get(0));
System.out.println(sd.getDescription());
System.out.println(sd.getEncrypted());
System.out.println(sd.getFrequency());
System.out.println(sd.getType());
}
Player p = Manager.createRealizedPlayer(ds);
p.addControllerListener(this);
p.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
RTSPClient rtsp = new RTSPClient(new InetSocketAddress("122.207.50.128", 554),
new InetSocketAddress("122.207.50.181", 12345),
"rtsp://122.207.50.128/wavAudioTest.wav");
rtsp.start();
rtsp.play();
}
}
public class Test {
public static void main(String[] args) throws Exception {
MediaLocator dl = new MediaLocator("rtsp://122.207.50.128/wavAudioTest.wav");
DataSource ds = Manager.createDataSource(dl);
Manager.createRealizedPlayer(ds).start();
}
}