summaryrefslogtreecommitdiff
path: root/chromium/media/gpu/video_frame_converter.h
blob: c995e0f083e0e3d4ff488179520d2ada6361d8f8 (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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_GPU_VIDEO_FRAME_CONVERTER_H_
#define MEDIA_GPU_VIDEO_FRAME_CONVERTER_H_

#include <memory>

#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequenced_task_runner.h"
#include "media/base/video_frame.h"
#include "media/gpu/media_gpu_export.h"

namespace media {

// Video decoders make use of a video frame pool to allocate output frames,
// which are sent to the client after decoding. However, the storage type of the
// allocated frame can be different from what the client expects. This class
// can be used to convert the type of output video frame in this case.
class MEDIA_GPU_EXPORT VideoFrameConverter {
 public:
  using OutputCB = base::RepeatingCallback<void(scoped_refptr<VideoFrame>)>;

  VideoFrameConverter();

  // Initialize the converter. This method must be called before any
  // ConvertFrame() is called.
  void Initialize(scoped_refptr<base::SequencedTaskRunner> parent_task_runner,
                  OutputCB output_cb);

  // Convert the frame and return the converted frame to the client by
  // |output_cb_|. This method must be called on |parent_task_runner_|.
  // The default implementation calls |output_cb_| with |frame| as-is.
  virtual void ConvertFrame(scoped_refptr<VideoFrame> frame);

  // Abort all pending frames. |output_cb_| should not be called for the input
  // frames passed before calling AbortPendingFrames(). This method must be
  // called on |parent_task_runner_|.
  virtual void AbortPendingFrames();

  // Return true if there is any pending frame. This method must be called on
  // |parent_task_runner_|.
  virtual bool HasPendingFrames() const;

 protected:
  // Deletion is only allowed via Destroy().
  virtual ~VideoFrameConverter();

  // The working task runner.
  scoped_refptr<base::SequencedTaskRunner> parent_task_runner_;

  // The callback to return converted frames back to client. This callback will
  // be called on |parent_task_runner_|.
  OutputCB output_cb_;

 private:
  friend struct std::default_delete<VideoFrameConverter>;
  // Called by std::default_delete.
  virtual void Destroy();

  DISALLOW_COPY_AND_ASSIGN(VideoFrameConverter);
};

}  // namespace media

namespace std {

// Specialize std::default_delete to call Destroy().
template <>
struct MEDIA_GPU_EXPORT default_delete<media::VideoFrameConverter> {
  void operator()(media::VideoFrameConverter* ptr) const;
};

}  // namespace std

#endif  // MEDIA_GPU_VIDEO_FRAME_CONVERTER_H_