Hold the AAHDecoderPump ThreadWrapper in a ref-counting pointer

Change-Id: Iff840dbd9e1f79a07e62c1481b2b0797f30247cb
diff --git a/media/libaah_rtp/aah_decoder_pump.cpp b/media/libaah_rtp/aah_decoder_pump.cpp
index cde9be4..b2efbe6 100644
--- a/media/libaah_rtp/aah_decoder_pump.cpp
+++ b/media/libaah_rtp/aah_decoder_pump.cpp
@@ -37,19 +37,28 @@
 
 AAH_DecoderPump::AAH_DecoderPump(OMXClient& omx)
     : omx_(omx)
-    , thread_(this)
     , thread_status_(OK)
     , renderer_(NULL)
     , last_queued_pts_valid_(false)
     , last_queued_pts_(0)
     , last_ts_transform_valid_(false)
     , last_volume_(0xFF) {
+    thread_ = new ThreadWrapper(this);
 }
 
 AAH_DecoderPump::~AAH_DecoderPump() {
     shutdown();
 }
 
+status_t AAH_DecoderPump::initCheck() {
+    if (thread_ == NULL) {
+        LOGE("Failed to allocate thread");
+        return NO_MEMORY;
+    }
+
+    return OK;
+}
+
 status_t AAH_DecoderPump::queueForDecode(MediaBuffer* buf) {
     if (NULL == buf) {
         return BAD_VALUE;
@@ -281,7 +290,7 @@
         return NULL;
     }
 
-    while (!thread_.exitPending()) {
+    while (!thread_->exitPending()) {
         status_t res;
         MediaBuffer* bufOut = NULL;
 
@@ -365,7 +374,7 @@
 
     // Fire up the pump thread.  It will take care of starting and stopping the
     // decoder.
-    ret_val = thread_.run("aah_decode_pump", ANDROID_PRIORITY_AUDIO);
+    ret_val = thread_->run("aah_decode_pump", ANDROID_PRIORITY_AUDIO);
     if (OK != ret_val) {
         LOGE("Failed to start work thread in %s (res = %d)",
                 __PRETTY_FUNCTION__, ret_val);
@@ -387,9 +396,9 @@
 }
 
 status_t AAH_DecoderPump::shutdown_l() {
-    thread_.requestExit();
+    thread_->requestExit();
     thread_cond_.signal();
-    thread_.requestExitAndWait();
+    thread_->requestExitAndWait();
 
     MBQueue::iterator I;
     for (I = in_queue_.begin(); I != in_queue_.end(); ++I) {
@@ -417,12 +426,12 @@
 
     // While its not time to shut down, and we have no data to process, wait.
     AutoMutex lock(&thread_lock_);
-    while (!thread_.exitPending() && in_queue_.empty())
+    while (!thread_->exitPending() && in_queue_.empty())
         thread_cond_.wait(thread_lock_);
 
     // At this point, if its not time to shutdown then we must have something to
     // process.  Go ahead and pop the front of the queue for processing.
-    if (!thread_.exitPending()) {
+    if (!thread_->exitPending()) {
         CHECK(!in_queue_.empty());
 
         *buffer = *(in_queue_.begin());
diff --git a/media/libaah_rtp/aah_decoder_pump.h b/media/libaah_rtp/aah_decoder_pump.h
index f5e7d03..c9b35cd 100644
--- a/media/libaah_rtp/aah_decoder_pump.h
+++ b/media/libaah_rtp/aah_decoder_pump.h
@@ -33,6 +33,7 @@
 class AAH_DecoderPump : public MediaSource {
   public:
     explicit AAH_DecoderPump(OMXClient& omx);
+    status_t initCheck();
 
     status_t queueForDecode(MediaBuffer* buf);
 
@@ -79,7 +80,7 @@
     OMXClient&          omx_;
     Mutex               init_lock_;
 
-    ThreadWrapper       thread_;
+    sp<ThreadWrapper>   thread_;
     Condition           thread_cond_;
     Mutex               thread_lock_;
     status_t            thread_status_;
diff --git a/media/libaah_rtp/aah_rx_player_substream.cpp b/media/libaah_rtp/aah_rx_player_substream.cpp
index b015fc9..708ea36 100644
--- a/media/libaah_rtp/aah_rx_player_substream.cpp
+++ b/media/libaah_rtp/aah_rx_player_substream.cpp
@@ -42,6 +42,9 @@
     if (decoder_ == NULL) {
         LOGE("%s failed to allocate decoder pump!", __PRETTY_FUNCTION__);
     }
+    if (OK != decoder_->initCheck()) {
+        LOGE("%s failed to initialize decoder pump!", __PRETTY_FUNCTION__);
+    }
 
     // cleanupBufferInProgress will reset most of the internal state variables.
     // Just need to make sure that buffer_in_progress_ is NULL before calling.