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