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_IPC_SERVICE_GPU_VIDEO_ENCODE_ACCELERATOR_H_ | 5 #ifndef MEDIA_GPU_IPC_SERVICE_GPU_VIDEO_ENCODE_ACCELERATOR_H_ |
6 #define MEDIA_GPU_IPC_SERVICE_GPU_VIDEO_ENCODE_ACCELERATOR_H_ | 6 #define MEDIA_GPU_IPC_SERVICE_GPU_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 <memory> | 11 #include <memory> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/synchronization/waitable_event.h" | |
17 #include "base/threading/thread.h" | |
16 #include "gpu/config/gpu_info.h" | 18 #include "gpu/config/gpu_info.h" |
17 #include "gpu/ipc/service/gpu_command_buffer_stub.h" | 19 #include "gpu/ipc/service/gpu_command_buffer_stub.h" |
18 #include "ipc/ipc_listener.h" | 20 #include "ipc/ipc_listener.h" |
19 #include "media/video/video_encode_accelerator.h" | 21 #include "media/video/video_encode_accelerator.h" |
20 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
21 | 23 |
22 struct AcceleratedVideoEncoderMsg_Encode_Params; | 24 struct AcceleratedVideoEncoderMsg_Encode_Params; |
23 | 25 |
24 namespace base { | 26 namespace base { |
25 class SharedMemory; | 27 class SharedMemory; |
26 } // namespace base | 28 } // namespace base |
27 | 29 |
28 namespace gpu { | 30 namespace gpu { |
29 struct GpuPreferences; | 31 struct GpuPreferences; |
30 } // namespace gpu | 32 } // namespace gpu |
31 | 33 |
32 namespace media { | 34 namespace media { |
33 | 35 |
34 // This class encapsulates the GPU process view of a VideoEncodeAccelerator, | 36 // This class encapsulates the GPU process view of a VideoEncodeAccelerator, |
35 // wrapping the platform-specific VideoEncodeAccelerator instance. It handles | 37 // wrapping the platform-specific VideoEncodeAccelerator instance. It handles |
36 // IPC coming in from the renderer and passes it to the underlying VEA. | 38 // IPC coming in from the renderer and passes it to the underlying VEA. |
37 class GpuVideoEncodeAccelerator | 39 class GpuVideoEncodeAccelerator |
38 : public IPC::Listener, | 40 : public IPC::Listener, |
41 public IPC::Sender, | |
39 public VideoEncodeAccelerator::Client, | 42 public VideoEncodeAccelerator::Client, |
40 public gpu::GpuCommandBufferStub::DestructionObserver { | 43 public gpu::GpuCommandBufferStub::DestructionObserver { |
41 public: | 44 public: |
42 GpuVideoEncodeAccelerator(int32_t host_route_id, | 45 GpuVideoEncodeAccelerator( |
43 gpu::GpuCommandBufferStub* stub); | 46 int32_t host_route_id, |
47 gpu::GpuCommandBufferStub* stub, | |
48 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner); | |
44 ~GpuVideoEncodeAccelerator() override; | 49 ~GpuVideoEncodeAccelerator() override; |
45 | 50 |
46 // Initialize this accelerator with the given parameters and send | 51 // Initialize this accelerator with the given parameters and send |
47 // |init_done_msg| when complete. | 52 // |init_done_msg| when complete. |
48 bool Initialize(VideoPixelFormat input_format, | 53 bool Initialize(VideoPixelFormat input_format, |
49 const gfx::Size& input_visible_size, | 54 const gfx::Size& input_visible_size, |
50 VideoCodecProfile output_profile, | 55 VideoCodecProfile output_profile, |
51 uint32_t initial_bitrate); | 56 uint32_t initial_bitrate); |
52 | 57 |
53 // IPC::Listener implementation | 58 // IPC::Listener implementation |
54 bool OnMessageReceived(const IPC::Message& message) override; | 59 bool OnMessageReceived(const IPC::Message& message) override; |
55 | 60 |
61 // IPC::Sender implementation | |
62 bool Send(IPC::Message* message) override; | |
63 | |
56 // VideoEncodeAccelerator::Client implementation. | 64 // VideoEncodeAccelerator::Client implementation. |
57 void RequireBitstreamBuffers(unsigned int input_count, | 65 void RequireBitstreamBuffers(unsigned int input_count, |
58 const gfx::Size& input_coded_size, | 66 const gfx::Size& input_coded_size, |
59 size_t output_buffer_size) override; | 67 size_t output_buffer_size) override; |
60 void BitstreamBufferReady(int32_t bitstream_buffer_id, | 68 void BitstreamBufferReady(int32_t bitstream_buffer_id, |
61 size_t payload_size, | 69 size_t payload_size, |
62 bool key_frame, | 70 bool key_frame, |
63 base::TimeDelta timestamp) override; | 71 base::TimeDelta timestamp) override; |
64 void NotifyError(VideoEncodeAccelerator::Error error) override; | 72 void NotifyError(VideoEncodeAccelerator::Error error) override; |
65 | 73 |
66 // gpu::GpuCommandBufferStub::DestructionObserver implementation. | 74 // gpu::GpuCommandBufferStub::DestructionObserver implementation. |
67 void OnWillDestroyStub() override; | 75 void OnWillDestroyStub() override; |
68 | 76 |
69 // Static query for supported profiles. This query calls the appropriate | 77 // Static query for supported profiles. This query calls the appropriate |
70 // platform-specific version. The returned supported profiles vector will | 78 // platform-specific version. The returned supported profiles vector will |
71 // not contain duplicates. | 79 // not contain duplicates. |
72 static gpu::VideoEncodeAcceleratorSupportedProfiles GetSupportedProfiles( | 80 static gpu::VideoEncodeAcceleratorSupportedProfiles GetSupportedProfiles( |
73 const gpu::GpuPreferences& gpu_preferences); | 81 const gpu::GpuPreferences& gpu_preferences); |
74 | 82 |
75 private: | 83 private: |
76 // Returns a vector of VEAFactoryFunctions for the current platform. | 84 // Returns a vector of VEAFactoryFunctions for the current platform. |
77 using VEAFactoryFunction = | 85 using VEAFactoryFunction = |
78 base::Callback<std::unique_ptr<VideoEncodeAccelerator>()>; | 86 base::Callback<std::unique_ptr<VideoEncodeAccelerator>()>; |
79 static std::vector<VEAFactoryFunction> GetVEAFactoryFunctions( | 87 static std::vector<VEAFactoryFunction> GetVEAFactoryFunctions( |
80 const gpu::GpuPreferences& gpu_preferences); | 88 const gpu::GpuPreferences& gpu_preferences); |
81 | 89 |
90 class MessageFilter; | |
91 | |
92 // Called on IO thread when |filter_| has been removed. | |
93 void OnFilterRemoved(); | |
94 | |
82 // IPC handlers, proxying VideoEncodeAccelerator for the renderer | 95 // IPC handlers, proxying VideoEncodeAccelerator for the renderer |
83 // process. | 96 // process. |
84 void OnEncode(const AcceleratedVideoEncoderMsg_Encode_Params& params); | 97 void OnEncode(const AcceleratedVideoEncoderMsg_Encode_Params& params); |
85 void OnUseOutputBitstreamBuffer(int32_t buffer_id, | 98 void OnUseOutputBitstreamBuffer(int32_t buffer_id, |
86 base::SharedMemoryHandle buffer_handle, | 99 base::SharedMemoryHandle buffer_handle, |
87 uint32_t buffer_size); | 100 uint32_t buffer_size); |
88 void OnRequestEncodingParametersChange(uint32_t bitrate, uint32_t framerate); | 101 void OnRequestEncodingParametersChange(uint32_t bitrate, uint32_t framerate); |
89 | 102 |
90 void OnDestroy(); | 103 void OnDestroy(); |
91 | 104 |
92 void EncodeFrameFinished(int32_t frame_id, | 105 // Operations that run on encoder worker thread. |
93 std::unique_ptr<base::SharedMemory> shm); | 106 void CreateEncodeFrameOnEncoderWorker( |
94 void Send(IPC::Message* message); | 107 const AcceleratedVideoEncoderMsg_Encode_Params& params); |
108 void DestroyOnEncoderWorker(); | |
109 | |
110 // Completes encode tasks with the received |frame|. | |
111 void OnEncodeFrameCreated(int32_t frame_id, | |
112 bool force_keyframe, | |
113 const scoped_refptr<media::VideoFrame>& frame); | |
114 | |
115 // Notifies renderer that input is completed. | |
Pawel Osciak
2016/11/09 01:03:02
Nit: perhaps we could rephrase "input is completed
emircan
2016/11/09 01:20:23
Done.
| |
116 void EncodeFrameFinished(int32_t frame_id); | |
117 | |
118 // Checks that function is called on the correct thread. If MessageFilter is | |
119 // used, checks if it is called on |io_task_runner_|. If not, checks if it is | |
120 // called on |main_task_runner_|. | |
121 bool CheckIfCalledOnCorrectThread(); | |
95 | 122 |
96 // Route ID to communicate with the host. | 123 // Route ID to communicate with the host. |
97 const uint32_t host_route_id_; | 124 const uint32_t host_route_id_; |
98 | 125 |
99 // Unowned pointer to the underlying gpu::GpuCommandBufferStub. |this| is | 126 // Unowned pointer to the underlying gpu::GpuCommandBufferStub. |this| is |
100 // registered as a DestuctionObserver of |stub_| and will self-delete when | 127 // registered as a DestuctionObserver of |stub_| and will self-delete when |
101 // |stub_| is destroyed. | 128 // |stub_| is destroyed. |
102 gpu::GpuCommandBufferStub* const stub_; | 129 gpu::GpuCommandBufferStub* const stub_; |
103 | 130 |
104 // Owned pointer to the underlying VideoEncodeAccelerator. | 131 // Owned pointer to the underlying VideoEncodeAccelerator. |
105 std::unique_ptr<VideoEncodeAccelerator> encoder_; | 132 std::unique_ptr<VideoEncodeAccelerator> encoder_; |
106 base::Callback<bool(void)> make_context_current_; | 133 base::Callback<bool(void)> make_context_current_; |
107 | 134 |
108 // Video encoding parameters. | 135 // Video encoding parameters. |
109 VideoPixelFormat input_format_; | 136 VideoPixelFormat input_format_; |
110 gfx::Size input_visible_size_; | 137 gfx::Size input_visible_size_; |
111 gfx::Size input_coded_size_; | 138 gfx::Size input_coded_size_; |
112 size_t output_buffer_size_; | 139 size_t output_buffer_size_; |
113 | 140 |
114 // Weak pointer for VideoFrames that refer back to |this|. | 141 // The message filter to run VEA encode methods on IO thread if VEA supports |
142 // it. | |
143 scoped_refptr<MessageFilter> filter_; | |
144 | |
145 // Used to wait on for |filter_| to be removed, before we can safely | |
146 // destroy the VEA. | |
147 base::WaitableEvent filter_removed_; | |
148 | |
149 // This thread services the operations necessary for encode so that they | |
150 // wouldn't block |main_task_runner_| or |io_task_runner_|. | |
151 base::Thread encoder_worker_thread_; | |
152 scoped_refptr<base::SingleThreadTaskRunner> encoder_worker_task_runner_; | |
153 | |
154 // GPU main thread task runner. | |
155 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
156 | |
157 // GPU IO thread task runner. | |
158 const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
159 | |
160 // Task runner used for posting encode tasks. If | |
161 // TryToSetupEncodeOnSeperateThread() is true, |io_task_runner_| is used, | |
162 // otherwise |main_thread_task_runner_|. | |
163 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; | |
164 | |
165 // Weak pointer for referring back to |this| on |encoder_worker_task_runner_|. | |
166 base::WeakPtrFactory<GpuVideoEncodeAccelerator> | |
167 weak_this_factory_for_encoder_worker_; | |
168 | |
169 // Weak pointer for VideoFrames that refer back to |this| on | |
170 // |main_task_runner| or |io_task_runner_|. | |
115 base::WeakPtrFactory<GpuVideoEncodeAccelerator> weak_this_factory_; | 171 base::WeakPtrFactory<GpuVideoEncodeAccelerator> weak_this_factory_; |
116 | 172 |
117 DISALLOW_COPY_AND_ASSIGN(GpuVideoEncodeAccelerator); | 173 DISALLOW_COPY_AND_ASSIGN(GpuVideoEncodeAccelerator); |
118 }; | 174 }; |
119 | 175 |
120 } // namespace media | 176 } // namespace media |
121 | 177 |
122 #endif // MEDIA_GPU_IPC_SERVICE_GPU_VIDEO_ENCODE_ACCELERATOR_H_ | 178 #endif // MEDIA_GPU_IPC_SERVICE_GPU_VIDEO_ENCODE_ACCELERATOR_H_ |
OLD | NEW |