OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_GPU_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_ | 5 #ifndef MEDIA_GPU_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_ |
6 #define MEDIA_GPU_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_ | 6 #define MEDIA_GPU_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <list> | 11 #include <list> |
12 #include <memory> | 12 #include <memory> |
13 #include <queue> | 13 #include <queue> |
14 #include <tuple> | 14 #include <tuple> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
20 #include "base/timer/timer.h" | 20 #include "base/timer/timer.h" |
21 #include "media/base/android/sdk_media_codec_bridge.h" | 21 #include "media/base/android/sdk_media_codec_bridge.h" |
22 #include "media/gpu/media_gpu_export.h" | 22 #include "media/gpu/media_gpu_export.h" |
23 #include "media/video/video_encode_accelerator.h" | 23 #include "media/video/video_encode_accelerator.h" |
24 | 24 |
25 namespace media { | 25 namespace media { |
| 26 |
26 class BitstreamBuffer; | 27 class BitstreamBuffer; |
27 } // namespace media | |
28 | 28 |
29 namespace media { | 29 // Android-specific implementation of VideoEncodeAccelerator, enabling |
30 | |
31 // Android-specific implementation of media::VideoEncodeAccelerator, enabling | |
32 // hardware-acceleration of video encoding, based on Android's MediaCodec class | 30 // hardware-acceleration of video encoding, based on Android's MediaCodec class |
33 // (http://developer.android.com/reference/android/media/MediaCodec.html). This | 31 // (http://developer.android.com/reference/android/media/MediaCodec.html). This |
34 // class expects to live and be called on a single thread (the GPU process' | 32 // class expects to live and be called on a single thread (the GPU process' |
35 // ChildThread). | 33 // ChildThread). |
36 class MEDIA_GPU_EXPORT AndroidVideoEncodeAccelerator | 34 class MEDIA_GPU_EXPORT AndroidVideoEncodeAccelerator |
37 : public media::VideoEncodeAccelerator { | 35 : public VideoEncodeAccelerator { |
38 public: | 36 public: |
39 AndroidVideoEncodeAccelerator(); | 37 AndroidVideoEncodeAccelerator(); |
40 ~AndroidVideoEncodeAccelerator() override; | 38 ~AndroidVideoEncodeAccelerator() override; |
41 | 39 |
42 // media::VideoEncodeAccelerator implementation. | 40 // VideoEncodeAccelerator implementation. |
43 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() | 41 VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() override; |
44 override; | 42 bool Initialize(VideoPixelFormat format, |
45 bool Initialize(media::VideoPixelFormat format, | |
46 const gfx::Size& input_visible_size, | 43 const gfx::Size& input_visible_size, |
47 media::VideoCodecProfile output_profile, | 44 VideoCodecProfile output_profile, |
48 uint32_t initial_bitrate, | 45 uint32_t initial_bitrate, |
49 Client* client) override; | 46 Client* client) override; |
50 void Encode(const scoped_refptr<media::VideoFrame>& frame, | 47 void Encode(const scoped_refptr<VideoFrame>& frame, |
51 bool force_keyframe) override; | 48 bool force_keyframe) override; |
52 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; | 49 void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override; |
53 void RequestEncodingParametersChange(uint32_t bitrate, | 50 void RequestEncodingParametersChange(uint32_t bitrate, |
54 uint32_t framerate) override; | 51 uint32_t framerate) override; |
55 void Destroy() override; | 52 void Destroy() override; |
56 | 53 |
57 private: | 54 private: |
58 enum { | 55 enum { |
59 // Arbitrary choice. | 56 // Arbitrary choice. |
60 INITIAL_FRAMERATE = 30, | 57 INITIAL_FRAMERATE = 30, |
61 // Until there are non-realtime users, no need for unrequested I-frames. | 58 // Until there are non-realtime users, no need for unrequested I-frames. |
62 IFRAME_INTERVAL = INT32_MAX, | 59 IFRAME_INTERVAL = INT32_MAX, |
63 }; | 60 }; |
64 | 61 |
65 // Impedance-mismatch fixers: MediaCodec is a poll-based API but VEA is a | 62 // Impedance-mismatch fixers: MediaCodec is a poll-based API but VEA is a |
66 // push-based API; these methods turn the crank to make the two work together. | 63 // push-based API; these methods turn the crank to make the two work together. |
67 void DoIOTask(); | 64 void DoIOTask(); |
68 void QueueInput(); | 65 void QueueInput(); |
69 void DequeueOutput(); | 66 void DequeueOutput(); |
70 | 67 |
71 // Start & stop |io_timer_| if the time seems right. | 68 // Start & stop |io_timer_| if the time seems right. |
72 void MaybeStartIOTimer(); | 69 void MaybeStartIOTimer(); |
73 void MaybeStopIOTimer(); | 70 void MaybeStopIOTimer(); |
74 | 71 |
75 // Used to DCHECK that we are called on the correct thread. | 72 // Used to DCHECK that we are called on the correct thread. |
76 base::ThreadChecker thread_checker_; | 73 base::ThreadChecker thread_checker_; |
77 | 74 |
78 // VideoDecodeAccelerator::Client callbacks go here. Invalidated once any | 75 // VideoDecodeAccelerator::Client callbacks go here. Invalidated once any |
79 // error triggers. | 76 // error triggers. |
80 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; | 77 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; |
81 | 78 |
82 std::unique_ptr<media::VideoCodecBridge> media_codec_; | 79 std::unique_ptr<VideoCodecBridge> media_codec_; |
83 | 80 |
84 // Bitstream buffers waiting to be populated & returned to the client. | 81 // Bitstream buffers waiting to be populated & returned to the client. |
85 std::vector<media::BitstreamBuffer> available_bitstream_buffers_; | 82 std::vector<BitstreamBuffer> available_bitstream_buffers_; |
86 | 83 |
87 // Frames waiting to be passed to the codec, queued until an input buffer is | 84 // Frames waiting to be passed to the codec, queued until an input buffer is |
88 // available. Each element is a tuple of <Frame, key_frame, enqueue_time>. | 85 // available. Each element is a tuple of <Frame, key_frame, enqueue_time>. |
89 typedef std::queue< | 86 typedef std::queue<std::tuple<scoped_refptr<VideoFrame>, bool, base::Time>> |
90 std::tuple<scoped_refptr<media::VideoFrame>, bool, base::Time>> | |
91 PendingFrames; | 87 PendingFrames; |
92 PendingFrames pending_frames_; | 88 PendingFrames pending_frames_; |
93 | 89 |
94 // Repeating timer responsible for draining pending IO to the codec. | 90 // Repeating timer responsible for draining pending IO to the codec. |
95 base::RepeatingTimer io_timer_; | 91 base::RepeatingTimer io_timer_; |
96 | 92 |
97 // The difference between number of buffers queued & dequeued at the codec. | 93 // The difference between number of buffers queued & dequeued at the codec. |
98 int32_t num_buffers_at_codec_; | 94 int32_t num_buffers_at_codec_; |
99 | 95 |
100 // A monotonically-growing value, used as a fake timestamp just to keep things | 96 // A monotonically-growing value, used as a fake timestamp just to keep things |
101 // appearing to move forward. | 97 // appearing to move forward. |
102 base::TimeDelta fake_input_timestamp_; | 98 base::TimeDelta fake_input_timestamp_; |
103 | 99 |
104 // Resolution of input stream. Set once in initialization and not allowed to | 100 // Resolution of input stream. Set once in initialization and not allowed to |
105 // change after. | 101 // change after. |
106 gfx::Size frame_size_; | 102 gfx::Size frame_size_; |
107 | 103 |
108 uint32_t last_set_bitrate_; // In bps. | 104 uint32_t last_set_bitrate_; // In bps. |
109 | 105 |
110 DISALLOW_COPY_AND_ASSIGN(AndroidVideoEncodeAccelerator); | 106 DISALLOW_COPY_AND_ASSIGN(AndroidVideoEncodeAccelerator); |
111 }; | 107 }; |
112 | 108 |
113 } // namespace media | 109 } // namespace media |
114 | 110 |
115 #endif // MEDIA_GPU_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_ | 111 #endif // MEDIA_GPU_ANDROID_VIDEO_ENCODE_ACCELERATOR_H_ |
OLD | NEW |