|
@@ -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,6 +51,112 @@ 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
|
+ if (playbackState.value != PlaybackStateCompat.STATE_PLAYING || currentSong.title.value.isNullOrEmpty()
|
|
81
|
+ || currentSong.title.value == noConnectionValue)
|
|
82
|
+ currentSong.setTitleArtist(resMain.getString("name"))
|
|
83
|
+
|
|
84
|
+ val starts = getTimestamp(resMain.getString("starts"))
|
|
85
|
+ val ends = getTimestamp(resMain.getString("ends"))
|
|
86
|
+
|
|
87
|
+ if (currentSong.startTime.value != starts)
|
|
88
|
+ currentSong.startTime.value = starts
|
|
89
|
+
|
|
90
|
+ currentSong.stopTime.value = ends
|
|
91
|
+
|
|
92
|
+ // I noticed that the server has a big (3 to 9 seconds !!) offset for current time.
|
|
93
|
+ // we can measure it when the player is playing, to compensate it and have our progress bar perfectly timed
|
|
94
|
+ // 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),
|
|
95
|
+ // 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.
|
|
96
|
+ if(isCompensatingLatency)
|
|
97
|
+ {
|
|
98
|
+ latencyCompensator = getTimestamp(res.getJSONObject("station").getString("schedulerTime")) - (currentSong.startTime.value ?: getTimestamp(res.getJSONObject("station").getString("schedulerTime")))
|
|
99
|
+ Log.d(tag, playerStoreTag + "latency compensator set to ${(latencyCompensator).toFloat()/1000} s")
|
|
100
|
+ }
|
|
101
|
+ currentTime.value = getTimestamp(res.getJSONObject("station").getString("schedulerTime")) - (latencyCompensator)
|
|
102
|
+
|
|
103
|
+ /*
|
|
104
|
+ val listeners = resMain.getInt("listeners")
|
|
105
|
+ listenersCount.value = listeners
|
|
106
|
+ Log.d(tag, playerStoreTag + "store updated")
|
|
107
|
+ */
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ private val scrape : (Any?) -> String =
|
|
111
|
+ {
|
|
112
|
+ URL(urlToScrape).readText()
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ /* initApi is called :
|
|
116
|
+ - at startup
|
|
117
|
+ - when a streamer changes.
|
|
118
|
+ the idea is to fetch the queue when a streamer changes (potentially Hanyuu), and at startup.
|
|
119
|
+ The Last Played is only fetched if it's empty (so, only at startup), not when a streamer changes.
|
|
120
|
+ */
|
|
121
|
+ fun initApi()
|
|
122
|
+ {
|
|
123
|
+ val post : (parameter: Any?) -> Unit = {
|
|
124
|
+ val result = JSONObject(it as String)
|
|
125
|
+ if (result.has("tracks"))
|
|
126
|
+ {
|
|
127
|
+ updateApi(result)
|
|
128
|
+ currentSongBackup.copy(currentSong)
|
|
129
|
+
|
|
130
|
+ isQueueUpdated.value = true
|
|
131
|
+
|
|
132
|
+ isLpUpdated.value = true
|
|
133
|
+ }
|
|
134
|
+ isInitialized = true
|
|
135
|
+ }
|
|
136
|
+ Async(scrape, post)
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ fun fetchApi(isCompensatingLatency: Boolean = false) {
|
|
140
|
+ val post: (parameter: Any?) -> Unit = {
|
|
141
|
+ val result = JSONObject(it as String)
|
|
142
|
+ if (!result.isNull("tracks"))
|
|
143
|
+ {
|
|
144
|
+ updateApi(result, isCompensatingLatency)
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+ Async(scrape, post)
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ private fun extractSong(songJSON: JSONObject) : Song {
|
|
151
|
+ val song = Song()
|
|
152
|
+ song.setTitleArtist(songJSON.getString("meta"))
|
|
153
|
+ song.startTime.value = songJSON.getLong("timestamp")
|
|
154
|
+ song.stopTime.value = song.startTime.value
|
|
155
|
+ song.type.value = songJSON.getInt("type")
|
|
156
|
+ return song
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+
|
46
|
160
|
// ##################################################
|
47
|
161
|
// ############## QUEUE / LP FUNCTIONS ##############
|
48
|
162
|
// ##################################################
|