|
@@ -7,9 +7,16 @@ import android.support.v4.media.session.PlaybackStateCompat
|
7
|
7
|
import android.util.Log
|
8
|
8
|
import androidx.lifecycle.MutableLiveData
|
9
|
9
|
import fr.forum_thalie.tsumugi.*
|
|
10
|
+import org.json.JSONObject
|
|
11
|
+import java.net.URL
|
|
12
|
+import java.text.ParseException
|
|
13
|
+import java.text.SimpleDateFormat
|
|
14
|
+import java.util.*
|
|
15
|
+import kotlin.collections.ArrayList
|
10
|
16
|
|
11
|
17
|
class PlayerStore {
|
12
|
18
|
|
|
19
|
+ private lateinit var urlToScrape: String
|
13
|
20
|
val isPlaying: MutableLiveData<Boolean> = MutableLiveData()
|
14
|
21
|
val isServiceStarted: MutableLiveData<Boolean> = MutableLiveData()
|
15
|
22
|
val volume: MutableLiveData<Int> = MutableLiveData()
|
|
@@ -27,6 +34,7 @@ class PlayerStore {
|
27
|
34
|
val listenersCount: MutableLiveData<Int> = MutableLiveData()
|
28
|
35
|
var latencyCompensator : Long = 0
|
29
|
36
|
var isInitialized: Boolean = false
|
|
37
|
+ var isStreamDown: Boolean = false
|
30
|
38
|
|
31
|
39
|
init {
|
32
|
40
|
playbackState.value = PlaybackStateCompat.STATE_STOPPED
|
|
@@ -43,10 +51,131 @@ class PlayerStore {
|
43
|
51
|
listenersCount.value = 0
|
44
|
52
|
}
|
45
|
53
|
|
|
54
|
+ fun initUrl(c: Context)
|
|
55
|
+ {
|
|
56
|
+ urlToScrape = c.getString(R.string.API_URL)
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ private fun getTimestamp(s: String) : Long
|
|
60
|
+ {
|
|
61
|
+ val dateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault())
|
|
62
|
+ try {
|
|
63
|
+ val t: Date? = dateFormat.parse(s)
|
|
64
|
+ return t!!.time
|
|
65
|
+ } catch (e: ParseException) {
|
|
66
|
+ e.printStackTrace()
|
|
67
|
+ }
|
|
68
|
+ return 0
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+ // ##################################################
|
|
73
|
+ // ################# API FUNCTIONS ##################
|
|
74
|
+ // ##################################################
|
|
75
|
+
|
|
76
|
+ private fun updateApi(res: JSONObject, isCompensatingLatency : Boolean = false) {
|
|
77
|
+ // If we're not in PLAYING state, update title / artist metadata. If we're playing, the ICY will take care of that.
|
|
78
|
+
|
|
79
|
+ val resMain = res.getJSONObject("tracks").getJSONObject("current")
|
|
80
|
+ val s = extractSong(resMain)
|
|
81
|
+ if (playbackState.value != PlaybackStateCompat.STATE_PLAYING || currentSong.title.value.isNullOrEmpty()
|
|
82
|
+ || currentSong.title.value == noConnectionValue)
|
|
83
|
+ currentSong.setTitleArtist("${s.artist.value} - ${s.title.value}")
|
|
84
|
+
|
|
85
|
+ val starts = s.startTime.value
|
|
86
|
+ val ends = s.stopTime.value
|
|
87
|
+
|
|
88
|
+ if (currentSong.startTime.value != starts)
|
|
89
|
+ currentSong.startTime.value = starts
|
|
90
|
+
|
|
91
|
+ currentSong.stopTime.value = ends
|
|
92
|
+
|
|
93
|
+ // I noticed that the server has a big (3 to 9 seconds !!) offset for current time.
|
|
94
|
+ // we can measure it when the player is playing, to compensate it and have our progress bar perfectly timed
|
|
95
|
+ // latencyCompensator is set to null when beginPlaying() (we can't measure it at the moment we start playing, since we're in the middle of a song),
|
|
96
|
+ // at this moment, we set it to 0. Then, next time the updateApi is called when we're playing, we measure the latency and we set out latencyComparator.
|
|
97
|
+ if(isCompensatingLatency)
|
|
98
|
+ {
|
|
99
|
+ latencyCompensator = getTimestamp(res.getJSONObject("station").getString("schedulerTime")) - (currentSong.startTime.value ?: getTimestamp(res.getJSONObject("station").getString("schedulerTime")))
|
|
100
|
+ Log.d(tag, "latency compensator set to ${(latencyCompensator).toFloat()/1000} s")
|
|
101
|
+ }
|
|
102
|
+ currentTime.value = getTimestamp(res.getJSONObject("station").getString("schedulerTime")) - (latencyCompensator)
|
|
103
|
+
|
|
104
|
+ /*
|
|
105
|
+ val listeners = resMain.getInt("listeners")
|
|
106
|
+ listenersCount.value = listeners
|
|
107
|
+ Log.d(tag, playerStoreTag + "store updated")
|
|
108
|
+ */
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ private val scrape : (Any?) -> String =
|
|
112
|
+ {
|
|
113
|
+ URL(urlToScrape).readText()
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ /* initApi is called :
|
|
117
|
+ - at startup
|
|
118
|
+ - when a streamer changes.
|
|
119
|
+ the idea is to fetch the queue when a streamer changes (potentially Hanyuu), and at startup.
|
|
120
|
+ The Last Played is only fetched if it's empty (so, only at startup), not when a streamer changes.
|
|
121
|
+ */
|
|
122
|
+ fun initApi()
|
|
123
|
+ {
|
|
124
|
+ val post : (parameter: Any?) -> Unit = {
|
|
125
|
+ val result = JSONObject(it as String)
|
|
126
|
+ if (result.has("tracks"))
|
|
127
|
+ {
|
|
128
|
+ updateApi(result)
|
|
129
|
+ currentSongBackup.copy(currentSong)
|
|
130
|
+
|
|
131
|
+ isQueueUpdated.value = true
|
|
132
|
+
|
|
133
|
+ isLpUpdated.value = true
|
|
134
|
+ }
|
|
135
|
+ isInitialized = true
|
|
136
|
+ }
|
|
137
|
+ Async(scrape, post)
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ fun fetchApi(isCompensatingLatency: Boolean = false) {
|
|
141
|
+ val post: (parameter: Any?) -> Unit = {
|
|
142
|
+ val result = JSONObject(it as String)
|
|
143
|
+ if (!result.isNull("tracks"))
|
|
144
|
+ {
|
|
145
|
+ updateApi(result, isCompensatingLatency)
|
|
146
|
+ }
|
|
147
|
+ }
|
|
148
|
+ Async(scrape, post)
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ private fun extractSong(songJSON: JSONObject) : Song {
|
|
152
|
+ val song = Song()
|
|
153
|
+ song.setTitleArtist(songJSON.getString("name"))
|
|
154
|
+ song.startTime.value = getTimestamp(songJSON.getString("starts"))
|
|
155
|
+ song.stopTime.value = getTimestamp(songJSON.getString("ends"))
|
|
156
|
+ song.type.value = 0 // only used for R/a/dio
|
|
157
|
+ return song
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+
|
46
|
161
|
// ##################################################
|
47
|
162
|
// ############## QUEUE / LP FUNCTIONS ##############
|
48
|
163
|
// ##################################################
|
49
|
164
|
|
|
165
|
+ fun updateQueue() {
|
|
166
|
+ if (queue.isNotEmpty()) {
|
|
167
|
+ queue.remove(queue.first())
|
|
168
|
+ Log.d(tag, queue.toString())
|
|
169
|
+ fetchLastRequest()
|
|
170
|
+ isQueueUpdated.value = true
|
|
171
|
+ } else if (isInitialized) {
|
|
172
|
+ fetchLastRequest()
|
|
173
|
+ } else {
|
|
174
|
+ Log.d(tag, "queue is empty! fetching anyway !!")
|
|
175
|
+ fetchLastRequest()
|
|
176
|
+ }
|
|
177
|
+ }
|
|
178
|
+
|
50
|
179
|
fun updateLp() {
|
51
|
180
|
// note : lp is empty at initialization. This check was needed when we used the R/a/dio API.
|
52
|
181
|
//if (lp.isNotEmpty()){
|
|
@@ -61,6 +190,74 @@ class PlayerStore {
|
61
|
190
|
}
|
62
|
191
|
|
63
|
192
|
|
|
193
|
+ private fun fetchLastRequest()
|
|
194
|
+ {
|
|
195
|
+ val sleepScrape: (Any?) -> String = {
|
|
196
|
+ /* we can maximize our chances to retrieve the last queued song by specifically waiting for the number of seconds we measure between ICY metadata and API change.
|
|
197
|
+ we add 2 seconds just to get a higher probability that the API has correctly updated. (the latency compensator can have a jitter of 1 second usually)
|
|
198
|
+ If, against all odds, the API hasn't updated yet, we will retry in the same amount of seconds. So we'll have the data anyway.
|
|
199
|
+ This way to fetch at the most probable time is a good compromise between fetch speed and fetch frequency
|
|
200
|
+ We don't fetch too often, and we start to fetch at the most *probable* time.
|
|
201
|
+ If there's no latencyCompensator measured yet, we only wait for 3 seconds.
|
|
202
|
+ If the song is the same, it will be called again. 3 seconds is a good compromise between speed and frequency:
|
|
203
|
+ it might be called twice, rarely 3 times, and it's only the 2 first songs ; after these, the latencyCompensator is set to fetch at the most probable time.
|
|
204
|
+ */
|
|
205
|
+ val sleepTime: Long = if (latencyCompensator > 0) latencyCompensator + 2000 else 3000
|
|
206
|
+ Thread.sleep(sleepTime) // we wait a bit (10s) for the API to get updated on R/a/dio side!
|
|
207
|
+ URL(urlToScrape).readText()
|
|
208
|
+ }
|
|
209
|
+
|
|
210
|
+ lateinit var post: (parameter: Any?) -> Unit
|
|
211
|
+
|
|
212
|
+ fun postFun(result: JSONObject)
|
|
213
|
+ {
|
|
214
|
+ if (result.has("tracks")) {
|
|
215
|
+ val resMain = result.getJSONObject("tracks")
|
|
216
|
+ /*
|
|
217
|
+ if ((resMain.has("isafkstream") && !resMain.getBoolean("isafkstream")) &&
|
|
218
|
+ queue.isNotEmpty())
|
|
219
|
+ {
|
|
220
|
+ queue.clear() //we're not requesting anything anymore.
|
|
221
|
+ isQueueUpdated.value = true
|
|
222
|
+ } else if (resMain.has("isafkstream") && resMain.getBoolean("isafkstream") &&
|
|
223
|
+ queue.isEmpty())
|
|
224
|
+ {
|
|
225
|
+ initApi()
|
|
226
|
+ } else
|
|
227
|
+ */
|
|
228
|
+ if (resMain.has("next") /*&& queue.isNotEmpty()*/) {
|
|
229
|
+ val queueJSON =
|
|
230
|
+ resMain.getJSONObject("next")
|
|
231
|
+ val t = extractSong(queueJSON)
|
|
232
|
+ if (queue.isNotEmpty() && t == queue.last())
|
|
233
|
+ {
|
|
234
|
+ Log.d(tag, playerStoreTag + "Song already in there: $t")
|
|
235
|
+ Async(sleepScrape, post)
|
|
236
|
+ } else {
|
|
237
|
+ queue.add(queue.size, t)
|
|
238
|
+ Log.d(tag, playerStoreTag + "added last queue song: $t")
|
|
239
|
+ isQueueUpdated.value = true
|
|
240
|
+ }
|
|
241
|
+ }
|
|
242
|
+ }
|
|
243
|
+ }
|
|
244
|
+
|
|
245
|
+ post = {
|
|
246
|
+ val result = JSONObject(it as String)
|
|
247
|
+ /* The goal is to pass the result to a function that will process it (postFun).
|
|
248
|
+ The magic trick is, under circumstances, the last queue song might not have been updated yet when we fetch it.
|
|
249
|
+ So if this is detected ==> if (t == queue.last() )
|
|
250
|
+ Then the function re-schedule an Async(sleepScrape, post).
|
|
251
|
+ To do that, the "post" must be defined BEFORE the function, but the function must be defined BEFORE the "post" value.
|
|
252
|
+ So I declare "post" as lateinit var, define the function, then define the "post" that calls the function. IT SHOULD WORK.
|
|
253
|
+ */
|
|
254
|
+ postFun(result)
|
|
255
|
+ }
|
|
256
|
+
|
|
257
|
+ Async(sleepScrape, post)
|
|
258
|
+ }
|
|
259
|
+
|
|
260
|
+
|
64
|
261
|
// ##################################################
|
65
|
262
|
// ############## PICTURE FUNCTIONS #################
|
66
|
263
|
// ##################################################
|