summaryrefslogtreecommitdiff
path: root/chromium/media/gpu/android/ndk_video_encode_accelerator.cc
blob: 478cc88fd6acc7935bb17dfa89e15b9e94f4d00b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/gpu/android/ndk_video_encode_accelerator.h"

#include "base/android/build_info.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "media/base/android/media_codec_util.h"
#include "media/base/android/media_jni_headers/VideoEncodeAcceleratorUtil_jni.h"
#include "media/base/bitstream_buffer.h"
#include "media/base/video_frame.h"
#include "media/gpu/android/mediacodec_stubs.h"
#include "third_party/libyuv/include/libyuv/convert_from.h"
#include "third_party/libyuv/include/libyuv/scale.h"

namespace media {

namespace {
// Default distance between key frames. About 100 seconds between key frames,
// the same default value we use on Windows.
constexpr uint32_t kDefaultGOPLength = 3000;

// Deliberately breaking naming convention rules, to match names from
// MediaCodec SDK.
constexpr int32_t BUFFER_FLAG_KEY_FRAME = 1;

enum PixelFormat {
  // Subset of MediaCodecInfo.CodecCapabilities.
  COLOR_FORMAT_YUV420_PLANAR = 19,
  COLOR_FORMAT_YUV420_SEMIPLANAR = 21,  // Same as NV12
};

struct AMediaFormatDeleter {
  inline void operator()(AMediaFormat* ptr) const {
    if (ptr)
      AMediaFormat_delete(ptr);
  }
};

using MediaFormatPtr = std::unique_ptr<AMediaFormat, AMediaFormatDeleter>;

MediaFormatPtr CreateVideoFormat(const std::string& mime,
                                 const VideoEncodeAccelerator::Config& config,
                                 int framerate,
                                 PixelFormat format) {
  const int iframe_interval = config.gop_length.value_or(kDefaultGOPLength);
  const gfx::Size& frame_size = config.input_visible_size;
  const Bitrate& bitrate = config.bitrate;
  MediaFormatPtr result(AMediaFormat_new());
  AMediaFormat_setString(result.get(), AMEDIAFORMAT_KEY_MIME, mime.c_str());
  AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_WIDTH,
                        frame_size.width());
  AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_HEIGHT,
                        frame_size.height());

  AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_FRAME_RATE, framerate);
  AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_I_FRAME_INTERVAL,
                        iframe_interval);
  AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_COLOR_FORMAT, format);
  if (config.require_low_delay) {
    AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_LATENCY, 1);
    // MediaCodec supports two priorities: 0 - realtime, 1 - best effort
    AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_PRIORITY, 0);
  }

  constexpr int32_t BITRATE_MODE_VBR = 1;
  constexpr int32_t BITRATE_MODE_CBR = 2;
  switch (bitrate.mode()) {
    case Bitrate::Mode::kConstant:
      AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_BITRATE_MODE,
                            BITRATE_MODE_CBR);
      break;
    case Bitrate::Mode::kVariable:
      AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_BITRATE_MODE,
                            BITRATE_MODE_VBR);
      break;
    default:
      NOTREACHED();
  }

  AMediaFormat_setInt32(result.get(), AMEDIAFORMAT_KEY_BIT_RATE,
                        base::saturated_cast<int32_t>(bitrate.target_bps()));
  return result;
}

BASE_FEATURE(kAndroidNdkVideoEncoder,
             "AndroidNdkVideoEncoder",
             base::FEATURE_ENABLED_BY_DEFAULT);

