Start and stop streams
Streams are controlled with three TinyML verbs that each take a <Stream> noun:
| TinyML | Effect |
|---|---|
<Start><Stream .../></Start> |
Starts a one-way stream and goes on to the next verb. |
<Connect><Stream .../></Connect> |
Starts a two-way stream. The call stays on it; later verbs are not run. |
<Stop><Stream/></Stop> |
Stops the stream running on this call leg and goes on to the next verb. |
Element and attribute names are case-sensitive. Write them exactly as shown.
<Start><Stream>: one-way stream
Section titled “<Start><Stream>: one-way stream”<?xml version="1.0" encoding="UTF-8"?><Response> <Start> <Stream url="wss://stream.example.com/calls?token=s3cr3t" name="transcriber" track="both_tracks" statusCallback="https://example.com/stream-status"/> </Start> <Dial> <Number>+15550100123</Number> </Dial></Response>The stream starts, and the call goes on to <Dial> at once. Both sides of the conversation are streamed for as long as the call lasts.
A call ends when its TinyML runs out of verbs. Follow <Start> with the verbs the call should do next, such as <Dial>, <Enqueue>, <Gather> or <Pause>.
<Connect><Stream>: two-way stream
Section titled “<Connect><Stream>: two-way stream”<?xml version="1.0" encoding="UTF-8"?><Response> <Connect> <Stream url="wss://bot.example.com/voice" name="voice-bot" encoding="audio/L16" sampleRate="16000" statusCallback="https://example.com/stream-status">{"customer_id": "C-1042", "language": "en-US"}</Stream> </Connect></Response>Your server receives the caller’s audio, and the audio it sends back is played to the caller. Any verbs after <Connect> are ignored.
When the stream ends, for example because your server closes the connection, the call is not moved on automatically: it stays connected with no audio. Redirect it or end it with the Calls API.
<Stream> attributes
Section titled “<Stream> attributes”| Attribute | Used by | Values | Default | Description |
|---|---|---|---|---|
url |
Start, Connect | Absolute ws:// or wss:// URL |
Required | Your WebSocket server. See URL rules. |
name |
Start, Connect | Text | None | A label for the stream. It is returned as StreamName in status callbacks. |
track |
Start, Connect | inbound_track, outbound_track, both_tracks |
inbound_track |
Which audio to stream. inbound_track is the audio received from the other party on this call leg (for example the caller); outbound_track is the audio sent to them. With <Connect>, use inbound_track. |
encoding |
Start, Connect | audio/x-mulaw, audio/L16 |
audio/x-mulaw |
Audio encoding in both directions. See Audio formats. |
sampleRate |
Start, Connect | 8000, 16000, 16k, auto |
8000 |
Only used with audio/L16. 16000, 16k and auto all mean “follow the call, up to 16 kHz”: you get 16000 Hz when the call’s codec carries wideband audio and 8000 Hz otherwise. audio/x-mulaw is always 8000 Hz. Any other value means 8000. |
statusCallback |
Start, Connect | Absolute http:// or https:// URL |
None | Where to send stream status callbacks. Without it, no callbacks are sent. |
statusCallbackMethod |
Start, Connect | GET, POST |
POST |
HTTP method for status callbacks. |
The sample rate you actually get is in the start message (start.media_format.sample_rate). Always use that value rather than the one you asked for.
Use the values exactly as listed, in lower case except for L16.
URL rules
Section titled “URL rules”- The scheme must be
ws://orwss://. - The host name may contain only letters, digits,
-and.. Literal IPv6 addresses are not supported. - A port, if given, must be digits only.
- If you add a query string, put a path before it:
wss://bot.example.com/?token=abcworks;wss://bot.example.com?token=abcdoes not. - No spaces, and no
user:password@part.
Custom parameters
Section titled “Custom parameters”To pass data to your server, put a JSON object as the text content of <Stream>:
<Stream url="wss://bot.example.com/voice">{"customer_id": "C-1042", "vip": true}</Stream>It arrives unchanged as custom_parameters in the start message. Values can be any JSON type, including nested objects.
- The content must be a single JSON object. Escape
<and&as<and&, as in any XML text. - Leave
<Stream>empty (<Stream url="..."/>) when you have no parameters. Content that is only spaces or line breaks is not valid JSON, and the stream does not start. <Parameter>child elements are not supported. A<Stream>with child elements does not start.- Keep the object small: a few kilobytes at most.
- If you send TinyML inline when you create a call (the
TinyMLfield ofPOST /Calls), SIPRTC URL-decodes it first. In that case encode+as%2Band%as%25inside the parameters. This does not apply to TinyML returned from your answer URL or sent when you update a live call.
Custom parameters are a good place for a per-call token that your server checks before it trusts the stream.
<Stop><Stream>
Section titled “<Stop><Stream>”<Response> <Stop><Stream/></Stop> <Enqueue>support</Enqueue></Response><Stop><Stream/> ends the stream running on this call leg and goes on to the next verb. A call leg has at most one stream, so attributes on the <Stream> (including name) are ignored. <Stop> also ends a live transcription running on the leg. If nothing is streaming, <Stop> does nothing. Unlike <Start>, it does not answer a call that has not been answered.
Your server receives a stop message and the connection is closed normally (code 1000). Your status callback receives stream-stopped.
When a stream can’t start
Section titled “When a stream can’t start”| Situation | What happens |
|---|---|
url is missing or breaks the URL rules |
No stream and no status callback. <Start> goes on to the next verb. With <Connect> the call stays connected with no audio. |
The content of <Stream> is not a JSON object |
Same as above. |
| The call leg already has a stream or a live transcription | Same as above. |
| SIPRTC cannot connect to your server (DNS, refused connection, TLS error, the server doesn’t answer with HTTP 101) | The stream was started, so you get stream-error and then stream-stopped callbacks. |
| Your server closes the connection | The stream ends (stream-stopped). It is not reopened. |
Start or stop a stream on a live call
Section titled “Start or stop a stream on a live call”To change what a live call is doing, send it new TinyML with the Calls API:
POST https://api.siprtc.io/v1/Accounts/{auth_id}/Calls/{call_sid}Authenticate with your Auth ID and Auth Secret (HTTP basic authentication), and send a JSON body:
| Field | Type | Description |
|---|---|---|
TinyML |
string | TinyML to run on the call now. |
Url |
string | Instead of TinyML: a URL that returns TinyML. |
Method |
string | GET or POST, the method used to fetch Url. |
For example, to hand a call from a voice bot to a queue:
curl -X POST "https://api.siprtc.io/v1/Accounts/$AUTH_ID/Calls/$CALL_SID" \ -u "$AUTH_ID:$AUTH_SECRET" \ -H "Content-Type: application/json" \ -d '{"TinyML": "<Response><Stop><Stream/></Stop><Enqueue>support</Enqueue></Response>"}'To put a voice bot on a call that is already connected:
curl -X POST "https://api.siprtc.io/v1/Accounts/$AUTH_ID/Calls/$CALL_SID" \ -u "$AUTH_ID:$AUTH_SECRET" \ -H "Content-Type: application/json" \ -d '{"TinyML": "<Response><Connect><Stream url=\"wss://bot.example.com/voice\"/></Connect></Response>"}'Things to know about updating a live call:
- The call stops what it is doing (a prompt, a queue, a connection to another party) and runs the new TinyML from the start. The update takes effect within a few seconds.
- A stream that is already running is not stopped by an update. Begin the new TinyML with
<Stop><Stream/></Stop>when you move a call away from a two-way stream; otherwise the stream keeps playing to the caller. - The call ends when the new TinyML runs out of verbs, so always include what the call should do next.
- An update whose
TinyML(orUrl) is identical to the one the call is already using is ignored. - A successful update returns
200 OKwith the call resource.409 Conflictmeans the update could not be applied. If the call is no longer live, you get the finished call’s record, or404 Not Found.
There is no separate Streams resource. Streams are started and stopped only through TinyML.