Merge branch 'langleyd/live_waveform' of https://github.com/vector-im/element-x-android into langleyd/live_waveform

This commit is contained in:
David Langley
2023-10-27 10:56:48 +01:00
38 changed files with 250 additions and 86 deletions

View File

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id("io.element.android-library")
alias(libs.plugins.anvil)
}
android {
namespace = "io.element.android.libraries.mediaplayer.api"
}
anvil {
generateDaggerFactories.set(true)
}
dependencies {
implementation(projects.libraries.matrix.api)
implementation(libs.coroutines.core)
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.mediaplayer.api
import io.element.android.libraries.matrix.api.core.EventId
import kotlinx.coroutines.flow.StateFlow
/**
* A media player for Element X.
*/
interface MediaPlayer : AutoCloseable {
/**
* The current state of the player.
*/
val state: StateFlow<State>
/**
* Acquires control of the player and starts playing the given media.
*/
fun acquireControlAndPlay(
uri: String,
mediaId: String,
mimeType: String,
)
/**
* Plays the current media.
*/
fun play()
/**
* Pauses the current media.
*/
fun pause()
/**
* Seeks the current media to the given position.
*/
fun seekTo(positionMs: Long)
/**
* Releases any resources associated with this player.
*/
override fun close()
data class State(
/**
* Whether the player is currently playing.
*/
val isPlaying: Boolean,
/**
* The id of the media which is currently playing.
*
* NB: This is usually the string representation of the [EventId] of the event
* which contains the media.
*/
val mediaId: String?,
/**
* The current position of the player.
*/
val currentPosition: Long,
)
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id("io.element.android-library")
alias(libs.plugins.anvil)
}
android {
namespace = "io.element.android.libraries.mediaplayer.impl"
}
anvil {
generateDaggerFactories.set(true)
}
dependencies {
api(projects.libraries.mediaplayer.api)
implementation(libs.androidx.media3.exoplayer)
implementation(libs.dagger)
implementation(projects.libraries.di)
implementation(libs.coroutines.core)
testImplementation(projects.tests.testutils)
testImplementation(libs.test.junit)
testImplementation(libs.test.truth)
testImplementation(libs.test.mockk)
testImplementation(libs.test.turbine)
testImplementation(libs.coroutines.core)
testImplementation(libs.coroutines.test)
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.mediaplayer.impl
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.RoomScope
import io.element.android.libraries.di.SingleIn
import io.element.android.libraries.mediaplayer.api.MediaPlayer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
/**
* Default implementation of [MediaPlayer] backed by a [SimplePlayer].
*/
@ContributesBinding(RoomScope::class)
@SingleIn(RoomScope::class)
class MediaPlayerImpl @Inject constructor(
private val player: SimplePlayer,
) : MediaPlayer {
private val listener = object : SimplePlayer.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) {
_state.update {
it.copy(
currentPosition = player.currentPosition,
isPlaying = isPlaying,
)
}
if (isPlaying) {
job = scope.launch { updateCurrentPosition() }
} else {
job?.cancel()
}
}
override fun onMediaItemTransition(mediaItem: MediaItem?) {
_state.update {
it.copy(
currentPosition = player.currentPosition,
mediaId = mediaItem?.mediaId,
)
}
}
}
init {
player.addListener(listener)
}
private val scope = CoroutineScope(Job() + Dispatchers.Main)
private var job: Job? = null
private val _state = MutableStateFlow(MediaPlayer.State(false, null, 0L))
override val state: StateFlow<MediaPlayer.State> = _state.asStateFlow()
override fun acquireControlAndPlay(uri: String, mediaId: String, mimeType: String) {
player.clearMediaItems()
player.setMediaItem(
MediaItem.Builder()
.setUri(uri)
.setMediaId(mediaId)
.setMimeType(mimeType)
.build()
)
player.prepare()
player.play()
}
override fun play() {
if (player.playbackState == Player.STATE_ENDED) {
// There's a bug with some ogg files that somehow report to
// have no duration.
// With such files, once playback has ended once, calling
// player.seekTo(0) and then player.play() results in the
// player starting and stopping playing immediately effectively
// playing no sound.
// This is a workaround which will reload the media file.
player.getCurrentMediaItem()?.let {
player.setMediaItem(it)
player.prepare()
player.play()
}
} else {
player.play()
}
}
override fun pause() {
player.pause()
}
override fun seekTo(positionMs: Long) {
player.seekTo(positionMs)
_state.update {
it.copy(currentPosition = player.currentPosition)
}
}
override fun close() {
player.release()
}
private suspend fun updateCurrentPosition() {
while (true) {
if (!_state.value.isPlaying) return
delay(100)
_state.update {
it.copy(currentPosition = player.currentPosition)
}
}
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.mediaplayer.impl
import android.content.Context
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
import dagger.Provides
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.di.RoomScope
/**
* A subset of media3 [Player] that only exposes the few methods we need making it easier to mock.
*/
interface SimplePlayer {
fun addListener(listener: Listener)
val currentPosition: Long
val playbackState: Int
fun clearMediaItems()
fun setMediaItem(mediaItem: MediaItem)
fun getCurrentMediaItem(): MediaItem?
fun prepare()
fun play()
fun pause()
fun seekTo(positionMs: Long)
fun release()
interface Listener {
fun onIsPlayingChanged(isPlaying: Boolean)
fun onMediaItemTransition(mediaItem: MediaItem?)
}
}
@ContributesTo(RoomScope::class)
@Module
object SimplePlayerModule {
@Provides
fun simplePlayerProvider(
@ApplicationContext context: Context,
): SimplePlayer = SimplePlayerImpl(ExoPlayer.Builder(context).build())
}
/**
* Default implementation of [SimplePlayer] backed by a media3 [Player].
*/
class SimplePlayerImpl(
private val p: Player
) : SimplePlayer {
override fun addListener(listener: SimplePlayer.Listener) {
p.addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) = listener.onIsPlayingChanged(isPlaying)
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) = listener.onMediaItemTransition(mediaItem)
})
}
override val currentPosition: Long
get() = p.currentPosition
override val playbackState: Int
get() = p.playbackState
override fun clearMediaItems() = p.clearMediaItems()
override fun setMediaItem(mediaItem: MediaItem) = p.setMediaItem(mediaItem)
override fun getCurrentMediaItem(): MediaItem? = p.currentMediaItem
override fun prepare() = p.prepare()
override fun play() = p.play()
override fun pause() = p.pause()
override fun seekTo(positionMs: Long) = p.seekTo(positionMs)
override fun release() = p.release()
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.mediaplayer.impl
import kotlinx.coroutines.test.runTest
import org.junit.Test
class MediaPlayerImplTest {
@Test
fun `default test`() = runTest {
// TODO
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id("io.element.android-library")
}
android {
namespace = "io.element.android.libraries.mediaplayer.test"
}
dependencies {
api(projects.libraries.mediaplayer.api)
implementation(projects.tests.testutils)
implementation(libs.coroutines.test)
implementation(libs.test.truth)
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.mediaplayer.test
import io.element.android.libraries.mediaplayer.api.MediaPlayer
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
/**
* Fake implementation of [MediaPlayer] for testing purposes.
*/
class FakeMediaPlayer : MediaPlayer {
private val _state = MutableStateFlow(MediaPlayer.State(false, null, 0L))
override val state: StateFlow<MediaPlayer.State> = _state.asStateFlow()
override fun acquireControlAndPlay(uri: String, mediaId: String, mimeType: String) {
_state.update {
it.copy(
isPlaying = true,
mediaId = mediaId,
currentPosition = it.currentPosition + 1000L,
)
}
}
override fun play() {
_state.update {
it.copy(
isPlaying = true,
currentPosition = it.currentPosition + 1000L,
)
}
}
override fun pause() {
_state.update {
it.copy(
isPlaying = false,
)
}
}
override fun seekTo(positionMs: Long) {
_state.update {
it.copy(
currentPosition = positionMs,
)
}
}
override fun close() {
// no-op
}
}