OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #include <math.h> | 5 #include <math.h> |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <iostream> | 9 #include <iostream> |
10 #include <map> | 10 #include <map> |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 void StartTrackFrames(); | 143 void StartTrackFrames(); |
144 void StopTrackFrames(); | 144 void StopTrackFrames(); |
145 void OnTrackFrame(int32_t result, pp::VideoFrame frame); | 145 void OnTrackFrame(int32_t result, pp::VideoFrame frame); |
146 | 146 |
147 void StopEncode(); | 147 void StopEncode(); |
148 | 148 |
149 void LogError(int32_t error, const std::string& message); | 149 void LogError(int32_t error, const std::string& message); |
150 void Log(const std::string& message); | 150 void Log(const std::string& message); |
151 | 151 |
152 void PostDataMessage(const void* buffer, uint32_t size); | 152 void PostDataMessage(const void* buffer, uint32_t size); |
153 void PostSignalMessage(const char* name); | |
154 | 153 |
155 typedef std::map<std::string, PP_VideoProfile> VideoProfileFromStringMap; | 154 typedef std::map<std::string, PP_VideoProfile> VideoProfileFromStringMap; |
156 VideoProfileFromStringMap profile_from_string_; | 155 VideoProfileFromStringMap profile_from_string_; |
157 | 156 |
158 typedef std::map<PP_VideoProfile, std::string> VideoProfileToStringMap; | 157 typedef std::map<PP_VideoProfile, std::string> VideoProfileToStringMap; |
159 VideoProfileToStringMap profile_to_string_; | 158 VideoProfileToStringMap profile_to_string_; |
160 | 159 |
161 bool is_encoding_; | 160 bool is_encoding_; |
162 bool is_receiving_track_frames_; | 161 bool is_receiving_track_frames_; |
163 | 162 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 frame_size_ = encoder_size_; | 242 frame_size_ = encoder_size_; |
244 | 243 |
245 int32_t attrib_list[] = {PP_MEDIASTREAMVIDEOTRACK_ATTRIB_FORMAT, | 244 int32_t attrib_list[] = {PP_MEDIASTREAMVIDEOTRACK_ATTRIB_FORMAT, |
246 frame_format_, | 245 frame_format_, |
247 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_WIDTH, | 246 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_WIDTH, |
248 frame_size_.width(), | 247 frame_size_.width(), |
249 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_HEIGHT, | 248 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_HEIGHT, |
250 frame_size_.height(), | 249 frame_size_.height(), |
251 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_NONE}; | 250 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_NONE}; |
252 | 251 |
253 pp::VarDictionary dict; | |
254 dict.Set(pp::Var("status"), pp::Var("configuring video track")); | |
255 dict.Set(pp::Var("width"), pp::Var(frame_size_.width())); | |
256 dict.Set(pp::Var("height"), pp::Var(frame_size_.height())); | |
257 PostMessage(dict); | |
258 | |
259 video_track_.Configure( | 252 video_track_.Configure( |
260 attrib_list, | 253 attrib_list, |
261 callback_factory_.NewCallback(&VideoEncoderInstance::OnConfiguredTrack)); | 254 callback_factory_.NewCallback(&VideoEncoderInstance::OnConfiguredTrack)); |
262 } | 255 } |
263 | 256 |
264 void VideoEncoderInstance::OnConfiguredTrack(int32_t result) { | 257 void VideoEncoderInstance::OnConfiguredTrack(int32_t result) { |
265 if (result != PP_OK) { | 258 if (result != PP_OK) { |
266 LogError(result, "Cannot configure track"); | 259 LogError(result, "Cannot configure track"); |
267 return; | 260 return; |
268 } | 261 } |
(...skipping 18 matching lines...) Expand all Loading... |
287 dict.Set(pp::Var("name"), pp::Var("supportedProfiles")); | 280 dict.Set(pp::Var("name"), pp::Var("supportedProfiles")); |
288 pp::VarArray js_profiles; | 281 pp::VarArray js_profiles; |
289 dict.Set(pp::Var("profiles"), js_profiles); | 282 dict.Set(pp::Var("profiles"), js_profiles); |
290 | 283 |
291 if (result != PP_OK) { | 284 if (result != PP_OK) { |
292 LogError(result, "Cannot get supported profiles"); | 285 LogError(result, "Cannot get supported profiles"); |
293 PostMessage(dict); | 286 PostMessage(dict); |
294 } | 287 } |
295 | 288 |
296 int32_t idx = 0; | 289 int32_t idx = 0; |
297 for (const PP_VideoProfileDescription& profile : profiles) | 290 for (uint32_t i = 0; i < profiles.size(); i++) { |
| 291 const PP_VideoProfileDescription& profile = profiles[i]; |
298 js_profiles.Set(idx++, pp::Var(VideoProfileToString(profile.profile))); | 292 js_profiles.Set(idx++, pp::Var(VideoProfileToString(profile.profile))); |
| 293 } |
299 PostMessage(dict); | 294 PostMessage(dict); |
300 } | 295 } |
301 | 296 |
302 void VideoEncoderInstance::StartEncoder() { | 297 void VideoEncoderInstance::StartEncoder() { |
303 video_encoder_ = pp::VideoEncoder(this); | 298 video_encoder_ = pp::VideoEncoder(this); |
304 | 299 |
305 int32_t error = video_encoder_.Initialize( | 300 int32_t error = video_encoder_.Initialize( |
306 frame_format_, frame_size_, video_profile_, 2000000, | 301 frame_format_, frame_size_, video_profile_, 2000000, |
307 PP_HARDWAREACCELERATION_WITHFALLBACK, | 302 PP_HARDWAREACCELERATION_WITHFALLBACK, |
308 callback_factory_.NewCallback( | 303 callback_factory_.NewCallback( |
309 &VideoEncoderInstance::OnInitializedEncoder)); | 304 &VideoEncoderInstance::OnInitializedEncoder)); |
310 if (error != PP_OK_COMPLETIONPENDING) { | 305 if (error != PP_OK_COMPLETIONPENDING) { |
311 LogError(error, "Cannot initialize encoder"); | 306 LogError(error, "Cannot initialize encoder"); |
312 return; | 307 return; |
313 } | 308 } |
314 } | 309 } |
315 | 310 |
316 void VideoEncoderInstance::OnInitializedEncoder(int32_t result) { | 311 void VideoEncoderInstance::OnInitializedEncoder(int32_t result) { |
317 if (result != PP_OK) { | 312 if (result != PP_OK) { |
318 LogError(result, "Encoder initialization failed"); | 313 LogError(result, "Encoder initialization failed"); |
319 return; | 314 return; |
320 } | 315 } |
321 | 316 |
322 is_encoding_ = true; | 317 is_encoding_ = true; |
323 PostSignalMessage("started"); | 318 Log("started"); |
324 | 319 |
325 if (video_encoder_.GetFrameCodedSize(&encoder_size_) != PP_OK) { | 320 if (video_encoder_.GetFrameCodedSize(&encoder_size_) != PP_OK) { |
326 LogError(result, "Cannot get encoder coded frame size"); | 321 LogError(result, "Cannot get encoder coded frame size"); |
327 return; | 322 return; |
328 } | 323 } |
329 | 324 |
330 video_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( | 325 video_encoder_.GetBitstreamBuffer(callback_factory_.NewCallbackWithOutput( |
331 &VideoEncoderInstance::OnGetBitstreamBuffer)); | 326 &VideoEncoderInstance::OnGetBitstreamBuffer)); |
332 | 327 |
333 if (encoder_size_ != frame_size_) | 328 if (encoder_size_ != frame_size_) |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 return; | 489 return; |
495 } | 490 } |
496 pp::Resource resource_track = var_track.AsResource(); | 491 pp::Resource resource_track = var_track.AsResource(); |
497 video_track_ = pp::MediaStreamVideoTrack(resource_track); | 492 video_track_ = pp::MediaStreamVideoTrack(resource_track); |
498 video_encoder_ = pp::VideoEncoder(); | 493 video_encoder_ = pp::VideoEncoder(); |
499 video_profile_ = VideoProfileFromString( | 494 video_profile_ = VideoProfileFromString( |
500 dict_message.Get("profile").AsString()); | 495 dict_message.Get("profile").AsString()); |
501 ConfigureTrack(); | 496 ConfigureTrack(); |
502 } else if (command == "stop") { | 497 } else if (command == "stop") { |
503 StopEncode(); | 498 StopEncode(); |
504 PostSignalMessage("stopped"); | 499 Log("stopped"); |
505 } else { | 500 } else { |
506 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Invalid command!")); | 501 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var("Invalid command!")); |
507 } | 502 } |
508 } | 503 } |
509 | 504 |
510 void VideoEncoderInstance::PostDataMessage(const void* buffer, uint32_t size) { | 505 void VideoEncoderInstance::PostDataMessage(const void* buffer, uint32_t size) { |
511 pp::VarDictionary dictionary; | 506 pp::VarDictionary dictionary; |
512 | 507 |
513 dictionary.Set(pp::Var("name"), pp::Var("data")); | 508 dictionary.Set(pp::Var("name"), pp::Var("data")); |
514 | 509 |
(...skipping 24 matching lines...) Expand all Loading... |
539 data_ptr = static_cast<uint8_t*>(array_buffer.Map()); | 534 data_ptr = static_cast<uint8_t*>(array_buffer.Map()); |
540 } | 535 } |
541 | 536 |
542 memcpy(data_ptr + data_offset, buffer, size); | 537 memcpy(data_ptr + data_offset, buffer, size); |
543 array_buffer.Unmap(); | 538 array_buffer.Unmap(); |
544 dictionary.Set(pp::Var("data"), array_buffer); | 539 dictionary.Set(pp::Var("data"), array_buffer); |
545 | 540 |
546 PostMessage(dictionary); | 541 PostMessage(dictionary); |
547 } | 542 } |
548 | 543 |
549 void VideoEncoderInstance::PostSignalMessage(const char* name) { | |
550 pp::VarDictionary dictionary; | |
551 dictionary.Set(pp::Var("name"), pp::Var(name)); | |
552 | |
553 PostMessage(dictionary); | |
554 } | |
555 | |
556 void VideoEncoderInstance::LogError(int32_t error, const std::string& message) { | 544 void VideoEncoderInstance::LogError(int32_t error, const std::string& message) { |
557 std::string msg("Error: "); | 545 std::string msg("Error: "); |
558 msg.append(pp::Var(error).DebugString()); | 546 msg.append(pp::Var(error).DebugString()); |
559 msg.append(" : "); | 547 msg.append(" : "); |
560 msg.append(message); | 548 msg.append(message); |
561 LogToConsole(PP_LOGLEVEL_ERROR, pp::Var(msg)); | 549 |
| 550 Log(msg); |
562 } | 551 } |
563 | 552 |
564 void VideoEncoderInstance::Log(const std::string& message) { | 553 void VideoEncoderInstance::Log(const std::string& message) { |
565 LogToConsole(PP_LOGLEVEL_LOG, pp::Var(message)); | 554 pp::VarDictionary dictionary; |
| 555 dictionary.Set(pp::Var("name"), pp::Var("log")); |
| 556 dictionary.Set(pp::Var("message"), pp::Var(message)); |
| 557 |
| 558 PostMessage(dictionary); |
566 } | 559 } |
567 | 560 |
568 pp::Instance* VideoEncoderModule::CreateInstance(PP_Instance instance) { | 561 pp::Instance* VideoEncoderModule::CreateInstance(PP_Instance instance) { |
569 return new VideoEncoderInstance(instance, this); | 562 return new VideoEncoderInstance(instance, this); |
570 } | 563 } |
571 | 564 |
572 } // anonymous namespace | 565 } // anonymous namespace |
573 | 566 |
574 namespace pp { | 567 namespace pp { |
575 // Factory function for your specialization of the Module object. | 568 // Factory function for your specialization of the Module object. |
576 Module* CreateModule() { | 569 Module* CreateModule() { |
577 return new VideoEncoderModule(); | 570 return new VideoEncoderModule(); |
578 } | 571 } |
579 } // namespace pp | 572 } // namespace pp |
OLD | NEW |