Transcoder: Fix video track transcoding crash.
The VideoTrackTranscoder had a bug where the encoder could
outlive the transcoder object, because the encoder owns the
output buffers. This caused a crash when the encoder sent async
callbacks to the transcoder after it had been released. This fix
gives the encoder a weak reference to the transcoder and gives
outstanding buffers a strong reference to the encoder.
Fixes: 160711746
Test: Transcoder unit tests (build_and_run_all_unit_tests.sh).
Test: New unit test to trigger the crash before the fix.
Change-Id: I8141591399b6cff642d2d322809d3254adbefaaf
diff --git a/media/libmediatranscoding/transcoder/MediaTranscoder.cpp b/media/libmediatranscoding/transcoder/MediaTranscoder.cpp
index bde1cf6..999e226 100644
--- a/media/libmediatranscoding/transcoder/MediaTranscoder.cpp
+++ b/media/libmediatranscoding/transcoder/MediaTranscoder.cpp
@@ -224,11 +224,11 @@
return AMEDIA_ERROR_INVALID_PARAMETER;
}
- std::unique_ptr<MediaTrackTranscoder> transcoder = nullptr;
- std::shared_ptr<AMediaFormat> format = nullptr;
+ std::shared_ptr<MediaTrackTranscoder> transcoder;
+ std::shared_ptr<AMediaFormat> format;
if (trackFormat == nullptr) {
- transcoder = std::make_unique<PassthroughTrackTranscoder>(shared_from_this());
+ transcoder = std::make_shared<PassthroughTrackTranscoder>(shared_from_this());
} else {
const char* srcMime = nullptr;
if (!AMediaFormat_getString(mSourceTrackFormats[trackIndex].get(), AMEDIAFORMAT_KEY_MIME,
@@ -253,7 +253,7 @@
}
}
- transcoder = std::make_unique<VideoTrackTranscoder>(shared_from_this());
+ transcoder = VideoTrackTranscoder::create(shared_from_this());
AMediaFormat* mergedFormat =
mergeMediaFormats(mSourceTrackFormats[trackIndex].get(), trackFormat);