我正在使用xuggler在java中构建RTSP流服务器,但我不确定如何实现正确的RTP打包。
我当前的方法是在输入容器上调用ReadNextPacket(packet)
,然后制作一个RTP数据包,其中有效负载由packet.getData()
和相应的头填充(有效负载类型基于流索引,时间戳由{{设置) 1}}等,并发送它。
有人能为我提供一个实用示例,说明如何以最依赖输入格式的方式将getTimestamp()
编码为正确的rtp有效负载吗?文档有点缺乏。
答案 0 :(得分:0)
我见过使用javax.media实现RTP服务器的代码。
class MediaConvertion {
private MediaLocator mediaLocator = null;
private DataSink dataSink = null;
private Processor mediaProcessor = null;
private static final Format[] FORMATS = new Format[] { new AudioFormat(
AudioFormat.DVI_RTP) };
private static final ContentDescriptor CONTENT_DESCRIPTOR = new ContentDescriptor(
ContentDescriptor.RAW_RTP);
public MediaConvertion(String url) throws IOException,
NoProcessorException, CannotRealizeException, NoDataSinkException,
NoDataSinkException {
mediaLocator = new MediaLocator(url);
}
public void setDataSource(DataSource ds) throws IOException,
NoProcessorException, CannotRealizeException, NoDataSinkException {
mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(ds,
FORMATS, CONTENT_DESCRIPTOR));
dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(),
mediaLocator);
}
public void startTransmitting() throws IOException {
mediaProcessor.start();
dataSink.open();
dataSink.start();
}
public void stopTransmitting() throws IOException {
dataSink.stop();
dataSink.close();
mediaProcessor.stop();
mediaProcessor.close();
}
}
public class MediaConverterExample extends Frame implements ActionListener {
Button st_stream;
static MediaConvertion mdcon;
public static void main(String args[]) throws IOException,
NoProcessorException, CannotRealizeException, NoDataSinkException,
MalformedURLException, NoDataSourceException {
Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
Format input2 = new AudioFormat(AudioFormat.MPEG);
Format output = new AudioFormat(AudioFormat.LINEAR);
PlugInManager.addPlugIn("com.sun.media.codec.audio.mp3.JavaDecoder",
new Format[] { input1, input2 }, new Format[] { output },
PlugInManager.CODEC);
File mediaFile = new File(args[1]);
DataSource source = Manager.createDataSource(new MediaLocator(mediaFile
.toURL()));
mdcon = new MediaConvertion(args[0]);
mdcon.setDataSource(source);
new MediaConverterExample();
}
public MediaConverterExample() {
st_stream = new Button("Start Streaming");
add(st_stream);
st_stream.addActionListener(this);
setVisible(true);
setSize(200, 300);
}
public void actionPerformed(ActionEvent ae) {
try {
mdcon.startTransmitting();
} catch (Exception e) {
}
}
}