Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / common / gpu / media / android_video_encode_accelerator.cc
index 05f18b6..f124b14 100644 (file)
@@ -33,16 +33,16 @@ enum {
 
 // Helper macros for dealing with failure.  If |result| evaluates false, emit
 // |log| to DLOG(ERROR), register |error| with the client, and return.
-#define RETURN_ON_FAILURE(result, log, error)                 \
-  do {                                                        \
-    if (!(result)) {                                          \
-      DLOG(ERROR) << log;                                     \
-      if (client_ptr_factory_.GetWeakPtr()) {                 \
-        client_ptr_factory_.GetWeakPtr()->NotifyError(error); \
-        client_ptr_factory_.InvalidateWeakPtrs();             \
-      }                                                       \
-      return;                                                 \
-    }                                                         \
+#define RETURN_ON_FAILURE(result, log, error)                  \
+  do {                                                         \
+    if (!(result)) {                                           \
+      DLOG(ERROR) << log;                                      \
+      if (client_ptr_factory_->GetWeakPtr()) {                 \
+        client_ptr_factory_->GetWeakPtr()->NotifyError(error); \
+        client_ptr_factory_.reset();                           \
+      }                                                        \
+      return;                                                  \
+    }                                                          \
   } while (0)
 
 // Because MediaCodec is thread-hostile (must be poked on a single thread) and
@@ -67,10 +67,8 @@ static inline const base::TimeDelta NoWaitTimeOut() {
   return base::TimeDelta::FromMicroseconds(0);
 }
 
-AndroidVideoEncodeAccelerator::AndroidVideoEncodeAccelerator(
-    media::VideoEncodeAccelerator::Client* client)
-    : client_ptr_factory_(client),
-      num_buffers_at_codec_(0),
+AndroidVideoEncodeAccelerator::AndroidVideoEncodeAccelerator()
+    : num_buffers_at_codec_(0),
       num_output_buffers_(-1),
       output_buffers_capacity_(0),
       last_set_bitrate_(0) {}
@@ -88,7 +86,7 @@ AndroidVideoEncodeAccelerator::GetSupportedProfiles() {
   std::vector<SupportedProfile> profiles;
 
 #if defined(ENABLE_WEBRTC)
-  const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
+  const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
   if (cmd_line->HasSwitch(switches::kDisableWebRtcHWEncoding))
     return profiles;
 #endif
@@ -102,7 +100,7 @@ AndroidVideoEncodeAccelerator::GetSupportedProfiles() {
       continue;
     }
     SupportedProfile profile;
-    profile.profile = media::VP8PROFILE_MAIN;
+    profile.profile = media::VP8PROFILE_ANY;
     // Wouldn't it be nice if MediaCodec exposed the maximum capabilities of the
     // encoder?  Sure would be.  Too bad it doesn't.  So we hard-code some
     // reasonable defaults.
@@ -114,11 +112,12 @@ AndroidVideoEncodeAccelerator::GetSupportedProfiles() {
   return profiles;
 }
 
-void AndroidVideoEncodeAccelerator::Initialize(
+bool AndroidVideoEncodeAccelerator::Initialize(
     VideoFrame::Format format,
     const gfx::Size& input_visible_size,
     media::VideoCodecProfile output_profile,
-    uint32 initial_bitrate) {
+    uint32 initial_bitrate,
+    Client* client) {
   DVLOG(3) << __PRETTY_FUNCTION__ << " format: " << format
            << ", input_visible_size: " << input_visible_size.ToString()
            << ", output_profile: " << output_profile
@@ -126,20 +125,23 @@ void AndroidVideoEncodeAccelerator::Initialize(
   DCHECK(!media_codec_);
   DCHECK(thread_checker_.CalledOnValidThread());
 
-  RETURN_ON_FAILURE(media::MediaCodecBridge::IsAvailable() &&
-                        media::MediaCodecBridge::SupportsSetParameters() &&
-                        format == VideoFrame::I420 &&
-                        output_profile == media::VP8PROFILE_MAIN,
-                    "Unexpected combo: " << format << ", " << output_profile,
-                    kInvalidArgumentError);
+  client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
+
+  if (!(media::MediaCodecBridge::SupportsSetParameters() &&
+        format == VideoFrame::I420 &&
+        output_profile == media::VP8PROFILE_ANY)) {
+    DLOG(ERROR) << "Unexpected combo: " << format << ", " << output_profile;
+    return false;
+  }
 
   last_set_bitrate_ = initial_bitrate;
 
   // Only consider using MediaCodec if it's likely backed by hardware.
-  RETURN_ON_FAILURE(!media::VideoCodecBridge::IsKnownUnaccelerated(
-                         media::kCodecVP8, media::MEDIA_CODEC_ENCODER),
-                    "No HW support",
-                    kPlatformFailureError);
+  if (media::VideoCodecBridge::IsKnownUnaccelerated(
+          media::kCodecVP8, media::MEDIA_CODEC_ENCODER)) {
+    DLOG(ERROR) << "No HW support";
+    return false;
+  }
 
   // TODO(fischman): when there is more HW out there with different color-space
   // support, this should turn into a negotiation with the codec for supported
@@ -153,25 +155,22 @@ void AndroidVideoEncodeAccelerator::Initialize(
                                              IFRAME_INTERVAL,
                                              COLOR_FORMAT_YUV420_SEMIPLANAR));
 
-  RETURN_ON_FAILURE(
-      media_codec_,
-      "Failed to create/start the codec: " << input_visible_size.ToString(),
-      kPlatformFailureError);
-
-  base::MessageLoop::current()->PostTask(
-      FROM_HERE,
-      base::Bind(&VideoEncodeAccelerator::Client::NotifyInitializeDone,
-                 client_ptr_factory_.GetWeakPtr()));
+  if (!media_codec_) {
+    DLOG(ERROR) << "Failed to create/start the codec: "
+                << input_visible_size.ToString();
+    return false;
+  }
 
   num_output_buffers_ = media_codec_->GetOutputBuffersCount();
   output_buffers_capacity_ = media_codec_->GetOutputBuffersCapacity();
   base::MessageLoop::current()->PostTask(
       FROM_HERE,
       base::Bind(&VideoEncodeAccelerator::Client::RequireBitstreamBuffers,
-                 client_ptr_factory_.GetWeakPtr(),
+                 client_ptr_factory_->GetWeakPtr(),
                  num_output_buffers_,
                  input_visible_size,
                  output_buffers_capacity_));
+  return true;
 }
 
 void AndroidVideoEncodeAccelerator::MaybeStartIOTimer() {
@@ -208,8 +207,8 @@ void AndroidVideoEncodeAccelerator::Encode(
                             frame->stride(VideoFrame::kUPlane) &&
                         frame->row_bytes(VideoFrame::kVPlane) ==
                             frame->stride(VideoFrame::kVPlane) &&
-                        gfx::Rect(frame->coded_size()) == frame->visible_rect(),
-                    "Non-packed frame, or visible rect != coded size",
+                        frame->coded_size() == frame->visible_rect().size(),
+                    "Non-packed frame, or visible_rect != coded_size",
                     kInvalidArgumentError);
 
   pending_frames_.push(MakeTuple(frame, force_keyframe, base::Time::Now()));
@@ -246,7 +245,7 @@ void AndroidVideoEncodeAccelerator::RequestEncodingParametersChange(
 void AndroidVideoEncodeAccelerator::Destroy() {
   DVLOG(3) << __PRETTY_FUNCTION__;
   DCHECK(thread_checker_.CalledOnValidThread());
-  client_ptr_factory_.InvalidateWeakPtrs();
+  client_ptr_factory_.reset();
   if (media_codec_) {
     if (io_timer_.IsRunning())
       io_timer_.Stop();
@@ -263,7 +262,7 @@ void AndroidVideoEncodeAccelerator::DoIOTask() {
 }
 
 void AndroidVideoEncodeAccelerator::QueueInput() {
-  if (!client_ptr_factory_.GetWeakPtr() || pending_frames_.empty())
+  if (!client_ptr_factory_->GetWeakPtr() || pending_frames_.empty())
     return;
 
   int input_buf_index = 0;
@@ -347,7 +346,7 @@ bool AndroidVideoEncodeAccelerator::DoOutputBuffersSuffice() {
 }
 
 void AndroidVideoEncodeAccelerator::DequeueOutput() {
-  if (!client_ptr_factory_.GetWeakPtr() ||
+  if (!client_ptr_factory_->GetWeakPtr() ||
       available_bitstream_buffers_.empty() || num_buffers_at_codec_ == 0) {
     return;
   }
@@ -405,7 +404,7 @@ void AndroidVideoEncodeAccelerator::DequeueOutput() {
   base::MessageLoop::current()->PostTask(
       FROM_HERE,
       base::Bind(&VideoEncodeAccelerator::Client::BitstreamBufferReady,
-                 client_ptr_factory_.GetWeakPtr(),
+                 client_ptr_factory_->GetWeakPtr(),
                  bitstream_buffer.id(),
                  size,
                  key_frame));