watson-developer-cloud / android-sdk

:high_brightness: Android SDK to use the IBM Watson services.
http://watson-developer-cloud.github.io/android-sdk/
Apache License 2.0
146 stars 94 forks source link

Speech to Text: OggOpusEncoder hanging on close after Inactivity Timeout #50

Open smccants opened 6 years ago

smccants commented 6 years ago

I'm working on a sample Speech-to-text Android App that will timeout after a certain amount of inactivity from the speaker. For testing purposes, I set the timeout at 1 second. However, there seems to be a race condition (which I reproduced in the example by setting the inactivity timeout to 1 second) that causes the MicrophoneHelper to hang on closing.

The hang occurs in MicrophoneCaptureThread.run() line 110 - encoder.close() call. I'm not quite sure what is hanging it up, but if you toggle the microphone and then let it timeout after one or two attempts it will fail this way. The final hang is in MicrophoneInputStream.consume(byte[]).

Here is the link to the sample application I used: https://github.com/watson-developer-cloud/android-sdk/blob/master/example/src/main/java/com/ibm/watson/developer_cloud/android/myapplication/MainActivity.java

The only change I made was to getRecognizeOptions() where I changed the timeout from 2000 to 1.

smccants commented 6 years ago

Workaround: I use a timer in my app which calls "microphoneHelper.closeInputStream();". This seems to avoid the problem.

smccants commented 6 years ago

The problem was originally reported here: https://developer.ibm.com/answers/questions/435993/speech-to-text-oggopusencoder-hanging-on-close-aft.html?childToView=436412#answer-436412

lpatino10 commented 6 years ago

Hey @smccants, thanks for bringing this issue up. I've assigned myself to take a look at this as soon as I get some time and I'll report back with any findings.

smccants commented 6 years ago

I think I'm running into a slightly different flavor of this problem. Any update on when it might be fixed?

smccants commented 6 years ago

It looks like the hang is a result of this thread never exiting:

"Thread-5@5044" prio=5 tid=0x5c7 nid=NA waiting
  java.lang.Thread.State: WAITING
     blocks Thread-5@5044
      at java.lang.Object.wait(Object.java:-1)
      at java.lang.Object.wait(Object.java:407)
      at java.io.PipedInputStream.awaitSpace(PipedInputStream.java:280)
      at java.io.PipedInputStream.receive(PipedInputStream.java:238)
      - locked <0x15d5> (a java.io.PipedInputStream)
      at java.io.PipedOutputStream.write(PipedOutputStream.java:149)
      at java.io.OutputStream.write(OutputStream.java:75)
      at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneInputStream.consume(MicrophoneInputStream.java:142)
      at com.ibm.watson.developer_cloud.android.library.audio.opus.OpusWriter.write(OpusWriter.java:236)
      at com.ibm.watson.developer_cloud.android.library.audio.opus.OpusWriter.flush(OpusWriter.java:209)
      at com.ibm.watson.developer_cloud.android.library.audio.opus.OpusWriter.writePacket(OpusWriter.java:184)
      at com.ibm.watson.developer_cloud.android.library.audio.opus.OggOpusEnc.encodeAndWrite(OggOpusEnc.java:116)
      at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneCaptureThread.run(MicrophoneCaptureThread.java:99)

While this thread is hung, the main thread is in a spin loop waiting for it:

"main@4487" prio=5 tid=0x1 nid=NA sleeping
  java.lang.Thread.State: TIMED_WAITING
     blocks main@4487
      at java.lang.Thread.sleep(Thread.java:-1)
      at java.lang.Thread.sleep(Thread.java:371)
      - locked <0x15bb> (a java.lang.Object)
      at java.lang.Thread.sleep(Thread.java:313)
      at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneCaptureThread.end(MicrophoneCaptureThread.java:126)
      at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneInputStream.close(MicrophoneInputStream.java:109)
      at com.ibm.watson.developer_cloud.android.library.audio.MicrophoneHelper.closeInputStream(MicrophoneHelper.java:61)
      at com.hcs.android.watsonspeech.MainActivity.turnOffSpeechRecognition(MainActivity.java:96)
      at com.hcs.android.watsonspeech.MainActivity$1.onCheckedChanged(MainActivity.java:51)
      at android.widget.CompoundButton.setChecked(CompoundButton.java:166)
      at android.widget.Switch.setChecked(Switch.java:1127)
      at com.hcs.android.watsonspeech.MainActivity$5.run(MainActivity.java:133)
      at android.os.Handler.handleCallback(Handler.java:751)