transcoding: add watchdog to prevent transcoder hang
Add a watchdog to monitor transcoder progress. Make transcoder
report heart beat regularly as long as there is new progress.
If heartbeat stops, watchdog will initiate a timeout to
1) Abandon old TranscoderWrapper. We try to shut it down nicely,
however, if it's really stuck, we'll have to leave it there.
2) Instantiate a new TranscoderWrapper with new looper.
3) Report Watchdog timeout to client.
Tests:
- New unit tests to MediaTranscoder, TranscodingSessionController
and MediaTranscodingService's simulated test (for error code reporting).
- Manually tested that long recording works properly without timeout.
bug: 169453212
Change-Id: Iae89e49e8e12d6078dc49eef2960efd03e91c431
diff --git a/media/libmediatranscoding/transcoder/tests/MediaSampleWriterTests.cpp b/media/libmediatranscoding/transcoder/tests/MediaSampleWriterTests.cpp
index 0a41b00..8b3905c 100644
--- a/media/libmediatranscoding/transcoder/tests/MediaSampleWriterTests.cpp
+++ b/media/libmediatranscoding/transcoder/tests/MediaSampleWriterTests.cpp
@@ -210,6 +210,8 @@
mLastProgress = progress;
mProgressUpdateCount++;
}
+
+ virtual void onHeartBeat(const MediaSampleWriter* writer __unused) override {}
// ~MediaSampleWriter::CallbackInterface
void waitForWritingFinished() {
diff --git a/media/libmediatranscoding/transcoder/tests/MediaTranscoderTests.cpp b/media/libmediatranscoding/transcoder/tests/MediaTranscoderTests.cpp
index 54d8b89..4e33ec3 100644
--- a/media/libmediatranscoding/transcoder/tests/MediaTranscoderTests.cpp
+++ b/media/libmediatranscoding/transcoder/tests/MediaTranscoderTests.cpp
@@ -81,6 +81,11 @@
}
}
+ virtual void onHeartBeat(const MediaTranscoder* transcoder __unused) override {
+ std::unique_lock<std::mutex> lock(mMutex);
+ mHeartBeatCount++;
+ }
+
virtual void onCodecResourceLost(const MediaTranscoder* transcoder __unused,
const std::shared_ptr<ndk::ScopedAParcel>& pausedState
__unused) override {}
@@ -100,6 +105,7 @@
}
media_status_t mStatus = AMEDIA_OK;
bool mFinished = false;
+ int32_t mHeartBeatCount = 0;
private:
std::mutex mMutex;
@@ -143,6 +149,7 @@
typedef enum {
kRunToCompletion,
+ kCheckHeartBeat,
kCancelAfterProgress,
kCancelAfterStart,
kPauseAfterProgress,
@@ -152,8 +159,9 @@
using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>;
media_status_t transcodeHelper(const char* srcPath, const char* destPath,
FormatConfigurationCallback formatCallback,
- TranscodeExecutionControl executionControl = kRunToCompletion) {
- auto transcoder = MediaTranscoder::create(mCallbacks);
+ TranscodeExecutionControl executionControl = kRunToCompletion,
+ int64_t heartBeatIntervalUs = -1) {
+ auto transcoder = MediaTranscoder::create(mCallbacks, heartBeatIntervalUs);
EXPECT_NE(transcoder, nullptr);
const int srcFd = open(srcPath, O_RDONLY);
@@ -200,6 +208,18 @@
case kPauseAfterStart:
transcoder->pause(&pausedState);
break;
+ case kCheckHeartBeat: {
+ mCallbacks->waitForProgressMade();
+ auto startTime = std::chrono::system_clock::now();
+ mCallbacks->waitForTranscodingFinished();
+ auto finishTime = std::chrono::system_clock::now();
+ int32_t expectedCount =
+ (finishTime - startTime) / std::chrono::microseconds(heartBeatIntervalUs);
+ // Here we relax the expected count by 1, in case the last heart-beat just
+ // missed the window, other than that the count should be exact.
+ EXPECT_GE(mCallbacks->mHeartBeatCount, expectedCount - 1);
+ break;
+ }
case kRunToCompletion:
default:
mCallbacks->waitForTranscodingFinished();
@@ -430,6 +450,18 @@
}
}
+TEST_F(MediaTranscoderTests, TestHeartBeat) {
+ const char* srcPath = "/data/local/tmp/TranscodingTestAssets/longtest_15s.mp4";
+ const char* destPath = "/data/local/tmp/MediaTranscoder_HeartBeat.MP4";
+
+ // Use a shorter value of 500ms than the default 1000ms to get more heart beat for testing.
+ const int64_t heartBeatIntervalUs = 500000LL;
+ EXPECT_EQ(transcodeHelper(srcPath, destPath, getAVCVideoFormat, kCheckHeartBeat,
+ heartBeatIntervalUs),
+ AMEDIA_OK);
+ EXPECT_TRUE(mCallbacks->mFinished);
+}
+
} // namespace android
int main(int argc, char** argv) {
diff --git a/media/libmediatranscoding/transcoder/tests/fuzzer/media_transcoder_fuzzer.cpp b/media/libmediatranscoding/transcoder/tests/fuzzer/media_transcoder_fuzzer.cpp
index 48d3406..ec36c0f 100644
--- a/media/libmediatranscoding/transcoder/tests/fuzzer/media_transcoder_fuzzer.cpp
+++ b/media/libmediatranscoding/transcoder/tests/fuzzer/media_transcoder_fuzzer.cpp
@@ -88,6 +88,8 @@
}
}
+ virtual void onHeartBeat(const MediaTranscoder* transcoder UNUSED_PARAM) override {}
+
virtual void onCodecResourceLost(const MediaTranscoder* transcoder UNUSED_PARAM,
const shared_ptr<ndk::ScopedAParcel>& pausedState
UNUSED_PARAM) override {}