static bool InitMediaCodec() {
  // We need at least Android P for AMediaCodec_getInputFormat(), but in
  // Android P we have issues with CFI and dynamic linker on arm64. However
  // GetSupportedProfiles() needs Q+, so just limit to Q.
  if (base::android::BuildInfo::GetInstance()->sdk_int() <
      base::android::SDK_VERSION_Q) {
    return false;
  }

  if (!base::FeatureList::IsEnabled(kAndroidNdkVideoEncoder))
    return false;

  media_gpu_android::StubPathMap paths;
  static const base::FilePath::CharType kMediacodecPath[] =
      FILE_PATH_LITERAL("libmediandk.so");

  paths[media_gpu_android::kModuleMediacodec].push_back(kMediacodecPath);
  if (!media_gpu_android::InitializeStubs(paths)) {
    LOG(ERROR) << "Failed on loading libmediandk.so symbols";
    return false;
  }
  return true;
}

}  // namespace

NdkVideoEncodeAccelerator::NdkVideoEncodeAccelerator(
    scoped_refptr<base::SequencedTaskRunner> runner)
    : task_runner_(runner) {}
NdkVideoEncodeAccelerator::~NdkVideoEncodeAccelerator() {
  // It's supposed to be cleared by Destroy(), it basically checks
  // that we destroy `this` correctly.
  DCHECK(!media_codec_);
}

bool NdkVideoEncodeAccelerator::IsSupported() {
  static const bool is_loaded = InitMediaCodec();
  return is_loaded;
}

VideoEncodeAccelerator::SupportedProfiles
NdkVideoEncodeAccelerator::GetSupportedProfiles() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  SupportedProfiles profiles;
  if (!IsSupported())
    return profiles;

  // Sadly the NDK doesn't provide a mechanism for accessing the equivalent of
  // the SDK's MediaCodecList, so we must call into Java to enumerate support.
  JNIEnv* env = base::android::AttachCurrentThread();
  auto java_profiles =
      Java_VideoEncodeAcceleratorUtil_getSupportedProfiles(env);
  if (java_profiles) {
    for (auto java_profile : java_profiles.ReadElements<jobject>()) {
      VideoEncodeAccelerator::SupportedProfile result;
      result.profile = static_cast<VideoCodecProfile>(
          Java_SupportedProfileAdapter_getProfile(env, java_profile));
      result.min_resolution = gfx::Size(
          Java_SupportedProfileAdapter_getMinWidth(env, java_profile),
          Java_SupportedProfileAdapter_getMinHeight(env, java_profile));
      result.max_resolution = gfx::Size(
          Java_SupportedProfileAdapter_getMaxWidth(env, java_profile),
          Java_SupportedProfileAdapter_getMaxHeight(env, java_profile));
      result.max_framerate_numerator =
          Java_SupportedProfileAdapter_getMaxFramerateNumerator(env,
                                                                java_profile);
      result.max_framerate_denominator =
          Java_SupportedProfileAdapter_getMaxFramerateDenominator(env,
                                                                  java_profile);
      if (Java_SupportedProfileAdapter_supportsCbr(env, java_profile))
        result.rate_control_modes |= kConstantMode;
      if (Java_SupportedProfileAdapter_supportsVbr(env, java_profile))
        result.rate_control_modes |= kVariableMode;
      profiles.push_back(result);
    }
  }
  return profiles;
}

