Detecting stream close

18 views
Skip to first unread message

Vinicius Carvalho

unread,
Aug 24, 2017, 12:27:31 AM8/24/17
to xuggler-users
Hi there, I have adapted a xuggler sample to take snapshots from an rtmp source. It works quite well, but if the stream is closed, the code hangs indefinitely. Is there a way to detect that a stream has been closed?

Here's the code adapted from https://www.javacodegeeks.com/2011/02/xuggler-tutorial-frames-capture-video.html

IContainer readContainer = IContainer.make();

int videoStreamId = 0;
int i = readContainer.open("rtmp://localhost:1935/show/stream", IContainer.Type.READ, null, true, false);

if (i >= 0) {
IMediaReader mediaReader = ToolFactory.makeReader(readContainer);
mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
mediaReader.addListener(new ImageSnapListener());
while (mediaReader.readPacket() == null) ;

}


private static class ImageSnapListener extends MediaListenerAdapter {
private Long lastPictureTaken = 0L;
final long SECONDS_BETWEEN_FRAMES = 1;
private static int mVideoStreamIndex = -1;
long mLastPtsWrite = Global.NO_PTS;

long MICRO_SECONDS_BETWEEN_FRAMES =
(long)(Global.DEFAULT_PTS_PER_SECOND * SECONDS_BETWEEN_FRAMES);
@Override
public void onVideoPicture(IVideoPictureEvent event) {
Long now = System.currentTimeMillis();
if (event.getStreamIndex() != mVideoStreamIndex) {
// if the selected video stream id is not yet set, go ahead an
// select this lucky video stream
if (mVideoStreamIndex == -1)
mVideoStreamIndex = event.getStreamIndex();
// no need to show frames from this video stream
else
return;
}
if (mLastPtsWrite == Global.NO_PTS)
mLastPtsWrite = event.getTimeStamp() - MICRO_SECONDS_BETWEEN_FRAMES;
// if it's time to write the next frame
if (event.getTimeStamp() - mLastPtsWrite >=
MICRO_SECONDS_BETWEEN_FRAMES) {

String outputFilename = dumpImageToFile(event.getImage());
double seconds = ((double) event.getTimeStamp()) /
Global.DEFAULT_PTS_PER_SECOND;
System.out.printf(
"at elapsed time of %6.3f seconds wrote: %s\n",
seconds, outputFilename);

// update last write time
mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES;
}

}

@Override
public void onClose(ICloseEvent event) {
System.out.println("Media closed");
}

@Override
public void onCloseCoder(ICloseCoderEvent event) {
System.out.println("Media closed");
}

private String dumpImageToFile(BufferedImage image) {
try {
String outputFilename = "snapshot" +
System.currentTimeMillis() + ".png";
ImageIO.write(image, "png", new File(outputFilename));
return outputFilename;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

So basically once the stream is closed, there's no new events (expected) but I can't act to destroy the container and free the thread

Any suggestions?

Thank you
Reply all
Reply to author
Forward
0 new messages