forked from deepmedia/Transcoder
-
Notifications
You must be signed in to change notification settings - Fork 2
single thumbnailer instance #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
lib/src/main/java/com/otaliastudios/transcoder/internal/CustomSegments.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| @file:Suppress("ReturnCount") | ||
|
|
||
| package com.otaliastudios.transcoder.internal | ||
|
|
||
| import android.media.MediaFormat | ||
| import com.otaliastudios.transcoder.common.TrackStatus | ||
| import com.otaliastudios.transcoder.common.TrackType | ||
| import com.otaliastudios.transcoder.internal.pipeline.Pipeline | ||
| import com.otaliastudios.transcoder.internal.utils.Logger | ||
| import com.otaliastudios.transcoder.internal.utils.mutableTrackMapOf | ||
| import com.otaliastudios.transcoder.source.DataSource | ||
|
|
||
| class CustomSegments( | ||
| private val sources: DataSources, | ||
| private val tracks: Tracks, | ||
| private val factory: (TrackType, Int, TrackStatus, MediaFormat) -> Pipeline, | ||
| ) { | ||
|
|
||
| private val log = Logger("Segments") | ||
| private var currentSegment: Segment? = null | ||
| val currentIndex = mutableTrackMapOf(-1, -1) | ||
| private val segmentMap = mutableMapOf<String, Segment?>() | ||
|
|
||
| fun hasNext(type: TrackType): Boolean { | ||
| if (!sources.has(type)) return false | ||
| log.v( | ||
| "hasNext($type): segment=${currentSegment} lastIndex=${sources.getOrNull(type)?.lastIndex}" + | ||
| " canAdvance=${currentSegment?.canAdvance()}" | ||
| ) | ||
| val segment = currentSegment ?: return true // not started | ||
| val lastIndex = sources.getOrNull(type)?.lastIndex ?: return false // no track! | ||
| return segment.canAdvance() || segment.index < lastIndex | ||
| } | ||
|
|
||
| fun hasNext() = hasNext(TrackType.VIDEO) | ||
|
|
||
|
|
||
| // it will be time dependent | ||
| // 1. make segments work for thumbnails as is | ||
| // 2. inject segments dynamically | ||
| // 3. seek to segment and destroy previous ones | ||
| // 4. destroy only if necessary, else reuse | ||
|
|
||
| fun getSegment(id: String): Segment? { | ||
| return segmentMap.getOrPut(id) { | ||
| destroySegment(id) | ||
| tryCreateSegment(id).also { | ||
| currentSegment = it | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun release() = destroySegment() | ||
|
|
||
| private fun tryCreateSegment(id: String): Segment? { | ||
| val index = sources[TrackType.VIDEO].indexOfFirst { it.mediaId() == id } | ||
| // Return null if out of bounds, either because segments are over or because the | ||
| // source set does not have sources for this track type. | ||
| val source = sources[TrackType.VIDEO].getOrNull(index) ?: return null | ||
| source.init() | ||
| log.i("tryCreateSegment(${TrackType.VIDEO}, $index): created!") | ||
| if (tracks.active.has(TrackType.VIDEO)) { | ||
| source.selectTrack(TrackType.VIDEO) | ||
| } | ||
| // Update current index before pipeline creation, for other components | ||
| // who check it during pipeline init. | ||
| currentIndex[TrackType.VIDEO] = index | ||
| val pipeline = factory( | ||
| TrackType.VIDEO, | ||
| index, | ||
| tracks.all[TrackType.VIDEO], | ||
| tracks.outputFormats[TrackType.VIDEO] | ||
| ) | ||
| return Segment(TrackType.VIDEO, index, pipeline) | ||
| } | ||
|
|
||
| private fun destroySegment(id: String? = null) { | ||
| currentSegment?.let { | ||
| it.release() | ||
| val source = sources[it.type][it.index] | ||
| if (tracks.active.has(it.type)) { | ||
| source.releaseTrack(it.type) | ||
| } | ||
| } | ||
| if (id == null) { | ||
| segmentMap.clear() | ||
| } | ||
| else { | ||
| segmentMap[id] = null | ||
| } | ||
| } | ||
| private fun DataSource.init() = if (!isInitialized) initialize() else Unit | ||
|
|
||
| private fun DataSource.deinit() = if (isInitialized) deinitialize() else Unit | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.