Refine tuner aidl hal threads.
Bug: 197763854
Test: VtsHalTvTunerTargetTest
Test: atest android.media.tv.tuner.cts
Test: sampletunertvinput
Change-Id: Id707438178ed93731919f0155cab805436147f86
diff --git a/tv/tuner/aidl/default/Demux.cpp b/tv/tuner/aidl/default/Demux.cpp
index 3937c6a..8e83d06 100644
--- a/tv/tuner/aidl/default/Demux.cpp
+++ b/tv/tuner/aidl/default/Demux.cpp
@@ -37,8 +37,7 @@
}
Demux::~Demux() {
- mFrontendInputThreadRunning = false;
- std::lock_guard<std::mutex> lock(mFrontendInputThreadLock);
+ close();
}
::ndk::ScopedAStatus Demux::setFrontendDataSource(int32_t in_frontendId) {
@@ -171,6 +170,8 @@
::ndk::ScopedAStatus Demux::close() {
ALOGV("%s", __FUNCTION__);
+ stopFrontendInput();
+
set<int64_t>::iterator it;
for (it = mPlaybackFilterIds.begin(); it != mPlaybackFilterIds.end(); it++) {
mDvrPlayback->removePlaybackFilter(*it);
@@ -180,8 +181,6 @@
mFilters.clear();
mLastUsedFilterId = -1;
mTuner->removeDemux(mDemuxId);
- mFrontendInputThreadRunning = false;
- std::lock_guard<std::mutex> lock(mFrontendInputThreadLock);
return ::ndk::ScopedAStatus::ok();
}
@@ -345,14 +344,7 @@
void Demux::startFrontendInputLoop() {
mFrontendInputThreadRunning = true;
- pthread_create(&mFrontendInputThread, NULL, __threadLoopFrontend, this);
- pthread_setname_np(mFrontendInputThread, "frontend_input_thread");
-}
-
-void* Demux::__threadLoopFrontend(void* user) {
- Demux* const self = static_cast<Demux*>(user);
- self->frontendInputThreadLoop();
- return 0;
+ mFrontendInputThread = std::thread(&Demux::frontendInputThreadLoop, this);
}
void Demux::frontendInputThreadLoop() {
@@ -360,7 +352,6 @@
return;
}
- std::lock_guard<std::mutex> lock(mFrontendInputThreadLock);
if (!mDvrPlayback) {
ALOGW("[Demux] No software Frontend input configured. Ending Frontend thread loop.");
mFrontendInputThreadRunning = false;
@@ -402,7 +393,9 @@
ALOGD("[Demux] stop frontend on demux");
mKeepFetchingDataFromFrontend = false;
mFrontendInputThreadRunning = false;
- std::lock_guard<std::mutex> lock(mFrontendInputThreadLock);
+ if (mFrontendInputThread.joinable()) {
+ mFrontendInputThread.join();
+ }
}
void Demux::setIsRecording(bool isRecording) {
diff --git a/tv/tuner/aidl/default/Demux.h b/tv/tuner/aidl/default/Demux.h
index 4d9b7fe..1b789bd 100644
--- a/tv/tuner/aidl/default/Demux.h
+++ b/tv/tuner/aidl/default/Demux.h
@@ -20,7 +20,10 @@
#include <fmq/AidlMessageQueue.h>
#include <math.h>
+#include <atomic>
#include <set>
+#include <thread>
+
#include "Dvr.h"
#include "Filter.h"
#include "Frontend.h"
@@ -155,12 +158,14 @@
std::shared_ptr<Dvr> mDvrRecord;
// Thread handlers
- pthread_t mFrontendInputThread;
+ std::thread mFrontendInputThread;
+
/**
* If a specific filter's writing loop is still running
*/
- bool mFrontendInputThreadRunning;
- bool mKeepFetchingDataFromFrontend;
+ std::atomic<bool> mFrontendInputThreadRunning;
+ std::atomic<bool> mKeepFetchingDataFromFrontend;
+
/**
* If the dvr recording is running.
*/
@@ -169,10 +174,6 @@
* Lock to protect writes to the FMQs
*/
std::mutex mWriteLock;
- /**
- * Lock to protect writes to the input status
- */
- std::mutex mFrontendInputThreadLock;
// temp handle single PES filter
// TODO handle mulptiple Pes filters
diff --git a/tv/tuner/aidl/default/Dvr.h b/tv/tuner/aidl/default/Dvr.h
index 586f885..ad8728e 100644
--- a/tv/tuner/aidl/default/Dvr.h
+++ b/tv/tuner/aidl/default/Dvr.h
@@ -129,7 +129,7 @@
* If a specific filter's writing loop is still running
*/
std::atomic<bool> mDvrThreadRunning;
- bool mKeepFetchingDataFromFrontend;
+
/**
* Lock to protect writes to the FMQs
*/
diff --git a/tv/tuner/aidl/default/Filter.cpp b/tv/tuner/aidl/default/Filter.cpp
index 9755e39..bf89d12 100644
--- a/tv/tuner/aidl/default/Filter.cpp
+++ b/tv/tuner/aidl/default/Filter.cpp
@@ -85,8 +85,7 @@
}
Filter::~Filter() {
- mFilterThreadRunning = false;
- std::lock_guard<std::mutex> lock(mFilterThreadLock);
+ close();
}
::ndk::ScopedAStatus Filter::getId64Bit(int64_t* _aidl_return) {
@@ -187,8 +186,12 @@
::ndk::ScopedAStatus Filter::stop() {
ALOGV("%s", __FUNCTION__);
+
mFilterThreadRunning = false;
- std::lock_guard<std::mutex> lock(mFilterThreadLock);
+ if (mFilterThread.joinable()) {
+ mFilterThread.join();
+ }
+
return ::ndk::ScopedAStatus::ok();
}
@@ -226,8 +229,8 @@
::ndk::ScopedAStatus Filter::close() {
ALOGV("%s", __FUNCTION__);
- mFilterThreadRunning = false;
- std::lock_guard<std::mutex> lock(mFilterThreadLock);
+ stop();
+
return mDemux->removeFilter(mFilterId);
}
@@ -376,22 +379,15 @@
}
::ndk::ScopedAStatus Filter::startFilterLoop() {
- pthread_create(&mFilterThread, NULL, __threadLoopFilter, this);
- pthread_setname_np(mFilterThread, "filter_waiting_loop");
+ mFilterThread = std::thread(&Filter::filterThreadLoop, this);
return ::ndk::ScopedAStatus::ok();
}
-void* Filter::__threadLoopFilter(void* user) {
- Filter* const self = static_cast<Filter*>(user);
- self->filterThreadLoop();
- return 0;
-}
-
void Filter::filterThreadLoop() {
if (!mFilterThreadRunning) {
return;
}
- std::lock_guard<std::mutex> lock(mFilterThreadLock);
+
ALOGD("[Filter] filter %" PRIu64 " threadLoop start.", mFilterId);
// For the first time of filter output, implementation needs to send the filter
diff --git a/tv/tuner/aidl/default/Filter.h b/tv/tuner/aidl/default/Filter.h
index 3f40256..2ca25af 100644
--- a/tv/tuner/aidl/default/Filter.h
+++ b/tv/tuner/aidl/default/Filter.h
@@ -24,7 +24,10 @@
#include <ion/ion.h>
#include <math.h>
#include <sys/stat.h>
+#include <atomic>
#include <set>
+#include <thread>
+
#include "Demux.h"
#include "Dvr.h"
#include "Frontend.h"
@@ -126,15 +129,14 @@
vector<DemuxFilterEvent> mFilterEvents;
// Thread handlers
- pthread_t mFilterThread;
+ std::thread mFilterThread;
// FMQ status local records
DemuxFilterStatus mFilterStatus;
/**
* If a specific filter's writing loop is still running
*/
- bool mFilterThreadRunning;
- bool mKeepFetchingDataFromFrontend;
+ std::atomic<bool> mFilterThreadRunning;
/**
* How many times a filter should write
@@ -204,7 +206,6 @@
* Lock to protect writes to the input status
*/
std::mutex mFilterStatusLock;
- std::mutex mFilterThreadLock;
std::mutex mFilterOutputLock;
std::mutex mRecordFilterOutputLock;