Class RecognitionSession

java.lang.Object
com.alibaba.dashscope.resources.audio.RecognitionSession
All Implemented Interfaces:
AutoCloseable

public final class RecognitionSession extends Object implements AutoCloseable
ASR realtime session (audio design §1.1): ws duplex over the task protocol. Pull model — no SDK threads; user discipline is one send thread + one receive thread, finally close().

Pumped events are buffered inside the session, so stop() may be called before consuming results (the design-doc pattern) and events()/results() can replay from the beginning at any time. close() is idempotent and the session can be re-start()ed (new task, new socket unless a shared WsConnection was supplied).

  • Method Details

    • start

      public void start()
      Connect (unless a shared connection was supplied), send run-task, block until task-started.
    • sendAudioFrame

      public void sendAudioFrame(byte[] data)
      Send one binary audio frame (zero-copy path upstream; at most one copy internally).
    • sendAudioFrame

      public void sendAudioFrame(ByteBuffer data)
    • updateContext

      public void updateContext(Map<String,Object> payloadInput)
      continue-task with the map carried as payload.input verbatim (2.x parity).
    • stop

      public void stop()
      finish-task and wait for task-finished with the client receive timeout.
    • stop

      public void stop(Duration timeout)
      finish-task and wait for task-finished, bounded by timeout (fixes the 2.x unbounded stopLatch.await() deadlock). Events pumped while waiting stay buffered for events()/results().
    • events

      public Iterable<com.fasterxml.jackson.databind.JsonNode> events()
      Raw event frames (header included), task-finished included; task-failed raises.
    • results

      public Iterable<AsrResult> results()
      Typed results: result-generated only, server heartbeat sentences filtered out.
    • getTaskId

      public String getTaskId()
    • getRequestId

      public String getRequestId()
    • getLastResponse

      public com.fasterxml.jackson.databind.JsonNode getLastResponse()
      The last text frame received (raw, header included).
    • getUsage

      public Usage getUsage()
    • getFirstPackageDelay

      public Duration getFirstPackageDelay()
      First audio frame to first result; null until both happened.
    • getLastPackageDelay

      public Duration getLastPackageDelay()
      finish-task to task-finished; null until both happened.
    • close

      public void close()
      Idempotent. With a shared connection this detaches the task only — unless the task owns the binary uplink and closed before its terminal event, which tears the whole shared connection down (binary frames carry no task id, so the lane cannot be handed over safely). The session may be re-started afterwards; on a torn-down shared connection the re-start fails fast.
      Specified by:
      close in interface AutoCloseable