Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ANR when recording an audio (WPB-9528) #3066

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.wire.android.ui.home.conversations.model.UriAsset
import com.wire.android.util.AUDIO_MIME_TYPE
import com.wire.android.util.CurrentScreen
import com.wire.android.util.CurrentScreenManager
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.getAudioLengthInMs
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase
Expand All @@ -59,7 +60,8 @@ class RecordAudioViewModel @Inject constructor(
private val generateAudioFileWithEffects: GenerateAudioFileWithEffectsUseCase,
private val currentScreenManager: CurrentScreenManager,
private val audioMediaRecorder: AudioMediaRecorder,
private val globalDataStore: GlobalDataStore
private val globalDataStore: GlobalDataStore,
private val dispatchers: DispatcherProvider
) : ViewModel() {

var state: RecordAudioState by mutableStateOf(RecordAudioState())
Expand Down Expand Up @@ -152,7 +154,7 @@ class RecordAudioViewModel @Inject constructor(
infoMessage.emit(RecordAudioInfoMessageType.UnableToRecordAudioCall.uiText)
}
} else {
viewModelScope.launch {
viewModelScope.launch(dispatchers.default()) {
val assetSizeLimit = getAssetSizeLimit(false)
audioMediaRecorder.setUp(assetSizeLimit)
if (audioMediaRecorder.startRecording()) {
Expand All @@ -169,31 +171,33 @@ class RecordAudioViewModel @Inject constructor(
}

fun stopRecording() {
if (state.buttonState == RecordAudioButtonState.RECORDING) {
audioMediaRecorder.stop()
}
audioMediaRecorder.release()
viewModelScope.launch(dispatchers.default()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep the viewModelScope.launch as it is and switch dispatchers for generateAudioFileWithEffects here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not only for generateAudioFileWithEffects, we need also to run getPlayableAudioFile(), and getAudioLengthInMs()and stop() in worker thread

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we can change of them individual, updating the state from a background thread can cause some issues.

Happy to talk about this in a 1:1 or in the next collective

if (state.buttonState == RecordAudioButtonState.RECORDING) {
audioMediaRecorder.stop()
}
audioMediaRecorder.release()

if (state.originalOutputFile != null && state.effectsOutputFile != null) {
generateAudioFileWithEffects(
context = context,
originalFilePath = state.originalOutputFile!!.path,
effectsFilePath = state.effectsOutputFile!!.path
)
if (state.originalOutputFile != null && state.effectsOutputFile != null) {
generateAudioFileWithEffects(
context = context,
originalFilePath = state.originalOutputFile!!.path,
effectsFilePath = state.effectsOutputFile!!.path
)

state = state.copy(
buttonState = RecordAudioButtonState.READY_TO_SEND,
audioState = AudioState.DEFAULT.copy(
totalTimeInMs = AudioState.TotalTimeInMs.Known(
getPlayableAudioFile()?.let {
getAudioLengthInMs(
dataPath = it.path.toPath(),
mimeType = AUDIO_MIME_TYPE
).toInt()
} ?: 0
state = state.copy(
buttonState = RecordAudioButtonState.READY_TO_SEND,
audioState = AudioState.DEFAULT.copy(
totalTimeInMs = AudioState.TotalTimeInMs.Known(
getPlayableAudioFile()?.let {
getAudioLengthInMs(
dataPath = it.path.toPath(),
mimeType = AUDIO_MIME_TYPE
).toInt()
} ?: 0
)
)
)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.wire.android.ui.home.messagecomposer.recordaudio
import android.content.Context
import app.cash.turbine.test
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.config.TestDispatcherProvider
import com.wire.android.datastore.GlobalDataStore
import com.wire.android.framework.FakeKaliumFileSystem
import com.wire.android.media.audiomessage.AudioState
Expand Down Expand Up @@ -277,6 +278,7 @@ class RecordAudioViewModelTest {
val globalDataStore = mockk<GlobalDataStore>()
val generateAudioFileWithEffects = mockk<GenerateAudioFileWithEffectsUseCase>()
val context = mockk<Context>()
val dispatchers = TestDispatcherProvider()

val viewModel by lazy {
RecordAudioViewModel(
Expand All @@ -287,7 +289,8 @@ class RecordAudioViewModelTest {
audioMediaRecorder = audioMediaRecorder,
getAssetSizeLimit = getAssetSizeLimit,
generateAudioFileWithEffects = generateAudioFileWithEffects,
globalDataStore = globalDataStore
globalDataStore = globalDataStore,
dispatchers = dispatchers
)
}

Expand Down
Loading