OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 <math.h> | |
6 #include <stdio.h> | |
7 #include <string.h> | |
8 | |
9 #include <iostream> | |
10 #include <sstream> | |
11 #include <vector> | |
12 | |
13 #include "ppapi/c/pp_errors.h" | |
14 #include "ppapi/c/ppb_console.h" | |
15 #include "ppapi/cpp/input_event.h" | |
16 #include "ppapi/cpp/instance.h" | |
17 #include "ppapi/cpp/media_stream_video_track.h" | |
18 #include "ppapi/cpp/module.h" | |
19 #include "ppapi/cpp/rect.h" | |
20 #include "ppapi/cpp/var.h" | |
21 #include "ppapi/cpp/var_array_buffer.h" | |
22 #include "ppapi/cpp/var_dictionary.h" | |
23 #include "ppapi/cpp/video_encoder.h" | |
24 #include "ppapi/cpp/video_frame.h" | |
25 #include "ppapi/utility/completion_callback_factory.h" | |
26 | |
27 // TODO(llandwerlin): turn on by default when we have software encode. | |
28 // #define USE_VP8_INSTEAD_OF_H264 | |
29 | |
30 // Use assert as a poor-man's CHECK, even in non-debug mode. | |
31 // Since <assert.h> redefines assert on every inclusion (it doesn't use | |
32 // include-guards), make sure this is the last file #include'd in this file. | |
33 #undef NDEBUG | |
34 #include <assert.h> | |
35 | |
36 namespace { | |
37 | |
38 std::string VideoProfileToString(PP_VideoProfile profile) { | |
39 switch (profile) { | |
40 case PP_VIDEOPROFILE_H264BASELINE: | |
41 return "h264baseline"; | |
42 case PP_VIDEOPROFILE_H264MAIN: | |
43 return "h264main"; | |
44 case PP_VIDEOPROFILE_H264EXTENDED: | |
45 return "h264extended"; | |
46 case PP_VIDEOPROFILE_H264HIGH: | |
47 return "h264high"; | |
48 case PP_VIDEOPROFILE_H264HIGH10PROFILE: | |
49 return "h264high10"; | |
50 case PP_VIDEOPROFILE_H264HIGH422PROFILE: | |
51 return "h264high422"; | |
52 case PP_VIDEOPROFILE_H264HIGH444PREDICTIVEPROFILE: | |
53 return "h264high444predictive"; | |
54 case PP_VIDEOPROFILE_H264SCALABLEBASELINE: | |
55 return "h264scalablebaseline"; | |
56 case PP_VIDEOPROFILE_H264SCALABLEHIGH: | |
57 return "h264scalablehigh"; | |
58 case PP_VIDEOPROFILE_H264STEREOHIGH: | |
59 return "h264stereohigh"; | |
60 case PP_VIDEOPROFILE_H264MULTIVIEWHIGH: | |
61 return "h264multiviewhigh"; | |
62 case PP_VIDEOPROFILE_VP8_ANY: | |
63 return "vp8"; | |
64 case PP_VIDEOPROFILE_VP9_ANY: | |
65 return "vp9"; | |
66 // No default to catch unhandled profiles. | |
67 } | |
68 return "unknown"; | |
69 } | |
70 | |
71 std::string HardwareAccelerationToString(PP_HardwareAcceleration acceleration) { | |
72 switch (acceleration) { | |
73 case PP_HARDWAREACCELERATION_ONLY: | |
74 return "hardware"; | |
75 case PP_HARDWAREACCELERATION_WITHFALLBACK: | |
76 return "hardware/software"; | |
77 case PP_HARDWAREACCELERATION_NONE: | |
78 return "software"; | |
79 // No default to catch unhandled accelerations. | |
80 } | |
81 return "unknown"; | |
82 } | |
83 | |
84 // This object is the global object representing this plugin library as long | |
85 // as it is loaded. | |
86 class MediaStreamVideoEncoderModule : public pp::Module { | |
bbudge
2015/03/02 17:52:03
VideoEncoderModule/Instance are better names, sinc
llandwerlin-old
2015/03/02 18:21:40
Done.
| |
87 public: | |
88 MediaStreamVideoEncoderModule() : pp::Module() {} | |
89 virtual ~MediaStreamVideoEncoderModule() {} | |
90 | |
91 virtual pp::Instance* CreateInstance(PP_Instance instance); | |
92 }; | |
93 | |
94 class MediaStreamVideoEncoderInstance : public pp::Instance { | |
95 public: | |
96 MediaStreamVideoEncoderInstance(PP_Instance instance, pp::Module* module); | |
97 virtual ~MediaStreamVideoEncoderInstance(); | |
98 | |
99 // pp::Instance implementation. | |
100 virtual void HandleMessage(const pp::Var& var_message); | |
101 | |
102 private: | |
103 void ConfigureTrack(); | |
104 void OnConfiguredTrack(int32_t result); | |
105 void ProbeEncoder(); | |
106 void OnEncoderProbed(int32_t result, | |
107 const std::vector<PP_VideoProfileDescription> profiles); | |
108 void OnInitializedEncoder(int32_t result); | |
109 void ScheduleNextEncode(); | |
110 void GetEncoderFrameTick(int32_t result); | |
111 void GetEncoderFrame(const pp::VideoFrame& track_frame); | |
112 void OnEncoderFrame(int32_t result, pp::VideoFrame encoder_frame, | |
113 pp::VideoFrame track_frame); | |
114 int32_t CopyTrackFrameToEncoderFrame(pp::VideoFrame dest, | |
115 pp::VideoFrame src); | |
bbudge
2015/03/02 17:52:04
nit: Either change the name to CopyFrame/CopyVideo
llandwerlin-old
2015/03/02 18:21:40
Done.
| |
116 void EncodeFrame(const pp::VideoFrame& frame); | |
117 void OnEncodeDone(int32_t result); | |
118 void OnGetBitstreamBuffer(int32_t result, PP_BitstreamBuffer buffer); | |
119 void StartTrackFrames(); | |
120 void StopTrackFrames(); | |
121 void OnTrackFrame(int32_t result, pp::VideoFrame frame); | |
122 | |
123 void StopEncode(); | |
124 | |
125 void LogError(int32_t error, const std::string& message); | |
126 void LogWarning(const std::string& message); | |
127 void Log(const std::string& message); | |
128 | |
129 void PostDataMessage(const void* buffer, uint32_t size); | |
130 void PostSignalMessage(const char* name); | |
131 | |
132 bool is_encoding_; | |
133 bool is_receiving_track_frames_; | |
134 | |
135 pp::VideoEncoder video_encoder_; | |
136 pp::MediaStreamVideoTrack video_track_; | |
137 pp::CompletionCallbackFactory<MediaStreamVideoEncoderInstance> | |
138 callback_factory_; | |
139 | |
140 PP_VideoProfile video_profile_; | |
141 PP_VideoFrame_Format frame_format_; | |
142 | |
143 pp::Size requested_size_; | |
144 pp::Size frame_size_; | |
145 pp::Size encoder_size_; | |
146 uint32_t encoded_frames_; | |
147 | |
148 pp::VideoFrame current_track_frame_; | |
149 }; | |
150 | |
151 MediaStreamVideoEncoderInstance::MediaStreamVideoEncoderInstance( | |
152 PP_Instance instance, | |
153 pp::Module* module) | |
154 : pp::Instance(instance), | |
155 is_encoding_(false), | |
156 callback_factory_(this), | |
157 #if defined(USE_VP8_INSTEAD_OF_H264) | |
158 video_profile_(PP_VIDEOPROFILE_VP8_ANY), | |
159 #else | |
160 video_profile_(PP_VIDEOPROFILE_H264MAIN), | |
161 #endif | |
162 frame_format_(PP_VIDEOFRAME_FORMAT_I420), | |
163 encoded_frames_(0) { | |
164 } | |
165 | |
166 MediaStreamVideoEncoderInstance::~MediaStreamVideoEncoderInstance() { | |
167 } | |
168 | |
169 void MediaStreamVideoEncoderInstance::ConfigureTrack() { | |
170 if (encoder_size_.IsEmpty()) | |
171 frame_size_ = requested_size_; | |
172 else | |
173 frame_size_ = encoder_size_; | |
174 | |
175 int32_t attrib_list[] = {PP_MEDIASTREAMVIDEOTRACK_ATTRIB_FORMAT, | |
176 frame_format_, | |
177 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_WIDTH, | |
178 frame_size_.width(), | |
179 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_HEIGHT, | |
180 frame_size_.height(), | |
181 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_NONE}; | |
182 | |
183 pp::VarDictionary dict; | |
184 dict.Set(pp::Var("status"), pp::Var("configuring video track")); | |
185 dict.Set(pp::Var("width"), pp::Var(frame_size_.width())); | |
186 dict.Set(pp::Var("height"), pp::Var(frame_size_.height())); | |
187 PostMessage(dict); | |
188 | |
189 video_track_.Configure( | |
190 attrib_list, callback_factory_.NewCallback( | |
191 &MediaStreamVideoEncoderInstance::OnConfiguredTrack)); | |
192 } | |
193 | |
194 void MediaStreamVideoEncoderInstance::OnConfiguredTrack(int32_t result) { | |
195 if (result != PP_OK) { | |
196 LogError(result, "Cannot configure track"); | |
197 return; | |
198 } | |
199 | |
200 if (is_encoding_) { | |
201 StartTrackFrames(); | |
202 ScheduleNextEncode(); | |
203 } else | |
204 ProbeEncoder(); | |
205 } | |
206 | |
207 void MediaStreamVideoEncoderInstance::ProbeEncoder() { | |
208 video_encoder_ = pp::VideoEncoder(this); | |
209 video_encoder_.GetSupportedProfiles(callback_factory_.NewCallbackWithOutput( | |
210 &MediaStreamVideoEncoderInstance::OnEncoderProbed)); | |
211 } | |
212 | |
213 void MediaStreamVideoEncoderInstance::OnEncoderProbed( | |
214 int32_t result, | |
215 const std::vector<PP_VideoProfileDescription> profiles) { | |
216 bool has_required_profile = false; | |
217 | |
218 Log("Available profiles:"); | |
219 for (const PP_VideoProfileDescription& profile : profiles) { | |
220 std::ostringstream oss; | |
221 oss << " profile=" << VideoProfileToString(profile.profile) | |
222 << " max_resolution=" << profile.max_resolution.width << "x" | |
223 << profile.max_resolution.height | |
224 << " max_framerate=" << profile.max_framerate_numerator << "/" | |
225 << profile.max_framerate_denominator << " acceleration=" | |
226 << HardwareAccelerationToString(profile.acceleration); | |
227 Log(oss.str()); | |
228 | |
229 has_required_profile |= profile.profile == video_profile_; | |
230 } | |
231 | |
232 if (!has_required_profile) { | |
233 std::ostringstream oss; | |
234 oss << "Cannot find required video profile: "; | |
235 oss << VideoProfileToString(video_profile_); | |
236 LogError(PP_ERROR_FAILED, oss.str()); | |
237 return; | |
238 } | |
239 | |
240 video_encoder_ = pp::VideoEncoder(this); | |
241 | |
242 pp::VarDictionary dict; | |
243 dict.Set(pp::Var("status"), pp::Var("initializing encoder")); | |
244 dict.Set(pp::Var("width"), pp::Var(encoder_size_.width())); | |
245 dict.Set(pp::Var("height"), pp::Var(encoder_size_.height())); | |
246 PostMessage(dict); | |
247 | |
248 int32_t error = video_encoder_.Initialize( | |
249 frame_format_, frame_size_, video_profile_, 2000000, | |
250 PP_HARDWAREACCELERATION_WITHFALLBACK, | |
251 callback_factory_.NewCallback( | |
252 &MediaStreamVideoEncoderInstance::OnInitializedEncoder)); | |
253 if (error != PP_OK_COMPLETIONPENDING) { | |
254 LogError(error, "Cannot initialize encoder"); | |
255 return; | |
256 } | |
257 } | |
258 | |
259 void MediaStreamVideoEncoderInstance::OnInitializedEncoder(int32_t result) { | |
260 if (result != PP_OK) { | |
261 LogError(result, "Encoder initialization failed"); | |
262 return; | |
263 } | |
264 | |
265 is_encoding_ = true; | |
266 | |
267 if (video_encoder_.GetFrameCodedSize(&encoder_size_) != PP_OK) { | |
268 LogError(result, "Cannot get encoder coded frame size"); | |
269 return; | |
270 } | |
271 | |
272 pp::VarDictionary dict; | |
273 dict.Set(pp::Var("status"), pp::Var("encoder initialized")); | |
274 dict.Set(pp::Var("width"), pp::Var(encoder_size_.width())); | |
275 dict.Set(pp::Var("height"), pp::Var(encoder_size_.height())); | |
276 PostMessage(dict); | |
277 | |
278 video_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( | |
279 &MediaStreamVideoEncoderInstance::OnGetBitstreamBuffer)); | |
280 | |
281 if (encoder_size_ != frame_size_) | |
282 ConfigureTrack(); | |
283 else { | |
284 StartTrackFrames(); | |
285 ScheduleNextEncode(); | |
286 } | |
287 } | |
288 | |
289 void MediaStreamVideoEncoderInstance::ScheduleNextEncode() { | |
290 pp::Module::Get()->core()->CallOnMainThread( | |
291 1000 / 30, callback_factory_.NewCallback( | |
292 &MediaStreamVideoEncoderInstance::GetEncoderFrameTick), | |
293 0); | |
294 } | |
295 | |
296 void MediaStreamVideoEncoderInstance::GetEncoderFrameTick(int32_t result) { | |
297 if (is_encoding_) { | |
298 if (!current_track_frame_.is_null()) { | |
299 pp::VideoFrame frame = current_track_frame_; | |
300 current_track_frame_.detach(); | |
301 GetEncoderFrame(frame); | |
302 } | |
303 ScheduleNextEncode(); | |
304 } | |
305 } | |
306 | |
307 void MediaStreamVideoEncoderInstance::GetEncoderFrame( | |
308 const pp::VideoFrame& track_frame) { | |
309 video_encoder_.GetVideoFrame(callback_factory_.NewCallbackWithOutput( | |
310 &MediaStreamVideoEncoderInstance::OnEncoderFrame, track_frame)); | |
311 } | |
312 | |
313 void MediaStreamVideoEncoderInstance::OnEncoderFrame( | |
314 int32_t result, | |
315 pp::VideoFrame encoder_frame, | |
316 pp::VideoFrame track_frame) { | |
317 if (result == PP_ERROR_ABORTED) { | |
318 video_track_.RecycleFrame(track_frame); | |
319 return; | |
320 } | |
321 if (result != PP_OK) { | |
322 video_track_.RecycleFrame(track_frame); | |
323 LogError(result, "Cannot get video frame from video encoder"); | |
324 return; | |
325 } | |
326 | |
327 track_frame.GetSize(&frame_size_); | |
328 | |
329 if (frame_size_ != encoder_size_) { | |
330 video_track_.RecycleFrame(track_frame); | |
331 LogError(PP_ERROR_FAILED, "MediaStreamVideoTrack frame size incorrect"); | |
332 return; | |
333 } | |
334 | |
335 if (CopyTrackFrameToEncoderFrame(encoder_frame, track_frame) == PP_OK) | |
336 EncodeFrame(encoder_frame); | |
337 video_track_.RecycleFrame(track_frame); | |
338 } | |
339 | |
340 int32_t MediaStreamVideoEncoderInstance::CopyTrackFrameToEncoderFrame( | |
341 pp::VideoFrame dest, | |
342 pp::VideoFrame src) { | |
bbudge
2015/03/02 17:52:03
pp::VideoFrame*, const pp::VideoFrame& ?
llandwerlin-old
2015/03/02 18:21:40
That's not possible with GetDataBuffer() because t
| |
343 if (dest.GetDataBufferSize() < src.GetDataBufferSize()) { | |
344 std::ostringstream oss; | |
345 oss << "Incorrect destination video frame buffer size : " | |
346 << dest.GetDataBufferSize() << " < " << src.GetDataBufferSize(); | |
347 LogError(PP_ERROR_FAILED, oss.str()); | |
348 return PP_ERROR_FAILED; | |
349 } | |
350 | |
351 memcpy(dest.GetDataBuffer(), src.GetDataBuffer(), src.GetDataBufferSize()); | |
352 return PP_OK; | |
353 } | |
354 | |
355 void MediaStreamVideoEncoderInstance::EncodeFrame(const pp::VideoFrame& frame) { | |
356 video_encoder_.Encode(frame, PP_FALSE, | |
357 callback_factory_.NewCallback( | |
358 &MediaStreamVideoEncoderInstance::OnEncodeDone)); | |
359 } | |
360 | |
361 void MediaStreamVideoEncoderInstance::OnEncodeDone(int32_t result) { | |
362 if (result == PP_ERROR_ABORTED) | |
363 return; | |
364 if (result != PP_OK) | |
365 LogError(result, "Encode failed"); | |
366 } | |
367 | |
368 void MediaStreamVideoEncoderInstance::OnGetBitstreamBuffer( | |
369 int32_t result, | |
370 PP_BitstreamBuffer buffer) { | |
371 if (result == PP_ERROR_ABORTED) | |
372 return; | |
373 if (result != PP_OK) { | |
374 LogError(result, "Cannot get bitstream buffer"); | |
375 return; | |
376 } | |
377 | |
378 encoded_frames_++; | |
379 PostDataMessage(buffer.buffer, buffer.size); | |
380 video_encoder_.RecycleBitstreamBuffer(buffer); | |
381 | |
382 video_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( | |
383 &MediaStreamVideoEncoderInstance::OnGetBitstreamBuffer)); | |
384 } | |
385 | |
386 void MediaStreamVideoEncoderInstance::StartTrackFrames() { | |
387 is_receiving_track_frames_ = true; | |
388 video_track_.GetFrame(callback_factory_.NewCallbackWithOutput( | |
389 &MediaStreamVideoEncoderInstance::OnTrackFrame)); | |
390 } | |
391 | |
392 void MediaStreamVideoEncoderInstance::StopTrackFrames() { | |
393 is_receiving_track_frames_ = false; | |
394 if (!current_track_frame_.is_null()) { | |
395 video_track_.RecycleFrame(current_track_frame_); | |
396 current_track_frame_.detach(); | |
397 } | |
398 } | |
399 | |
400 void MediaStreamVideoEncoderInstance::OnTrackFrame(int32_t result, | |
401 pp::VideoFrame frame) { | |
402 if (result == PP_ERROR_ABORTED) | |
403 return; | |
404 | |
405 if (!current_track_frame_.is_null()) { | |
406 video_track_.RecycleFrame(current_track_frame_); | |
407 current_track_frame_.detach(); | |
408 } | |
409 | |
410 if (result != PP_OK) { | |
411 LogError(result, "Cannot get video frame from video track"); | |
412 return; | |
413 } | |
414 | |
415 LogWarning("Got track frame to encode"); | |
bbudge
2015/03/02 17:52:03
s/LogWarning/Log
llandwerlin-old
2015/03/02 18:21:40
Oh sorry, I should have removed this.
| |
416 | |
417 current_track_frame_ = frame; | |
418 if (is_receiving_track_frames_) | |
419 video_track_.GetFrame(callback_factory_.NewCallbackWithOutput( | |
420 &MediaStreamVideoEncoderInstance::OnTrackFrame)); | |
421 } | |
422 | |
423 void MediaStreamVideoEncoderInstance::StopEncode() { | |
424 video_encoder_.Close(); | |
425 StopTrackFrames(); | |
426 video_track_.Close(); | |
427 is_encoding_ = false; | |
428 encoded_frames_ = 0; | |
429 } | |
430 | |
431 // | |
432 | |
433 void MediaStreamVideoEncoderInstance::HandleMessage( | |
434 const pp::Var& var_message) { | |
435 if (!var_message.is_dictionary()) { | |
436 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Invalid message!")); | |
437 return; | |
438 } | |
439 | |
440 pp::VarDictionary dict_message(var_message); | |
441 std::string command = dict_message.Get("command").AsString(); | |
442 | |
443 if (command == "start") { | |
444 requested_size_ = pp::Size(dict_message.Get("width").AsInt(), | |
445 dict_message.Get("height").AsInt()); | |
446 pp::Var var_track = dict_message.Get("track"); | |
447 if (!var_track.is_resource()) { | |
448 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Given track is not a resource")); | |
449 return; | |
450 } | |
451 pp::Resource resource_track = var_track.AsResource(); | |
452 video_track_ = pp::MediaStreamVideoTrack(resource_track); | |
453 video_encoder_ = pp::VideoEncoder(); | |
454 ConfigureTrack(); | |
455 } else if (command == "stop") { | |
456 StopEncode(); | |
457 PostSignalMessage("stopped"); | |
458 } else { | |
459 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Invalid command!")); | |
460 } | |
461 } | |
462 | |
463 void MediaStreamVideoEncoderInstance::PostDataMessage(const void* buffer, | |
464 uint32_t size) { | |
465 pp::VarDictionary dictionary; | |
466 | |
467 dictionary.Set(pp::Var("name"), pp::Var("data")); | |
468 | |
469 pp::VarArrayBuffer array_buffer(size); | |
470 void* data_ptr = array_buffer.Map(); | |
471 memcpy(data_ptr, buffer, size); | |
472 array_buffer.Unmap(); | |
473 dictionary.Set(pp::Var("data"), array_buffer); | |
474 | |
475 PostMessage(dictionary); | |
476 } | |
477 | |
478 void MediaStreamVideoEncoderInstance::PostSignalMessage(const char* name) { | |
479 pp::VarDictionary dictionary; | |
480 dictionary.Set(pp::Var("name"), pp::Var(name)); | |
481 | |
482 PostMessage(dictionary); | |
483 } | |
484 | |
485 void MediaStreamVideoEncoderInstance::LogError(int32_t error, | |
486 const std::string& message) { | |
487 std::string msg("Error: "); | |
488 msg.append(pp::Var(error).DebugString()); | |
489 msg.append(" : "); | |
490 msg.append(message); | |
491 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var(msg)); | |
492 } | |
493 | |
494 void MediaStreamVideoEncoderInstance::LogWarning(const std::string& message) { | |
495 std::string msg("Warning: "); | |
496 msg.append(message); | |
497 LogToConsole(PP_LOGLEVEL_WARNING, pp::Var(msg)); | |
498 } | |
499 | |
500 void MediaStreamVideoEncoderInstance::Log(const std::string& message) { | |
501 LogToConsole(PP_LOGLEVEL_LOG, pp::Var(message)); | |
502 } | |
503 | |
504 pp::Instance* MediaStreamVideoEncoderModule::CreateInstance( | |
505 PP_Instance instance) { | |
506 return new MediaStreamVideoEncoderInstance(instance, this); | |
507 } | |
508 | |
509 } // anonymous namespace | |
510 | |
511 namespace pp { | |
512 // Factory function for your specialization of the Module object. | |
513 Module* CreateModule() { | |
514 return new MediaStreamVideoEncoderModule(); | |
515 } | |
516 } // namespace pp | |
OLD | NEW |