Clean Up Playback Thread in DVR Test Implementation

Bug: 194476544
Test: atest VtsHalTvTunerV1_0TargetTest && atest
VtsHalTvTunerV1_1TargetTest

Change-Id: I639e8314a499c07758c4927fa10cb4ff0e6dcb50
diff --git a/tv/tuner/1.0/default/Dvr.cpp b/tv/tuner/1.0/default/Dvr.cpp
index c62b878..40879f2 100644
--- a/tv/tuner/1.0/default/Dvr.cpp
+++ b/tv/tuner/1.0/default/Dvr.cpp
@@ -37,7 +37,10 @@
     mDemux = demux;
 }
 
-Dvr::~Dvr() {}
+Dvr::~Dvr() {
+    // make sure thread has joined
+    close();
+}
 
 Return<void> Dvr::getQueueDesc(getQueueDesc_cb _hidl_cb) {
     ALOGV("%s", __FUNCTION__);
@@ -112,8 +115,7 @@
     }
 
     if (mType == DvrType::PLAYBACK) {
-        pthread_create(&mDvrThread, NULL, __threadLoopPlayback, this);
-        pthread_setname_np(mDvrThread, "playback_waiting_loop");
+        mDvrThread = std::thread(&Dvr::playbackThreadLoop, this);
     } else if (mType == DvrType::RECORD) {
         mRecordStatus = RecordStatus::DATA_READY;
         mDemux->setIsRecording(mType == DvrType::RECORD);
@@ -128,9 +130,11 @@
     ALOGV("%s", __FUNCTION__);
 
     mDvrThreadRunning = false;
-
-    lock_guard<mutex> lock(mDvrThreadLock);
-
+    if (mDvrThread.joinable()) {
+        mDvrThread.join();
+    }
+    // thread should always be joinable if it is running,
+    // so it should be safe to assume recording stopped.
     mDemux->setIsRecording(false);
 
     return Result::SUCCESS;
@@ -146,7 +150,7 @@
 
 Return<Result> Dvr::close() {
     ALOGV("%s", __FUNCTION__);
-
+    stop();
     return Result::SUCCESS;
 }
 
@@ -173,15 +177,8 @@
     return mDvrEventFlag;
 }
 
-void* Dvr::__threadLoopPlayback(void* user) {
-    Dvr* const self = static_cast<Dvr*>(user);
-    self->playbackThreadLoop();
-    return 0;
-}
-
 void Dvr::playbackThreadLoop() {
     ALOGD("[Dvr] playback threadLoop start.");
-    lock_guard<mutex> lock(mDvrThreadLock);
     mDvrThreadRunning = true;
 
     while (mDvrThreadRunning) {