|
|
@ -19,15 +19,25 @@ import java.util.List; |
|
|
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) |
|
|
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) |
|
|
|
class MediaEncoderEngine { |
|
|
|
class MediaEncoderEngine { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final static int STOP_BY_USER = 0; |
|
|
|
|
|
|
|
final static int STOP_BY_MAX_DURATION = 1; |
|
|
|
|
|
|
|
final static int STOP_BY_MAX_SIZE = 2; |
|
|
|
|
|
|
|
|
|
|
|
private WorkerHandler mWorker; |
|
|
|
private WorkerHandler mWorker; |
|
|
|
private ArrayList<MediaEncoder> mEncoders; |
|
|
|
private ArrayList<MediaEncoder> mEncoders; |
|
|
|
private MediaMuxer mMediaMuxer; |
|
|
|
private MediaMuxer mMediaMuxer; |
|
|
|
private int mMediaMuxerStartCount; |
|
|
|
private int mMediaMuxerStartCount; |
|
|
|
private boolean mMediaMuxerStarted; |
|
|
|
private boolean mMediaMuxerStarted; |
|
|
|
private Controller mController; |
|
|
|
private Controller mController; |
|
|
|
|
|
|
|
private Listener mListener; |
|
|
|
|
|
|
|
private int mStopReason = STOP_BY_USER; |
|
|
|
|
|
|
|
private int mPossibleStopReason; |
|
|
|
|
|
|
|
private final Object mLock = new Object(); |
|
|
|
|
|
|
|
|
|
|
|
MediaEncoderEngine(@NonNull File file, @NonNull VideoMediaEncoder videoEncoder, @Nullable AudioMediaEncoder audioEncoder) { |
|
|
|
MediaEncoderEngine(@NonNull File file, @NonNull VideoMediaEncoder videoEncoder, @Nullable AudioMediaEncoder audioEncoder, |
|
|
|
|
|
|
|
final int maxDuration, final long maxSize, @Nullable Listener listener) { |
|
|
|
mWorker = WorkerHandler.get("EncoderEngine"); |
|
|
|
mWorker = WorkerHandler.get("EncoderEngine"); |
|
|
|
|
|
|
|
mListener = listener; |
|
|
|
mController = new Controller(); |
|
|
|
mController = new Controller(); |
|
|
|
mEncoders = new ArrayList<>(); |
|
|
|
mEncoders = new ArrayList<>(); |
|
|
|
mEncoders.add(videoEncoder); |
|
|
|
mEncoders.add(videoEncoder); |
|
|
@ -44,32 +54,58 @@ class MediaEncoderEngine { |
|
|
|
mWorker.post(new Runnable() { |
|
|
|
mWorker.post(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
|
|
|
|
// Trying to convert the size constraints to duration constraints,
|
|
|
|
|
|
|
|
// because they are super easy to check.
|
|
|
|
|
|
|
|
int bitRate = 0; |
|
|
|
|
|
|
|
for (MediaEncoder encoder : mEncoders) { |
|
|
|
|
|
|
|
bitRate += encoder.getBitRate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
int bytePerSecond = bitRate / 8; |
|
|
|
|
|
|
|
long sizeMaxDuration = (maxSize / bytePerSecond) * 1000L; |
|
|
|
|
|
|
|
long finalMaxDuration = Long.MAX_VALUE; |
|
|
|
|
|
|
|
if (maxSize > 0 && maxDuration > 0) { |
|
|
|
|
|
|
|
mPossibleStopReason = sizeMaxDuration < maxDuration ? STOP_BY_MAX_SIZE : STOP_BY_MAX_DURATION; |
|
|
|
|
|
|
|
finalMaxDuration = Math.min(sizeMaxDuration, maxDuration); |
|
|
|
|
|
|
|
} else if (maxSize > 0) { |
|
|
|
|
|
|
|
mPossibleStopReason = STOP_BY_MAX_SIZE; |
|
|
|
|
|
|
|
finalMaxDuration = sizeMaxDuration; |
|
|
|
|
|
|
|
} else if (maxDuration > 0) { |
|
|
|
|
|
|
|
mPossibleStopReason = STOP_BY_MAX_DURATION; |
|
|
|
|
|
|
|
finalMaxDuration = maxDuration; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Log.e("MediaEncoderEngine", "Computed a max duration of " + (finalMaxDuration / 1000F)); |
|
|
|
for (MediaEncoder encoder : mEncoders) { |
|
|
|
for (MediaEncoder encoder : mEncoders) { |
|
|
|
encoder.prepare(mController); |
|
|
|
encoder.prepare(mController, finalMaxDuration); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Stuff here might be called from multiple threads.
|
|
|
|
class Controller { |
|
|
|
class Controller { |
|
|
|
|
|
|
|
|
|
|
|
int start(MediaFormat format) { |
|
|
|
int start(MediaFormat format) { |
|
|
|
if (mMediaMuxerStarted) { |
|
|
|
synchronized (mLock) { |
|
|
|
throw new IllegalStateException("Trying to start but muxer started already"); |
|
|
|
if (mMediaMuxerStarted) { |
|
|
|
} |
|
|
|
throw new IllegalStateException("Trying to start but muxer started already"); |
|
|
|
int track = mMediaMuxer.addTrack(format); |
|
|
|
} |
|
|
|
mMediaMuxerStartCount++; |
|
|
|
int track = mMediaMuxer.addTrack(format); |
|
|
|
if (mMediaMuxerStartCount == mEncoders.size()) { |
|
|
|
mMediaMuxerStartCount++; |
|
|
|
mMediaMuxer.start(); |
|
|
|
if (mMediaMuxerStartCount == mEncoders.size()) { |
|
|
|
mMediaMuxerStarted = true; |
|
|
|
mMediaMuxer.start(); |
|
|
|
|
|
|
|
mMediaMuxerStarted = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return track; |
|
|
|
} |
|
|
|
} |
|
|
|
return track; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
boolean isStarted() { |
|
|
|
boolean isStarted() { |
|
|
|
return mMediaMuxerStarted; |
|
|
|
synchronized (mLock) { |
|
|
|
|
|
|
|
return mMediaMuxerStarted; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Synchronization does not seem needed here.
|
|
|
|
void write(int track, ByteBuffer encodedData, MediaCodec.BufferInfo info) { |
|
|
|
void write(int track, ByteBuffer encodedData, MediaCodec.BufferInfo info) { |
|
|
|
if (!mMediaMuxerStarted) { |
|
|
|
if (!mMediaMuxerStarted) { |
|
|
|
throw new IllegalStateException("Trying to write before muxer started"); |
|
|
|
throw new IllegalStateException("Trying to write before muxer started"); |
|
|
@ -77,6 +113,16 @@ class MediaEncoderEngine { |
|
|
|
Log.e("MediaEncoderEngine", "Writing data." + track); |
|
|
|
Log.e("MediaEncoderEngine", "Writing data." + track); |
|
|
|
mMediaMuxer.writeSampleData(track, encodedData, info); |
|
|
|
mMediaMuxer.writeSampleData(track, encodedData, info); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void requestStop() { |
|
|
|
|
|
|
|
synchronized (mLock) { |
|
|
|
|
|
|
|
mMediaMuxerStartCount--; |
|
|
|
|
|
|
|
if (mMediaMuxerStartCount == 0) { |
|
|
|
|
|
|
|
mStopReason = mPossibleStopReason; |
|
|
|
|
|
|
|
stop(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void start() { |
|
|
|
void start() { |
|
|
@ -101,7 +147,7 @@ class MediaEncoderEngine { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void stop(final Runnable onStop) { |
|
|
|
void stop() { |
|
|
|
mWorker.post(new Runnable() { |
|
|
|
mWorker.post(new Runnable() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
@ -111,17 +157,31 @@ class MediaEncoderEngine { |
|
|
|
for (MediaEncoder encoder : mEncoders) { |
|
|
|
for (MediaEncoder encoder : mEncoders) { |
|
|
|
encoder.release(); |
|
|
|
encoder.release(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Exception error = null; |
|
|
|
if (mMediaMuxer != null) { |
|
|
|
if (mMediaMuxer != null) { |
|
|
|
// stop() throws an exception if you haven't fed it any data.
|
|
|
|
// stop() throws an exception if you haven't fed it any data.
|
|
|
|
// We can just swallow I think.
|
|
|
|
// But also in other occasions. So this is a signal that something
|
|
|
|
mMediaMuxer.stop(); |
|
|
|
// went wrong, and we propagate that to the listener.
|
|
|
|
mMediaMuxer.release(); |
|
|
|
try { |
|
|
|
|
|
|
|
mMediaMuxer.stop(); |
|
|
|
|
|
|
|
mMediaMuxer.release(); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
error = e; |
|
|
|
|
|
|
|
} |
|
|
|
mMediaMuxer = null; |
|
|
|
mMediaMuxer = null; |
|
|
|
} |
|
|
|
} |
|
|
|
onStop.run(); |
|
|
|
if (mListener != null) mListener.onEncoderStop(mStopReason, error); |
|
|
|
|
|
|
|
mStopReason = STOP_BY_USER; |
|
|
|
|
|
|
|
mListener = null; |
|
|
|
mMediaMuxerStartCount = 0; |
|
|
|
mMediaMuxerStartCount = 0; |
|
|
|
mMediaMuxerStarted = false; |
|
|
|
mMediaMuxerStarted = false; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface Listener { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@EncoderThread |
|
|
|
|
|
|
|
void onEncoderStop(int stopReason, @Nullable Exception e); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|