offloading_video_encoder.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_OFFLOADING_VIDEO_ENCODER_H_
  5. #define MEDIA_BASE_OFFLOADING_VIDEO_ENCODER_H_
  6. #include <memory>
  7. #include <type_traits>
  8. #include "base/sequence_checker.h"
  9. #include "media/base/video_encoder.h"
  10. namespace base {
  11. class SequencedTaskRunner;
  12. }
  13. namespace media {
  14. // A wrapper around video encoder that offloads all the calls to a dedicated
  15. // task runner. It's used to move synchronous software encoding work off the
  16. // current (main) thread.
  17. class MEDIA_EXPORT OffloadingVideoEncoder final : public VideoEncoder {
  18. public:
  19. // |work_runner| - task runner for encoding work
  20. // |callback_runner| - all encoder's callbacks will be executed on this task
  21. // runner.
  22. OffloadingVideoEncoder(
  23. std::unique_ptr<VideoEncoder> wrapped_encoder,
  24. const scoped_refptr<base::SequencedTaskRunner> work_runner,
  25. const scoped_refptr<base::SequencedTaskRunner> callback_runner);
  26. // Uses current task runner for callbacks and asks thread pool for a new task
  27. // runner to do actual encoding work.
  28. explicit OffloadingVideoEncoder(
  29. std::unique_ptr<VideoEncoder> wrapped_encoder);
  30. ~OffloadingVideoEncoder() override;
  31. void Initialize(VideoCodecProfile profile,
  32. const Options& options,
  33. OutputCB output_cb,
  34. EncoderStatusCB done_cb) override;
  35. void Encode(scoped_refptr<VideoFrame> frame,
  36. bool key_frame,
  37. EncoderStatusCB done_cb) override;
  38. void ChangeOptions(const Options& options,
  39. OutputCB output_cb,
  40. EncoderStatusCB done_cb) override;
  41. void Flush(EncoderStatusCB done_cb) override;
  42. private:
  43. template <class T>
  44. T WrapCallback(T cb);
  45. std::unique_ptr<VideoEncoder> wrapped_encoder_;
  46. const scoped_refptr<base::SequencedTaskRunner> work_runner_;
  47. const scoped_refptr<base::SequencedTaskRunner> callback_runner_;
  48. SEQUENCE_CHECKER(sequence_checker_);
  49. };
  50. } // namespace media
  51. #endif // MEDIA_BASE_OFFLOADING_VIDEO_ENCODER_H_