bool NdkVideoEncodeAccelerator::Initialize(
    const Config& config,
    Client* client,
    std::unique_ptr<MediaLog> media_log) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(!media_codec_);
  DCHECK(client);

  if (!IsSupported()) {
    MEDIA_LOG(ERROR, log_) << "Unsupported Android version.";
    return false;
  }

  callback_weak_ptr_ = callback_weak_factory_.GetWeakPtr();
  client_ptr_factory_ = std::make_unique<base::WeakPtrFactory<Client>>(client);
  config_ = config;
  effective_bitrate_ = config.bitrate;
  log_ = std::move(media_log);
  VideoCodec codec = VideoCodecProfileToVideoCodec(config.output_profile);

  if (config.input_format != PIXEL_FORMAT_I420 &&
      config.input_format != PIXEL_FORMAT_NV12) {
    MEDIA_LOG(ERROR, log_) << "Unexpected combo: " << config.input_format
                           << ", " << GetProfileName(config.output_profile);
    return false;
  }

  // Non 16x16 aligned resolutions don't work with MediaCodec unfortunately, see
  // https://crbug.com/1084702 for details.
  if (config.input_visible_size.width() % 16 != 0 ||
      config.input_visible_size.height() % 16 != 0) {
    MEDIA_LOG(ERROR, log_) << "MediaCodec is only tested with resolutions "
                              "that are 16x16 aligned.";
    return false;
  }

  auto mime = MediaCodecUtil::CodecToAndroidMimeType(codec);
  if (!IsThereGoodMediaCodecFor(codec)) {
    MEDIA_LOG(ERROR, log_) << "No suitable MedicCodec found for: " << mime;
    return false;
  }

  effective_framerate_ = config.initial_framerate.value_or(kDefaultFramerate);
  auto media_format = CreateVideoFormat(mime, config, effective_framerate_,
                                        COLOR_FORMAT_YUV420_SEMIPLANAR);

  media_codec_.reset(AMediaCodec_createEncoderByType(mime.c_str()));
  if (!media_codec_) {
    MEDIA_LOG(ERROR, log_) << "Can't create media codec for mime type: "
                           << mime;
    return false;
  }
  media_status_t status =
      AMediaCodec_configure(media_codec_.get(), media_format.get(), nullptr,
                            nullptr, AMEDIACODEC_CONFIGURE_FLAG_ENCODE);
  if (status != AMEDIA_OK) {
    MEDIA_LOG(ERROR, log_) << "Can't configure media codec. Error " << status;
    return false;
  }

  if (!SetInputBufferLayout()) {
    MEDIA_LOG(ERROR, log_) << "Can't get input buffer layout from MediaCodec";
    return false;
  }

  // Set MediaCodec callbacks and switch it to async mode
  AMediaCodecOnAsyncNotifyCallback callbacks{
      &NdkVideoEncodeAccelerator::OnAsyncInputAvailable,
      &NdkVideoEncodeAccelerator::OnAsyncOutputAvailable,
      &NdkVideoEncodeAccelerator::OnAsyncFormatChanged,
      &NdkVideoEncodeAccelerator::OnAsyncError,
  };
  status =
      AMediaCodec_setAsyncNotifyCallback(media_codec_.get(), callbacks, this);
  if (status != AMEDIA_OK) {
    MEDIA_LOG(ERROR, log_) << "Can't set media codec callback. Error "
                           << status;
    return false;
  }

  status = AMediaCodec_start(media_codec_.get());
  if (status != AMEDIA_OK) {
    MEDIA_LOG(ERROR, log_) << "Can't start media codec. Error " << status;
    return false;
  }

  // Conservative upper bound for output buffer size: decoded size + 2KB.
  // Adding 2KB just in case the frame is really small, we don't want to
  // end up with no space for a video codec's headers.
  const size_t output_buffer_capacity =
      VideoFrame::AllocationSize(config.input_format,
                                 config.input_visible_size) +
      2048;
  task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&VideoEncodeAccelerator::Client::RequireBitstreamBuffers,
                     client_ptr_factory_->GetWeakPtr(), 1,
                     config.input_visible_size, output_buffer_capacity));

  return true;
}

void NdkVideoEncodeAccelerator::Encode(scoped_refptr<VideoFrame> frame,
                                       bool force_keyframe) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(media_codec_);
  VideoEncoder::PendingEncode encode;
  encode.frame = std::move(frame);
  encode.key_frame = force_keyframe;
  pending_frames_.push_back(std::move(encode));
  FeedInput();
}

void NdkVideoEncodeAccelerator::UseOutputBitstreamBuffer(
    BitstreamBuffer buffer) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  available_bitstream_buffers_.push_back(std::move(buffer));
  DrainOutput();
}

