Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cast / sender / vp8_encoder.h
blobfe2c54c80371726c0ddbf1330d2b483e99b3b4f1
1 // Copyright 2014 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.
5 #ifndef MEDIA_CAST_SENDER_CODECS_VP8_VP8_ENCODER_H_
6 #define MEDIA_CAST_SENDER_CODECS_VP8_VP8_ENCODER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "media/cast/cast_config.h"
12 #include "media/cast/sender/software_video_encoder.h"
13 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h"
14 #include "ui/gfx/geometry/size.h"
16 namespace media {
17 class VideoFrame;
20 namespace media {
21 namespace cast {
23 class Vp8Encoder : public SoftwareVideoEncoder {
24 public:
25 explicit Vp8Encoder(const VideoSenderConfig& video_config);
27 ~Vp8Encoder() final;
29 // SoftwareVideoEncoder implementations.
30 void Initialize() final;
31 void Encode(const scoped_refptr<media::VideoFrame>& video_frame,
32 const base::TimeTicks& reference_time,
33 SenderEncodedFrame* encoded_frame) final;
34 void UpdateRates(uint32 new_bitrate) final;
35 void GenerateKeyFrame() final;
36 void LatestFrameIdToReference(uint32 frame_id) final;
38 private:
39 enum { kNumberOfVp8VideoBuffers = 3 };
41 enum Vp8Buffers {
42 kAltRefBuffer = 0,
43 kGoldenBuffer = 1,
44 kLastBuffer = 2,
45 kNoBuffer = 3 // Note: must be last.
48 enum Vp8BufferState {
49 kBufferStartState,
50 kBufferSent,
51 kBufferAcked
54 struct BufferState {
55 uint32 frame_id;
56 Vp8BufferState state;
59 bool is_initialized() const {
60 // ConfigureForNewFrameSize() sets the timebase denominator value to
61 // non-zero if the encoder is successfully initialized, and it is zero
62 // otherwise.
63 return config_.g_timebase.den != 0;
66 // If the |encoder_| is live, attempt reconfiguration to allow it to encode
67 // frames at a new |frame_size|. Otherwise, tear it down and re-create a new
68 // |encoder_| instance.
69 void ConfigureForNewFrameSize(const gfx::Size& frame_size);
71 // Calculate which next Vp8 buffers to update with the next frame.
72 Vp8Buffers GetNextBufferToUpdate();
74 // Get encoder flags for our referenced encoder buffers.
75 // Return which previous frame to reference.
76 uint32 GetCodecReferenceFlags(vpx_codec_flags_t* flags);
78 // Get encoder flags for our encoder buffers to update with next frame.
79 void GetCodecUpdateFlags(Vp8Buffers buffer_to_update,
80 vpx_codec_flags_t* flags);
82 const VideoSenderConfig cast_config_;
83 const bool use_multiple_video_buffers_;
85 // VP8 internal objects. These are valid for use only while is_initialized()
86 // returns true.
87 vpx_codec_enc_cfg_t config_;
88 vpx_codec_ctx_t encoder_;
90 // Set to true to request the next frame emitted by Vp8Encoder be a key frame.
91 bool key_frame_requested_;
93 // Saves the current bitrate setting, for when the |encoder_| is reconfigured
94 // for different frame sizes.
95 int bitrate_kbit_;
97 // The |VideoFrame::timestamp()| of the last encoded frame. This is used to
98 // predict the duration of the next frame.
99 base::TimeDelta last_frame_timestamp_;
101 // The last encoded frame's ID.
102 uint32 last_encoded_frame_id_;
104 // Used to track which buffers are old enough to be re-used.
105 uint32 last_acked_frame_id_;
107 // Used by GetNextBufferToUpdate() to track how many consecutive times the
108 // newest buffer had to be overwritten.
109 int undroppable_frames_;
111 // Tracks the lifecycle and dependency state of each of the three buffers.
112 BufferState buffer_state_[kNumberOfVp8VideoBuffers];
114 // This is bound to the thread where Initialize() is called.
115 base::ThreadChecker thread_checker_;
117 DISALLOW_COPY_AND_ASSIGN(Vp8Encoder);
120 } // namespace cast
121 } // namespace media
123 #endif // MEDIA_CAST_SENDER_CODECS_VP8_VP8_ENCODER_H_