Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / gpu / media / android_video_encode_accelerator.h
blobf44865e929f058fabac9345d51766ffa5944beff
1 // Copyright 2013 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 CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_
6 #define CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_
8 #include <list>
9 #include <queue>
10 #include <vector>
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/thread_checker.h"
14 #include "base/timer/timer.h"
15 #include "base/tuple.h"
16 #include "content/common/content_export.h"
17 #include "media/base/android/media_codec_bridge.h"
18 #include "media/video/video_encode_accelerator.h"
20 namespace media {
21 class BitstreamBuffer;
22 } // namespace media
24 namespace content {
26 // Android-specific implementation of media::VideoEncodeAccelerator, enabling
27 // hardware-acceleration of video encoding, based on Android's MediaCodec class
28 // (http://developer.android.com/reference/android/media/MediaCodec.html). This
29 // class expects to live and be called on a single thread (the GPU process'
30 // ChildThread).
31 class CONTENT_EXPORT AndroidVideoEncodeAccelerator
32 : public media::VideoEncodeAccelerator {
33 public:
34 AndroidVideoEncodeAccelerator();
35 ~AndroidVideoEncodeAccelerator() override;
37 // media::VideoEncodeAccelerator implementation.
38 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles()
39 override;
40 bool Initialize(media::VideoPixelFormat format,
41 const gfx::Size& input_visible_size,
42 media::VideoCodecProfile output_profile,
43 uint32 initial_bitrate,
44 Client* client) override;
45 void Encode(const scoped_refptr<media::VideoFrame>& frame,
46 bool force_keyframe) override;
47 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override;
48 void RequestEncodingParametersChange(uint32 bitrate,
49 uint32 framerate) override;
50 void Destroy() override;
52 private:
53 enum {
54 // Arbitrary choice.
55 INITIAL_FRAMERATE = 30,
56 // Until there are non-realtime users, no need for unrequested I-frames.
57 IFRAME_INTERVAL = kint32max,
60 // Impedance-mismatch fixers: MediaCodec is a poll-based API but VEA is a
61 // push-based API; these methods turn the crank to make the two work together.
62 void DoIOTask();
63 void QueueInput();
64 void DequeueOutput();
66 // Returns true if we don't need more or bigger output buffers.
67 bool DoOutputBuffersSuffice();
69 // Start & stop |io_timer_| if the time seems right.
70 void MaybeStartIOTimer();
71 void MaybeStopIOTimer();
73 // Used to DCHECK that we are called on the correct thread.
74 base::ThreadChecker thread_checker_;
76 // VideoDecodeAccelerator::Client callbacks go here. Invalidated once any
77 // error triggers.
78 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_;
80 scoped_ptr<media::VideoCodecBridge> media_codec_;
82 // Bitstream buffers waiting to be populated & returned to the client.
83 std::vector<media::BitstreamBuffer> available_bitstream_buffers_;
85 // Frames waiting to be passed to the codec, queued until an input buffer is
86 // available. Each element is a tuple of <Frame, key_frame, enqueue_time>.
87 typedef std::queue<
88 base::Tuple<scoped_refptr<media::VideoFrame>, bool, base::Time>>
89 PendingFrames;
90 PendingFrames pending_frames_;
92 // Repeating timer responsible for draining pending IO to the codec.
93 base::RepeatingTimer<AndroidVideoEncodeAccelerator> io_timer_;
95 // The difference between number of buffers queued & dequeued at the codec.
96 int32 num_buffers_at_codec_;
98 // A monotonically-growing value, used as a fake timestamp just to keep things
99 // appearing to move forward.
100 base::TimeDelta fake_input_timestamp_;
102 // Number of requested output buffers and their capacity.
103 int num_output_buffers_; // -1 until RequireBitstreamBuffers.
104 size_t output_buffers_capacity_; // 0 until RequireBitstreamBuffers.
106 uint32 last_set_bitrate_; // In bps.
108 DISALLOW_COPY_AND_ASSIGN(AndroidVideoEncodeAccelerator);
111 } // namespace content
113 #endif // CONTENT_COMMON_GPU_MEDIA_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_