Skip to content
Snippets Groups Projects
Commit b9420895 authored by Boris Grozev's avatar Boris Grozev
Browse files

Prevents WebmDataSink from (crashing because of) writing to a closed file.

parent 55e712e2
No related branches found
No related tags found
No related merge requests found
...@@ -40,6 +40,13 @@ public class WebmDataSink ...@@ -40,6 +40,13 @@ public class WebmDataSink
private RecorderEventHandler eventHandler; private RecorderEventHandler eventHandler;
private long ssrc = -1; private long ssrc = -1;
/**
* Whether this <tt>DataSink</tt> is open and should write to its
* <tt>WebmWriter</tt>.
*/
private boolean open = false;
private final Object openCloseSyncRoot = new Object();
/** /**
* Whether we are in a state of waiting for a keyframe and discarding * Whether we are in a state of waiting for a keyframe and discarding
* non-key frames. * non-key frames.
...@@ -129,21 +136,25 @@ public void addDataSinkListener(DataSinkListener dataSinkListener) ...@@ -129,21 +136,25 @@ public void addDataSinkListener(DataSinkListener dataSinkListener)
@Override @Override
public void close() public void close()
{ {
if (writer != null) synchronized (openCloseSyncRoot)
writer.close();
if (eventHandler != null && firstFrameTime != -1 && lastFramePts != -1)
{ {
RecorderEvent event = new RecorderEvent(); if (writer != null)
event.setType(RecorderEvent.Type.RECORDING_ENDED); writer.close();
event.setSsrc(ssrc); if (eventHandler != null && firstFrameTime != -1 && lastFramePts != -1)
event.setFilename(filename); {
RecorderEvent event = new RecorderEvent();
event.setType(RecorderEvent.Type.RECORDING_ENDED);
event.setSsrc(ssrc);
event.setFilename(filename);
// make sure that the difference in the 'instant'-s of the // make sure that the difference in the 'instant'-s of the
// STARTED and ENDED events matches the duration of the file // STARTED and ENDED events matches the duration of the file
event.setDuration(lastFramePts); event.setDuration(lastFramePts);
event.setMediaType(MediaType.VIDEO); event.setMediaType(MediaType.VIDEO);
eventHandler.handleEvent(event); eventHandler.handleEvent(event);
}
open = false;
} }
} }
...@@ -171,22 +182,27 @@ public MediaLocator getOutputLocator() ...@@ -171,22 +182,27 @@ public MediaLocator getOutputLocator()
@Override @Override
public void open() throws IOException, SecurityException public void open() throws IOException, SecurityException
{ {
if (dataSource instanceof PushBufferDataSource) synchronized (openCloseSyncRoot)
{ {
PushBufferDataSource pbds = (PushBufferDataSource) dataSource; if (dataSource instanceof PushBufferDataSource)
PushBufferStream[] streams = pbds.getStreams();
//XXX: should we allow for multiple streams in the data source?
for (PushBufferStream stream : streams)
{ {
//XXX whats the proper way to check for this? and handle? PushBufferDataSource pbds = (PushBufferDataSource) dataSource;
if (!stream.getFormat().matches(new VideoFormat("VP8"))) PushBufferStream[] streams = pbds.getStreams();
throw new IOException("Unsupported stream format");
stream.setTransferHandler(this); //XXX: should we allow for multiple streams in the data source?
for (PushBufferStream stream : streams)
{
//XXX whats the proper way to check for this? and handle?
if (!stream.getFormat().matches(new VideoFormat("VP8")))
throw new IOException("Unsupported stream format");
stream.setTransferHandler(this);
}
} }
dataSource.connect();
open = true;
} }
dataSource.connect();
} }
/** /**
...@@ -261,6 +277,10 @@ public void setSource(DataSource dataSource) ...@@ -261,6 +277,10 @@ public void setSource(DataSource dataSource)
@Override @Override
public void transferData(PushBufferStream stream) public void transferData(PushBufferStream stream)
{ {
synchronized (openCloseSyncRoot)
{
if (!open)
return;
try try
{ {
stream.read(buffer); stream.read(buffer);
...@@ -391,6 +411,7 @@ else if (height*16 == width*9) ...@@ -391,6 +411,7 @@ else if (height*16 == width*9)
lastFramePts = fd.pts; lastFramePts = fd.pts;
} }
} //synchronized
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment