react-native-audio-browser 0.2.0-next.86 → 0.2.0-next.88
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/audiobrowser/player/MediaSessionCallback.kt +6 -2
- package/android/src/main/java/com/audiobrowser/player/PlaybackStateStore.kt +1 -3
- package/android/src/main/java/com/audiobrowser/player/Player.kt +11 -0
- package/android/src/main/java/com/audiobrowser/player/SessionViewListener.kt +25 -5
- package/android/src/test/java/com/audiobrowser/AudioFixtures.kt +32 -0
- package/android/src/test/java/com/audiobrowser/LegacyRatingTest.kt +6 -19
- package/android/src/test/java/com/audiobrowser/ServiceFixtures.kt +48 -0
- package/android/src/test/java/com/audiobrowser/ServiceTaskRemovedTest.kt +2 -34
- package/android/src/test/java/com/audiobrowser/SessionPositionUpdatesTest.kt +4 -63
- package/android/src/test/java/com/audiobrowser/player/HandleTrackLoadSeekTest.kt +182 -0
- package/android/src/test/java/com/audiobrowser/player/LiveStreamEndTest.kt +6 -24
- package/android/src/test/java/com/audiobrowser/player/PlayerRetryTest.kt +15 -40
- package/android/src/test/java/com/audiobrowser/player/PlayerSetupEndedTest.kt +2 -28
- package/android/src/test/java/com/audiobrowser/player/PlayerSuppressionTest.kt +3 -28
- package/android/src/test/java/com/audiobrowser/player/SessionPlayTest.kt +6 -32
- package/android/src/test/java/com/audiobrowser/player/SessionViewListenerTest.kt +204 -0
- package/android/src/test/java/com/audiobrowser/player/SessionViewTest.kt +75 -31
- package/package.json +1 -1
|
@@ -808,13 +808,17 @@ class MediaSessionCallback(private val player: Player) :
|
|
|
808
808
|
return scope.future {
|
|
809
809
|
val audioBrowser = player.awaitBrowser()
|
|
810
810
|
|
|
811
|
-
//
|
|
811
|
+
// Media3 sets whatever this returns: the controller's position would undo the handler's seek.
|
|
812
812
|
// Reads hop to Main: ExoPlayer is main-thread confined and this runs on IO.
|
|
813
813
|
suspend fun currentPlayerState(): MediaSession.MediaItemsWithStartPosition =
|
|
814
814
|
withContext(Dispatchers.Main) {
|
|
815
815
|
val currentItems = player.tracks.map { TrackFactory.toMedia3(it) }
|
|
816
816
|
val currentIndex = player.currentIndex ?: 0
|
|
817
|
-
MediaSession.MediaItemsWithStartPosition(
|
|
817
|
+
MediaSession.MediaItemsWithStartPosition(
|
|
818
|
+
currentItems,
|
|
819
|
+
currentIndex,
|
|
820
|
+
player.restorablePositionMs,
|
|
821
|
+
)
|
|
818
822
|
}
|
|
819
823
|
|
|
820
824
|
val browserManager = audioBrowser.browserManager
|
|
@@ -109,9 +109,7 @@ class PlaybackStateStore(private val player: Player) {
|
|
|
109
109
|
/** Saves the current track and its position; does nothing without a current track. */
|
|
110
110
|
fun save() {
|
|
111
111
|
val track = player.currentTrack ?: return
|
|
112
|
-
|
|
113
|
-
val positionMs =
|
|
114
|
-
player.position.takeUnless { player.isCurrentItemLive || it == 0L } ?: C.TIME_UNSET
|
|
112
|
+
val positionMs = player.restorablePositionMs
|
|
115
113
|
|
|
116
114
|
prefs.edit {
|
|
117
115
|
putLong(KEY_POSITION_MS, positionMs)
|
|
@@ -1455,6 +1455,17 @@ internal constructor(
|
|
|
1455
1455
|
val isCurrentItemLive: Boolean
|
|
1456
1456
|
get() = sessionPlayer.isCurrentMediaItemLive
|
|
1457
1457
|
|
|
1458
|
+
/**
|
|
1459
|
+
* [position] to set the queue at again, or [C.TIME_UNSET], where Media3 starts at the default
|
|
1460
|
+
* position: for a live item, for 0, which a live item also reports before it has loaded, and
|
|
1461
|
+
* after the end, so the track plays again.
|
|
1462
|
+
*/
|
|
1463
|
+
internal val restorablePositionMs: Long
|
|
1464
|
+
get() =
|
|
1465
|
+
position.takeUnless {
|
|
1466
|
+
isCurrentItemLive || it == 0L || sessionPlayer.playbackState == MediaPlayer.STATE_ENDED
|
|
1467
|
+
} ?: C.TIME_UNSET
|
|
1468
|
+
|
|
1458
1469
|
// MARK: - Buffer Configuration
|
|
1459
1470
|
|
|
1460
1471
|
/** Takes effect for future buffering; what is buffered stays. */
|
|
@@ -14,11 +14,14 @@ import androidx.media3.common.Tracks
|
|
|
14
14
|
import androidx.media3.common.VideoSize
|
|
15
15
|
import androidx.media3.common.text.Cue
|
|
16
16
|
import androidx.media3.common.text.CueGroup
|
|
17
|
+
import androidx.media3.exoplayer.source.ForwardingTimeline
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* A session listener hearing the engine's events with [view]'s state and playing, and never its
|
|
20
21
|
* errors, which [InterceptingPlayer.getPlayerError] hides: the session copies an event's own
|
|
21
|
-
* arguments, so the view has to be in them.
|
|
22
|
+
* arguments, so the view has to be in them. A state, playing, timeline or position it was already
|
|
23
|
+
* told is not told again: the session pushes each one to legacy controllers whether or not it
|
|
24
|
+
* changed. Every other event passes as it is.
|
|
22
25
|
*/
|
|
23
26
|
internal class SessionViewListener(
|
|
24
27
|
private val target: MediaPlayer.Listener,
|
|
@@ -26,17 +29,19 @@ internal class SessionViewListener(
|
|
|
26
29
|
) : MediaPlayer.Listener {
|
|
27
30
|
private var toldState = view.playbackState
|
|
28
31
|
private var toldPlaying = view.isPlaying
|
|
32
|
+
private var toldTimeline = view.currentTimeline
|
|
29
33
|
|
|
30
34
|
/**
|
|
31
35
|
* Tells the session the view's state and playing where they moved since it was last told: the
|
|
32
36
|
* view can move with no engine event, as when the retry loop lets go of an idle engine.
|
|
33
37
|
*/
|
|
34
38
|
fun refresh() {
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
onPlaybackStateChanged(view.playbackState)
|
|
40
|
+
onIsPlayingChanged(view.isPlaying)
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
override fun onPlaybackStateChanged(playbackState: Int) {
|
|
44
|
+
if (view.playbackState == toldState) return
|
|
40
45
|
toldState = view.playbackState
|
|
41
46
|
target.onPlaybackStateChanged(toldState)
|
|
42
47
|
}
|
|
@@ -46,6 +51,7 @@ internal class SessionViewListener(
|
|
|
46
51
|
@Suppress("DEPRECATION") target.onPlayerStateChanged(view.playWhenReady, view.playbackState)
|
|
47
52
|
|
|
48
53
|
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
|
54
|
+
if (view.isPlaying == toldPlaying) return
|
|
49
55
|
toldPlaying = view.isPlaying
|
|
50
56
|
target.onIsPlayingChanged(toldPlaying)
|
|
51
57
|
}
|
|
@@ -57,8 +63,11 @@ internal class SessionViewListener(
|
|
|
57
63
|
override fun onEvents(player: MediaPlayer, events: MediaPlayer.Events) =
|
|
58
64
|
target.onEvents(player, events)
|
|
59
65
|
|
|
60
|
-
override fun onTimelineChanged(timeline: Timeline, reason: Int)
|
|
66
|
+
override fun onTimelineChanged(timeline: Timeline, reason: Int) {
|
|
67
|
+
if (IgnoringPlaceholders(timeline) == IgnoringPlaceholders(toldTimeline)) return
|
|
68
|
+
toldTimeline = timeline
|
|
61
69
|
target.onTimelineChanged(timeline, reason)
|
|
70
|
+
}
|
|
62
71
|
|
|
63
72
|
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) =
|
|
64
73
|
target.onMediaItemTransition(mediaItem, reason)
|
|
@@ -102,7 +111,10 @@ internal class SessionViewListener(
|
|
|
102
111
|
oldPosition: MediaPlayer.PositionInfo,
|
|
103
112
|
newPosition: MediaPlayer.PositionInfo,
|
|
104
113
|
reason: Int,
|
|
105
|
-
)
|
|
114
|
+
) {
|
|
115
|
+
if (oldPosition == newPosition) return
|
|
116
|
+
target.onPositionDiscontinuity(oldPosition, newPosition, reason)
|
|
117
|
+
}
|
|
106
118
|
|
|
107
119
|
override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters) =
|
|
108
120
|
target.onPlaybackParametersChanged(playbackParameters)
|
|
@@ -146,3 +158,11 @@ internal class SessionViewListener(
|
|
|
146
158
|
|
|
147
159
|
override fun onMetadata(metadata: Metadata) = target.onMetadata(metadata)
|
|
148
160
|
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Reads every period as real: a re-prepare marks the same period a placeholder, then real again.
|
|
164
|
+
*/
|
|
165
|
+
private class IgnoringPlaceholders(timeline: Timeline) : ForwardingTimeline(timeline) {
|
|
166
|
+
override fun getPeriod(periodIndex: Int, period: Period, setIds: Boolean): Period =
|
|
167
|
+
super.getPeriod(periodIndex, period, setIds).apply { isPlaceholder = false }
|
|
168
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.net.Uri
|
|
4
|
+
import java.nio.ByteBuffer
|
|
5
|
+
import java.nio.ByteOrder
|
|
6
|
+
import java.time.Duration
|
|
7
|
+
import org.junit.rules.TemporaryFolder
|
|
8
|
+
|
|
9
|
+
/** A WAV of [duration] of 8 kHz mono 8-bit silence, its data size in its header. */
|
|
10
|
+
fun silentWavBytes(duration: Duration): ByteArray {
|
|
11
|
+
val samples = (duration.toMillis() * 8).toInt()
|
|
12
|
+
val header =
|
|
13
|
+
ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
|
|
14
|
+
put("RIFF".toByteArray())
|
|
15
|
+
putInt(36 + samples)
|
|
16
|
+
put("WAVEfmt ".toByteArray())
|
|
17
|
+
putInt(16)
|
|
18
|
+
putShort(1)
|
|
19
|
+
putShort(1)
|
|
20
|
+
putInt(8_000)
|
|
21
|
+
putInt(8_000)
|
|
22
|
+
putShort(1)
|
|
23
|
+
putShort(8)
|
|
24
|
+
put("data".toByteArray())
|
|
25
|
+
putInt(samples)
|
|
26
|
+
}
|
|
27
|
+
return header.array() + ByteArray(samples) { 0x80.toByte() }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The `file:` URI of [silentWavBytes] written to `silence.wav` here: a second call throws. */
|
|
31
|
+
fun TemporaryFolder.silentWavUri(duration: Duration): String =
|
|
32
|
+
Uri.fromFile(newFile("silence.wav").apply { writeBytes(silentWavBytes(duration)) }).toString()
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
package com.audiobrowser
|
|
2
2
|
|
|
3
|
-
import android.content.ComponentName
|
|
4
|
-
import android.os.Binder
|
|
5
3
|
import android.os.Bundle
|
|
6
4
|
import android.os.Looper
|
|
7
5
|
import androidx.media3.common.HeartRating
|
|
@@ -13,9 +11,7 @@ import org.junit.After
|
|
|
13
11
|
import org.junit.Assert.assertEquals
|
|
14
12
|
import org.junit.Test
|
|
15
13
|
import org.junit.runner.RunWith
|
|
16
|
-
import org.robolectric.Robolectric
|
|
17
14
|
import org.robolectric.RobolectricTestRunner
|
|
18
|
-
import org.robolectric.RuntimeEnvironment
|
|
19
15
|
import org.robolectric.Shadows.shadowOf
|
|
20
16
|
|
|
21
17
|
/**
|
|
@@ -26,15 +22,7 @@ import org.robolectric.Shadows.shadowOf
|
|
|
26
22
|
@RunWith(RobolectricTestRunner::class)
|
|
27
23
|
class LegacyRatingTest {
|
|
28
24
|
|
|
29
|
-
private val controller =
|
|
30
|
-
val application = RuntimeEnvironment.getApplication()
|
|
31
|
-
shadowOf(application)
|
|
32
|
-
.setComponentNameAndServiceForBindService(
|
|
33
|
-
ComponentName(application, HeadlessTaskService::class.java),
|
|
34
|
-
Binder(),
|
|
35
|
-
)
|
|
36
|
-
Robolectric.buildService(Service::class.java).create()
|
|
37
|
-
}
|
|
25
|
+
private val controller = buildService()
|
|
38
26
|
private val service = controller.get()
|
|
39
27
|
private val player = service.player
|
|
40
28
|
private val legacyController =
|
|
@@ -48,7 +36,6 @@ class LegacyRatingTest {
|
|
|
48
36
|
Bundle.EMPTY,
|
|
49
37
|
false,
|
|
50
38
|
)
|
|
51
|
-
private val session = service.onGetSession(legacyController)!!
|
|
52
39
|
|
|
53
40
|
@After
|
|
54
41
|
fun destroy() {
|
|
@@ -62,13 +49,13 @@ class LegacyRatingTest {
|
|
|
62
49
|
val result =
|
|
63
50
|
player
|
|
64
51
|
.getMediaSessionCallback()
|
|
65
|
-
.onSetRating(session, legacyController, mediaId, HeartRating(heart))
|
|
52
|
+
.onSetRating(service.session, legacyController, mediaId, HeartRating(heart))
|
|
66
53
|
.get()
|
|
67
54
|
idle()
|
|
68
55
|
return result.resultCode
|
|
69
56
|
}
|
|
70
57
|
|
|
71
|
-
private fun currentMediaId() = session.player.currentMediaItem!!.mediaId
|
|
58
|
+
private fun currentMediaId() = service.session.player.currentMediaItem!!.mediaId
|
|
72
59
|
|
|
73
60
|
private fun loadTrack() {
|
|
74
61
|
controller.bind()
|
|
@@ -85,11 +72,11 @@ class LegacyRatingTest {
|
|
|
85
72
|
|
|
86
73
|
assertEquals(SessionResult.RESULT_SUCCESS, rate(currentMediaId(), true))
|
|
87
74
|
assertEquals(true, player.currentTrack?.favorited)
|
|
88
|
-
assertEquals(HeartRating(true), session.player.mediaMetadata.userRating)
|
|
75
|
+
assertEquals(HeartRating(true), service.session.player.mediaMetadata.userRating)
|
|
89
76
|
|
|
90
77
|
assertEquals(SessionResult.RESULT_SUCCESS, rate(currentMediaId(), false))
|
|
91
78
|
assertEquals(false, player.currentTrack?.favorited)
|
|
92
|
-
assertEquals(HeartRating(false), session.player.mediaMetadata.userRating)
|
|
79
|
+
assertEquals(HeartRating(false), service.session.player.mediaMetadata.userRating)
|
|
93
80
|
}
|
|
94
81
|
|
|
95
82
|
@Test
|
|
@@ -98,6 +85,6 @@ class LegacyRatingTest {
|
|
|
98
85
|
|
|
99
86
|
assertEquals(SessionResult.RESULT_ERROR_NOT_SUPPORTED, rate("b", true))
|
|
100
87
|
assertEquals(false, player.currentTrack?.favorited)
|
|
101
|
-
assertEquals(HeartRating(false), session.player.mediaMetadata.userRating)
|
|
88
|
+
assertEquals(HeartRating(false), service.session.player.mediaMetadata.userRating)
|
|
102
89
|
}
|
|
103
90
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
package com.audiobrowser
|
|
2
|
+
|
|
3
|
+
import android.content.ComponentName
|
|
4
|
+
import android.os.Binder
|
|
5
|
+
import android.os.Bundle
|
|
6
|
+
import android.os.Process
|
|
7
|
+
import androidx.annotation.OptIn
|
|
8
|
+
import androidx.media3.common.util.UnstableApi
|
|
9
|
+
import androidx.media3.session.MediaLibraryService.MediaLibrarySession
|
|
10
|
+
import androidx.media3.session.MediaSession
|
|
11
|
+
import org.robolectric.Robolectric
|
|
12
|
+
import org.robolectric.RuntimeEnvironment
|
|
13
|
+
import org.robolectric.Shadows.shadowOf
|
|
14
|
+
import org.robolectric.android.controller.ServiceController
|
|
15
|
+
|
|
16
|
+
/** A created [Service], whose binding to HeadlessTaskService connects. */
|
|
17
|
+
fun buildService(): ServiceController<Service> {
|
|
18
|
+
val application = RuntimeEnvironment.getApplication()
|
|
19
|
+
// Robolectric connects an unregistered binding with a null ComponentName, which the
|
|
20
|
+
// HeadlessTaskService connection's non-null parameter rejects.
|
|
21
|
+
shadowOf(application)
|
|
22
|
+
.setComponentNameAndServiceForBindService(
|
|
23
|
+
ComponentName(application, HeadlessTaskService::class.java),
|
|
24
|
+
Binder(),
|
|
25
|
+
)
|
|
26
|
+
return Robolectric.buildService(Service::class.java).create()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The service's session, which it otherwise hands only to a connecting controller. */
|
|
30
|
+
@get:OptIn(UnstableApi::class)
|
|
31
|
+
val Service.session: MediaLibrarySession
|
|
32
|
+
get() {
|
|
33
|
+
// onGetSession ignores its argument.
|
|
34
|
+
val session =
|
|
35
|
+
onGetSession(
|
|
36
|
+
MediaSession.ControllerInfo.createTestOnlyControllerInfo(
|
|
37
|
+
packageName,
|
|
38
|
+
0,
|
|
39
|
+
Process.myUid(),
|
|
40
|
+
0,
|
|
41
|
+
0,
|
|
42
|
+
false,
|
|
43
|
+
Bundle.EMPTY,
|
|
44
|
+
false,
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
return checkNotNull(session) { "the session is released" }
|
|
48
|
+
}
|
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
package com.audiobrowser
|
|
2
2
|
|
|
3
3
|
import android.app.SearchManager
|
|
4
|
-
import android.content.ComponentName
|
|
5
4
|
import android.content.ContextWrapper
|
|
6
5
|
import android.content.Intent
|
|
7
|
-
import android.os.Binder
|
|
8
|
-
import android.os.Bundle
|
|
9
6
|
import android.os.Looper
|
|
10
7
|
import android.os.Process
|
|
11
|
-
import androidx.annotation.OptIn
|
|
12
8
|
import androidx.media3.common.C
|
|
13
|
-
import androidx.media3.common.util.UnstableApi
|
|
14
9
|
import androidx.media3.session.MediaController
|
|
15
|
-
import androidx.media3.session.MediaSession
|
|
16
10
|
import com.audiobrowser.model.PlayerUpdateOptions
|
|
17
11
|
import com.audiobrowser.player.PlaybackStateStore
|
|
18
12
|
import com.margelo.nitro.audiobrowser.AppKilledPlaybackBehavior
|
|
@@ -26,7 +20,6 @@ import org.junit.Assert.assertNull
|
|
|
26
20
|
import org.junit.Assert.assertTrue
|
|
27
21
|
import org.junit.Test
|
|
28
22
|
import org.junit.runner.RunWith
|
|
29
|
-
import org.robolectric.Robolectric
|
|
30
23
|
import org.robolectric.RobolectricTestRunner
|
|
31
24
|
import org.robolectric.RuntimeEnvironment
|
|
32
25
|
import org.robolectric.Shadows.shadowOf
|
|
@@ -34,17 +27,7 @@ import org.robolectric.Shadows.shadowOf
|
|
|
34
27
|
@RunWith(RobolectricTestRunner::class)
|
|
35
28
|
class ServiceTaskRemovedTest {
|
|
36
29
|
|
|
37
|
-
private val controller =
|
|
38
|
-
// Robolectric connects an unregistered binding with a null ComponentName, which the
|
|
39
|
-
// HeadlessTaskService connection's non-null parameter rejects.
|
|
40
|
-
val application = RuntimeEnvironment.getApplication()
|
|
41
|
-
shadowOf(application)
|
|
42
|
-
.setComponentNameAndServiceForBindService(
|
|
43
|
-
ComponentName(application, HeadlessTaskService::class.java),
|
|
44
|
-
Binder(),
|
|
45
|
-
)
|
|
46
|
-
Robolectric.buildService(Service::class.java).create()
|
|
47
|
-
}
|
|
30
|
+
private val controller = buildService()
|
|
48
31
|
private val service = controller.get()
|
|
49
32
|
private val player = service.player
|
|
50
33
|
|
|
@@ -61,7 +44,6 @@ class ServiceTaskRemovedTest {
|
|
|
61
44
|
idle()
|
|
62
45
|
}
|
|
63
46
|
|
|
64
|
-
@OptIn(UnstableApi::class)
|
|
65
47
|
private fun connectController(packageName: String) {
|
|
66
48
|
val application = RuntimeEnvironment.getApplication()
|
|
67
49
|
shadowOf(application.packageManager)
|
|
@@ -70,21 +52,7 @@ class ServiceTaskRemovedTest {
|
|
|
70
52
|
object : ContextWrapper(application) {
|
|
71
53
|
override fun getPackageName() = packageName
|
|
72
54
|
}
|
|
73
|
-
|
|
74
|
-
val session =
|
|
75
|
-
service.onGetSession(
|
|
76
|
-
MediaSession.ControllerInfo.createTestOnlyControllerInfo(
|
|
77
|
-
packageName,
|
|
78
|
-
0,
|
|
79
|
-
Process.myUid(),
|
|
80
|
-
0,
|
|
81
|
-
0,
|
|
82
|
-
false,
|
|
83
|
-
Bundle.EMPTY,
|
|
84
|
-
false,
|
|
85
|
-
)
|
|
86
|
-
)
|
|
87
|
-
val future = MediaController.Builder(context, session!!.token).buildAsync()
|
|
55
|
+
val future = MediaController.Builder(context, service.session.token).buildAsync()
|
|
88
56
|
idle()
|
|
89
57
|
// Throws if the session refused the controller.
|
|
90
58
|
future.get()
|
|
@@ -1,20 +1,10 @@
|
|
|
1
1
|
package com.audiobrowser
|
|
2
2
|
|
|
3
|
-
import android.content.ComponentName
|
|
4
|
-
import android.net.Uri
|
|
5
|
-
import android.os.Binder
|
|
6
3
|
import android.os.Bundle
|
|
7
4
|
import android.os.Looper
|
|
8
|
-
import android.os.Process
|
|
9
|
-
import androidx.annotation.OptIn
|
|
10
5
|
import androidx.media3.common.C
|
|
11
|
-
import androidx.media3.common.util.UnstableApi
|
|
12
|
-
import androidx.media3.session.MediaSession
|
|
13
6
|
import androidx.media3.session.countLegacyStateUpdates
|
|
14
7
|
import com.audiobrowser.util.TrackFactory
|
|
15
|
-
import java.io.File
|
|
16
|
-
import java.nio.ByteBuffer
|
|
17
|
-
import java.nio.ByteOrder
|
|
18
8
|
import java.time.Duration
|
|
19
9
|
import org.junit.After
|
|
20
10
|
import org.junit.Assert.assertEquals
|
|
@@ -24,7 +14,6 @@ import org.junit.Rule
|
|
|
24
14
|
import org.junit.Test
|
|
25
15
|
import org.junit.rules.TemporaryFolder
|
|
26
16
|
import org.junit.runner.RunWith
|
|
27
|
-
import org.robolectric.Robolectric
|
|
28
17
|
import org.robolectric.RobolectricTestRunner
|
|
29
18
|
import org.robolectric.RuntimeEnvironment
|
|
30
19
|
import org.robolectric.Shadows.shadowOf
|
|
@@ -69,7 +58,7 @@ class SessionPositionUpdatesTest {
|
|
|
69
58
|
assertEquals(0, legacyStateUpdatesOverSteadyPlayback(Duration.ofSeconds(30)))
|
|
70
59
|
|
|
71
60
|
// The counter hears a real change.
|
|
72
|
-
val updates = session
|
|
61
|
+
val updates = service.session.countLegacyStateUpdates()
|
|
73
62
|
player.pause()
|
|
74
63
|
advance(Duration.ofMillis(500))
|
|
75
64
|
assertTrue(updates() > 0)
|
|
@@ -80,26 +69,18 @@ class SessionPositionUpdatesTest {
|
|
|
80
69
|
.getInternalMutablePackageInfo(application.packageName)
|
|
81
70
|
.applicationInfo!!
|
|
82
71
|
.metaData = metaData
|
|
83
|
-
|
|
84
|
-
// HeadlessTaskService connection's non-null parameter rejects.
|
|
85
|
-
shadowOf(application)
|
|
86
|
-
.setComponentNameAndServiceForBindService(
|
|
87
|
-
ComponentName(application, HeadlessTaskService::class.java),
|
|
88
|
-
Binder(),
|
|
89
|
-
)
|
|
90
|
-
controller = Robolectric.buildService(Service::class.java).create()
|
|
72
|
+
controller = buildService()
|
|
91
73
|
}
|
|
92
74
|
|
|
93
75
|
/** Plays a silent WAV, then counts the legacy playback states sent over [duration]. */
|
|
94
76
|
private fun legacyStateUpdatesOverSteadyPlayback(duration: Duration): Int {
|
|
95
|
-
val session = session()
|
|
96
77
|
player.exoPlayer.trackSelectionParameters =
|
|
97
78
|
player.exoPlayer.trackSelectionParameters
|
|
98
79
|
.buildUpon()
|
|
99
80
|
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
|
|
100
81
|
.build()
|
|
101
82
|
goOnline(application)
|
|
102
|
-
val src =
|
|
83
|
+
val src = folder.silentWavUri(Duration.ofSeconds(45))
|
|
103
84
|
player.exoPlayer.setMediaItems(listOf(TrackFactory.toMedia3(TestFixtures.track(src = src))))
|
|
104
85
|
player.exoPlayer.prepare()
|
|
105
86
|
player.play()
|
|
@@ -107,30 +88,13 @@ class SessionPositionUpdatesTest {
|
|
|
107
88
|
// Lets the updates that starting playback sends settle.
|
|
108
89
|
advance(Duration.ofSeconds(1))
|
|
109
90
|
|
|
110
|
-
val updates = session.countLegacyStateUpdates()
|
|
91
|
+
val updates = service.session.countLegacyStateUpdates()
|
|
111
92
|
repeat((duration.toMillis() / 500).toInt()) { advance(Duration.ofMillis(500)) }
|
|
112
93
|
|
|
113
94
|
assertTrue(player.exoPlayer.isPlaying)
|
|
114
95
|
return updates()
|
|
115
96
|
}
|
|
116
97
|
|
|
117
|
-
@OptIn(UnstableApi::class)
|
|
118
|
-
private fun session(): MediaSession {
|
|
119
|
-
// onGetSession ignores its argument.
|
|
120
|
-
return service.onGetSession(
|
|
121
|
-
MediaSession.ControllerInfo.createTestOnlyControllerInfo(
|
|
122
|
-
application.packageName,
|
|
123
|
-
0,
|
|
124
|
-
Process.myUid(),
|
|
125
|
-
0,
|
|
126
|
-
0,
|
|
127
|
-
false,
|
|
128
|
-
Bundle.EMPTY,
|
|
129
|
-
false,
|
|
130
|
-
)
|
|
131
|
-
)!!
|
|
132
|
-
}
|
|
133
|
-
|
|
134
98
|
private fun advance(duration: Duration) {
|
|
135
99
|
ShadowSystemClock.advanceBy(duration)
|
|
136
100
|
Thread.sleep(10)
|
|
@@ -147,27 +111,4 @@ class SessionPositionUpdatesTest {
|
|
|
147
111
|
advance(Duration.ofMillis(100))
|
|
148
112
|
}
|
|
149
113
|
}
|
|
150
|
-
|
|
151
|
-
/** 8 kHz mono 8-bit silence. */
|
|
152
|
-
private fun silentWav(seconds: Int): File {
|
|
153
|
-
val samples = 8_000 * seconds
|
|
154
|
-
val header =
|
|
155
|
-
ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN).apply {
|
|
156
|
-
put("RIFF".toByteArray())
|
|
157
|
-
putInt(36 + samples)
|
|
158
|
-
put("WAVEfmt ".toByteArray())
|
|
159
|
-
putInt(16)
|
|
160
|
-
putShort(1)
|
|
161
|
-
putShort(1)
|
|
162
|
-
putInt(8_000)
|
|
163
|
-
putInt(8_000)
|
|
164
|
-
putShort(1)
|
|
165
|
-
putShort(8)
|
|
166
|
-
put("data".toByteArray())
|
|
167
|
-
putInt(samples)
|
|
168
|
-
}
|
|
169
|
-
return folder.newFile("silence.wav").apply {
|
|
170
|
-
writeBytes(header.array() + ByteArray(samples) { 0x80.toByte() })
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
114
|
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import androidx.media3.common.C
|
|
6
|
+
import androidx.media3.common.Player as MediaPlayer
|
|
7
|
+
import androidx.media3.common.Timeline
|
|
8
|
+
import androidx.media3.session.MediaController
|
|
9
|
+
import com.audiobrowser.AudioBrowser
|
|
10
|
+
import com.audiobrowser.TestFixtures
|
|
11
|
+
import com.audiobrowser.browser.BrowserManager
|
|
12
|
+
import com.audiobrowser.buildService
|
|
13
|
+
import com.audiobrowser.session
|
|
14
|
+
import com.audiobrowser.silentWavUri
|
|
15
|
+
import com.audiobrowser.util.JsPromise
|
|
16
|
+
import com.audiobrowser.util.TrackFactory
|
|
17
|
+
import com.margelo.nitro.audiobrowser.NativeBrowserConfiguration
|
|
18
|
+
import com.margelo.nitro.audiobrowser.Track
|
|
19
|
+
import com.margelo.nitro.audiobrowser.TrackLoadEvent
|
|
20
|
+
import com.margelo.nitro.core.Promise
|
|
21
|
+
import java.time.Duration
|
|
22
|
+
import java.util.concurrent.TimeUnit
|
|
23
|
+
import org.junit.After
|
|
24
|
+
import org.junit.Assert.assertEquals
|
|
25
|
+
import org.junit.Assert.fail
|
|
26
|
+
import org.junit.Rule
|
|
27
|
+
import org.junit.Test
|
|
28
|
+
import org.junit.rules.TemporaryFolder
|
|
29
|
+
import org.junit.runner.RunWith
|
|
30
|
+
import org.mockito.Mockito.RETURNS_DEFAULTS
|
|
31
|
+
import org.mockito.Mockito.mock
|
|
32
|
+
import org.mockito.stubbing.Answer
|
|
33
|
+
import org.robolectric.RobolectricTestRunner
|
|
34
|
+
import org.robolectric.RuntimeEnvironment
|
|
35
|
+
import org.robolectric.Shadows.shadowOf
|
|
36
|
+
import org.robolectric.annotation.Config
|
|
37
|
+
import org.robolectric.shadows.ShadowSystemClock
|
|
38
|
+
|
|
39
|
+
/** A controller's set of media items keeps where a handleTrackLoad left the player. */
|
|
40
|
+
@RunWith(RobolectricTestRunner::class)
|
|
41
|
+
@Config(sdk = [28])
|
|
42
|
+
class HandleTrackLoadSeekTest {
|
|
43
|
+
|
|
44
|
+
@get:Rule val folder = TemporaryFolder()
|
|
45
|
+
|
|
46
|
+
private val service = buildService()
|
|
47
|
+
private val player = service.get().player
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Playlist changes since the handler's player calls ran, which change it too; null until they
|
|
51
|
+
* have.
|
|
52
|
+
*/
|
|
53
|
+
private var setsAfterHandler: Int? = null
|
|
54
|
+
|
|
55
|
+
@After
|
|
56
|
+
fun destroy() {
|
|
57
|
+
service.destroy()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private fun handlingTrackLoadWith(onLoad: (Track) -> Unit) {
|
|
61
|
+
// JS runs the handler's player calls on the main thread before its promise settles.
|
|
62
|
+
val handler: (TrackLoadEvent) -> Promise<Promise<Unit>> = { event ->
|
|
63
|
+
val inner = JsPromise<Unit>()
|
|
64
|
+
val outer = JsPromise<Promise<Unit>>()
|
|
65
|
+
Handler(Looper.getMainLooper()).post {
|
|
66
|
+
onLoad(event.track)
|
|
67
|
+
setsAfterHandler = 0
|
|
68
|
+
inner.resolve(Unit)
|
|
69
|
+
outer.resolve(inner.promise)
|
|
70
|
+
}
|
|
71
|
+
outer.promise
|
|
72
|
+
}
|
|
73
|
+
val configuration =
|
|
74
|
+
NativeBrowserConfiguration(
|
|
75
|
+
path = null,
|
|
76
|
+
request = null,
|
|
77
|
+
requestResolver = null,
|
|
78
|
+
browse = null,
|
|
79
|
+
browseResolver = null,
|
|
80
|
+
media = null,
|
|
81
|
+
artwork = null,
|
|
82
|
+
nowPlayingArtwork = null,
|
|
83
|
+
routes = null,
|
|
84
|
+
singleTrack = null,
|
|
85
|
+
handleTrackLoad = handler,
|
|
86
|
+
androidControllerOfflineError = null,
|
|
87
|
+
viewMoreTitle = null,
|
|
88
|
+
carPlayLoadingTitle = null,
|
|
89
|
+
resolveAlbumPath = null,
|
|
90
|
+
formatNavigationError = null,
|
|
91
|
+
)
|
|
92
|
+
val browserManager = BrowserManager()
|
|
93
|
+
player.browser =
|
|
94
|
+
mock(
|
|
95
|
+
AudioBrowser::class.java,
|
|
96
|
+
Answer {
|
|
97
|
+
when {
|
|
98
|
+
it.method.name == "getConfiguration" -> configuration
|
|
99
|
+
it.method.name.startsWith("getBrowserManager") -> browserManager
|
|
100
|
+
else -> RETURNS_DEFAULTS.answer(it)
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
player.notifyBrowserConfigurationReady()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* As a controller starts a track: media3 passes no index or position on a reset. Returns once
|
|
109
|
+
* media3 has set the items the callback returned, after the handler.
|
|
110
|
+
*/
|
|
111
|
+
private fun setFromController(track: Track) {
|
|
112
|
+
player.exoPlayer.addListener(
|
|
113
|
+
object : MediaPlayer.Listener {
|
|
114
|
+
override fun onTimelineChanged(timeline: Timeline, reason: Int) {
|
|
115
|
+
if (reason == MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED) {
|
|
116
|
+
setsAfterHandler = setsAfterHandler?.plus(1)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
)
|
|
121
|
+
val future =
|
|
122
|
+
MediaController.Builder(RuntimeEnvironment.getApplication(), service.get().session.token)
|
|
123
|
+
.buildAsync()
|
|
124
|
+
idleUntil("the controller to connect") { future.isDone }
|
|
125
|
+
future.get().setMediaItem(TrackFactory.toMedia3(track))
|
|
126
|
+
idleUntil("media3 to set the items") { (setsAfterHandler ?: 0) >= 1 }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private fun idleUntil(what: String, condition: () -> Boolean) {
|
|
130
|
+
repeat(200) {
|
|
131
|
+
if (condition()) return
|
|
132
|
+
ShadowSystemClock.advanceBy(Duration.ofMillis(100))
|
|
133
|
+
Thread.sleep(10)
|
|
134
|
+
shadowOf(Looper.getMainLooper()).idle()
|
|
135
|
+
}
|
|
136
|
+
fail("timed out waiting for $what, the engine in state ${player.exoPlayer.playbackState}")
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private fun silentTrack(duration: Duration) =
|
|
140
|
+
TestFixtures.track(id = "a", src = folder.silentWavUri(duration))
|
|
141
|
+
|
|
142
|
+
@Test
|
|
143
|
+
fun `a load then a seek in the handler holds the seek`() {
|
|
144
|
+
val track = silentTrack(Duration.ofMinutes(2))
|
|
145
|
+
handlingTrackLoadWith {
|
|
146
|
+
player.load(it)
|
|
147
|
+
player.seekTo(60, TimeUnit.SECONDS)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
setFromController(track)
|
|
151
|
+
|
|
152
|
+
assertEquals(60_000L, player.exoPlayer.currentPosition)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
@Test
|
|
156
|
+
fun `a setQueue at a position in the handler holds the position`() {
|
|
157
|
+
val track = silentTrack(Duration.ofMinutes(2))
|
|
158
|
+
handlingTrackLoadWith { player.setQueue(arrayOf(it), 0, 60_000) }
|
|
159
|
+
|
|
160
|
+
setFromController(track)
|
|
161
|
+
|
|
162
|
+
assertEquals(60_000L, player.exoPlayer.currentPosition)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
@Test
|
|
166
|
+
fun `an ended track a handler leaves is set again at its start`() {
|
|
167
|
+
val track = silentTrack(Duration.ofMillis(500))
|
|
168
|
+
player.exoPlayer.trackSelectionParameters =
|
|
169
|
+
player.exoPlayer.trackSelectionParameters
|
|
170
|
+
.buildUpon()
|
|
171
|
+
.setTrackTypeDisabled(C.TRACK_TYPE_AUDIO, true)
|
|
172
|
+
.build()
|
|
173
|
+
player.setQueue(arrayOf(track))
|
|
174
|
+
player.play()
|
|
175
|
+
idleUntil("the track to end") { player.exoPlayer.playbackState == MediaPlayer.STATE_ENDED }
|
|
176
|
+
handlingTrackLoadWith {}
|
|
177
|
+
|
|
178
|
+
setFromController(track)
|
|
179
|
+
|
|
180
|
+
assertEquals(0L, player.exoPlayer.currentPosition)
|
|
181
|
+
}
|
|
182
|
+
}
|