-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
159 additions
and
42 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
lib/src/main/java/com/otaliastudios/transcoder/internal/Codecs.kt
This file contains 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,69 @@ | ||
package com.otaliastudios.transcoder.internal | ||
|
||
import android.media.MediaCodec | ||
import android.media.MediaFormat | ||
import android.view.Surface | ||
import com.otaliastudios.transcoder.common.TrackStatus | ||
import com.otaliastudios.transcoder.common.TrackType | ||
import com.otaliastudios.transcoder.internal.media.MediaFormatProvider | ||
import com.otaliastudios.transcoder.internal.utils.Logger | ||
import com.otaliastudios.transcoder.internal.utils.TrackMap | ||
import com.otaliastudios.transcoder.internal.utils.trackMapOf | ||
import com.otaliastudios.transcoder.source.DataSource | ||
import com.otaliastudios.transcoder.strategy.TrackStrategy | ||
|
||
/** | ||
* Encoders are shared between segments. This is not strictly needed but it is more efficient | ||
* and solves timestamp issues that arise due to the fact that MediaCodec can alter the timestamps | ||
* internally, so if we use different MediaCodec instances we don't have guarantees on monotonic | ||
* output timestamps, even if input timestamps are. This would later create crashes when passing | ||
* data to MediaMuxer / MPEG4Writer. | ||
*/ | ||
internal class Codecs( | ||
private val sources: DataSources, | ||
private val tracks: Tracks, | ||
private val current: TrackMap<Int> | ||
) { | ||
|
||
private val log = Logger("Codecs") | ||
|
||
val encoders = object : TrackMap<Pair<MediaCodec, Surface?>> { | ||
|
||
override fun has(type: TrackType) = tracks.all[type] == TrackStatus.COMPRESSING | ||
|
||
private val lazyAudio by lazy { | ||
val format = tracks.outputFormats.audio | ||
val codec = MediaCodec.createEncoderByType(format.getString(MediaFormat.KEY_MIME)!!) | ||
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE) | ||
codec to null | ||
} | ||
|
||
private val lazyVideo by lazy { | ||
val format = tracks.outputFormats.video | ||
val codec = MediaCodec.createEncoderByType(format.getString(MediaFormat.KEY_MIME)!!) | ||
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE) | ||
codec to codec.createInputSurface() | ||
} | ||
|
||
override fun get(type: TrackType) = when (type) { | ||
TrackType.AUDIO -> lazyAudio | ||
TrackType.VIDEO -> lazyVideo | ||
} | ||
} | ||
|
||
val ownsEncoderStart = object : TrackMap<Boolean> { | ||
override fun has(type: TrackType) = true | ||
override fun get(type: TrackType) = current[type] == 0 | ||
} | ||
|
||
val ownsEncoderStop = object : TrackMap<Boolean> { | ||
override fun has(type: TrackType) = true | ||
override fun get(type: TrackType) = current[type] == sources[type].lastIndex | ||
} | ||
|
||
fun release() { | ||
encoders.forEach { | ||
it.first.release() | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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.