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

media / cast / encoding / video_encoder.h [blame]

// Copyright 2014 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_CAST_ENCODING_VIDEO_ENCODER_H_
#define MEDIA_CAST_ENCODING_VIDEO_ENCODER_H_

#include <memory>

#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "media/base/video_frame.h"
#include "media/cast/cast_callbacks.h"
#include "media/cast/cast_config.h"
#include "media/cast/cast_environment.h"

namespace media {

class VideoEncoderMetricsProvider;

namespace cast {

struct SenderEncodedFrame;

// This interface encapsulates logic for encoding video frames, and abstracts
// out complexities related to specific encoder implementations.
//
// NOTE: All public methods must be called from the MAIN CastEnvironment thread.
class VideoEncoder {
 public:
  // Callback used to deliver an encoded frame on the MAIN thread.
  using FrameEncodedCallback =
      base::RepeatingCallback<void(std::unique_ptr<SenderEncodedFrame>)>;

  // Creates a VideoEncoder instance from the given `video_config` and based on
  // the current platform's hardware/library support; or null if no
  // implementation will suffice.  The instance will run `status_change_cb` at
  // some point in the future to indicate initialization success/failure.
  //
  // All VideoEncoder instances returned by this function support encoding
  // sequences of differently-size VideoFrames.
  static std::unique_ptr<VideoEncoder> Create(
      const scoped_refptr<CastEnvironment>& cast_environment,
      const FrameSenderConfig& video_config,
      std::unique_ptr<VideoEncoderMetricsProvider> metrics_provider,
      StatusChangeCallback status_change_cb,
      FrameEncodedCallback output_cb,
      const CreateVideoEncodeAcceleratorCallback& create_vea_cb);

  virtual ~VideoEncoder() {}

  // If true is returned, the Encoder has accepted the request and will process
  // it asynchronously, running `output_cb_` on the MAIN CastEnvironment thread
  // with the result. If false is returned, the video frame has been dropped and
  // the `output_cb_` will not be called. This may be due to the encoder being
  // overloaded, in which case the calling class can just try again. More
  // serious errors will be reported through the `status_change_cb_`.
  [[nodiscard]] virtual bool EncodeVideoFrame(
      scoped_refptr<media::VideoFrame> video_frame,
      base::TimeTicks reference_time) = 0;

  // Inform the encoder about the new target bit rate.
  virtual void SetBitRate(int new_bit_rate) = 0;

  // Inform the encoder to encode the next frame as a key frame.
  virtual void GenerateKeyFrame() = 0;
};

}  // namespace cast
}  // namespace media

#endif  // MEDIA_CAST_ENCODING_VIDEO_ENCODER_H_