Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit 5ffd27c

Browse files
qinminCommit bot
qinmin
authored and
Commit bot
committed
Pass buffer offset to PlayOutputBuffer() call
DequeueOutputBuffer returns a buffer index along with size and offset information. The offset is however ignored in PlayOutputBuffer() call. This change fixes this issue by passing the offset to PlayOutputBuffer(). BUG=519058 Review URL: https://codereview.chromium.org/1285733002 Cr-Commit-Position: refs/heads/master@{#344924}
1 parent f18270a commit 5ffd27c

14 files changed

+46
-20
lines changed

media/base/android/audio_decoder_job.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void AudioDecoderJob::ResetTimestampHelper() {
9696

9797
void AudioDecoderJob::ReleaseOutputBuffer(
9898
int output_buffer_index,
99+
size_t offset,
99100
size_t size,
100101
bool render_output,
101102
base::TimeDelta current_presentation_timestamp,
@@ -104,7 +105,7 @@ void AudioDecoderJob::ReleaseOutputBuffer(
104105
if (render_output) {
105106
int64 head_position = (static_cast<AudioCodecBridge*>(
106107
media_codec_bridge_.get()))->PlayOutputBuffer(
107-
output_buffer_index, size);
108+
output_buffer_index, size, offset);
108109
size_t new_frames_count = size / bytes_per_frame_;
109110
frame_count_ += new_frames_count;
110111
audio_timestamp_helper_->AddFrames(new_frames_count);

media/base/android/audio_decoder_job.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class AudioDecoderJob : public MediaDecoderJob {
4343
// MediaDecoderJob implementation.
4444
void ReleaseOutputBuffer(
4545
int output_buffer_index,
46+
size_t offset,
4647
size_t size,
4748
bool render_output,
4849
base::TimeDelta current_presentation_timestamp,

media/base/android/media_codec_audio_decoder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ void MediaCodecAudioDecoder::OnOutputFormatChanged() {
158158
}
159159

160160
void MediaCodecAudioDecoder::Render(int buffer_index,
161+
size_t offset,
161162
size_t size,
162163
bool render_output,
163164
base::TimeDelta pts,
@@ -171,7 +172,7 @@ void MediaCodecAudioDecoder::Render(int buffer_index,
171172
if (render_output) {
172173
int64 head_position =
173174
(static_cast<AudioCodecBridge*>(media_codec_bridge_.get()))
174-
->PlayOutputBuffer(buffer_index, size);
175+
->PlayOutputBuffer(buffer_index, size, offset);
175176

176177
size_t new_frames_count = size / bytes_per_frame_;
177178
frame_count_ += new_frames_count;

media/base/android/media_codec_audio_decoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class MediaCodecAudioDecoder : public MediaCodecDecoder {
4444
ConfigStatus ConfigureInternal() override;
4545
void OnOutputFormatChanged() override;
4646
void Render(int buffer_index,
47+
size_t offset,
4748
size_t size,
4849
bool render_output,
4950
base::TimeDelta pts,

media/base/android/media_codec_bridge.cc

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "media/base/android/media_codec_bridge.h"
66

7+
#include <algorithm>
78

89
#include "base/android/build_info.h"
910
#include "base/android/jni_android.h"
@@ -492,19 +493,25 @@ bool MediaCodecBridge::CopyFromOutputBuffer(int index,
492493
size_t offset,
493494
void* dst,
494495
int dst_size) {
495-
JNIEnv* env = AttachCurrentThread();
496-
ScopedJavaLocalRef<jobject> j_buffer(
497-
Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index));
498-
void* src_data =
499-
reinterpret_cast<uint8*>(env->GetDirectBufferAddress(j_buffer.obj())) +
500-
offset;
501-
int src_capacity = env->GetDirectBufferCapacity(j_buffer.obj()) - offset;
496+
void* src_data = nullptr;
497+
int src_capacity = GetOutputBufferAddress(index, offset, &src_data);
502498
if (src_capacity < dst_size)
503499
return false;
504500
memcpy(dst, src_data, dst_size);
505501
return true;
506502
}
507503

504+
int MediaCodecBridge::GetOutputBufferAddress(int index,
505+
size_t offset,
506+
void** addr) {
507+
JNIEnv* env = AttachCurrentThread();
508+
ScopedJavaLocalRef<jobject> j_buffer(
509+
Java_MediaCodecBridge_getOutputBuffer(env, j_media_codec_.obj(), index));
510+
*addr = reinterpret_cast<uint8*>(
511+
env->GetDirectBufferAddress(j_buffer.obj())) + offset;
512+
return env->GetDirectBufferCapacity(j_buffer.obj()) - offset;
513+
}
514+
508515
bool MediaCodecBridge::FillInputBuffer(int index,
509516
const uint8* data,
510517
size_t size) {
@@ -697,16 +704,19 @@ bool AudioCodecBridge::ConfigureMediaFormat(jobject j_format,
697704
return true;
698705
}
699706

700-
int64 AudioCodecBridge::PlayOutputBuffer(int index, size_t size) {
707+
int64 AudioCodecBridge::PlayOutputBuffer(
708+
int index, size_t size, size_t offset) {
701709
DCHECK_LE(0, index);
702710
int numBytes = base::checked_cast<int>(size);
703-
JNIEnv* env = AttachCurrentThread();
704-
ScopedJavaLocalRef<jobject> buf =
705-
Java_MediaCodecBridge_getOutputBuffer(env, media_codec(), index);
706-
uint8* buffer = static_cast<uint8*>(env->GetDirectBufferAddress(buf.obj()));
707711

708-
ScopedJavaLocalRef<jbyteArray> byte_array =
709-
base::android::ToJavaByteArray(env, buffer, numBytes);
712+
void* buffer = nullptr;
713+
int capacity = GetOutputBufferAddress(index, offset, &buffer);
714+
numBytes = std::min(capacity, numBytes);
715+
CHECK_GE(numBytes, 0);
716+
717+
JNIEnv* env = AttachCurrentThread();
718+
ScopedJavaLocalRef<jbyteArray> byte_array = base::android::ToJavaByteArray(
719+
env, static_cast<uint8*>(buffer), numBytes);
710720
return Java_MediaCodecBridge_playOutputBuffer(
711721
env, media_codec(), byte_array.obj());
712722
}

media/base/android/media_codec_bridge.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ class MEDIA_EXPORT MediaCodecBridge {
202202
// started.
203203
bool StartInternal() WARN_UNUSED_RESULT;
204204

205+
// Called to get the buffer address given the output buffer index and offset.
206+
// This function returns the size of the output and |addr| is the pointer to
207+
// the address to read.
208+
int GetOutputBufferAddress(int index, size_t offset, void** addr);
209+
205210
jobject media_codec() { return j_media_codec_.obj(); }
206211
MediaCodecDirection direction_;
207212

@@ -236,7 +241,7 @@ class AudioCodecBridge : public MediaCodecBridge {
236241
// Play the output buffer. This call must be called after
237242
// DequeueOutputBuffer() and before ReleaseOutputBuffer. Returns the playback
238243
// head position expressed in frames.
239-
int64 PlayOutputBuffer(int index, size_t size);
244+
int64 PlayOutputBuffer(int index, size_t size, size_t offset);
240245

241246
// Set the volume of the audio output.
242247
void SetVolume(double volume);

media/base/android/media_codec_decoder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ bool MediaCodecDecoder::DepleteOutputBufferQueue() {
633633

634634
case MEDIA_CODEC_OK:
635635
// We got the decoded frame
636-
Render(buffer_index, size, true, pts, eos_encountered);
636+
Render(buffer_index, offset, size, true, pts, eos_encountered);
637637
break;
638638

639639
case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER:

media/base/android/media_codec_decoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class MediaCodecDecoder {
232232
// Renders the decoded frame and releases output buffer, or posts
233233
// a delayed task to do it at a later time,
234234
virtual void Render(int buffer_index,
235+
size_t offset,
235236
size_t size,
236237
bool render_output,
237238
base::TimeDelta pts,

media/base/android/media_codec_video_decoder.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ void MediaCodecVideoDecoder::OnOutputFormatChanged() {
185185
}
186186

187187
void MediaCodecVideoDecoder::Render(int buffer_index,
188+
size_t offset,
188189
size_t size,
189190
bool render_output,
190191
base::TimeDelta pts,

media/base/android/media_codec_video_decoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class MediaCodecVideoDecoder : public MediaCodecDecoder {
5656
void SynchronizePTSWithTime(base::TimeDelta current_time) override;
5757
void OnOutputFormatChanged() override;
5858
void Render(int buffer_index,
59+
size_t offset,
5960
size_t size,
6061
bool render_output,
6162
base::TimeDelta pts,

media/base/android/media_decoder_job.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ void MediaDecoderJob::DecodeInternal(
495495
base::Bind(&MediaDecoderJob::ReleaseOutputBuffer,
496496
base::Unretained(this),
497497
buffer_index,
498+
offset,
498499
size,
499500
render_output,
500501
presentation_timestamp,
@@ -518,8 +519,8 @@ void MediaDecoderJob::DecodeInternal(
518519
}
519520
ReleaseOutputCompletionCallback completion_callback = base::Bind(
520521
callback, status);
521-
ReleaseOutputBuffer(buffer_index, size, render_output, presentation_timestamp,
522-
completion_callback);
522+
ReleaseOutputBuffer(buffer_index, offset, size, render_output,
523+
presentation_timestamp, completion_callback);
523524
}
524525

525526
void MediaDecoderJob::OnDecodeCompleted(

media/base/android/media_decoder_job.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class MediaDecoderJob {
126126
// |render_output| is true. Upon completion, |callback| will be called.
127127
virtual void ReleaseOutputBuffer(
128128
int output_buffer_index,
129+
size_t offset,
129130
size_t size,
130131
bool render_output,
131132
base::TimeDelta current_presentation_timestamp,

media/base/android/video_decoder_job.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ void VideoDecoderJob::SetDemuxerConfigs(const DemuxerConfigs& configs) {
7878

7979
void VideoDecoderJob::ReleaseOutputBuffer(
8080
int output_buffer_index,
81+
size_t offset,
8182
size_t size,
8283
bool render_output,
8384
base::TimeDelta current_presentation_timestamp,

media/base/android/video_decoder_job.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class VideoDecoderJob : public MediaDecoderJob {
4343
// MediaDecoderJob implementation.
4444
void ReleaseOutputBuffer(
4545
int output_buffer_index,
46+
size_t offset,
4647
size_t size,
4748
bool render_output,
4849
base::TimeDelta current_presentation_timestamp,

0 commit comments

Comments
 (0)