OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #ifndef CONTENT_RENDERER_MEDIA_RECORDER_VPX_ENCODER_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_RECORDER_VPX_ENCODER_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "content/renderer/media_recorder/video_track_recorder.h" |
| 11 |
| 12 extern "C" { |
| 13 // VPX_CODEC_DISABLE_COMPAT excludes parts of the libvpx API that provide |
| 14 // backwards compatibility for legacy applications using the library. |
| 15 #define VPX_CODEC_DISABLE_COMPAT 1 |
| 16 #include "third_party/libvpx/source/libvpx/vpx/vp8cx.h" |
| 17 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h" |
| 18 } |
| 19 |
| 20 namespace content { |
| 21 |
| 22 // Class encapsulating all libvpx interactions for VP8/VP9 encoding. |
| 23 class VpxEncoder final : public VideoTrackRecorder::Encoder { |
| 24 public: |
| 25 // Originally from remoting/codec/scoped_vpx_codec.h. |
| 26 // TODO(mcasas): Refactor into a common location. |
| 27 struct VpxCodecDeleter { |
| 28 void operator()(vpx_codec_ctx_t* codec); |
| 29 }; |
| 30 typedef std::unique_ptr<vpx_codec_ctx_t, VpxCodecDeleter> |
| 31 ScopedVpxCodecCtxPtr; |
| 32 |
| 33 static void ShutdownEncoder(std::unique_ptr<base::Thread> encoding_thread, |
| 34 ScopedVpxCodecCtxPtr encoder); |
| 35 |
| 36 VpxEncoder( |
| 37 bool use_vp9, |
| 38 const VideoTrackRecorder::OnEncodedVideoCB& on_encoded_video_callback, |
| 39 int32_t bits_per_second); |
| 40 |
| 41 private: |
| 42 // VideoTrackRecorder::Encoder implementation. |
| 43 ~VpxEncoder() override; |
| 44 void EncodeOnEncodingTaskRunner(scoped_refptr<media::VideoFrame> frame, |
| 45 base::TimeTicks capture_timestamp) override; |
| 46 bool CanEncodeAlphaChannel() override; |
| 47 |
| 48 void ConfigureEncoderOnEncodingTaskRunner(const gfx::Size& size, |
| 49 vpx_codec_enc_cfg_t* codec_config, |
| 50 ScopedVpxCodecCtxPtr* encoder); |
| 51 void DoEncode(vpx_codec_ctx_t* const encoder, |
| 52 const gfx::Size& frame_size, |
| 53 uint8_t* const data, |
| 54 uint8_t* const y_plane, |
| 55 int y_stride, |
| 56 uint8_t* const u_plane, |
| 57 int u_stride, |
| 58 uint8_t* const v_plane, |
| 59 int v_stride, |
| 60 const base::TimeDelta& duration, |
| 61 bool force_keyframe, |
| 62 std::string* const output_data, |
| 63 bool* const keyframe); |
| 64 |
| 65 // Returns true if |codec_config| has been filled in at least once. |
| 66 bool IsInitialized(const vpx_codec_enc_cfg_t& codec_config) const; |
| 67 |
| 68 // Estimate the frame duration from |frame| and |last_frame_timestamp_|. |
| 69 base::TimeDelta EstimateFrameDuration( |
| 70 const scoped_refptr<media::VideoFrame>& frame); |
| 71 |
| 72 // Force usage of VP9 for encoding, instead of VP8 which is the default. |
| 73 const bool use_vp9_; |
| 74 |
| 75 // VPx internal objects: configuration and encoder. |encoder_| is a special |
| 76 // scoped pointer to guarantee proper destruction, particularly when |
| 77 // reconfiguring due to parameters change. Only used on |
| 78 // VideoTrackRecorder::Encoder::encoding_thread_. |
| 79 vpx_codec_enc_cfg_t codec_config_; |
| 80 ScopedVpxCodecCtxPtr encoder_; |
| 81 |
| 82 vpx_codec_enc_cfg_t alpha_codec_config_; |
| 83 ScopedVpxCodecCtxPtr alpha_encoder_; |
| 84 |
| 85 std::vector<uint8_t> alpha_dummy_planes_; |
| 86 size_t v_plane_offset_; |
| 87 size_t u_plane_stride_; |
| 88 size_t v_plane_stride_; |
| 89 bool last_frame_had_alpha_ = false; |
| 90 |
| 91 // The |media::VideoFrame::timestamp()| of the last encoded frame. This is |
| 92 // used to predict the duration of the next frame. Only used on |
| 93 // VideoTrackRecorder::Encoder::encoding_thread_. |
| 94 base::TimeDelta last_frame_timestamp_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(VpxEncoder); |
| 97 }; |
| 98 |
| 99 } // namespace content |
| 100 |
| 101 #endif // CONTENT_RENDERER_MEDIA_RECORDER_VPX_ENCODER_H_ |
OLD | NEW |