gpt4 book ai didi

java - 无法使用 Xuggler 通过 RTMP 向 FMS 连续不间断地发布直播流

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:50 27 4
gpt4 key购买 nike

我正在尝试将实时 RTMP 流视频从 link1 转发到 link2。但是,当视频在输入端停止或暂停时,我的 java 应用程序将停止读取数据包并收到错误“无法读取 RTMP header 数据包”。代码如下-

import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IContainerFormat;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.IVideoPicture;

public class XugglerRecorder
{
public static void main(String[] args)
{
String url = "rtmp://IP:PORT/live2/16_8_2013";

IContainer readContainer = IContainer.make();
readContainer.setInputBufferLength(4096);
IContainer writeContainer=IContainer.make();
//writeContainer.setInputBufferLength(0);
IContainerFormat containerFormat_live = IContainerFormat.make();
containerFormat_live.setOutputFormat("flv","rtmp://IP:PORT/live/abc", null);
int retVal= writeContainer.open("rtmp://192.168.1.198:1935/live/abc", IContainer.Type.WRITE, containerFormat_live);
//writeContainer.setInputBufferLength(0);
if (retVal < 0) {
System.err.println("Could not open output container for live stream");
System.exit(1);
}

if (readContainer.open(url, IContainer.Type.READ, null, true, false) < 0) {
throw new RuntimeException("unable to open read container");
}

IStream video = writeContainer.addNewStream(0);
if (video == null) {
throw new RuntimeException("unable to add video stream");
}

IPacket packet = IPacket.make();
while (readContainer.readNextPacket(packet) >= 0 && !packet.isKeyPacket()) {}

IStreamCoder inVideoCoder = null;
int videoStreamId = -1;
for (int i = 0; i < readContainer.getNumStreams(); ++i) {
IStream stream = readContainer.getStream(i);
IStreamCoder coder = stream.getStreamCoder();
if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
inVideoCoder = coder;
videoStreamId = i;

if (inVideoCoder.open(null, null) < 0) {
throw new RuntimeException("Unable to open input video coder");
}

//for getting frame params need to decode at least one key frame
IVideoPicture picture = IVideoPicture.make(inVideoCoder.getPixelType(), 0, 0);

int bytesDecoded = inVideoCoder.decodeVideo(picture, packet, 0);
if (bytesDecoded < 0) {
throw new RuntimeException("Unable to decode video packet");
}
}
}

if (videoStreamId == -1) {
throw new RuntimeException("unable to find video stream");
}

IStreamCoder outVideoCoder = video.getStreamCoder();
outVideoCoder.setCodec(inVideoCoder.getCodec());
outVideoCoder.setHeight(inVideoCoder.getHeight());
outVideoCoder.setWidth(inVideoCoder.getWidth());
outVideoCoder.setPixelType(inVideoCoder.getPixelType());
outVideoCoder.setBitRate(inVideoCoder.getBitRate());
outVideoCoder.setTimeBase(inVideoCoder.getTimeBase());
if (outVideoCoder.open(null, null) < 0) {
throw new RuntimeException("unable to open output video coder");
}

if (writeContainer.writeHeader() < 0) {
throw new RuntimeException("unable to write header");
}

int i = 0;
doit(readContainer, packet, writeContainer);
if (writeContainer.writeTrailer() < 0) {
throw new RuntimeException("unable to write trailer");
}

}

private static void doit(IContainer readContainer, IPacket packet,
IContainer writeContainer) {
int i = 0;
while (readContainer.readNextPacket(packet) >= 0) {
if(readContainer.readNextPacket(packet)<0)
{
System.out.println("Packet null Hello");
try{
doit(readContainer, packet, writeContainer);
}catch(Exception e){e.printStackTrace();}
continue;
}

if(readContainer.readNextPacket(packet)==-1 ){
System.out.println("Packet is absent");
}

if (packet.getStreamIndex() != 0) {
continue;
}

if (writeContainer.writePacket(packet) < 0 && readContainer.readNextPacket(packet)>=0) {
try{
System.out.println(" packet sleep");
}catch(Exception e){e.printStackTrace();}
}

}
}
}

我可以通过 RTMP 将实时视频发布到 FMS。但无法在视频流停止或暂停之前存储视频的保存点。如果输入流有任何时间滞后,那么我的应用程序应该继续检查并等待流而不是停止。请帮助我摆脱这个困境。提前致谢。

任何有关如何调试此问题的帮助或提示将不胜感激。

最佳答案

我对Xuggler了解不多。

您可以 try catch 异常。

或者您可以尝试在 main 方法中再次调用 doit() 。假设如果没有更多数据包可供读取,则方法 doit() 结束。因此,它不会再次尝试读取。

或者你可以尝试添加类似的内容while (container.readPacket==null) {} 在 doit() 的开头或者我认为它会是while (container.readPacket!=null) {}

尝试不检查 isKeyPacket 并看看会发生什么。

关于java - 无法使用 Xuggler 通过 RTMP 向 FMS 连续不间断地发布直播流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20234771/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com