Motown: Delay audio track completion of Prime requests until a packet arrives.
The audio track currently does nothing about priming (which is fine) and
calls the callback immediately when a Prime request arrives (which is not
fine). Ideally, it would wait to call the callback until its queue had reached
its low-water mark. This CL gets partway there by waiting until the first
packet arrives. This doesn't necessarily address potential underflow issues,
but it does make sure we've seen at least one packet so we know how to set
the rate control transform.

R=kulakowski@chromium.org

Review URL: https://codereview.chromium.org/1836953002 .
diff --git a/services/media/audio/audio_pipe.cc b/services/media/audio/audio_pipe.cc
index 66ae598..eba55cd 100644
--- a/services/media/audio/audio_pipe.cc
+++ b/services/media/audio/audio_pipe.cc
@@ -131,6 +131,22 @@
                            std::move(regions),
                            start_pts,
                            next_pts_)));
+
+   if (!prime_callback_.is_null()) {
+     // Prime was requested. Call the callback to indicate priming is complete.
+     // TODO(dalesat): Don't do this until low water mark is reached.
+     prime_callback_.Run();
+     prime_callback_.reset();
+   }
+}
+
+void AudioPipe::OnPrimeRequested(const PrimeCallback& cbk) {
+  if (!prime_callback_.is_null()) {
+    // Prime was already requested. Complete the old one and warn.
+    LOG(WARNING) << "multiple prime requests received";
+    prime_callback_.Run();
+  }
+  prime_callback_ = cbk;
 }
 
 bool AudioPipe::OnFlushRequested(const FlushCallback& cbk) {
diff --git a/services/media/audio/audio_pipe.h b/services/media/audio/audio_pipe.h
index 4a2b4fb..d1fd7e2 100644
--- a/services/media/audio/audio_pipe.h
+++ b/services/media/audio/audio_pipe.h
@@ -81,6 +81,7 @@
 
  protected:
   void OnPacketReceived(MediaPacketStatePtr state) override;
+  void OnPrimeRequested(const PrimeCallback& cbk) override;
   bool OnFlushRequested(const FlushCallback& cbk) override;
 
  private:
@@ -90,6 +91,8 @@
   // State used for timestamp interpolation
   bool next_pts_known_ = 0;
   int64_t next_pts_;
+
+  PrimeCallback prime_callback_;
 };
 
 }  // namespace audio
diff --git a/services/media/common/media_pipe_base.cc b/services/media/common/media_pipe_base.cc
index 6c8e2c5..c5de4ac 100644
--- a/services/media/common/media_pipe_base.cc
+++ b/services/media/common/media_pipe_base.cc
@@ -137,7 +137,7 @@
 }
 
 void MediaPipeBase::Prime(const PrimeCallback& cbk) {
-  cbk.Run();
+  OnPrimeRequested(cbk);
 }
 
 void MediaPipeBase::Flush(const FlushCallback& cbk) {
diff --git a/services/media/common/media_pipe_base.h b/services/media/common/media_pipe_base.h
index 96c8afc..514e301 100644
--- a/services/media/common/media_pipe_base.h
+++ b/services/media/common/media_pipe_base.h
@@ -76,6 +76,7 @@
 
   // Interface to be implemented by derived classes
   virtual void OnPacketReceived(MediaPacketStatePtr state) = 0;
+  virtual void OnPrimeRequested(const PrimeCallback& cbk) = 0;
   virtual bool OnFlushRequested(const FlushCallback& cbk) = 0;
 
   MappedSharedBufferPtr buffer_;