OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "content/common/gpu/client/gpu_channel_host.h" | |
9 #include "content/common/gpu/gpu_messages.h" | |
10 #include "content/common/gpu/media/gpu_video_encode_accelerator.h" | |
11 #include "media/base/video_frame.h" | |
12 | |
13 namespace content { | |
14 | |
15 GpuVideoEncodeAcceleratorHost::GpuVideoEncodeAcceleratorHost( | |
16 media::VideoEncodeAccelerator::Client* client, | |
17 const scoped_refptr<GpuChannelHost>& gpu_channel_host, | |
18 int32 route_id) | |
19 : client_(client), | |
20 channel_(gpu_channel_host), | |
21 route_id_(route_id), | |
22 next_frame_id_(0) { | |
23 channel_->AddRoute(route_id_, AsWeakPtr()); | |
24 } | |
25 | |
26 GpuVideoEncodeAcceleratorHost::~GpuVideoEncodeAcceleratorHost() { | |
27 if (channel_) | |
28 channel_->RemoveRoute(route_id_); | |
29 } | |
30 | |
31 // static | |
32 std::vector<media::VideoEncodeAccelerator::SupportedProfile> | |
33 GpuVideoEncodeAcceleratorHost::GetSupportedProfiles() { | |
34 return GpuVideoEncodeAccelerator::GetSupportedProfiles(); | |
35 } | |
36 | |
37 bool GpuVideoEncodeAcceleratorHost::OnMessageReceived( | |
38 const IPC::Message& message) { | |
39 bool handled = true; | |
40 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAcceleratorHost, message) | |
41 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInitializeDone, | |
42 OnNotifyInitializeDone) | |
43 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers, | |
44 OnRequireBitstreamBuffers) | |
45 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyEncodeDone, | |
46 OnNotifyEncodeDone) | |
47 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_BitstreamBufferReady, | |
48 OnBitstreamBufferReady) | |
49 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyError, | |
50 OnNotifyError) | |
51 IPC_MESSAGE_UNHANDLED(handled = false) | |
52 IPC_END_MESSAGE_MAP() | |
53 DCHECK(handled); | |
54 return handled; | |
55 } | |
56 | |
57 void GpuVideoEncodeAcceleratorHost::OnChannelError() { | |
58 DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::OnChannelError()"; | |
59 OnNotifyError(kPlatformFailureError); | |
60 if (channel_) { | |
61 channel_->RemoveRoute(route_id_); | |
62 channel_ = NULL; | |
63 } | |
64 } | |
65 | |
66 void GpuVideoEncodeAcceleratorHost::Initialize( | |
67 media::VideoFrame::Format input_format, | |
68 const gfx::Size& input_visible_size, | |
69 media::VideoCodecProfile output_profile, | |
70 int32 initial_bitrate) { | |
71 if (!channel_) | |
72 return; | |
73 Send(new AcceleratedVideoEncoderMsg_Initialize(route_id_, | |
74 input_format, | |
75 input_visible_size, | |
76 output_profile, | |
77 initial_bitrate)); | |
78 } | |
79 | |
80 void GpuVideoEncodeAcceleratorHost::Encode( | |
81 const scoped_refptr<media::VideoFrame>& frame, | |
82 bool force_keyframe) { | |
83 if (!channel_) | |
84 return; | |
85 base::SharedMemoryHandle handle = | |
86 channel_->ShareToGpuProcess(frame->shared_memory_handle()); | |
87 if (!base::SharedMemory::IsHandleValid(handle)) { | |
88 DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::Encode(): failed to " | |
89 "duplicate buffer handle for GPU process"; | |
Ami GONE FROM CHROMIUM
2013/08/05 18:44:38
This is the message that someone would get if they
sheu
2013/08/06 06:16:36
Good point.
| |
90 OnNotifyError(kPlatformFailureError); | |
91 return; | |
92 } | |
93 | |
94 // We assume that planar frame data passed here is packed and contiguous. | |
95 const size_t plane_count = media::VideoFrame::NumPlanes(frame->format()); | |
96 size_t frame_size = 0; | |
97 for (size_t i = 0; i < plane_count; ++i) { | |
98 DCHECK(frame->data(i) == (frame->data(0) + frame_size)); | |
99 frame_size += frame->stride(i) * frame->rows(i); | |
100 } | |
101 | |
102 Send(new AcceleratedVideoEncoderMsg_Encode( | |
103 route_id_, next_frame_id_, handle, frame_size, force_keyframe)); | |
104 frame_map_[next_frame_id_] = frame; | |
105 | |
106 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. | |
107 next_frame_id_ = (next_frame_id_ + 1) & 0x3FFFFFFF; | |
108 } | |
109 | |
110 void GpuVideoEncodeAcceleratorHost::UseOutputBitstreamBuffer( | |
111 const media::BitstreamBuffer& buffer) { | |
112 if (!channel_) | |
113 return; | |
114 base::SharedMemoryHandle handle = | |
115 channel_->ShareToGpuProcess(buffer.handle()); | |
116 if (!base::SharedMemory::IsHandleValid(handle)) { | |
117 DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::UseOutputBitstreamBuffer(): " | |
118 "failed to duplicate buffer handle for GPU process: " | |
119 "buffer.id()=" << buffer.id(); | |
120 OnNotifyError(kPlatformFailureError); | |
121 return; | |
122 } | |
123 Send(new AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer( | |
124 route_id_, buffer.id(), handle, buffer.size())); | |
125 } | |
126 | |
127 void GpuVideoEncodeAcceleratorHost::RequestEncodingParameterChange( | |
128 int32 bitrate) { | |
129 if (!channel_) | |
130 return; | |
131 Send(new AcceleratedVideoEncoderMsg_RequestEncodingParameterChange(route_id_, | |
132 bitrate)); | |
133 } | |
134 | |
135 void GpuVideoEncodeAcceleratorHost::Destroy() { | |
136 if (channel_) | |
137 Send(new GpuChannelMsg_DestroyVideoEncoder(route_id_)); | |
138 delete this; | |
139 } | |
140 | |
141 void GpuVideoEncodeAcceleratorHost::OnNotifyInitializeDone() { | |
142 DVLOG(2) << "GpuVideoEncodeAcceleratorHost::OnNotifyInitializeDone()"; | |
143 if (client_) | |
144 client_->NotifyInitializeDone(); | |
145 } | |
146 | |
147 void GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers( | |
148 int input_count, | |
149 const gfx::Size& input_coded_size, | |
150 uint32 output_buffer_size) { | |
151 DVLOG(2) << "GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers(): " | |
152 "input_count=" << input_count | |
153 << ", input_coded_size=" << input_coded_size.ToString() | |
154 << ", output_buffer_size=" << output_buffer_size; | |
155 if (client_) { | |
156 client_->RequireBitstreamBuffers( | |
157 input_count, input_coded_size, output_buffer_size); | |
158 } | |
159 } | |
160 | |
161 void GpuVideoEncodeAcceleratorHost::OnNotifyEncodeDone(int32 frame_id) { | |
162 DVLOG(3) << "GpuVideoEncodeAcceleratorHost::OnNotifyEncodeDone(): " | |
163 "frame_id=" << frame_id; | |
164 FrameMap::iterator iter = frame_map_.find(frame_id); | |
165 if (iter == frame_map_.end()) { | |
Ami GONE FROM CHROMIUM
2013/08/05 18:44:38
l.164, 165, and 171 can be collapsed into:
if (fra
sheu
2013/08/06 06:16:36
Done.
| |
166 DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::OnNotifyEncodeDone(): " | |
167 "invalid frame_id=" << frame_id; | |
168 OnNotifyError(kPlatformFailureError); | |
169 return; | |
170 } | |
171 frame_map_.erase(frame_id); | |
172 } | |
173 | |
174 void GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady( | |
175 int32 bitstream_buffer_id, | |
176 uint32 payload_size, | |
177 bool key_frame) { | |
178 DVLOG(3) << "GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady(): " | |
179 "bitstream_buffer_id=" << bitstream_buffer_id | |
180 << ", payload_size=" << payload_size | |
181 << ", key_frame=" << key_frame; | |
182 if (client_) | |
183 client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame); | |
184 } | |
185 | |
186 void GpuVideoEncodeAcceleratorHost::OnNotifyError(Error error) { | |
187 DVLOG(2) << "GpuVideoEncodeAcceleratorHost::OnNotifyError(): error=" << error; | |
188 if (client_) { | |
189 client_->NotifyError(error); | |
190 client_ = NULL; | |
191 } | |
192 } | |
193 | |
194 void GpuVideoEncodeAcceleratorHost::Send(IPC::Message* message) { | |
195 if (!channel_) { | |
196 DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::Send(): no channel"; | |
197 delete message; | |
198 return; | |
199 OnNotifyError(kPlatformFailureError); | |
200 } else if (!channel_->Send(message)) { | |
201 DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::Send(): sending failed: " | |
202 "message->type()=" << message->type(); | |
203 OnNotifyError(kPlatformFailureError); | |
204 return; | |
205 } | |
206 } | |
207 | |
208 } // namespace content | |
OLD | NEW |