Skip to content

Commit

Permalink
Cancel ASR's Recognize event after ASR finished. #2506
Browse files Browse the repository at this point in the history
    * Even if finish ASR, async's response(directives) will be arrived on event channel.
      To cancel(close) this channel, add API at Request.
  • Loading branch information
sangho.lee authored and shshshl09 committed Jul 10, 2024
1 parent ed15f5c commit ad9e969
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions nugu-agent/src/main/java/com/skt/nugu/sdk/agent/DefaultASRAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ class DefaultASRAgent(
}
}

override fun onCancellableRetrieved(cancelable: ASRAgentInterface.StartRecognitionCallback.Cancellable) {
expectSpeechHandler?.startRecognitionCallback?.let {
runCatching {
it.onCancellableRetrieved(cancelable)
}
}
}

override fun onError(
dialogRequestId: String,
errorType: ASRAgentInterface.StartRecognitionCallback.ErrorType
Expand Down Expand Up @@ -868,6 +876,7 @@ class DefaultASRAgent(
} else {
currentRequest = Pair(it, param)
param.callback?.onSuccess(it.eventMessage.dialogRequestId)
param.callback?.onCancellableRetrieved(createASRCancellable(it))
}
}

Expand All @@ -876,6 +885,17 @@ class DefaultASRAgent(
}
}

private fun createASRCancellable(request: SpeechRecognizer.Request) =
object : ASRAgentInterface.StartRecognitionCallback.Cancellable {
override fun cancel() {
if (currentRequest?.first == request && state.isRecognizing()) {
stopRecognition(true, ASRAgentInterface.CancelCause.LOCAL_API)
} else {
request.cancelRequest()
}
}
}

override val configurations: Map<NamespaceAndName, BlockingPolicy> = HashMap<NamespaceAndName, BlockingPolicy>().apply {
this[EXPECT_SPEECH] = BlockingPolicy.sharedInstanceFactory.get(
BlockingPolicy.MEDIUM_AUDIO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ interface ASRAgentInterface {
ERROR_TAKE_TOO_LONG_START_RECOGNITION,
ERROR_UNKNOWN
}

interface Cancellable {
fun cancel()
}

fun onCancellableRetrieved(cancelable: Cancellable) = Unit
}

/**
Expand Down

0 comments on commit ad9e969

Please sign in to comment.