refactor: improve background streaming service and error handling

- Updated BackgroundStreamingService to start in the foreground immediately to prevent timeout issues, with a placeholder notification.
- Enhanced error handling in BackgroundStreamingHandler to catch exceptions when starting the foreground service, ensuring active streams are cleared if the service fails to start.
- Refactored saveStreamStatesForRecovery method to improve logging and clarity in stream state management.
- Added checks to close suspended stream controllers when transitioning to the foreground, enhancing resource management.
This commit is contained in:
cogwheel0
2025-10-05 23:16:44 +05:30
parent 8b06d5a179
commit a8ae2644f5
4 changed files with 136 additions and 22 deletions

View File

@@ -31,15 +31,22 @@ class BackgroundStreamingService : Service() {
override fun onCreate() {
super.onCreate()
println("BackgroundStreamingService: Service created")
// Immediately start foreground to prevent timeout - will update with proper notification later
startForeground(NOTIFICATION_ID, createNotification(1))
println("BackgroundStreamingService: Service created with foreground notification")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// Always ensure we're foreground first to prevent timeout exceptions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundWithNotification(1)
}
when (intent?.action) {
ACTION_START -> {
val streamCount = intent.getIntExtra("streamCount", 1)
acquireWakeLock()
startForegroundWithNotification(streamCount)
updateNotification(streamCount)
println("BackgroundStreamingService: Started foreground service for $streamCount streams")
}
ACTION_STOP -> {
@@ -51,7 +58,7 @@ class BackgroundStreamingService : Service() {
updateNotification(streamCount)
}
}
return START_STICKY // Restart if killed by system
}
@@ -239,14 +246,20 @@ class BackgroundStreamingHandler(private val activity: MainActivity) : MethodCal
}
private fun startForegroundService() {
val serviceIntent = Intent(context, BackgroundStreamingService::class.java)
serviceIntent.putExtra("streamCount", activeStreams.size)
serviceIntent.action = BackgroundStreamingService.ACTION_START
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent)
} else {
context.startService(serviceIntent)
try {
val serviceIntent = Intent(context, BackgroundStreamingService::class.java)
serviceIntent.putExtra("streamCount", activeStreams.size)
serviceIntent.action = BackgroundStreamingService.ACTION_START
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent)
} else {
context.startService(serviceIntent)
}
} catch (e: Exception) {
println("BackgroundStreamingHandler: Failed to start foreground service: ${e.message}")
// Clear active streams as we couldn't start the service
activeStreams.clear()
}
}