Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: ppapi/examples/video_encode/video_encode.cc

Issue 937643006: Pepper: add video_encoder example (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bbudge-ppb-video-encoder-impl
Patch Set: Nits update Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
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 DidChangeView(const pp::Rect& position,
101 const pp::Rect& clip_ignored);
102 virtual void HandleMessage(const pp::Var& var_message);
103
104 private:
105 void ConfigureTrack();
106 void OnConfiguredTrack(int32_t result);
107 void ProbeEncoder();
108 void OnEncoderProbed(int32_t result,
109 const std::vector<PP_VideoProfileDescription> profiles);
110 void InitializeEncoder();
111 void OnInitializedEncoder(int32_t result);
112 void ScheduleNextGetEncoderFrame();
113 void GetEncoderFrameTick(int32_t result);
114 void GetEncoderFrame();
115 void GetTrackFrame(const pp::VideoFrame& encoder_frame);
116 void OnGetTrackFrame(int32_t result,
117 pp::VideoFrame track_frame,
118 pp::VideoFrame encoder_frame);
119 void OnGetEncoderFrame(int32_t result, const pp::VideoFrame& frame);
120 int32_t CopyTrackFrameToEncoderFrame(pp::VideoFrame dest, pp::VideoFrame src);
121 void EncodeFrame(const pp::VideoFrame& frame);
122 void OnEncodeDone(int32_t result);
123 void OnGetBitstreamBuffer(int32_t result, PP_BitstreamBuffer buffer);
124 void StopEncode();
125
126 void LogError(int32_t error, const std::string& message);
127 void LogWarning(const std::string& message);
128 void Log(const std::string& message);
129
130 void PostDataMessage(const void* buffer, uint32_t size);
131 void PostSignalMessage(const char* name);
132
133 bool is_encoding_;
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 frame_size_;
144 pp::Size plugin_size_;
145 pp::Size encoder_size_;
146 uint32_t encoded_frames_;
147
148 pp::VideoFrame encoder_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::DidChangeView(
170 const pp::Rect& position,
171 const pp::Rect& clip_ignored) {
172 plugin_size_ = position.size();
173 }
174
175 void MediaStreamVideoEncoderInstance::ConfigureTrack() {
176 if (encoder_size_.IsEmpty())
177 frame_size_ = plugin_size_;
178 else
179 frame_size_ = encoder_size_;
180
181 int32_t attrib_list[] = {PP_MEDIASTREAMVIDEOTRACK_ATTRIB_FORMAT,
182 frame_format_,
183 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_WIDTH,
184 frame_size_.width(),
185 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_HEIGHT,
186 frame_size_.height(),
187 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_NONE};
188
189 pp::VarDictionary dict;
190 dict.Set(pp::Var("status"), pp::Var("configuring video track"));
191 dict.Set(pp::Var("width"), pp::Var(frame_size_.width()));
192 dict.Set(pp::Var("height"), pp::Var(frame_size_.height()));
193 PostMessage(dict);
194
195 video_track_.Configure(
196 attrib_list, callback_factory_.NewCallback(
197 &MediaStreamVideoEncoderInstance::OnConfiguredTrack));
198 }
199
200 void MediaStreamVideoEncoderInstance::OnConfiguredTrack(int32_t result) {
201 if (result != PP_OK) {
202 LogError(result, "Cannot configure track");
203 return;
204 }
205
206 if (is_encoding_)
207 GetEncoderFrame();
208 else
209 ProbeEncoder();
210 }
211
212 void MediaStreamVideoEncoderInstance::ProbeEncoder() {
213 video_encoder_ = pp::VideoEncoder(this);
214 video_encoder_.GetSupportedProfiles(callback_factory_.NewCallbackWithOutput(
215 &MediaStreamVideoEncoderInstance::OnEncoderProbed));
216 }
217
218 void MediaStreamVideoEncoderInstance::OnEncoderProbed(
219 int32_t result,
220 const std::vector<PP_VideoProfileDescription> profiles) {
221 bool has_required_profile = false;
222
223 Log("Available profiles:");
224 for (const PP_VideoProfileDescription& profile : profiles) {
225 std::ostringstream oss;
226 oss << " profile=" << VideoProfileToString(profile.profile)
227 << " max_resolution=" << profile.max_resolution.width << "x"
228 << profile.max_resolution.height
229 << " max_framerate=" << profile.max_framerate_numerator << "/"
230 << profile.max_framerate_denominator << " acceleration="
231 << HardwareAccelerationToString(profile.acceleration);
232 Log(oss.str());
233
234 has_required_profile |= profile.profile == video_profile_;
235 }
236
237 if (!has_required_profile) {
238 std::ostringstream oss;
239 oss << "Cannot find required video profile: ";
240 oss << VideoProfileToString(video_profile_);
241 LogError(PP_ERROR_FAILED, oss.str());
242 return;
243 }
244
245 InitializeEncoder();
bbudge 2015/02/19 21:54:38 Suggestion: inline the function here, so nobody wi
llandwerlin-old 2015/02/27 14:19:44 Done.
246 }
247
248 void MediaStreamVideoEncoderInstance::InitializeEncoder() {
249 video_encoder_ = pp::VideoEncoder(this);
250
251 pp::VarDictionary dict;
252 dict.Set(pp::Var("status"), pp::Var("initializing encoder"));
253 dict.Set(pp::Var("width"), pp::Var(encoder_size_.width()));
254 dict.Set(pp::Var("height"), pp::Var(encoder_size_.height()));
255 PostMessage(dict);
256
257 int32_t error = video_encoder_.Initialize(
258 frame_format_, frame_size_, video_profile_, 2000000,
259 PP_HARDWAREACCELERATION_WITHFALLBACK,
260 callback_factory_.NewCallback(
261 &MediaStreamVideoEncoderInstance::OnInitializedEncoder));
262 if (error != PP_OK_COMPLETIONPENDING) {
263 LogError(error, "Cannot initialize encoder");
264 return;
265 }
266 }
267
268 void MediaStreamVideoEncoderInstance::OnInitializedEncoder(int32_t result) {
269 if (result != PP_OK) {
270 LogError(result, "Encoder initialization failed");
271 return;
272 }
273
274 is_encoding_ = true;
275
276 if (video_encoder_.GetFrameCodedSize(&encoder_size_) != PP_OK) {
277 LogError(result, "Cannot get encoder coded frame size");
278 return;
279 }
280
281 pp::VarDictionary dict;
282 dict.Set(pp::Var("status"), pp::Var("encoder initialized"));
283 dict.Set(pp::Var("width"), pp::Var(encoder_size_.width()));
284 dict.Set(pp::Var("height"), pp::Var(encoder_size_.height()));
285 PostMessage(dict);
286
287 video_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput(
288 &MediaStreamVideoEncoderInstance::OnGetBitstreamBuffer));
289
290 if (encoder_size_ != frame_size_)
291 ConfigureTrack();
292 else
293 GetEncoderFrame();
294 }
295
296 void MediaStreamVideoEncoderInstance::ScheduleNextGetEncoderFrame() {
297 pp::Module::Get()->core()->CallOnMainThread(
298 1000 / 30, callback_factory_.NewCallback(
299 &MediaStreamVideoEncoderInstance::GetEncoderFrameTick),
300 0);
301 }
302
303 void MediaStreamVideoEncoderInstance::GetEncoderFrameTick(int32_t result) {
304 if (is_encoding_)
305 GetEncoderFrame();
306 }
307
308 void MediaStreamVideoEncoderInstance::GetEncoderFrame() {
309 video_encoder_.GetVideoFrame(callback_factory_.NewCallbackWithOutput(
310 &MediaStreamVideoEncoderInstance::OnGetEncoderFrame));
311 }
312
313 void MediaStreamVideoEncoderInstance::OnGetEncoderFrame(
314 int32_t result,
315 const pp::VideoFrame& frame) {
316 if (result == PP_ERROR_ABORTED)
317 return;
318 if (result != PP_OK) {
319 LogError(result, "Cannot get video frame from video encoder");
320 return;
321 }
322
323 GetTrackFrame(frame);
324 }
325
326 void MediaStreamVideoEncoderInstance::GetTrackFrame(
327 const pp::VideoFrame& encoder_frame) {
328 video_track_.GetFrame(callback_factory_.NewCallbackWithOutput(
329 &MediaStreamVideoEncoderInstance::OnGetTrackFrame, encoder_frame));
330 }
331
332 void MediaStreamVideoEncoderInstance::OnGetTrackFrame(
333 int32_t result,
334 pp::VideoFrame track_frame,
335 pp::VideoFrame encoder_frame) {
336 if (result == PP_ERROR_ABORTED)
337 return;
338 if (result != PP_OK) {
339 LogError(result, "Cannot get video frame from video track");
340 return;
341 }
342
343 track_frame.GetSize(&frame_size_);
344
345 if (frame_size_ != encoder_size_) {
346 video_track_.RecycleFrame(track_frame);
347 LogError(PP_ERROR_FAILED, "MediaStreamVideoTrack frame size incorrect");
348 return;
349 }
350
351 if (CopyTrackFrameToEncoderFrame(encoder_frame, track_frame) == PP_OK) {
352 EncodeFrame(encoder_frame);
353 ScheduleNextGetEncoderFrame();
354 }
355 video_track_.RecycleFrame(track_frame);
356 }
357
358 int32_t MediaStreamVideoEncoderInstance::CopyTrackFrameToEncoderFrame(
359 pp::VideoFrame dest,
360 pp::VideoFrame src) {
361 if (dest.GetDataBufferSize() < src.GetDataBufferSize()) {
362 std::ostringstream oss;
363 oss << "Incorrect destination video frame buffer size : "
364 << dest.GetDataBufferSize() << " < " << src.GetDataBufferSize();
365 LogError(PP_ERROR_FAILED, oss.str());
366 return PP_ERROR_FAILED;
367 }
368
369 memcpy(dest.GetDataBuffer(), src.GetDataBuffer(), src.GetDataBufferSize());
370 return PP_OK;
371 }
372
373 void MediaStreamVideoEncoderInstance::EncodeFrame(const pp::VideoFrame& frame) {
374 video_encoder_.Encode(frame, PP_FALSE,
375 callback_factory_.NewCallback(
376 &MediaStreamVideoEncoderInstance::OnEncodeDone));
377 }
378
379 void MediaStreamVideoEncoderInstance::OnEncodeDone(int32_t result) {
380 if (result != PP_OK)
381 LogError(result, "Encode failed");
382 }
383
384 void MediaStreamVideoEncoderInstance::OnGetBitstreamBuffer(
385 int32_t result,
386 PP_BitstreamBuffer buffer) {
387 if (result == PP_ERROR_ABORTED)
388 return;
389 if (result != PP_OK) {
390 LogError(result, "Cannot get bitstream buffer");
391 return;
392 }
393
394 encoded_frames_++;
395 PostDataMessage(buffer.buffer, buffer.size);
396 video_encoder_.RecycleBitstreamBuffer(buffer);
397
398 video_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput(
399 &MediaStreamVideoEncoderInstance::OnGetBitstreamBuffer));
400 }
401
402 void MediaStreamVideoEncoderInstance::StopEncode() {
403 video_encoder_.Close();
404 video_track_.Close();
405 is_encoding_ = false;
406 encoded_frames_ = 0;
407 }
408
409 //
410
411 void MediaStreamVideoEncoderInstance::HandleMessage(
412 const pp::Var& var_message) {
413 if (!var_message.is_dictionary()) {
414 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Invalid message!"));
415 return;
416 }
417
418 pp::VarDictionary var_dictionary_message(var_message);
419 std::string command = var_dictionary_message.Get("command").AsString();
420
421 if (command == "start") {
422 pp::Var var_track = var_dictionary_message.Get("track");
423 if (!var_track.is_resource()) {
424 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Given track is not a resource"));
425 return;
426 }
427 pp::Resource resource_track = var_track.AsResource();
428 video_track_ = pp::MediaStreamVideoTrack(resource_track);
429 video_encoder_ = pp::VideoEncoder();
430 ConfigureTrack();
431 } else if (command == "stop") {
432 StopEncode();
433 PostSignalMessage("stopped");
434 } else {
435 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Invalid command!"));
436 }
437 }
438
439 void MediaStreamVideoEncoderInstance::PostDataMessage(const void* buffer,
440 uint32_t size) {
441 pp::VarDictionary dictionary;
442
443 dictionary.Set(pp::Var("name"), pp::Var("data"));
444
445 pp::VarArrayBuffer array_buffer(size);
446 void* data_ptr = array_buffer.Map();
447 memcpy(data_ptr, buffer, size);
448 array_buffer.Unmap();
449 dictionary.Set(pp::Var("data"), array_buffer);
450
451 PostMessage(dictionary);
452 }
453
454 void MediaStreamVideoEncoderInstance::PostSignalMessage(const char* name) {
455 pp::VarDictionary dictionary;
456 dictionary.Set(pp::Var("name"), pp::Var(name));
457
458 PostMessage(dictionary);
459 }
460
461 void MediaStreamVideoEncoderInstance::LogError(int32_t error,
462 const std::string& message) {
463 std::string msg("Error: ");
464 msg.append(pp::Var(error).DebugString());
465 msg.append(" : ");
466 msg.append(message);
467 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var(msg));
468 }
469
470 void MediaStreamVideoEncoderInstance::LogWarning(const std::string& message) {
471 std::string msg("Warning: ");
472 msg.append(message);
473 LogToConsole(PP_LOGLEVEL_WARNING, pp::Var(msg));
474 }
475
476 void MediaStreamVideoEncoderInstance::Log(const std::string& message) {
477 LogToConsole(PP_LOGLEVEL_LOG, pp::Var(message));
478 }
479
480 pp::Instance* MediaStreamVideoEncoderModule::CreateInstance(
481 PP_Instance instance) {
482 return new MediaStreamVideoEncoderInstance(instance, this);
483 }
484
485 } // anonymous namespace
486
487 namespace pp {
488 // Factory function for your specialization of the Module object.
489 Module* CreateModule() {
490 return new MediaStreamVideoEncoderModule();
491 }
492 } // namespace pp
OLDNEW
« no previous file with comments | « no previous file | ppapi/examples/video_encode/video_encode.html » ('j') | ppapi/examples/video_encode/video_encode.html » ('J')

Powered by Google App Engine
This is Rietveld 408576698