-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
SenderReceiverService.kt
471 lines (396 loc) · 15.7 KB
/
SenderReceiverService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package org.rocstreaming.rocdroid
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.drawable.Icon
import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioPlaybackCaptureConfiguration
import android.media.AudioRecord
import android.media.AudioTrack
import android.media.MediaRecorder
import android.media.projection.MediaProjection
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import org.rocstreaming.roctoolkit.Address
import org.rocstreaming.roctoolkit.ChannelSet
import org.rocstreaming.roctoolkit.Family
import org.rocstreaming.roctoolkit.FrameEncoding
import org.rocstreaming.roctoolkit.PortType
import org.rocstreaming.roctoolkit.Protocol
import org.rocstreaming.roctoolkit.Receiver
import org.rocstreaming.roctoolkit.ReceiverConfig
import org.rocstreaming.roctoolkit.Sender
import org.rocstreaming.roctoolkit.SenderConfig
private const val SAMPLE_RATE = 44100
private const val BUFFER_SIZE = 100
private const val DEFAULT_RTP_PORT_SOURCE = 10001
private const val DEFAULT_RTP_PORT_REPAIR = 10002
private const val CHANNEL_ID = "SenderReceiverService"
private const val NOTIFICATION_ID = 1
private const val BROADCAST_STOP_SENDER_ACTION =
"org.rocstreaming.rocdroid.NotificationSenderStopAction"
private const val BROADCAST_STOP_RECEIVER_ACTION =
"org.rocstreaming.rocdroid.NotificationReceiverStopAction"
private const val LOG_TAG = "[rocdroid.SenderReceiverService]"
class SenderReceiverService : Service() {
private var receiverThread: Thread? = null
private var senderThread: Thread? = null
private var receiverChanged: ((Boolean) -> Unit)? = null
private var senderChanged: ((Boolean) -> Unit)? = null
private var isForegroundRunning = false
private val notificationStopActionReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
BROADCAST_STOP_SENDER_ACTION -> stopSender()
BROADCAST_STOP_RECEIVER_ACTION -> stopReceiver()
}
}
}
private val binder = LocalBinder()
inner class LocalBinder : Binder() {
fun getService(): SenderReceiverService = this@SenderReceiverService
}
override fun onBind(intent: Intent): IBinder {
Log.d(LOG_TAG, "Bind Service")
return binder
}
override fun onCreate() {
Log.d(LOG_TAG, "Creating Sender/Receiver Service")
createNotificationChannel()
registerReceiver(
notificationStopActionReceiver,
IntentFilter().apply {
addAction(BROADCAST_STOP_SENDER_ACTION)
addAction(BROADCAST_STOP_RECEIVER_ACTION)
}
)
}
override fun onDestroy() {
Log.d(LOG_TAG, "Destroying Sender/Receiver Service")
super.onDestroy()
unregisterReceiver(notificationStopActionReceiver)
}
private fun createNotificationChannel() {
Log.d(LOG_TAG, "Creating Notification Channel")
val channel = NotificationChannel(
CHANNEL_ID,
getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_LOW
)
val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
service.createNotificationChannel(channel)
}
private fun buildNotification(sending: Boolean, receiving: Boolean): Notification {
Log.d(
LOG_TAG,
String.format(
"Building Notification for %s %s",
if (sending) "Sender" else "",
if (receiving) "Receiver" else ""
)
)
val mainActivityIntent = Intent(this, MainActivity::class.java)
val pendingMainActivityIntent = PendingIntent.getActivity(
this,
0,
mainActivityIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val stopSenderIntent = Intent(BROADCAST_STOP_SENDER_ACTION)
val pendingStopSenderIntent = PendingIntent.getBroadcast(
this@SenderReceiverService,
0,
stopSenderIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val stopReceiverIntent = Intent(BROADCAST_STOP_RECEIVER_ACTION)
val pendingStopReceiverIntent = PendingIntent.getBroadcast(
this@SenderReceiverService,
0,
stopReceiverIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val stopSenderAction = Notification.Action.Builder(
Icon.createWithResource(this@SenderReceiverService, R.drawable.ic_stop),
getString(R.string.notification_stop_sender_action),
pendingStopSenderIntent
).build()
val stopReceiverAction = Notification.Action.Builder(
Icon.createWithResource(this@SenderReceiverService, R.drawable.ic_stop),
getString(R.string.notification_stop_receiver_action),
pendingStopReceiverIntent
).build()
return Notification.Builder(this, CHANNEL_ID).apply {
setContentTitle(getString(R.string.notification_title))
setContentText(getContentText(sending, receiving))
setSmallIcon(R.drawable.ic_notification)
setVisibility(Notification.VISIBILITY_PUBLIC)
setContentIntent(pendingMainActivityIntent)
if (sending) {
addAction(stopSenderAction)
}
if (receiving) {
addAction(stopReceiverAction)
}
}.build()
}
private fun updateNotification(sending: Boolean, receiving: Boolean) {
Log.d(
LOG_TAG,
String.format(
"Updating Notification for %s %s",
if (sending) "Sender" else "",
if (receiving) "Receiver" else ""
)
)
if (!isForegroundRunning) {
return
}
if (receiving || sending) {
val notification = buildNotification(sending, receiving)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID, notification)
} else {
stopForegroundService()
}
}
private fun getContentText(sending: Boolean, receiving: Boolean): String {
Log.d(LOG_TAG, "Getting Notification Content Text")
if (sending && receiving) {
return getString(R.string.notification_sender_and_receiver_running)
}
if (receiving) {
return getString(R.string.notification_receiver_running)
}
if (sending) {
return getString(R.string.notification_sender_running)
}
return getString(R.string.notification_sender_and_receiver_not_running) // this shouldn't happen
}
fun preStartSender() {
Log.d(LOG_TAG, "Prestart Sender")
if (isForegroundRunning) {
updateNotification(true, isReceiverAlive())
} else {
startForegroundService(true, isReceiverAlive())
}
}
fun startSender(ip: String, projection: MediaProjection?) {
Log.d(LOG_TAG, "Starting Sender")
if (senderThread?.isAlive == true) return
senderThread = Thread {
val record = createAudioRecord(projection)
val config = SenderConfig.Builder(
SAMPLE_RATE,
ChannelSet.STEREO,
FrameEncoding.PCM_FLOAT
).build()
org.rocstreaming.roctoolkit.Context().use { context ->
if (record.state != AudioRecord.STATE_INITIALIZED) return@use
record.startRecording()
Sender(context, config).use useSender@{ sender ->
sender.bind(Address(Family.AUTO, "0.0.0.0", 0))
try {
sender.connect(
PortType.AUDIO_SOURCE,
Protocol.RTP_RS8M_SOURCE,
Address(Family.AUTO, ip, DEFAULT_RTP_PORT_SOURCE)
)
sender.connect(
PortType.AUDIO_REPAIR,
Protocol.RS8M_REPAIR,
Address(Family.AUTO, ip, DEFAULT_RTP_PORT_REPAIR)
)
} catch (e: Exception) {
AlertDialog.Builder(this@SenderReceiverService).apply {
setTitle(getString(R.string.invalid_ip_title))
setMessage(getString(R.string.invalid_ip_message))
setCancelable(false)
setPositiveButton(R.string.ok) { _, _ -> }
}.show()
return@useSender
}
senderChanged?.invoke(true)
val samples = FloatArray(BUFFER_SIZE)
while (!Thread.currentThread().isInterrupted) {
record.read(samples, 0, samples.size, AudioRecord.READ_BLOCKING)
sender.write(samples)
}
}
record.stop()
record.release()
senderChanged?.invoke(false)
updateNotification(false, isReceiverAlive())
}
}
senderThread!!.start()
}
fun startReceiver() {
Log.d(LOG_TAG, "Starting Receiver")
if (receiverThread?.isAlive == true) return
receiverThread = Thread {
val audioTrack = createAudioTrack()
audioTrack.play()
val config = ReceiverConfig.Builder(
SAMPLE_RATE,
ChannelSet.STEREO,
FrameEncoding.PCM_FLOAT
).build()
org.rocstreaming.roctoolkit.Context().use { context ->
Receiver(context, config).use { receiver ->
receiver.bind(
PortType.AUDIO_SOURCE,
Protocol.RTP_RS8M_SOURCE,
Address(Family.AUTO, "0.0.0.0", DEFAULT_RTP_PORT_SOURCE)
)
receiver.bind(
PortType.AUDIO_REPAIR,
Protocol.RS8M_REPAIR,
Address(Family.AUTO, "0.0.0.0", DEFAULT_RTP_PORT_REPAIR)
)
receiverChanged?.invoke(true)
val samples = FloatArray(BUFFER_SIZE)
while (!Thread.currentThread().isInterrupted) {
receiver.read(samples)
audioTrack.write(samples, 0, samples.size, AudioTrack.WRITE_BLOCKING)
}
}
}
audioTrack.release()
receiverChanged?.invoke(false)
updateNotification(isSenderAlive(), false)
}
receiverThread!!.start()
if (isForegroundRunning) {
updateNotification(isSenderAlive(), true)
} else {
startForegroundService(isSenderAlive(), true)
}
}
fun stopSender() {
Log.d(LOG_TAG, "Stopping Sender")
senderThread?.interrupt()
}
fun stopReceiver() {
Log.d(LOG_TAG, "Stopping Receiver")
receiverThread?.interrupt()
}
fun isReceiverAlive(): Boolean {
Log.d(LOG_TAG, "Checking If Receiver Alive")
return receiverThread?.isAlive == true
}
fun isSenderAlive(): Boolean {
Log.d(LOG_TAG, "Checking If Sender Alive")
return senderThread?.isAlive == true
}
private fun startForegroundService(sending: Boolean, receiving: Boolean) {
Log.d(
LOG_TAG,
String.format(
"Starting Foreground Service for %s %s",
if (sending) "Sender" else "",
if (receiving) "Receiver" else ""
)
)
isForegroundRunning = true
startForeground(NOTIFICATION_ID, buildNotification(sending, receiving))
}
private fun stopForegroundService() {
Log.d(LOG_TAG, "Stopping Foreground Service")
isForegroundRunning = false
stopForeground(true)
}
private fun createAudioTrack(): AudioTrack {
Log.d(LOG_TAG, "Creating Audio Track")
val audioAttributes = AudioAttributes.Builder().apply {
setUsage(AudioAttributes.USAGE_MEDIA)
setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
}.build()
val audioFormat = AudioFormat.Builder().apply {
setSampleRate(SAMPLE_RATE)
setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
}.build()
val bufferSize = AudioTrack.getMinBufferSize(
audioFormat.sampleRate,
audioFormat.channelMask,
audioFormat.encoding
)
return AudioTrack.Builder().apply {
setAudioAttributes(audioAttributes)
setAudioFormat(audioFormat)
setBufferSizeInBytes(bufferSize)
setTransferMode(AudioTrack.MODE_STREAM)
setPerformanceMode(AudioTrack.PERFORMANCE_MODE_LOW_LATENCY)
}.build()
}
private fun createAudioRecord(projection: MediaProjection?): AudioRecord {
Log.d(LOG_TAG, "Creating Audio Record")
val format = AudioFormat.Builder().apply {
setSampleRate(SAMPLE_RATE)
setChannelMask(AudioFormat.CHANNEL_IN_STEREO)
setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
}.build()
val bufferSize = AudioRecord.getMinBufferSize(
SAMPLE_RATE,
AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_FLOAT
)
return if (projection != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
createPaybackRecord(projection, format, bufferSize)
} else {
AudioRecord.Builder().apply {
setAudioSource(MediaRecorder.AudioSource.DEFAULT)
setAudioFormat(format)
setBufferSizeInBytes(bufferSize)
}.build()
}
}
@RequiresApi(Build.VERSION_CODES.Q)
private fun createPaybackRecord(
projection: MediaProjection,
format: AudioFormat,
bufferSize: Int
): AudioRecord {
Log.d(LOG_TAG, "Creating Playback Record")
val config = AudioPlaybackCaptureConfiguration.Builder(projection).apply {
addMatchingUsage(AudioAttributes.USAGE_MEDIA)
addMatchingUsage(AudioAttributes.USAGE_UNKNOWN)
addMatchingUsage(AudioAttributes.USAGE_GAME)
}.build()
return AudioRecord.Builder().apply {
setAudioPlaybackCaptureConfig(config)
setAudioFormat(format)
setBufferSizeInBytes(bufferSize)
}.build()
}
fun setSenderStateChangedListeners(
senderChanged: (Boolean) -> Unit
) {
Log.d(LOG_TAG, "Setting Sender State Changed Listener")
this.senderChanged = senderChanged
}
fun setReceiverStateChangedListeners(
receiverChanged: (Boolean) -> Unit
) {
Log.d(LOG_TAG, "Setting Receiver State Changed Listener")
this.receiverChanged = receiverChanged
}
fun removeListeners() {
Log.d(LOG_TAG, "Removing State Changed Listeners")
this.receiverChanged = null
this.senderChanged = null
}
}