Skip to content

Commit 20319b9

Browse files
committed
Fix NetworkOnMainThreadException in Pepper video streaming
1 parent 3026592 commit 20319b9

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎app/src/pepper/java/ch/fhnw/pepper_realtime/controller/VideoInputControllerImpl.kt‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,7 @@ class VideoInputControllerImpl @Inject constructor(
8484

8585
Log.i(TAG, "Starting video streaming at 1 FPS using Pepper's head camera")
8686

87-
// Build TakePicture action
88-
try {
89-
takePictureAction = TakePictureBuilder.with(qiContext).build()
90-
Log.d(TAG, "TakePicture action built successfully")
91-
} catch (e: Exception) {
92-
Log.e(TAG, "Failed to build TakePicture action", e)
93-
return false
94-
}
95-
87+
// Set streaming state and start the loop (TakePicture will be built on IO thread)
9688
_isStreaming.value = true
9789
startStreamingLoop()
9890
return true
@@ -102,6 +94,18 @@ class VideoInputControllerImpl @Inject constructor(
10294
streamingJob = serviceScope.launch {
10395
Log.i(TAG, "Streaming loop started")
10496

97+
// Build TakePicture action on IO thread to avoid NetworkOnMainThreadException
98+
try {
99+
takePictureAction = withContext(Dispatchers.IO) {
100+
TakePictureBuilder.with(qiContext).build()
101+
}
102+
Log.d(TAG, "TakePicture action built successfully")
103+
} catch (e: Exception) {
104+
Log.e(TAG, "Failed to build TakePicture action", e)
105+
_isStreaming.value = false
106+
return@launch
107+
}
108+
105109
while (isActive && _isStreaming.value) {
106110
try {
107111
captureAndSendFrame()

0 commit comments

Comments
 (0)