Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ public class DefaultDataSink implements DataSink {
*/
private static class QueuedSample {
private final TrackType mType;
private ByteBuffer mByteBuffer;

private final int mSize;
private final long mTimeUs;
private final int mFlags;

private QueuedSample(@NonNull TrackType type,
@NonNull ByteBuffer byteBuffer,
@NonNull MediaCodec.BufferInfo bufferInfo) {
mType = type;
mByteBuffer = byteBuffer;
mSize = bufferInfo.size;
mTimeUs = bufferInfo.presentationTimeUs;
mFlags = bufferInfo.flags;
Expand All @@ -52,14 +56,9 @@ private QueuedSample(@NonNull TrackType type,

private final static Logger LOG = new Logger("DefaultDataSink");

// We must be able to handle potentially big buffers (e.g. first keyframe) in the queue.
// Got crashes with 152kb - let's use 256kb. TODO use a dynamic queue instead
private final static int BUFFER_SIZE = 256 * 1024;

private boolean mMuxerStarted = false;
private final MediaMuxer mMuxer;
private final List<QueuedSample> mQueue = new ArrayList<>();
private ByteBuffer mQueueBuffer;
private final MutableTrackMap<TrackStatus> mStatus = mutableTrackMapOf(null);
private final MutableTrackMap<MediaFormat> mLastFormat = mutableTrackMapOf(null);
private final MutableTrackMap<Integer> mMuxerIndex = mutableTrackMapOf(null);
Expand Down Expand Up @@ -177,19 +176,14 @@ public void writeTrack(@NonNull TrackType type, @NonNull ByteBuffer byteBuffer,
private void enqueue(@NonNull TrackType type,
@NonNull ByteBuffer buffer,
@NonNull MediaCodec.BufferInfo bufferInfo) {
if (mQueueBuffer == null) {
mQueueBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE).order(ByteOrder.nativeOrder());
}
LOG.v("enqueue(" + type + "): offset=" + bufferInfo.offset
+ "\trealOffset=" + buffer.position()
+ "\tsize=" + bufferInfo.size
+ "\trealSize=" + buffer.remaining()
+ "\tavailable=" + mQueueBuffer.remaining()
+ "\ttotal=" + BUFFER_SIZE);
buffer.limit(bufferInfo.offset + bufferInfo.size);
buffer.position(bufferInfo.offset);
mQueueBuffer.put(buffer);
mQueue.add(new QueuedSample(type, bufferInfo));
+ "\trealSize=" + buffer.remaining());

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(bufferInfo.size).order(ByteOrder.nativeOrder());
byteBuffer.put(buffer);
mQueue.add(new QueuedSample(type, byteBuffer, bufferInfo));
}

/**
Expand All @@ -198,19 +192,16 @@ private void enqueue(@NonNull TrackType type,
*/
private void drainQueue() {
if (mQueue.isEmpty()) return;
mQueueBuffer.flip();
LOG.i("Output format determined, writing pending data into the muxer. "
+ "samples:" + mQueue.size() + " "
+ "bytes:" + mQueueBuffer.limit());
+ "samples:" + mQueue.size());
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int offset = 0;
for (QueuedSample sample : mQueue) {
bufferInfo.set(offset, sample.mSize, sample.mTimeUs, sample.mFlags);
writeTrack(sample.mType, mQueueBuffer, bufferInfo);
offset += sample.mSize;
bufferInfo.set(0, sample.mSize, sample.mTimeUs, sample.mFlags);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offsetが常に0が入るようになっていますが問題ないですか?(内部で使われていない?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1つのBufferに1回しか書き込まないので常に0で問題なしです!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうか。QueuedSampleにBufferを持たせるように変えたからBufferの参照方法も変わりますね。
良さそう。

sample.mByteBuffer.position(0);
writeTrack(sample.mType, sample.mByteBuffer, bufferInfo);
sample.mByteBuffer = null;
}
mQueue.clear();
mQueueBuffer = null;
}

@Override
Expand Down