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

media / base / async_destroy_video_decoder.h [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_ASYNC_DESTROY_VIDEO_DECODER_H_
#define MEDIA_BASE_ASYNC_DESTROY_VIDEO_DECODER_H_

#include <memory>
#include <type_traits>
#include "media/base/video_decoder.h"

namespace media {

// Some VideoDecoder implementations must do non-synchronous cleanup before
// they are destroyed. This wrapper implementation allows a VideoDecoder
// to schedule its own cleanup tasks before its memory is released.
// The underlying type must implement a static
// `DestroyAsync(std::unique_ptr<T>)` function which fires any pending
// callbacks, stops and destroys the decoder. After this call, external
// resources (e.g. raw pointers) held by the decoder might be invalidated
// immediately. So if the decoder is destroyed asynchronously (e.g. DeleteSoon),
// external resources must be released in this call.
template <typename T>
class AsyncDestroyVideoDecoder final : public VideoDecoder {
 public:
  explicit AsyncDestroyVideoDecoder(std::unique_ptr<T> wrapped_decoder)
      : wrapped_decoder_(std::move(wrapped_decoder)) {
    static_assert(std::is_base_of<VideoDecoder, T>::value,
                  "T must implement 'media::VideoDecoder'");
    DCHECK(wrapped_decoder_);
  }

  ~AsyncDestroyVideoDecoder() override {
    if (wrapped_decoder_)
      T::DestroyAsync(std::move(wrapped_decoder_));
  }

  VideoDecoderType GetDecoderType() const override {
    DCHECK(wrapped_decoder_);
    return wrapped_decoder_->GetDecoderType();
  }

  bool IsPlatformDecoder() const override {
    DCHECK(wrapped_decoder_);
    return wrapped_decoder_->IsPlatformDecoder();
  }

  void Initialize(const VideoDecoderConfig& config,
                  bool low_delay,
                  CdmContext* cdm_context,
                  InitCB init_cb,
                  const OutputCB& output_cb,
                  const WaitingCB& waiting_cb) override {
    DCHECK(wrapped_decoder_);
    wrapped_decoder_->Initialize(config, low_delay, cdm_context,
                                 std::move(init_cb), output_cb, waiting_cb);
  }

  void Decode(scoped_refptr<DecoderBuffer> buffer,
              DecodeCB decode_cb) override {
    DCHECK(wrapped_decoder_);
    wrapped_decoder_->Decode(std::move(buffer), std::move(decode_cb));
  }

  void Reset(base::OnceClosure closure) override {
    DCHECK(wrapped_decoder_);
    wrapped_decoder_->Reset(std::move(closure));
  }

  bool NeedsBitstreamConversion() const override {
    DCHECK(wrapped_decoder_);
    return wrapped_decoder_->NeedsBitstreamConversion();
  }

  bool CanReadWithoutStalling() const override {
    DCHECK(wrapped_decoder_);
    return wrapped_decoder_->CanReadWithoutStalling();
  }

  int GetMaxDecodeRequests() const override {
    DCHECK(wrapped_decoder_);
    return wrapped_decoder_->GetMaxDecodeRequests();
  }

  bool FramesHoldExternalResources() const override {
    DCHECK(wrapped_decoder_);
    return wrapped_decoder_->FramesHoldExternalResources();
  }

 private:
  std::unique_ptr<T> wrapped_decoder_;
};

}  // namespace media

#endif  // MEDIA_BASE_ASYNC_DESTROY_VIDEO_DECODER_H_