void NdkVideoEncodeAccelerator::RequestEncodingParametersChange(
    const Bitrate& bitrate,
    uint32_t framerate) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  MediaFormatPtr format(AMediaFormat_new());

  if (effective_framerate_ != framerate)
    AMediaFormat_setInt32(format.get(), AMEDIAFORMAT_KEY_FRAME_RATE, framerate);
  if (effective_bitrate_ != bitrate) {
    AMediaFormat_setInt32(format.get(), AMEDIACODEC_KEY_VIDEO_BITRATE,
                          bitrate.target_bps());
  }
  media_status_t status =
      AMediaCodec_setParameters(media_codec_.get(), format.get());

  if (status != AMEDIA_OK) {
    NotifyMediaCodecError("Failed to change bitrate and framerate", status);
    return;
  }
  effective_framerate_ = framerate;
  effective_bitrate_ = bitrate;
}

void NdkVideoEncodeAccelerator::Destroy() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  client_ptr_factory_.reset();
  callback_weak_factory_.InvalidateWeakPtrs();
  if (media_codec_) {
    AMediaCodec_stop(media_codec_.get());

    // Internally this calls AMediaFormat_delete(), and before exiting
    // AMediaFormat_delete() drains all calls on the internal thread that
    // calls OnAsyncXXXXX() functions. (Even though this fact is not documented)
    // It means by the time we actually destruct `this`, no OnAsyncXXXXX()
    // functions will use it via saved `userdata` pointers.
    media_codec_.reset();
  }
  delete this;
}

bool NdkVideoEncodeAccelerator::SetInputBufferLayout() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!media_codec_)
    return false;

  MediaFormatPtr input_format(AMediaCodec_getInputFormat(media_codec_.get()));
  if (!input_format) {
    return false;
  }

  if (!AMediaFormat_getInt32(input_format.get(), AMEDIAFORMAT_KEY_STRIDE,
                             &input_buffer_stride_)) {
    input_buffer_stride_ = config_.input_visible_size.width();
  }
  if (!AMediaFormat_getInt32(input_format.get(), AMEDIAFORMAT_KEY_SLICE_HEIGHT,
                             &input_buffer_yplane_height_)) {
    input_buffer_yplane_height_ = config_.input_visible_size.height();
  }
  return true;
}

base::TimeDelta NdkVideoEncodeAccelerator::AssignMonotonicTimestamp(
    base::TimeDelta real_timestamp) {
  base::TimeDelta step = base::Seconds(1) / effective_framerate_;
  auto result = next_timestamp_;
  generated_to_real_timestamp_map_[result] = real_timestamp;
  next_timestamp_ += step;
  return result;
}

base::TimeDelta NdkVideoEncodeAccelerator::RetrieveRealTimestamp(
    base::TimeDelta monotonic_timestamp) {
  base::TimeDelta result;
  auto it = generated_to_real_timestamp_map_.find(monotonic_timestamp);
  if (it != generated_to_real_timestamp_map_.end()) {
    result = it->second;
    generated_to_real_timestamp_map_.erase(it);
  }
  return result;
}

