libmedia: offloaded playback support

- start() returns a status so that upper layers can
recreate a non offloaded track in case of error.

- Added states to handle offloaded tracks specific:
  - waiting for stream end (drain) notification by
audio flinger
  - allow pause while waiting for stream end notification

- getPosition() queries the render position directly from
audio HAL.

- disable APIs not applicable to offloaded tracks

- Modified track restoring behavior for invalidated
offloaded tracks: just send the callback and wait for
upper layers to create a new track.

- Added wait for stream end management in audio track client
proxy. Similar to obtainBuffer and should be factored in.

Change-Id: I0fc48117946364cb255afd653195498891f622bd
Signed-off-by: Eric Laurent <elaurent@google.com>
diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h
index 58e0deb..da13a7f 100644
--- a/include/media/AudioTrack.h
+++ b/include/media/AudioTrack.h
@@ -277,7 +277,7 @@
      * make it active. If set, the callback will start being called.
      * If the track was previously paused, volume is ramped up over the first mix buffer.
      */
-            void        start();
+            status_t        start();
 
     /* Stop a track.
      * In static buffer mode, the track is stopped immediately.
@@ -635,11 +635,12 @@
             void setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount);
             audio_io_handle_t getOutput_l();
 
-            status_t getPosition_l(uint32_t *position);
-
             // FIXME enum is faster than strcmp() for parameter 'from'
             status_t restoreTrack_l(const char *from);
 
+            bool     isOffloaded() const
+                { return (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; }
+
     // may be changed if IAudioTrack is re-created
     sp<IAudioTrack>         mAudioTrack;
     sp<IMemory>             mCblkMemory;
@@ -676,7 +677,9 @@
         STATE_ACTIVE,
         STATE_STOPPED,
         STATE_PAUSED,
+        STATE_PAUSED_STOPPING,
         STATE_FLUSHED,
+        STATE_STOPPING,
     }                       mState;
 
     callback_t              mCbf;                   // callback handler for events, or NULL
@@ -694,7 +697,7 @@
     // These are private to processAudioBuffer(), and are not protected by a lock
     uint32_t                mRemainingFrames;       // number of frames to request in obtainBuffer()
     bool                    mRetryOnPartialBuffer;  // sleep and retry after partial obtainBuffer()
-    int                     mObservedSequence;      // last observed value of mSequence
+    uint32_t                mObservedSequence;      // last observed value of mSequence
 
     sp<IMemory>             mSharedBuffer;
     uint32_t                mLoopPeriod;            // in frames, zero means looping is disabled
@@ -736,6 +739,7 @@
 
     sp<DeathNotifier>       mDeathNotifier;
     uint32_t                mSequence;              // incremented for each new IAudioTrack attempt
+    audio_io_handle_t       mOutput;                // cached output io handle
 };
 
 class TimedAudioTrack : public AudioTrack
diff --git a/include/private/media/AudioTrackShared.h b/include/private/media/AudioTrackShared.h
index 6129c80..b890180 100644
--- a/include/private/media/AudioTrackShared.h
+++ b/include/private/media/AudioTrackShared.h
@@ -296,6 +296,7 @@
 
     bool        getStreamEndDone() const;
 
+    status_t    waitStreamEndDone(const struct timespec *requested);
 };
 
 class StaticAudioTrackClientProxy : public AudioTrackClientProxy {
@@ -379,8 +380,8 @@
 
 protected:
     size_t      mAvailToClient; // estimated frames available to client prior to releaseBuffer()
-private:
     int32_t     mFlush;         // our copy of cblk->u.mStreaming.mFlush, for streaming output only
+private:
     bool        mDeferWake;     // whether another releaseBuffer() is expected soon
 };