PlayerStore.kt 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package io.r_a_d.radio2.playerstore
  2. import android.content.Context
  3. import android.graphics.Bitmap
  4. import android.graphics.BitmapFactory
  5. import android.support.v4.media.session.PlaybackStateCompat
  6. import android.util.Log
  7. import androidx.lifecycle.MutableLiveData
  8. import androidx.lifecycle.ViewModel
  9. import io.r_a_d.radio2.*
  10. import org.json.JSONObject
  11. import java.io.IOException
  12. import java.io.InputStream
  13. import java.net.URL
  14. class PlayerStore {
  15. val isPlaying: MutableLiveData<Boolean> = MutableLiveData()
  16. val isServiceStarted: MutableLiveData<Boolean> = MutableLiveData()
  17. val volume: MutableLiveData<Int> = MutableLiveData()
  18. val playbackState: MutableLiveData<Int> = MutableLiveData()
  19. val currentTime: MutableLiveData<Long> = MutableLiveData()
  20. val streamerPicture: MutableLiveData<Bitmap> = MutableLiveData()
  21. val streamerName: MutableLiveData<String> = MutableLiveData()
  22. val currentSong : Song = Song()
  23. val currentSongBackup: Song = Song()
  24. val lp : ArrayList<Song> = ArrayList()
  25. val queue : ArrayList<Song> = ArrayList()
  26. val isQueueUpdated: MutableLiveData<Boolean> = MutableLiveData()
  27. val isLpUpdated: MutableLiveData<Boolean> = MutableLiveData()
  28. val isMuted : MutableLiveData<Boolean> = MutableLiveData()
  29. val listenersCount: MutableLiveData<Int> = MutableLiveData()
  30. private val urlToScrape = "https://r-a-d.io/api"
  31. var latencyCompensator : Long = 0
  32. var isInitialized: Boolean = false
  33. var isStreamDown: Boolean = false
  34. init {
  35. playbackState.value = PlaybackStateCompat.STATE_STOPPED
  36. isPlaying.value = false
  37. isServiceStarted.value = false
  38. streamerName.value = ""
  39. volume.value = preferenceStore.getInt("volume", 100)
  40. currentTime.value = System.currentTimeMillis()
  41. isQueueUpdated.value = false
  42. isLpUpdated.value = false
  43. isMuted.value = false
  44. currentSong.title.value = noConnectionValue
  45. listenersCount.value = 0
  46. }
  47. // ##################################################
  48. // ################# API FUNCTIONS ##################
  49. // ##################################################
  50. private fun updateApi(resMain: JSONObject, isCompensatingLatency : Boolean = false) {
  51. // If we're not in PLAYING state, update title / artist metadata. If we're playing, the ICY will take care of that.
  52. if (playbackState.value != PlaybackStateCompat.STATE_PLAYING || currentSong.title.value.isNullOrEmpty()
  53. || currentSong.title.value == noConnectionValue)
  54. currentSong.setTitleArtist(resMain.getString("np"))
  55. // only update the value if the song has changed. This avoids to trigger observers when they shouldn't be triggered
  56. if (currentSong.startTime.value != resMain.getLong("start_time")*1000)
  57. currentSong.startTime.value = resMain.getLong("start_time")*1000
  58. // I noticed that the server has a big (3 to 9 seconds !!) offset for current time.
  59. // we can measure it when the player is playing, to compensate it and have our progress bar perfectly timed
  60. // 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),
  61. // 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.
  62. if(isCompensatingLatency)
  63. {
  64. latencyCompensator = resMain.getLong("current")*1000 - (currentSong.startTime.value ?: resMain.getLong("current")*1000)
  65. Log.d(tag, playerStoreTag + "latency compensator set to ${(latencyCompensator).toFloat()/1000} s")
  66. }
  67. currentSong.stopTime.value = resMain.getLong("end_time")*1000
  68. currentTime.value = (resMain.getLong("current"))*1000 - (latencyCompensator)
  69. val newStreamer = resMain.getJSONObject("dj").getString("djname")
  70. if (newStreamer != streamerName.value)
  71. {
  72. val streamerPictureUrl =
  73. "${urlToScrape}/dj-image/${resMain.getJSONObject("dj").getString("djimage")}"
  74. fetchPicture(streamerPictureUrl)
  75. streamerName.value = newStreamer
  76. }
  77. val listeners = resMain.getInt("listeners")
  78. listenersCount.value = listeners
  79. Log.d(tag, playerStoreTag + "store updated")
  80. }
  81. private val scrape : (Any?) -> String =
  82. {
  83. URL(urlToScrape).readText()
  84. }
  85. /* initApi is called :
  86. - at startup
  87. - when a streamer changes.
  88. the idea is to fetch the queue when a streamer changes (potentially Hanyuu), and at startup.
  89. The Last Played is only fetched if it's empty (so, only at startup), not when a streamer changes.
  90. */
  91. fun initApi()
  92. {
  93. val post : (parameter: Any?) -> Unit = {
  94. val result = JSONObject(it as String)
  95. if (result.has("main"))
  96. {
  97. val resMain = result.getJSONObject("main")
  98. updateApi(resMain)
  99. currentSongBackup.copy(currentSong)
  100. queue.clear()
  101. if (resMain.has("queue") && resMain.getBoolean("isafkstream"))
  102. {
  103. val queueJSON =
  104. resMain.getJSONArray("queue")
  105. for (i in 0 until queueJSON.length())
  106. {
  107. val t = extractSong(queueJSON[i] as JSONObject)
  108. if (t != currentSong) // if the API is too slow and didn't remove the first song from queue...
  109. queue.add(queue.size, t)
  110. }
  111. }
  112. isQueueUpdated.value = true
  113. Log.d(tag, playerStoreTag + queue.toString())
  114. if (resMain.has("lp"))
  115. {
  116. val queueJSON =
  117. resMain.getJSONArray("lp")
  118. // if my stack is empty, I fill it entirely (startup)
  119. if (lp.isEmpty())
  120. {
  121. for (i in 0 until queueJSON.length())
  122. lp.add(lp.size, extractSong(queueJSON[i] as JSONObject))
  123. }
  124. }
  125. Log.d(tag, playerStoreTag + lp.toString())
  126. isLpUpdated.value = true
  127. }
  128. isInitialized = true
  129. }
  130. Async(scrape, post)
  131. }
  132. fun fetchApi(isCompensatingLatency: Boolean = false) {
  133. val post: (parameter: Any?) -> Unit = {
  134. val result = JSONObject(it as String)
  135. if (!result.isNull("main"))
  136. {
  137. val res = result.getJSONObject("main")
  138. updateApi(res, isCompensatingLatency)
  139. }
  140. }
  141. Async(scrape, post)
  142. }
  143. // ##################################################
  144. // ############## QUEUE / LP FUNCTIONS ##############
  145. // ##################################################
  146. fun updateLp() {
  147. // note : lp must never be empty. There should always be some songs "last played".
  148. // if not, then the function has been called before initialization. No need to do anything.
  149. if (lp.isNotEmpty()){
  150. val n = Song()
  151. n.copy(currentSongBackup)
  152. lp.add(0, n)
  153. currentSongBackup.copy(currentSong)
  154. isLpUpdated.value = true
  155. Log.d(tag, playerStoreTag + lp.toString())
  156. }
  157. }
  158. fun updateQueue() {
  159. if (queue.isNotEmpty()) {
  160. queue.remove(queue.first())
  161. Log.d(tag, playerStoreTag + queue.toString())
  162. fetchLastRequest()
  163. isQueueUpdated.value = true
  164. } else if (isInitialized) {
  165. fetchLastRequest()
  166. } else {
  167. Log.d(tag, playerStoreTag + "queue is empty!")
  168. }
  169. }
  170. private fun fetchLastRequest()
  171. {
  172. val sleepScrape: (Any?) -> String = {
  173. /* 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.
  174. 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)
  175. 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.
  176. This way to fetch at the most probable time is a good compromise between fetch speed and fetch frequency
  177. We don't fetch too often, and we start to fetch at the most *probable* time.
  178. If there's no latencyCompensator measured yet, we only wait for 3 seconds.
  179. If the song is the same, it will be called again. 3 seconds is a good compromise between speed and frequency:
  180. 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.
  181. */
  182. val sleepTime: Long = if (latencyCompensator > 0) latencyCompensator + 2000 else 3000
  183. Thread.sleep(sleepTime) // we wait a bit (10s) for the API to get updated on R/a/dio side!
  184. URL(urlToScrape).readText()
  185. }
  186. lateinit var post: (parameter: Any?) -> Unit
  187. fun postFun(result: JSONObject)
  188. {
  189. if (result.has("main")) {
  190. val resMain = result.getJSONObject("main")
  191. if ((resMain.has("isafkstream") && !resMain.getBoolean("isafkstream")) &&
  192. queue.isNotEmpty())
  193. {
  194. queue.clear() //we're not requesting anything anymore.
  195. isQueueUpdated.value = true
  196. } else if (resMain.has("isafkstream") && resMain.getBoolean("isafkstream") &&
  197. queue.isEmpty())
  198. {
  199. initApi()
  200. } else if (resMain.has("queue") && queue.isNotEmpty()) {
  201. val queueJSON =
  202. resMain.getJSONArray("queue")
  203. val t = extractSong(queueJSON[4] as JSONObject)
  204. if (t == queue.last())
  205. {
  206. Log.d(tag, playerStoreTag + "Song already in there: $t")
  207. Async(sleepScrape, post)
  208. } else {
  209. queue.add(queue.size, t)
  210. Log.d(tag, playerStoreTag + "added last queue song: $t")
  211. isQueueUpdated.value = true
  212. }
  213. }
  214. }
  215. }
  216. post = {
  217. val result = JSONObject(it as String)
  218. /* The goal is to pass the result to a function that will process it (postFun).
  219. The magic trick is, under circumstances, the last queue song might not have been updated yet when we fetch it.
  220. So if this is detected ==> if (t == queue.last() )
  221. Then the function re-schedule an Async(sleepScrape, post).
  222. To do that, the "post" must be defined BEFORE the function, but the function must be defined BEFORE the "post" value.
  223. So I declare "post" as lateinit var, define the function, then define the "post" that calls the function. IT SHOULD WORK.
  224. */
  225. postFun(result)
  226. }
  227. Async(sleepScrape, post)
  228. }
  229. private fun extractSong(songJSON: JSONObject) : Song {
  230. val song = Song()
  231. song.setTitleArtist(songJSON.getString("meta"))
  232. song.startTime.value = songJSON.getLong("timestamp")
  233. song.stopTime.value = song.startTime.value
  234. song.type.value = songJSON.getInt("type")
  235. return song
  236. }
  237. // ##################################################
  238. // ############## PICTURE FUNCTIONS #################
  239. // ##################################################
  240. private fun fetchPicture(fileUrl: String)
  241. {
  242. val scrape: (Any?) -> Bitmap? = {
  243. var k: InputStream? = null
  244. var pic: Bitmap? = null
  245. try {
  246. k = URL(fileUrl).content as InputStream
  247. val options = BitmapFactory.Options()
  248. options.inSampleSize = 1
  249. // this makes 1/2 of origin image size from width and height.
  250. // it alleviates the memory for API16-API19 especially
  251. pic = BitmapFactory.decodeStream(k, null, options)
  252. k.close()
  253. } catch (e: IOException) {
  254. e.printStackTrace()
  255. } finally {
  256. k?.close()
  257. }
  258. pic
  259. }
  260. val post : (parameter: Any?) -> Unit = {
  261. streamerPicture.postValue(it as Bitmap?)
  262. }
  263. Async(scrape, post)
  264. }
  265. fun initPicture(c: Context) {
  266. streamerPicture.value = BitmapFactory.decodeResource(c.resources,
  267. R.drawable.actionbar_logo
  268. )
  269. }
  270. private val playerStoreTag = "====PlayerStore===="
  271. companion object {
  272. val instance by lazy {
  273. PlayerStore()
  274. }
  275. }
  276. }