OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_ | |
6 #define MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_ | |
7 | |
8 #include <mfapi.h> | |
9 #include <mfidl.h> | |
10 #include <stdint.h> | |
11 #include <strmif.h> | |
12 | |
13 #include <deque> | |
14 #include <memory> | |
15 | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/sequenced_task_runner.h" | |
18 #include "base/single_thread_task_runner.h" | |
19 #include "base/threading/thread_checker.h" | |
20 #include "base/win/scoped_comptr.h" | |
21 #include "media/gpu/media_gpu_export.h" | |
22 #include "media/video/video_encode_accelerator.h" | |
23 | |
24 namespace media { | |
25 | |
26 // Media Foundation implementation of the VideoEncodeAccelerator interface for | |
27 // Windows. | |
28 // This class saves the task runner on which it is constructed and returns | |
29 // encoded data to the client using that same task runner. This class has | |
30 // DCHECKs to makes sure that methods are called in sequence. It starts an | |
31 // internal encoder thread on which VideoEncodeAccelerator implementation tasks | |
32 // are posted. | |
33 class MEDIA_GPU_EXPORT MediaFoundationVideoEncodeAccelerator | |
34 : public VideoEncodeAccelerator { | |
35 public: | |
36 MediaFoundationVideoEncodeAccelerator(); | |
37 ~MediaFoundationVideoEncodeAccelerator() override; | |
grt (UTC plus 2)
2016/07/18 08:35:04
should this be private? the contract seems to be t
emircan
2016/07/18 20:45:22
I will make it protected as the interface suggests
| |
38 | |
39 // VideoEncodeAccelerator implementation. | |
40 VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() override; | |
41 bool Initialize(VideoPixelFormat input_format, | |
42 const gfx::Size& input_visible_size, | |
43 VideoCodecProfile output_profile, | |
44 uint32_t initial_bitrate, | |
45 Client* client) override; | |
46 void Encode(const scoped_refptr<VideoFrame>& frame, | |
47 bool force_keyframe) override; | |
48 void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override; | |
49 void RequestEncodingParametersChange(uint32_t bitrate, | |
50 uint32_t framerate) override; | |
51 void Destroy() override; | |
52 | |
53 // Preload dlls required for encoding. | |
54 static void PreSandboxInitialization(); | |
55 | |
56 private: | |
57 // Holds output buffers coming from the client ready to be filled. | |
58 struct BitstreamBufferRef; | |
59 | |
60 // Holds output buffers coming from the encoder. | |
61 class EncodeOutput; | |
62 | |
63 // Initializes and allocates memory for input and output samples. | |
64 bool InitializeInputOutputSamples(); | |
65 | |
66 // Initializes encoder parameters for real-time use. | |
67 bool SetEncoderModes(); | |
68 | |
69 // Encoding tasks to be run on |encoder_thread_|. | |
70 void EncodeTask(const scoped_refptr<VideoFrame>& frame, bool force_keyframe); | |
71 | |
72 // Checks for and copies encoded output on |encoder_thread_|. | |
73 void ProcessOutput(); | |
74 | |
75 // Inserts the output buffers for reuse on |encoder_thread_|. | |
76 void UseOutputBitstreamBufferTask( | |
77 std::unique_ptr<BitstreamBufferRef> buffer_ref); | |
78 | |
79 // Copies EncodeOutput into a BitstreamBuffer and returns it to the |client_|. | |
80 void ReturnBitstreamBuffer( | |
81 std::unique_ptr<EncodeOutput> encode_output, | |
82 std::unique_ptr<MediaFoundationVideoEncodeAccelerator::BitstreamBufferRef> | |
83 buffer_ref); | |
84 | |
85 // Changes encode parameters on |encoder_thread_|. | |
86 void RequestEncodingParametersChangeTask(uint32_t bitrate, | |
87 uint32_t framerate); | |
88 | |
89 // Destroys encode session on |encoder_thread_|. | |
90 void DestroyTask(); | |
91 | |
92 // Bitstream buffers ready to be used to return encoded output as a FIFO. | |
93 std::deque<std::unique_ptr<BitstreamBufferRef>> bitstream_buffer_queue_; | |
94 | |
95 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO. | |
96 std::deque<std::unique_ptr<EncodeOutput>> encoder_output_queue_; | |
97 | |
98 gfx::Size input_visible_size_; | |
99 size_t bitstream_buffer_size_; | |
100 int32_t frame_rate_; | |
101 int32_t target_bitrate_; | |
102 size_t u_plane_offset_; | |
103 size_t v_plane_offset_; | |
104 | |
105 base::win::ScopedComPtr<IMFTransform> encoder_; | |
106 base::win::ScopedComPtr<ICodecAPI> codec_api_; | |
107 | |
108 DWORD input_stream_count_min_; | |
109 DWORD input_stream_count_max_; | |
110 DWORD output_stream_count_min_; | |
111 DWORD output_stream_count_max_; | |
112 | |
113 base::win::ScopedComPtr<IMFSample> input_sample_; | |
114 base::win::ScopedComPtr<IMFSample> output_sample_; | |
115 | |
116 // To expose client callbacks from VideoEncodeAccelerator. | |
117 // NOTE: all calls to this object *MUST* be executed on |client_task_runner_|. | |
118 base::WeakPtr<Client> client_; | |
119 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; | |
120 | |
121 // Our original calling task runner for the child thread. | |
122 const scoped_refptr<base::SequencedTaskRunner> client_task_runner_; | |
123 // Sequence checker to enforce that the methods of this object are called in | |
124 // sequence. | |
125 base::SequenceChecker sequence_checker_; | |
126 | |
127 // This thread services tasks posted from the VEA API entry points by the | |
128 // GPU child thread and CompressionCallback() posted from device thread. | |
129 base::Thread encoder_thread_; | |
130 scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_; | |
131 | |
132 // Declared last to ensure that all weak pointers are invalidated before | |
133 // other destructors run. | |
134 base::WeakPtrFactory<MediaFoundationVideoEncodeAccelerator> | |
135 encoder_task_weak_factory_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(MediaFoundationVideoEncodeAccelerator); | |
138 }; | |
139 | |
140 } // namespace media | |
141 | |
142 #endif // MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_ | |
OLD | NEW |