AudioTrack: Enforce worker thread join in dtor

Ensure that the AudioTrackThread is joined in dtor. In some
cases if the dtor was called while the worker thread was in
restoreTrack_l() (which had an error due to audioserver restart)
the mStatus check incorrectly prevented join.

Test: atest CtsMediaAudioTestCases
Test: YT playback and killing audioserver
Test: atest test_callback_race
Bug: 333897807
Merged-In: I144710cb9865d45508d2f97402f748dad7e63a07
Change-Id: I144710cb9865d45508d2f97402f748dad7e63a07
diff --git a/media/libaudioclient/AudioTrack.cpp b/media/libaudioclient/AudioTrack.cpp
index f3f662f..6772201 100644
--- a/media/libaudioclient/AudioTrack.cpp
+++ b/media/libaudioclient/AudioTrack.cpp
@@ -236,7 +236,6 @@
 AudioTrack::AudioTrack(const AttributionSourceState& attributionSource)
     : mClientAttributionSource(attributionSource)
 {
-
 }
 
 AudioTrack::AudioTrack(
@@ -257,7 +256,6 @@
         float maxRequiredSpeed,
         audio_port_handle_t selectedDeviceId)
 {
-    // make_unique does not aggregate init until c++20
     mSetParams = std::make_unique<SetParams>(
         streamType, sampleRate, format, channelMask, frameCount, flags, callback,
         notificationFrames, nullptr /*sharedBuffer*/, false /*threadCanCallJava*/,
@@ -377,9 +375,6 @@
 }
 
 void AudioTrack::stopAndJoinCallbacks() {
-    // Prevent nullptr crash if it did not open properly.
-    if (mStatus != NO_ERROR) return;
-
     // Make sure that callback function exits in the case where
     // it is looping on buffer full condition in obtainBuffer().
     // Otherwise the callback thread will never exit.
@@ -896,6 +891,7 @@
     const int64_t beginNs = systemTime();
 
     AutoMutex lock(mLock);
+    if (mProxy == nullptr) return;  // not successfully initialized.
     mediametrics::Defer defer([&]() {
         mediametrics::LogItem(mMetricsId)
             .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)