void NdkVideoEncodeAccelerator::FeedInput() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(media_codec_);

  if (error_occurred_)
    return;

  if (media_codec_input_buffers_.empty() || pending_frames_.empty())
    return;

  scoped_refptr<VideoFrame> frame = std::move(pending_frames_.front().frame);
  bool key_frame = pending_frames_.front().key_frame;
  pending_frames_.pop_front();

  if (key_frame) {
    // Signal to the media codec that it needs to include a key frame
    MediaFormatPtr format(AMediaFormat_new());
    AMediaFormat_setInt32(format.get(), AMEDIACODEC_KEY_REQUEST_SYNC_FRAME, 0);
    media_status_t status =
        AMediaCodec_setParameters(media_codec_.get(), format.get());

    if (status != AMEDIA_OK) {
      NotifyMediaCodecError("Failed to request a keyframe", status);
      return;
    }
  }

  size_t buffer_idx = media_codec_input_buffers_.front();
  media_codec_input_buffers_.pop_front();

  size_t capacity = 0;
  uint8_t* buffer_ptr =
      AMediaCodec_getInputBuffer(media_codec_.get(), buffer_idx, &capacity);
  if (!buffer_ptr) {
    NotifyError("Can't obtain input buffer from media codec.",
                kPlatformFailureError);
    return;
  }

  uint8_t* dst_y = buffer_ptr;
  const int dst_stride_y = input_buffer_stride_;
  const int uv_plane_offset =
      input_buffer_yplane_height_ * input_buffer_stride_;
  uint8_t* dst_uv = buffer_ptr + uv_plane_offset;
  const int dst_stride_uv = input_buffer_stride_;

  const gfx::Size uv_plane_size = VideoFrame::PlaneSizeInSamples(
      PIXEL_FORMAT_NV12, VideoFrame::kUVPlane, frame->coded_size());
  const size_t queued_size =
      // size of Y-plane plus padding till UV-plane
      uv_plane_offset +
      // size of all UV-plane lines but the last one
      (uv_plane_size.height() - 1) * dst_stride_uv +
      // size of the very last line in UV-plane (it's not padded to full stride)
      uv_plane_size.width() * 2;

  if (queued_size > capacity) {
    auto message = base::StringPrintf(
        "Frame doesn't fit into the input buffer. queued_size: %zu capacity: "
        "%zu",
        queued_size, capacity);
    NotifyError(message, kPlatformFailureError);
    return;
  }

  bool converted = false;
  if (frame->format() == PIXEL_FORMAT_I420) {
    converted = !libyuv::I420ToNV12(
        frame->data(VideoFrame::kYPlane), frame->stride(VideoFrame::kYPlane),
        frame->data(VideoFrame::kUPlane), frame->stride(VideoFrame::kUPlane),
        frame->data(VideoFrame::kVPlane), frame->stride(VideoFrame::kVPlane),
        dst_y, dst_stride_y, dst_uv, dst_stride_uv, frame->coded_size().width(),
        frame->coded_size().height());
  } else if (frame->format() == PIXEL_FORMAT_NV12) {
    // No actual scaling will be performed since src and dst sizes are the same
    // NV12Scale() works simply as glorified memcpy.
    converted = !libyuv::NV12Scale(
        frame->visible_data(VideoFrame::kYPlane),
        frame->stride(VideoFrame::kYPlane),
        frame->visible_data(VideoFrame::kUVPlane),
        frame->stride(VideoFrame::kUVPlane), frame->coded_size().width(),
        frame->coded_size().height(), dst_y, dst_stride_y, dst_uv,
        dst_stride_uv, frame->coded_size().width(),
        frame->coded_size().height(), libyuv::kFilterBox);
  }

  if (!converted) {
    NotifyError("Failed to copy pixels to input buffer.",
                kPlatformFailureError);
    return;
  }

  // MediaCodec uses timestamps for rate control purposes, but we can't rely
  // on real frame timestamps to be consistent with configured frame rate.
  // That's why we map real frame timestamps to generate ones that a
  // monotonically increase according to the configured frame rate.
  // We do the opposite for each output buffer, to restore accurate frame
  // timestamps.
  auto generate_timestamp = AssignMonotonicTimestamp(frame->timestamp());
  uint64_t flags = 0;  // Unfortunately BUFFER_FLAG_KEY_FRAME has no effect here
  media_status_t status = AMediaCodec_queueInputBuffer(
      media_codec_.get(), buffer_idx, /*offset=*/0, queued_size,
      generate_timestamp.InMicroseconds(), flags);
  if (status != AMEDIA_OK) {
    NotifyMediaCodecError("Failed to queueInputBuffer", status);
    return;
  }
}

