Move ExoPlayer initialization to a Dagger module and optimize its dependencies (#4296)
Currently, ExoPlayer is initialized explicitly in `ViewMediaFragment` with all its dependencies, including many that are not useful for viewing Mastodon media attachments. This pull request moves most ExoPlayer initialization and configuration to a new Dagger module, and instead a `Provider<ExoPlayer>` factory is injected in the Fragment so it can create new instances when needed. The following ExoPlayer components will be configured: - **Renderers**: all of them (audio, video, metadata, subtitles) except for the `CameraMotionRenderer`. - **Extractors**: FLAC, Wav, Mp4, Ogg, Matroska/WebM and MP3 containers, to provide the same support as Firefox or Chrome browsers. Other container formats that are either image formats (already covered by Glide), not web-friendly or reserved for live streaming are skipped. - **MediaSource**: only progressive download (through OkHttp) is provided. Live streaming support using protocols like RTSP, MPEG/Dash or HLS is skipped, because Mastodon servers don't use these protocols to download attachments. The Mastodon documentation mentions the [supported media formats for attachments](https://docs.joinmastodon.org/user/posting/#media) and this covers them and even more. The docs also mentions that the video and audio files are transcoded to MP4 and MP3 upon upload but that was not the case in the past (for example WebM was used) and it could change again in the future. Specifying these components manually allows reducing the APK size by about 200 KB thanks to R8 shrinking. There are also a few extra code changes: - Remove the code specific to API < 24 since the min SDK of the app is now 24. - Add support for pausing a video when unplugging headphones. - Specify the audio attributes according to content type to help the Android audio mixer.
This commit is contained in:
parent
f09a5b00e0
commit
9901376d38
5 changed files with 179 additions and 74 deletions
139
app/src/main/java/com/keylesspalace/tusky/di/PlayerModule.kt
Normal file
139
app/src/main/java/com/keylesspalace/tusky/di/PlayerModule.kt
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* Copyright 2024 Tusky Contributors
|
||||
*
|
||||
* This file is a part of Tusky.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
||||
* see <http://www.gnu.org/licenses>.
|
||||
*/
|
||||
|
||||
package com.keylesspalace.tusky.di
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Looper
|
||||
import androidx.annotation.OptIn
|
||||
import androidx.media3.common.C
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.datasource.DataSource
|
||||
import androidx.media3.datasource.DefaultDataSource
|
||||
import androidx.media3.datasource.okhttp.OkHttpDataSource
|
||||
import androidx.media3.exoplayer.DefaultRenderersFactory
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.exoplayer.RenderersFactory
|
||||
import androidx.media3.exoplayer.audio.AudioSink
|
||||
import androidx.media3.exoplayer.audio.DefaultAudioSink
|
||||
import androidx.media3.exoplayer.audio.MediaCodecAudioRenderer
|
||||
import androidx.media3.exoplayer.mediacodec.MediaCodecSelector
|
||||
import androidx.media3.exoplayer.metadata.MetadataRenderer
|
||||
import androidx.media3.exoplayer.source.MediaSource
|
||||
import androidx.media3.exoplayer.source.ProgressiveMediaSource
|
||||
import androidx.media3.exoplayer.text.TextRenderer
|
||||
import androidx.media3.exoplayer.video.MediaCodecVideoRenderer
|
||||
import androidx.media3.extractor.ExtractorsFactory
|
||||
import androidx.media3.extractor.flac.FlacExtractor
|
||||
import androidx.media3.extractor.mkv.MatroskaExtractor
|
||||
import androidx.media3.extractor.mp3.Mp3Extractor
|
||||
import androidx.media3.extractor.mp4.FragmentedMp4Extractor
|
||||
import androidx.media3.extractor.mp4.Mp4Extractor
|
||||
import androidx.media3.extractor.ogg.OggExtractor
|
||||
import androidx.media3.extractor.wav.WavExtractor
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
@Module
|
||||
@OptIn(UnstableApi::class)
|
||||
object PlayerModule {
|
||||
@Provides
|
||||
fun provideAudioSink(context: Context): AudioSink {
|
||||
return DefaultAudioSink.Builder(context)
|
||||
.build()
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideRenderersFactory(context: Context, audioSink: AudioSink): RenderersFactory {
|
||||
return RenderersFactory { eventHandler,
|
||||
videoRendererEventListener,
|
||||
audioRendererEventListener,
|
||||
textRendererOutput,
|
||||
metadataRendererOutput ->
|
||||
arrayOf(
|
||||
MediaCodecVideoRenderer(
|
||||
context,
|
||||
MediaCodecSelector.DEFAULT,
|
||||
DefaultRenderersFactory.DEFAULT_ALLOWED_VIDEO_JOINING_TIME_MS,
|
||||
eventHandler,
|
||||
videoRendererEventListener,
|
||||
DefaultRenderersFactory.MAX_DROPPED_VIDEO_FRAME_COUNT_TO_NOTIFY
|
||||
),
|
||||
MediaCodecAudioRenderer(
|
||||
context,
|
||||
MediaCodecSelector.DEFAULT,
|
||||
eventHandler,
|
||||
audioRendererEventListener,
|
||||
audioSink
|
||||
),
|
||||
TextRenderer(
|
||||
textRendererOutput,
|
||||
eventHandler.looper
|
||||
),
|
||||
MetadataRenderer(
|
||||
metadataRendererOutput,
|
||||
eventHandler.looper
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideExtractorsFactory(): ExtractorsFactory {
|
||||
// Extractors order is optimized according to
|
||||
// https://docs.google.com/document/d/1w2mKaWMxfz2Ei8-LdxqbPs1VLe_oudB-eryXXw9OvQQ
|
||||
return ExtractorsFactory {
|
||||
arrayOf(
|
||||
FlacExtractor(),
|
||||
WavExtractor(),
|
||||
Mp4Extractor(),
|
||||
FragmentedMp4Extractor(),
|
||||
OggExtractor(),
|
||||
MatroskaExtractor(),
|
||||
Mp3Extractor()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideDataSourceFactory(context: Context, okHttpClient: OkHttpClient): DataSource.Factory {
|
||||
return DefaultDataSource.Factory(context, OkHttpDataSource.Factory(okHttpClient))
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideMediaSourceFactory(
|
||||
dataSourceFactory: DataSource.Factory,
|
||||
extractorsFactory: ExtractorsFactory
|
||||
): MediaSource.Factory {
|
||||
// Only progressive download is supported for Mastodon attachments
|
||||
return ProgressiveMediaSource.Factory(dataSourceFactory, extractorsFactory)
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideExoPlayer(
|
||||
context: Context,
|
||||
renderersFactory: RenderersFactory,
|
||||
mediaSourceFactory: MediaSource.Factory
|
||||
): ExoPlayer {
|
||||
return ExoPlayer.Builder(context, renderersFactory, mediaSourceFactory)
|
||||
.setLooper(Looper.getMainLooper())
|
||||
.setHandleAudioBecomingNoisy(true) // automatically pause when unplugging headphones
|
||||
.setWakeMode(C.WAKE_MODE_NONE) // playback is always in the foreground
|
||||
.build()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue