summaryrefslogtreecommitdiff
path: root/chromium/media/base/video_encoder.h
blob: 90a1547812258b9e53517ba5b028eb5777607147 (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
// Copyright 2020 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_BASE_VIDEO_ENCODER_H_
#define MEDIA_BASE_VIDEO_ENCODER_H_

#include "base/callback.h"
#include "base/macros.h"
#include "base/optional.h"
#include "media/base/media_export.h"
#include "media/base/status.h"
#include "media/base/video_codecs.h"
#include "ui/gfx/geometry/size.h"

namespace media {

class VideoFrame;

// Encoded video frame, its data and metadata.
struct MEDIA_EXPORT VideoEncoderOutput {
  VideoEncoderOutput();
  VideoEncoderOutput(VideoEncoderOutput&&);
  ~VideoEncoderOutput();

  // Feel free take this buffer out and use underlying memory as is without
  // copying.
  std::unique_ptr<uint8_t[]> data;
  size_t size;

  base::TimeDelta timestamp;
  bool key_frame;
};

class MEDIA_EXPORT VideoEncoder {
 public:
  struct MEDIA_EXPORT Options {
    Options();
    ~Options();
    base::Optional<uint64_t> bitrate;
    double framerate = 30.0;

    int width = 0;
    int height = 0;

    base::Optional<int> threads;
    base::Optional<int> keyframe_interval = 10000;
  };

  // Callback for VideoEncoder to report an encoded video frame whenever it
  // becomes available.
  using OutputCB = base::RepeatingCallback<void(VideoEncoderOutput output)>;

  // Callback to report success and errors in encoder calls.
  using StatusCB = base::OnceCallback<void(Status error)>;

  VideoEncoder();
  virtual ~VideoEncoder();

  // Initializes a VideoEncoder with the given |options|, executing the
  // |done_cb| upon completion. |output_cb| is called for each encoded frame
  // produced by the coder.
  //
  // Note:
  // 1) Can't be called more than once for the same instance of the encoder.
  // 2) No VideoEncoder calls should be made before |done_cb| is executed.
  virtual void Initialize(VideoCodecProfile profile,
                          const Options& options,
                          OutputCB output_cb,
                          StatusCB done_cb) = 0;

  // Requests a |frame| to be encoded. The status of the encoder and the frame
  // are returned via the provided callback |done_cb|.
  //
  // |done_cb| will not be called from within this method, and that it will be
  // called even if Encode() is never called again.

  // After the frame, or several frames, are encoded the encoder calls
  // |output_cb| specified in Initialize() for available VideoEncoderOutput.
  // |output_cb| may be called before or after |done_cb|,
  // including before Encode() returns.
  // Encode() does not expect EOS frames, use Flush() to finalize the stream
  // and harvest the outputs.
  virtual void Encode(scoped_refptr<const VideoFrame> frame,
                      bool key_frame,
                      StatusCB done_cb) = 0;

  // Adjust encoder options for future frames, executing the
  // |done_cb| upon completion.
  //
  // Note:
  // 1. Not all options can be changed on the fly.
  // 2. ChangeOptions() should be called after calling Flush() and waiting
  // for it to finish.
  virtual void ChangeOptions(const Options& options, StatusCB done_cb) = 0;

  // Requests all outputs for already encoded frames to be
  // produced via |output_cb| and calls |dene_cb| after that.
  virtual void Flush(StatusCB done_cb) = 0;

 protected:
  DISALLOW_COPY_AND_ASSIGN(VideoEncoder);
};

}  // namespace media

#endif  // MEDIA_BASE_VIDEO_ENCODER_H_