void NdkVideoEncodeAccelerator::NotifyMediaCodecError(std::string message,
                                                      media_status_t error) {
  auto message_and_code = base::StringPrintf("%s MediaCodec error code: %d",
                                             message.c_str(), error);
  NotifyError(message_and_code, kPlatformFailureError);
}

void NdkVideoEncodeAccelerator::NotifyError(base::StringPiece message,
                                            Error code) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  MEDIA_LOG(ERROR, log_) << message;
  LOG(ERROR) << message;
  if (!error_occurred_) {
    client_ptr_factory_->GetWeakPtr()->NotifyError(code);
    error_occurred_ = true;
  }
}

void NdkVideoEncodeAccelerator::OnAsyncInputAvailable(AMediaCodec* codec,
                                                      void* userdata,
                                                      int32_t index) {
  auto* self = reinterpret_cast<NdkVideoEncodeAccelerator*>(userdata);
  DCHECK(self);

  self->task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&NdkVideoEncodeAccelerator::OnInputAvailable,
                                self->callback_weak_ptr_, index));
}

void NdkVideoEncodeAccelerator::OnInputAvailable(int32_t index) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  media_codec_input_buffers_.push_back(index);
  FeedInput();
}

void NdkVideoEncodeAccelerator::OnAsyncOutputAvailable(
    AMediaCodec* codec,
    void* userdata,
    int32_t index,
    AMediaCodecBufferInfo* bufferInfo) {
  auto* self = reinterpret_cast<NdkVideoEncodeAccelerator*>(userdata);
  DCHECK(self);

  self->task_runner_->PostTask(
      FROM_HERE, base::BindOnce(&NdkVideoEncodeAccelerator::OnOutputAvailable,
                                self->callback_weak_ptr_, index, *bufferInfo));
}

void NdkVideoEncodeAccelerator::OnOutputAvailable(int32_t index,
                                                  AMediaCodecBufferInfo info) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  media_codec_output_buffers_.push_back({index, info});
  DrainOutput();
}

void NdkVideoEncodeAccelerator::OnAsyncError(AMediaCodec* codec,
                                             void* userdata,
                                             media_status_t error,
                                             int32_t actionCode,
                                             const char* detail) {
  auto* self = reinterpret_cast<NdkVideoEncodeAccelerator*>(userdata);
  DCHECK(self);

  self->task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&NdkVideoEncodeAccelerator::NotifyMediaCodecError,
                     self->callback_weak_ptr_, "Media codec async error.",
                     error));
}

bool NdkVideoEncodeAccelerator::DrainConfig() {
  if (media_codec_output_buffers_.empty())
    return false;

  MCOutput output_buffer = media_codec_output_buffers_.front();
  AMediaCodecBufferInfo& mc_buffer_info = output_buffer.info;
  const size_t mc_buffer_size = static_cast<size_t>(mc_buffer_info.size);

  // Check that the first buffer in the queue contains config data.
  if ((mc_buffer_info.flags & AMEDIACODEC_BUFFER_FLAG_CODEC_CONFIG) == 0)
    return false;

  media_codec_output_buffers_.pop_front();
  size_t capacity = 0;
  uint8_t* buf_data = AMediaCodec_getOutputBuffer(
      media_codec_.get(), output_buffer.buffer_index, &capacity);

  if (!buf_data) {
    NotifyError("Can't obtain output buffer from media codec.",
                kPlatformFailureError);
    return false;
  }

  if (mc_buffer_info.offset + mc_buffer_size > capacity) {
    auto message = base::StringPrintf(
        "Invalid output buffer layout."
        "offset: %d size: %zu capacity: %zu",
        mc_buffer_info.offset, mc_buffer_size, capacity);
    NotifyError(message, kPlatformFailureError);
    return false;
  }

  config_data_.resize(mc_buffer_size);
  memcpy(config_data_.data(), buf_data + mc_buffer_info.offset, mc_buffer_size);
  AMediaCodec_releaseOutputBuffer(media_codec_.get(),
                                  output_buffer.buffer_index, false);
  return true;
}

