Clean up error handling in createTrack and openRecord

Outside callers now use initCheck() to determine whether a TrackBase
has been created successfully, instead of relying on internal knowledge.

Previously, callers needed to know that a TrackBase was only valid if it's
getCbk() != 0.  For a Track (playback), they needed to know to also check
the track's name (track index).  Now, outsiders can just call initCheck().

Other changes:
 - Return a 0 reference if track creation fails
 - Remove a dead line of code in AudioFlinger::openRecord

Change-Id: If374924a3f6fd27906f625aa83dd0a1e3f506e00
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 2c6d3d9..1bac09e 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -1271,7 +1271,6 @@
 
     // return to handle to client
     recordHandle = new RecordHandle(recordTrack);
-    lStatus = NO_ERROR;
 
 Exit:
     *status = lStatus;
diff --git a/services/audioflinger/PlaybackTracks.h b/services/audioflinger/PlaybackTracks.h
index 5600411c..43fb946 100644
--- a/services/audioflinger/PlaybackTracks.h
+++ b/services/audioflinger/PlaybackTracks.h
@@ -33,6 +33,7 @@
                                 int sessionId,
                                 IAudioFlinger::track_flags_t flags);
     virtual             ~Track();
+    virtual status_t    initCheck() const;
 
     static  void        appendDumpHeader(String8& result);
             void        dump(char* buffer, size_t size);
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 507ff9f..0da7c61 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -1248,8 +1248,12 @@
             track = TimedTrack::create(this, client, streamType, sampleRate, format,
                     channelMask, frameCount, sharedBuffer, sessionId);
         }
-        if (track == 0 || track->getCblk() == 0 || track->name() < 0) {
-            lStatus = NO_MEMORY;
+
+        // new Track always returns non-NULL,
+        // but TimedTrack::create() is a factory that could fail by returning NULL
+        lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY;
+        if (lStatus != NO_ERROR) {
+            track.clear();
             goto Exit;
         }
 
@@ -4497,8 +4501,9 @@
         track = new RecordTrack(this, client, sampleRate,
                       format, channelMask, frameCount, sessionId);
 
-        if (track->getCblk() == 0) {
-            lStatus = NO_MEMORY;
+        lStatus = track->initCheck();
+        if (lStatus != NO_ERROR) {
+            track.clear();
             goto Exit;
         }
         mTracks.add(track);
diff --git a/services/audioflinger/TrackBase.h b/services/audioflinger/TrackBase.h
index 7365ea2..00a91b7 100644
--- a/services/audioflinger/TrackBase.h
+++ b/services/audioflinger/TrackBase.h
@@ -47,6 +47,7 @@
                                 int sessionId,
                                 bool isOut);
     virtual             ~TrackBase();
+    virtual status_t    initCheck() const { return getCblk() != 0 ? NO_ERROR : NO_MEMORY; }
 
     virtual status_t    start(AudioSystem::sync_event_t event,
                              int triggerSession) = 0;
diff --git a/services/audioflinger/Tracks.cpp b/services/audioflinger/Tracks.cpp
index 6ea6f2f..6039078 100644
--- a/services/audioflinger/Tracks.cpp
+++ b/services/audioflinger/Tracks.cpp
@@ -361,6 +361,15 @@
     ALOGV("PlaybackThread::Track destructor");
 }
 
+status_t AudioFlinger::PlaybackThread::Track::initCheck() const
+{
+    status_t status = TrackBase::initCheck();
+    if (status == NO_ERROR && mName < 0) {
+        status = NO_MEMORY;
+    }
+    return status;
+}
+
 void AudioFlinger::PlaybackThread::Track::destroy()
 {
     // NOTE: destroyTrack_l() can remove a strong reference to this Track