Make Android MediaEncoder loop not run when there aren't any frames left.

This CL makes the loop stop when all frames have been delivered and
start again when a new frame is inserted.

BUG=webrtc:5680

Review-Url: https://codereview.webrtc.org/2000103002
Cr-Commit-Position: refs/heads/master@{#12860}
diff --git a/webrtc/api/java/jni/androidmediaencoder_jni.cc b/webrtc/api/java/jni/androidmediaencoder_jni.cc
index 6a1c581..8fbd594 100644
--- a/webrtc/api/java/jni/androidmediaencoder_jni.cc
+++ b/webrtc/api/java/jni/androidmediaencoder_jni.cc
@@ -226,6 +226,7 @@
   int current_encoding_time_ms_;  // Overall encoding time in the current second
   int64_t last_input_timestamp_ms_;  // Timestamp of last received yuv frame.
   int64_t last_output_timestamp_ms_;  // Timestamp of last encoded frame.
+  bool output_delivery_loop_running_; // Is the onMessage loop running
 
   struct InputFrameInfo {
     InputFrameInfo(int64_t encode_start_time,
@@ -306,6 +307,7 @@
     inited_(false),
     use_surface_(false),
     picture_id_(0),
+    output_delivery_loop_running_(false),
     egl_context_(egl_context) {
   ScopedLocalRefFrame local_ref_frame(jni);
   // It would be nice to avoid spinning up a new thread per MediaCodec, and
@@ -473,7 +475,13 @@
   // unclear how to signal such a failure to the app, so instead we stay silent
   // about it and let the next app-called API method reveal the borkedness.
   DeliverPendingOutputs(jni);
-  codec_thread_->PostDelayed(kMediaCodecPollMs, this);
+
+  // If there aren't more frames to deliver, we can stop the loop
+  if (!input_frame_infos_.empty()) {
+    codec_thread_->PostDelayed(kMediaCodecPollMs, this);
+  } else {
+    output_delivery_loop_running_ = false;
+  }
 }
 
 bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() {
@@ -587,7 +595,6 @@
   }
 
   inited_ = true;
-  codec_thread_->PostDelayed(kMediaCodecPollMs, this);
   return WEBRTC_VIDEO_CODEC_OK;
 }
 
@@ -733,6 +740,11 @@
 
   current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_;
 
+  if (!output_delivery_loop_running_) {
+    output_delivery_loop_running_ = true;
+    codec_thread_->PostDelayed(kMediaCodecPollMs, this);
+  }
+
   if (!DeliverPendingOutputs(jni)) {
     ALOGE << "Failed deliver pending outputs.";
     ResetCodecOnCodecThread();
@@ -852,6 +864,7 @@
   rtc::MessageQueueManager::Clear(this);
   inited_ = false;
   use_surface_ = false;
+  output_delivery_loop_running_ = false;
   ALOGD << "EncoderReleaseOnCodecThread done.";
   return WEBRTC_VIDEO_CODEC_OK;
 }
@@ -939,7 +952,7 @@
     int64_t frame_encoding_time_ms = 0;
     last_output_timestamp_ms_ =
         GetOutputBufferInfoPresentationTimestampUs(jni, j_output_buffer_info) /
-        1000;
+        rtc::kNumMicrosecsPerMillisec;
     if (!input_frame_infos_.empty()) {
       const InputFrameInfo& frame_info = input_frame_infos_.front();
       output_timestamp_ = frame_info.frame_timestamp;