void NdkVideoEncodeAccelerator::DrainOutput() {
  if (error_occurred_)
    return;

  // Config data (e.g. PPS and SPS for H.264) needs to be handled differently,
  // because we save it for later rather than giving it as an output
  // straight away.
  if (DrainConfig())
    return;

  if (media_codec_output_buffers_.empty() ||
      available_bitstream_buffers_.empty()) {
    return;
  }

  MCOutput output_buffer = media_codec_output_buffers_.front();
  AMediaCodecBufferInfo& mc_buffer_info = output_buffer.info;
  const size_t mc_buffer_size = static_cast<size_t>(mc_buffer_info.size);
  media_codec_output_buffers_.pop_front();

  if ((mc_buffer_info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) != 0)
    return;
  const bool key_frame = (mc_buffer_info.flags & BUFFER_FLAG_KEY_FRAME) != 0;

  BitstreamBuffer bitstream_buffer =
      std::move(available_bitstream_buffers_.back());
  available_bitstream_buffers_.pop_back();

  const size_t config_size = key_frame ? config_data_.size() : 0u;
  if (config_size + mc_buffer_size > bitstream_buffer.size()) {
    auto message = base::StringPrintf(
        "Encoded output is too large. mc output size: %zu"
        " bitstream buffer size: %zu"
        " config size: %zu",
        mc_buffer_size, bitstream_buffer.size(), config_size);
    NotifyError(message, kPlatformFailureError);
    return;
  }

  size_t capacity = 0;
  uint8_t* buf_data = AMediaCodec_getOutputBuffer(
      media_codec_.get(), output_buffer.buffer_index, &capacity);

  if (!buf_data) {
    NotifyError("Can't obtain output buffer from media codec.",
                kPlatformFailureError);
    return;
  }

  if (mc_buffer_info.offset + mc_buffer_size > capacity) {
    auto message = base::StringPrintf(
        "Invalid output buffer layout."
        "offset: %d size: %zu capacity: %zu",
        mc_buffer_info.offset, mc_buffer_size, capacity);
    NotifyError(message, kPlatformFailureError);
    return;
  }

  base::UnsafeSharedMemoryRegion region = bitstream_buffer.TakeRegion();
  auto mapping =
      region.MapAt(bitstream_buffer.offset(), bitstream_buffer.size());
  if (!mapping.IsValid()) {
    NotifyError("Failed to map SHM", kPlatformFailureError);
    return;
  }

  uint8_t* output_dst = mapping.GetMemoryAs<uint8_t>();
  if (config_size > 0) {
    memcpy(output_dst, config_data_.data(), config_size);
    output_dst += config_size;
  }
  memcpy(output_dst, buf_data, mc_buffer_size);

  auto timestamp = base::Microseconds(mc_buffer_info.presentationTimeUs);
  timestamp = RetrieveRealTimestamp(timestamp);
  task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&VideoEncodeAccelerator::Client::BitstreamBufferReady,
                     client_ptr_factory_->GetWeakPtr(), bitstream_buffer.id(),
                     BitstreamBufferMetadata(mc_buffer_size + config_size,
                                             key_frame, timestamp)));
  AMediaCodec_releaseOutputBuffer(media_codec_.get(),
                                  output_buffer.buffer_index, false);
}

bool NdkVideoEncodeAccelerator::IsThereGoodMediaCodecFor(VideoCodec codec) {
  // GetSupportedProfiles() already accounts for codec support, so if the codec
  // is listed it's supported.
  static const SupportedProfiles kSupportedProfiles = GetSupportedProfiles();
  for (const auto& profile : kSupportedProfiles) {
    if (codec == VideoCodecProfileToVideoCodec(profile.profile))
      return true;
  }
  return false;
}

}  // namespace media