Skip to content

Commit

Permalink
Resolve the memory leaks in demo short-form app
Browse files Browse the repository at this point in the history
Issue: #1839
PiperOrigin-RevId: 696080063
(cherry picked from commit c3d4722)
  • Loading branch information
tianyif authored and icbaker committed Nov 19, 2024
1 parent a46716c commit 57d0721
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
per sample.
* Fix playback hanging on DASH multi-period streams when CEA-608 subtitles
are enabled ([#1863](https://github.com/androidx/media/issues/1863)).
* Demo app
* Resolve the memory leaks in demo short-form app
([#1839](https://github.com/androidx/media/issues/1839)).

## 1.5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.viewpager2.widget.ViewPager2

class ViewPagerActivity : AppCompatActivity() {
private lateinit var viewPagerView: ViewPager2
private lateinit var adapter: ViewPagerMediaAdapter
private lateinit var onPageChangeCallback: ViewPager2.OnPageChangeCallback
private var numberOfPlayers = 3
private var mediaItemDatabase = MediaItemDatabase()

Expand All @@ -40,23 +40,24 @@ class ViewPagerActivity : AppCompatActivity() {
Log.d(TAG, "Using a pool of $numberOfPlayers players")
viewPagerView = findViewById(R.id.viewPager)
viewPagerView.offscreenPageLimit = 1
viewPagerView.registerOnPageChangeCallback(
object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
adapter.onPageSelected(position)
}
}
)
}

override fun onStart() {
super.onStart()
adapter = ViewPagerMediaAdapter(mediaItemDatabase, numberOfPlayers, this)
val adapter = ViewPagerMediaAdapter(mediaItemDatabase, numberOfPlayers, applicationContext)
viewPagerView.adapter = adapter
onPageChangeCallback =
object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
adapter.onPageSelected(position)
}
}
viewPagerView.registerOnPageChangeCallback(onPageChangeCallback)
}

override fun onStop() {
adapter.onDestroy()
viewPagerView.unregisterOnPageChangeCallback(onPageChangeCallback)
viewPagerView.adapter = null
super.onStop()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ViewPagerMediaAdapter(
private val currentMediaItemsAndIndexes: ArrayDeque<Pair<MediaItem, Int>> = ArrayDeque()
private var playerPool: PlayerPool
private val holderMap: MutableMap<Int, ViewPagerMediaHolder>
private var currentPlayingIndex: Int = C.INDEX_UNSET
private val preloadControl: DefaultPreloadControl

companion object {
private const val TAG = "ViewPagerMediaAdapter"
Expand All @@ -65,8 +65,10 @@ class ViewPagerMediaAdapter(
)
.setPrioritizeTimeOverSizeThresholds(true)
.build()
preloadControl = DefaultPreloadControl()
val preloadManagerBuilder =
DefaultPreloadManager.Builder(context, DefaultPreloadControl()).setLoadControl(loadControl)
DefaultPreloadManager.Builder(context.applicationContext, preloadControl)
.setLoadControl(loadControl)
playerPool = PlayerPool(numberOfPlayers, preloadManagerBuilder)
holderMap = mutableMapOf()
preloadManager = preloadManagerBuilder.build()
Expand All @@ -76,6 +78,13 @@ class ViewPagerMediaAdapter(
preloadManager.invalidate()
}

override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
playerPool.destroyPlayers()
preloadManager.release()
holderMap.clear()
super.onDetachedFromRecyclerView(recyclerView)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewPagerMediaHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.media_item_view_pager, parent, false)
Expand Down Expand Up @@ -128,14 +137,9 @@ class ViewPagerMediaAdapter(
return Int.MAX_VALUE
}

fun onDestroy() {
playerPool.destroyPlayers()
preloadManager.release()
}

fun onPageSelected(position: Int) {
currentPlayingIndex = position
holderMap[position]?.playIfPossible()
preloadControl.currentPlayingIndex = position
preloadManager.setCurrentPlayingIndex(position)
preloadManager.invalidate()
}
Expand Down Expand Up @@ -168,7 +172,9 @@ class ViewPagerMediaAdapter(
preloadManager.remove(itemAndIndex.first)
}

inner class DefaultPreloadControl : TargetPreloadStatusControl<Int> {
inner class DefaultPreloadControl(var currentPlayingIndex: Int = C.INDEX_UNSET) :
TargetPreloadStatusControl<Int> {

override fun getTargetPreloadStatus(rankingData: Int): DefaultPreloadManager.Status? {
if (abs(rankingData - currentPlayingIndex) == 2) {
return DefaultPreloadManager.Status(STAGE_LOADED_FOR_DURATION_MS, 500L)
Expand Down

0 comments on commit 57d0721

Please sign in to comment.