mirror of
https://github.com/AsamK/signal-cli.git
synced 2026-03-15 02:40:23 +00:00
Merge 9493381f57b28b56943ef174428023f09f29ae3c into 2885ffeee867203f5f21f65a9af2a080f92188ec
This commit is contained in:
commit
fd0bb1cbc4
@ -91,6 +91,14 @@ dependencies {
|
||||
implementation(libs.logback)
|
||||
implementation(libs.zxing)
|
||||
implementation(project(":libsignal-cli"))
|
||||
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testImplementation(platform(libs.junit.jupiter.bom))
|
||||
testRuntimeOnly(libs.junit.launcher)
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
configurations {
|
||||
|
||||
372
docs/CALL_TUNNEL.md
Normal file
372
docs/CALL_TUNNEL.md
Normal file
@ -0,0 +1,372 @@
|
||||
# Voice Call Support
|
||||
|
||||
## Overview
|
||||
|
||||
signal-cli supports voice calls by spawning a subprocess called
|
||||
`signal-call-tunnel` for each call. The tunnel handles WebRTC negotiation and
|
||||
audio transport. signal-cli communicates with it over a Unix domain socket using
|
||||
newline-delimited JSON messages, relaying signaling between the tunnel and the
|
||||
Signal protocol.
|
||||
|
||||
```
|
||||
signal-cli signal-call-tunnel
|
||||
| |
|
||||
|-- spawn (config on stdin) --------->|
|
||||
| |
|
||||
|<======= ctrl.sock (JSON) ==========>|
|
||||
| signaling relay | WebRTC
|
||||
| | audio I/O
|
||||
| |
|
||||
```
|
||||
|
||||
Each call gets its own tunnel process and control socket inside a temporary
|
||||
directory (`/tmp/sc-<random>/`). When the call ends, signal-cli kills the
|
||||
process and deletes the directory.
|
||||
|
||||
Audio device names (`inputDeviceName`, `outputDeviceName`) are opaque strings
|
||||
returned by the tunnel in its `ready` message. signal-cli passes them through
|
||||
to JSON-RPC clients, which use them to connect audio via platform APIs.
|
||||
|
||||
---
|
||||
|
||||
## Spawning the Tunnel
|
||||
|
||||
For each call, signal-cli:
|
||||
|
||||
1. Creates a temporary directory `/tmp/sc-<random>/` (mode `0700`)
|
||||
2. Generates a random 32-byte auth token
|
||||
3. Spawns `signal-call-tunnel` with config JSON on stdin
|
||||
4. Connects to the control socket (retries up to 50x at 200 ms intervals)
|
||||
5. Authenticates with the auth token
|
||||
|
||||
The `signal-call-tunnel` binary is located by searching (in order):
|
||||
|
||||
1. `SIGNAL_CALL_TUNNEL_BIN` environment variable
|
||||
2. `<signal-cli install dir>/bin/signal-call-tunnel`
|
||||
3. `signal-call-tunnel` on `PATH`
|
||||
|
||||
### Config JSON
|
||||
|
||||
Written to the tunnel's stdin before it starts:
|
||||
|
||||
```json
|
||||
{
|
||||
"call_id": 12345,
|
||||
"is_outgoing": true,
|
||||
"control_socket_path": "/tmp/sc-a1b2c3/ctrl.sock",
|
||||
"control_token": "dG9rZW4...",
|
||||
"local_device_id": 1,
|
||||
"input_device_name": "signal_input",
|
||||
"output_device_name": "signal_output"
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Type | Description |
|
||||
|-------|------|-------------|
|
||||
| `call_id` | unsigned 64-bit integer | Call identifier (use unsigned representation) |
|
||||
| `is_outgoing` | boolean | Whether this is an outgoing call |
|
||||
| `control_socket_path` | string | Path where the tunnel creates its control socket |
|
||||
| `control_token` | string | Base64-encoded 32-byte auth token |
|
||||
| `local_device_id` | integer | Signal device ID |
|
||||
| `input_device_name` | string (optional) | Requested input audio device name |
|
||||
| `output_device_name` | string (optional) | Requested output audio device name |
|
||||
|
||||
If `input_device_name` or `output_device_name` are omitted, the tunnel
|
||||
chooses default names. On Linux, these are per-call unique names (e.g.,
|
||||
`signal_input_<call_id>`). On macOS, these are the fixed names `signal_input`
|
||||
and `signal_output`, which must match the pre-installed BlackHole drivers.
|
||||
|
||||
---
|
||||
|
||||
## Control Socket Protocol
|
||||
|
||||
Unix SOCK_STREAM at `ctrl.sock`. Newline-delimited JSON messages.
|
||||
|
||||
### Authentication
|
||||
|
||||
The first message from signal-cli **must** be an auth message. The token is
|
||||
a random 32-byte value generated per call and passed in the startup config.
|
||||
The tunnel performs constant-time comparison.
|
||||
|
||||
```json
|
||||
{"type":"auth","token":"<base64-encoded token>"}
|
||||
```
|
||||
|
||||
### signal-cli -> Tunnel
|
||||
|
||||
| Type | When | Fields |
|
||||
|------|------|--------|
|
||||
| `auth` | First message | `token` |
|
||||
| `createOutgoingCall` | Outgoing call setup | `callId`, `peerId` |
|
||||
| `proceed` | After offer/receivedOffer | `callId`, `hideIp`, `iceServers` |
|
||||
| `receivedOffer` | Incoming call | `callId`, `peerId`, `opaque`, `age`, `senderDeviceId`, `senderIdentityKey`, `receiverIdentityKey` |
|
||||
| `receivedAnswer` | Outgoing call answered | `opaque`, `senderDeviceId`, `senderIdentityKey`, `receiverIdentityKey` |
|
||||
| `receivedIce` | ICE candidates arrive | `candidates` (array of base64 opaque blobs) |
|
||||
| `accept` | User accepts incoming call | *(none)* |
|
||||
| `hangup` | End the call | *(none)* |
|
||||
|
||||
### Tunnel -> signal-cli
|
||||
|
||||
| Type | When | Fields |
|
||||
|------|------|--------|
|
||||
| `ready` | Control socket bound, audio devices created | `inputDeviceName`, `outputDeviceName` |
|
||||
| `sendOffer` | Tunnel generated an offer | `callId`, `opaque`, `callMediaType` |
|
||||
| `sendAnswer` | Tunnel generated an answer | `callId`, `opaque` |
|
||||
| `sendIce` | ICE candidates gathered | `callId`, `candidates` (array of `{"opaque":"..."}`) |
|
||||
| `sendHangup` | Tunnel wants to hang up | `callId`, `hangupType` |
|
||||
| `sendBusy` | Line is busy | `callId` |
|
||||
| `stateChange` | Call state transition | `state`, `reason` (optional) |
|
||||
| `error` | Something went wrong | `message` |
|
||||
|
||||
Opaque blobs and identity keys are base64-encoded. ICE servers use the format:
|
||||
|
||||
```json
|
||||
{"urls":["turn:example.com"],"username":"u","password":"p"}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Startup Sequence
|
||||
|
||||
```
|
||||
signal-cli signal-call-tunnel
|
||||
| |
|
||||
|-- spawn process ------------------> |
|
||||
| (config JSON on stdin) |
|
||||
| | initialize
|
||||
| | bind ctrl.sock
|
||||
| |
|
||||
|-- connect to ctrl.sock -------------->|
|
||||
| (retries: 50x @ 200ms) |
|
||||
|<-------- ready -----------------------|
|
||||
| {"type":"ready", |
|
||||
| "inputDeviceName":"...", |
|
||||
| "outputDeviceName":"..."} |
|
||||
|-- auth ------------------------------>|
|
||||
| {"type":"auth","token":"<b64>"} |
|
||||
| | constant-time token verify
|
||||
| |
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Call Flows
|
||||
|
||||
### Outgoing call
|
||||
|
||||
```
|
||||
signal-cli signal-call-tunnel Remote Phone
|
||||
| | |
|
||||
|-- spawn + config ------->| |
|
||||
|<-- ready ----------------| |
|
||||
|-- auth ----------------->| |
|
||||
|-- createOutgoingCall --->| |
|
||||
|-- proceed (TURN) ------->| |
|
||||
| | create offer |
|
||||
|<-- sendOffer ------------| |
|
||||
|-- offer via Signal -------------------------------->|
|
||||
|<-- answer via Signal -------------------------------|
|
||||
|-- receivedAnswer ------->| (+ identity keys) |
|
||||
|<-- sendIce --------------| |
|
||||
|-- ICE via Signal -------------------------------> |
|
||||
|<-- ICE via Signal -------------------------------- |
|
||||
|-- receivedIce ---------->| |
|
||||
| | ICE connects |
|
||||
|<-- stateChange:Connected | |
|
||||
```
|
||||
|
||||
### Incoming call
|
||||
|
||||
```
|
||||
signal-cli signal-call-tunnel Remote Phone
|
||||
| | |
|
||||
|<-- offer via Signal --------------------------------|
|
||||
|-- spawn + config ------->| |
|
||||
|<-- ready ----------------| |
|
||||
|-- auth ----------------->| |
|
||||
|-- receivedOffer -------->| (+ identity keys) |
|
||||
|-- proceed (TURN) ------->| |
|
||||
| | process offer |
|
||||
|<-- sendAnswer -----------| |
|
||||
|-- answer via Signal -------------------------------->|
|
||||
|<-- sendIce --------------| |
|
||||
|-- ICE via Signal ------------------------------> |
|
||||
|<-- ICE via Signal -------------------------------- |
|
||||
|-- receivedIce ---------->| |
|
||||
| | ICE connecting... |
|
||||
| | |
|
||||
| (user accepts call) | |
|
||||
| Java defers accept | |
|
||||
| | |
|
||||
|<-- stateChange:Ringing --| (tunnel ready to accept)|
|
||||
|-- accept --------------->| (deferred accept sent) |
|
||||
| | accept |
|
||||
|<-- stateChange:Connected | |
|
||||
```
|
||||
|
||||
### JSON-RPC client perspective
|
||||
|
||||
An external application (bot, UI, test script) interacts via JSON-RPC only.
|
||||
It never touches the control socket directly.
|
||||
|
||||
```
|
||||
JSON-RPC Client signal-cli daemon
|
||||
| |
|
||||
|-- startCall(recipient) ------------->|
|
||||
|<-- {callId, state, -|
|
||||
| inputDeviceName, |
|
||||
| outputDeviceName} |
|
||||
| |
|
||||
|<-- callEvent: RINGING_OUTGOING ------|
|
||||
| ... remote answers ... |
|
||||
|<-- callEvent: CONNECTED -------------|
|
||||
| |
|
||||
| connect to audio devices |
|
||||
| (via platform audio APIs) |
|
||||
| |
|
||||
|-- hangupCall(callId) --------------->| (or: receive callEvent ENDED)
|
||||
|<-- callEvent: ENDED -----------------|
|
||||
| disconnect from audio devices |
|
||||
```
|
||||
|
||||
For incoming calls:
|
||||
|
||||
```
|
||||
JSON-RPC Client signal-cli daemon
|
||||
| |
|
||||
|<-- callEvent: RINGING_INCOMING ------| (includes callId, device names)
|
||||
| |
|
||||
|-- acceptCall(callId) --------------->|
|
||||
|<-- {callId, state, -|
|
||||
| inputDeviceName, |
|
||||
| outputDeviceName} |
|
||||
| |
|
||||
|<-- callEvent: CONNECTING ------------|
|
||||
|<-- callEvent: CONNECTED -------------|
|
||||
| |
|
||||
| connect to audio devices |
|
||||
| (via platform audio APIs) |
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## State Machine
|
||||
|
||||
Call states as seen by JSON-RPC clients:
|
||||
|
||||
```
|
||||
startCall()
|
||||
|
|
||||
v
|
||||
+----- RINGING_OUTGOING ----+ RINGING_INCOMING -----+
|
||||
| | | | |
|
||||
| (timeout | (answered) | (rejected) | acceptCall() | (timeout
|
||||
| ~60s) | | | | ~60s)
|
||||
v v v v v
|
||||
ENDED CONNECTED ENDED CONNECTING ENDED
|
||||
| |
|
||||
| v
|
||||
| CONNECTED
|
||||
| |
|
||||
| (hangup/error) | (hangup/error)
|
||||
v v
|
||||
ENDED ENDED
|
||||
```
|
||||
|
||||
For outgoing calls, `CONNECTED` fires directly when the tunnel reports
|
||||
`Connected` state -- there is no intermediate `CONNECTING` event.
|
||||
|
||||
For incoming calls, `CONNECTING` is set by Java when the user calls
|
||||
`acceptCall()`, before the tunnel completes ICE negotiation.
|
||||
|
||||
Both directions have a 60-second ring timeout.
|
||||
|
||||
Reconnection (ICE restart):
|
||||
|
||||
```
|
||||
CONNECTED --> RECONNECTING --> CONNECTED (ICE restart succeeded)
|
||||
|
|
||||
v
|
||||
ENDED (ICE restart failed)
|
||||
```
|
||||
|
||||
`RECONNECTING` maps from the tunnel's `Connecting` state, which is emitted
|
||||
during ICE restarts (not during initial connection).
|
||||
|
||||
---
|
||||
|
||||
## CallManager.java
|
||||
|
||||
`lib/src/main/java/org/asamk/signal/manager/helper/CallManager.java`
|
||||
|
||||
Manages the call lifecycle from the Java side:
|
||||
|
||||
1. Creates a temp directory and generates a random auth token
|
||||
2. Spawns `signal-call-tunnel` with config JSON on stdin
|
||||
3. Connects to the control socket (retries up to 50x at 200 ms intervals),
|
||||
authenticates, and relays signaling between the tunnel and the Signal protocol
|
||||
4. Parses `inputDeviceName` and `outputDeviceName` from the tunnel's `ready`
|
||||
message and includes them in `CallInfo`
|
||||
5. Translates tunnel state changes into `CallInfo.State` values and fires
|
||||
`callEvent` JSON-RPC notifications to connected clients
|
||||
6. Defers the `accept` message for incoming calls until the tunnel reports
|
||||
`Ringing` state (sending earlier causes the tunnel to drop it)
|
||||
7. Schedules a 60-second ring timeout for both incoming and outgoing calls
|
||||
8. On hangup: sends hangup message, kills the process, deletes the control socket
|
||||
|
||||
---
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
### Peer ID consistency
|
||||
|
||||
The `peerId` field in `createOutgoingCall` and `receivedOffer` must be the actual
|
||||
remote peer UUID (e.g., `senderAddress.toString()`). The tunnel rejects ICE
|
||||
candidates if the peer ID doesn't match across calls, causing "Ignoring
|
||||
peer-reflexive ICE candidate because the ufrag is unknown."
|
||||
|
||||
### sendHangup semantics
|
||||
|
||||
`sendHangup` from the tunnel is a request to send a hangup message via Signal
|
||||
protocol. It is **not** a local state change -- local state transitions come
|
||||
exclusively from `stateChange` events. For single-device clients, ignore
|
||||
`AcceptedOnAnotherDevice`, `DeclinedOnAnotherDevice`, and
|
||||
`BusyOnAnotherDevice` hangup types in the `hangupType` field -- sending these to
|
||||
the remote peer causes it to terminate the call prematurely.
|
||||
|
||||
### Call ID serialization
|
||||
|
||||
Call IDs can exceed `Long.MAX_VALUE` in Java. Use `Long.toUnsignedString()` when
|
||||
serializing to JSON for the tunnel (which expects unsigned 64-bit integers). In
|
||||
the config JSON, `call_id` should also use unsigned representation.
|
||||
|
||||
### Incoming hangup filtering
|
||||
|
||||
When receiving hangup messages via Signal protocol, only honor `NORMAL` type
|
||||
hangups. `ACCEPTED`, `DECLINED`, and `BUSY` types are multi-device coordination
|
||||
messages and should be ignored by single-device clients.
|
||||
|
||||
### JSON-RPC call ID types
|
||||
|
||||
JSON-RPC clients may send call IDs as various numeric types (Long, BigInteger,
|
||||
Integer). Use `Number.longValue()` rather than direct casting when extracting
|
||||
call IDs from JSON-RPC parameters.
|
||||
|
||||
### Identity key format
|
||||
|
||||
Identity keys in `senderIdentityKey` and `receiverIdentityKey` must be **raw
|
||||
32-byte Curve25519 public keys** (without the 0x05 DJB type prefix). If the
|
||||
33-byte serialized form is used instead, SRTP key derivation produces different
|
||||
keys on each side, causing authentication failures.
|
||||
|
||||
---
|
||||
|
||||
## File Layout
|
||||
|
||||
```
|
||||
/tmp/sc-<random>/
|
||||
ctrl.sock control socket (signal-cli <-> tunnel)
|
||||
```
|
||||
|
||||
The control socket is created with mode `0700` on the parent directory. The
|
||||
directory and its contents are deleted when the call ends.
|
||||
@ -37,7 +37,11 @@ dependencies {
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
useJUnitPlatform()
|
||||
useJUnitPlatform {
|
||||
if (!project.hasProperty("includeIntegration")) {
|
||||
excludeTags("integration")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
|
||||
@ -64,6 +64,10 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
import org.asamk.signal.manager.api.CallOffer;
|
||||
import org.asamk.signal.manager.api.TurnServer;
|
||||
|
||||
public interface Manager extends Closeable {
|
||||
|
||||
static boolean isValidNumber(final String e164Number, final String countryCode) {
|
||||
@ -413,9 +417,37 @@ public interface Manager extends Closeable {
|
||||
|
||||
InputStream retrieveSticker(final StickerPackId stickerPackId, final int stickerId) throws IOException;
|
||||
|
||||
// --- Voice call methods ---
|
||||
|
||||
CallInfo startCall(RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
|
||||
|
||||
CallInfo acceptCall(long callId) throws IOException;
|
||||
|
||||
void hangupCall(long callId) throws IOException;
|
||||
|
||||
void rejectCall(long callId) throws IOException;
|
||||
|
||||
List<CallInfo> listActiveCalls();
|
||||
|
||||
void sendCallOffer(RecipientIdentifier.Single recipient, CallOffer offer) throws IOException, UnregisteredRecipientException;
|
||||
|
||||
void sendCallAnswer(RecipientIdentifier.Single recipient, long callId, byte[] answerOpaque) throws IOException, UnregisteredRecipientException;
|
||||
|
||||
void sendIceUpdate(RecipientIdentifier.Single recipient, long callId, List<byte[]> iceCandidates) throws IOException, UnregisteredRecipientException;
|
||||
|
||||
void sendHangup(RecipientIdentifier.Single recipient, long callId, MessageEnvelope.Call.Hangup.Type type) throws IOException, UnregisteredRecipientException;
|
||||
|
||||
void sendBusy(RecipientIdentifier.Single recipient, long callId) throws IOException, UnregisteredRecipientException;
|
||||
|
||||
List<TurnServer> getTurnServerInfo() throws IOException;
|
||||
|
||||
@Override
|
||||
void close();
|
||||
|
||||
void addCallEventListener(CallEventListener listener);
|
||||
|
||||
void removeCallEventListener(CallEventListener listener);
|
||||
|
||||
interface ReceiveMessageHandler {
|
||||
|
||||
ReceiveMessageHandler EMPTY = (envelope, e) -> {
|
||||
@ -423,4 +455,9 @@ public interface Manager extends Closeable {
|
||||
|
||||
void handleMessage(MessageEnvelope envelope, Throwable e);
|
||||
}
|
||||
|
||||
interface CallEventListener {
|
||||
|
||||
void handleCallEvent(CallInfo callInfo, String reason);
|
||||
}
|
||||
}
|
||||
|
||||
21
lib/src/main/java/org/asamk/signal/manager/api/CallInfo.java
Normal file
21
lib/src/main/java/org/asamk/signal/manager/api/CallInfo.java
Normal file
@ -0,0 +1,21 @@
|
||||
package org.asamk.signal.manager.api;
|
||||
|
||||
public record CallInfo(
|
||||
long callId,
|
||||
State state,
|
||||
RecipientAddress recipient,
|
||||
String inputDeviceName,
|
||||
String outputDeviceName,
|
||||
boolean isOutgoing
|
||||
) {
|
||||
|
||||
public enum State {
|
||||
IDLE,
|
||||
RINGING_INCOMING,
|
||||
RINGING_OUTGOING,
|
||||
CONNECTING,
|
||||
CONNECTED,
|
||||
RECONNECTING,
|
||||
ENDED
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package org.asamk.signal.manager.api;
|
||||
|
||||
public record CallOffer(
|
||||
long callId,
|
||||
Type type,
|
||||
byte[] opaque
|
||||
) {
|
||||
|
||||
public enum Type {
|
||||
AUDIO,
|
||||
VIDEO
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package org.asamk.signal.manager.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record TurnServer(
|
||||
String username,
|
||||
String password,
|
||||
List<String> urls
|
||||
) {
|
||||
}
|
||||
@ -0,0 +1,858 @@
|
||||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
import org.asamk.signal.manager.api.TurnServer;
|
||||
import org.asamk.signal.manager.api.UnregisteredRecipientException;
|
||||
import org.asamk.signal.manager.internal.SignalDependencies;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Manages active voice calls: tracks state, spawns/monitors the signal-call-tunnel
|
||||
* subprocess, routes incoming call messages, and handles timeouts.
|
||||
*/
|
||||
public class CallManager implements AutoCloseable {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CallManager.class);
|
||||
private static final long RING_TIMEOUT_MS = 60_000;
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private final Context context;
|
||||
private final SignalAccount account;
|
||||
private final SignalDependencies dependencies;
|
||||
private final Map<Long, CallState> activeCalls = new ConcurrentHashMap<>();
|
||||
private final List<Manager.CallEventListener> callEventListeners = new CopyOnWriteArrayList<>();
|
||||
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
var t = new Thread(r, "call-timeout-scheduler");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
|
||||
public CallManager(final Context context) {
|
||||
this.context = context;
|
||||
this.account = context.getAccount();
|
||||
this.dependencies = context.getDependencies();
|
||||
}
|
||||
|
||||
public void addCallEventListener(Manager.CallEventListener listener) {
|
||||
callEventListeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeCallEventListener(Manager.CallEventListener listener) {
|
||||
callEventListeners.remove(listener);
|
||||
}
|
||||
|
||||
private void fireCallEvent(CallState state, String reason) {
|
||||
var callInfo = state.toCallInfo();
|
||||
for (var listener : callEventListeners) {
|
||||
try {
|
||||
listener.handleCallEvent(callInfo, reason);
|
||||
} catch (Throwable e) {
|
||||
logger.warn("Call event listener failed, ignoring", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CallInfo startOutgoingCall(
|
||||
final RecipientIdentifier.Single recipient
|
||||
) throws IOException, UnregisteredRecipientException {
|
||||
var callId = generateCallId();
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
|
||||
var recipientAddress = context.getRecipientHelper()
|
||||
.resolveSignalServiceAddress(recipientId)
|
||||
.getServiceId();
|
||||
var recipientApiAddress = account.getRecipientAddressResolver()
|
||||
.resolveRecipientAddress(recipientId)
|
||||
.toApiRecipientAddress();
|
||||
|
||||
// Create per-call socket directory
|
||||
var callDir = Files.createTempDirectory(Path.of("/tmp"), "sc-");
|
||||
Files.setPosixFilePermissions(callDir, PosixFilePermissions.fromString("rwx------"));
|
||||
var controlSocketPath = callDir.resolve("ctrl.sock").toString();
|
||||
|
||||
var state = new CallState(callId,
|
||||
CallInfo.State.RINGING_OUTGOING,
|
||||
recipientApiAddress,
|
||||
recipient,
|
||||
true,
|
||||
controlSocketPath,
|
||||
callDir);
|
||||
activeCalls.put(callId, state);
|
||||
fireCallEvent(state, null);
|
||||
|
||||
// Spawn call tunnel binary and connect control channel
|
||||
spawnMediaTunnel(state);
|
||||
|
||||
// Fetch TURN servers
|
||||
var turnServers = getTurnServers();
|
||||
|
||||
// Send createOutgoingCall + proceed via control channel
|
||||
var peerIdStr = recipientAddress.toString();
|
||||
sendControlMessage(state, "{\"type\":\"createOutgoingCall\",\"callId\":" + callIdJson(callId)
|
||||
+ ",\"peerId\":\"" + escapeJson(peerIdStr) + "\"}");
|
||||
sendProceed(state, callId, turnServers);
|
||||
|
||||
// Schedule ring timeout
|
||||
scheduler.schedule(() -> handleRingTimeout(callId), RING_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
|
||||
logger.info("Started outgoing call {} to {}", callId, recipient);
|
||||
return state.toCallInfo();
|
||||
}
|
||||
|
||||
public CallInfo acceptIncomingCall(final long callId) throws IOException {
|
||||
var state = activeCalls.get(callId);
|
||||
if (state == null) {
|
||||
throw new IOException("No active call with id " + callId);
|
||||
}
|
||||
if (state.state != CallInfo.State.RINGING_INCOMING) {
|
||||
throw new IOException("Call " + callId + " is not in RINGING_INCOMING state (current: " + state.state + ")");
|
||||
}
|
||||
|
||||
// Defer the accept until the tunnel reports Ringing state.
|
||||
// Sending accept too early (while RingRTC is in ConnectingBeforeAccepted)
|
||||
// causes it to be silently dropped.
|
||||
state.acceptPending = true;
|
||||
// If the tunnel is already in Ringing state, send immediately
|
||||
sendAcceptIfReady(state);
|
||||
|
||||
state.state = CallInfo.State.CONNECTING;
|
||||
fireCallEvent(state, null);
|
||||
|
||||
logger.info("Accepted incoming call {}", callId);
|
||||
return state.toCallInfo();
|
||||
}
|
||||
|
||||
public void hangupCall(final long callId) throws IOException {
|
||||
var state = activeCalls.get(callId);
|
||||
if (state == null) {
|
||||
throw new IOException("No active call with id " + callId);
|
||||
}
|
||||
endCall(callId, "local_hangup");
|
||||
}
|
||||
|
||||
public void rejectCall(final long callId) throws IOException {
|
||||
var state = activeCalls.get(callId);
|
||||
if (state == null) {
|
||||
throw new IOException("No active call with id " + callId);
|
||||
}
|
||||
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var busyMessage = new org.whispersystems.signalservice.api.messages.calls.BusyMessage(callId);
|
||||
var callMessage = org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage.forBusy(
|
||||
busyMessage, null);
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to send busy message for call {}", callId, e);
|
||||
}
|
||||
|
||||
endCall(callId, "rejected");
|
||||
}
|
||||
|
||||
public List<CallInfo> listActiveCalls() {
|
||||
return activeCalls.values().stream().map(CallState::toCallInfo).toList();
|
||||
}
|
||||
|
||||
public List<TurnServer> getTurnServers() throws IOException {
|
||||
try {
|
||||
var result = dependencies.getCallingApi().getTurnServerInfo();
|
||||
var turnServerList = result.successOrThrow();
|
||||
return turnServerList.stream()
|
||||
.map(info -> new TurnServer(info.getUsername(), info.getPassword(), info.getUrls()))
|
||||
.toList();
|
||||
} catch (Throwable e) {
|
||||
logger.warn("Failed to get TURN server info, returning empty list", e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
// --- Incoming call message handling ---
|
||||
|
||||
public void handleIncomingOffer(
|
||||
final org.asamk.signal.manager.storage.recipients.RecipientId senderId,
|
||||
final long callId,
|
||||
final MessageEnvelope.Call.Offer.Type type,
|
||||
final byte[] opaque
|
||||
) {
|
||||
var senderAddress = account.getRecipientAddressResolver()
|
||||
.resolveRecipientAddress(senderId)
|
||||
.toApiRecipientAddress();
|
||||
|
||||
RecipientIdentifier.Single senderIdentifier;
|
||||
if (senderAddress.number().isPresent()) {
|
||||
senderIdentifier = new RecipientIdentifier.Number(senderAddress.number().get());
|
||||
} else if (senderAddress.uuid().isPresent()) {
|
||||
senderIdentifier = new RecipientIdentifier.Uuid(senderAddress.uuid().get());
|
||||
} else {
|
||||
logger.warn("Cannot identify sender for call {}", callId);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.debug("Incoming offer opaque ({} bytes)", opaque == null ? 0 : opaque.length);
|
||||
|
||||
Path callDir;
|
||||
try {
|
||||
callDir = Files.createTempDirectory(Path.of("/tmp"), "sc-");
|
||||
Files.setPosixFilePermissions(callDir, PosixFilePermissions.fromString("rwx------"));
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to create socket directory for incoming call {}", callId, e);
|
||||
return;
|
||||
}
|
||||
var controlSocketPath = callDir.resolve("ctrl.sock").toString();
|
||||
|
||||
var state = new CallState(callId,
|
||||
CallInfo.State.RINGING_INCOMING,
|
||||
senderAddress,
|
||||
senderIdentifier,
|
||||
false,
|
||||
controlSocketPath,
|
||||
callDir);
|
||||
state.rawOfferOpaque = opaque;
|
||||
activeCalls.put(callId, state);
|
||||
|
||||
// Spawn call tunnel binary immediately
|
||||
spawnMediaTunnel(state);
|
||||
|
||||
// Get identity keys for the receivedOffer message
|
||||
// Use raw 32-byte Curve25519 public key (without 0x05 DJB prefix) to match Signal Android
|
||||
byte[] localIdentityKey = getRawIdentityKeyBytes(account.getAciIdentityKeyPair().getPublicKey().serialize());
|
||||
byte[] remoteIdentityKey = getRemoteIdentityKey(state);
|
||||
|
||||
// Fetch TURN servers
|
||||
List<TurnServer> turnServers;
|
||||
try {
|
||||
turnServers = getTurnServers();
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to get TURN servers for incoming call {}", callId, e);
|
||||
turnServers = List.of();
|
||||
}
|
||||
|
||||
// Send receivedOffer to subprocess
|
||||
var opaqueB64 = java.util.Base64.getEncoder().encodeToString(opaque);
|
||||
var senderIdKeyB64 = java.util.Base64.getEncoder().encodeToString(remoteIdentityKey);
|
||||
var receiverIdKeyB64 = java.util.Base64.getEncoder().encodeToString(localIdentityKey);
|
||||
var peerIdStr = senderAddress.toString();
|
||||
sendControlMessage(state, "{\"type\":\"receivedOffer\",\"callId\":" + callIdJson(callId)
|
||||
+ ",\"peerId\":\"" + escapeJson(peerIdStr) + "\""
|
||||
+ ",\"senderDeviceId\":1"
|
||||
+ ",\"opaque\":\"" + opaqueB64 + "\""
|
||||
+ ",\"age\":0"
|
||||
+ ",\"senderIdentityKey\":\"" + senderIdKeyB64 + "\""
|
||||
+ ",\"receiverIdentityKey\":\"" + receiverIdKeyB64 + "\""
|
||||
+ "}");
|
||||
|
||||
// Send proceed with TURN servers
|
||||
sendProceed(state, callId, turnServers);
|
||||
|
||||
fireCallEvent(state, null);
|
||||
|
||||
// Schedule ring timeout
|
||||
scheduler.schedule(() -> handleRingTimeout(callId), RING_TIMEOUT_MS, TimeUnit.MILLISECONDS);
|
||||
|
||||
logger.info("Incoming call {} from {}", callId, senderAddress);
|
||||
}
|
||||
|
||||
public void handleIncomingAnswer(final long callId, final byte[] opaque) {
|
||||
var state = activeCalls.get(callId);
|
||||
if (state == null) {
|
||||
logger.warn("Received answer for unknown call {}", callId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get identity keys
|
||||
// Use raw 32-byte Curve25519 public key (without 0x05 DJB prefix) to match Signal Android
|
||||
byte[] localIdentityKey = getRawIdentityKeyBytes(account.getAciIdentityKeyPair().getPublicKey().serialize());
|
||||
byte[] remoteIdentityKey = getRemoteIdentityKey(state);
|
||||
|
||||
// Forward raw opaque to subprocess
|
||||
var opaqueB64 = java.util.Base64.getEncoder().encodeToString(opaque);
|
||||
var senderIdKeyB64 = java.util.Base64.getEncoder().encodeToString(remoteIdentityKey);
|
||||
var receiverIdKeyB64 = java.util.Base64.getEncoder().encodeToString(localIdentityKey);
|
||||
sendControlMessage(state, "{\"type\":\"receivedAnswer\""
|
||||
+ ",\"opaque\":\"" + opaqueB64 + "\""
|
||||
+ ",\"senderDeviceId\":1"
|
||||
+ ",\"senderIdentityKey\":\"" + senderIdKeyB64 + "\""
|
||||
+ ",\"receiverIdentityKey\":\"" + receiverIdKeyB64 + "\""
|
||||
+ "}");
|
||||
|
||||
state.state = CallInfo.State.CONNECTING;
|
||||
fireCallEvent(state, null);
|
||||
|
||||
logger.info("Received answer for call {}", callId);
|
||||
}
|
||||
|
||||
public void handleIncomingIceCandidate(final long callId, final byte[] opaque) {
|
||||
var state = activeCalls.get(callId);
|
||||
if (state == null) {
|
||||
logger.debug("Received ICE candidate for unknown call {}", callId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Forward to subprocess as receivedIce
|
||||
var b64 = java.util.Base64.getEncoder().encodeToString(opaque);
|
||||
sendControlMessage(state, "{\"type\":\"receivedIce\",\"candidates\":[\"" + b64 + "\"]}");
|
||||
logger.debug("Forwarded ICE candidate to tunnel for call {}", callId);
|
||||
}
|
||||
|
||||
public void handleIncomingHangup(final long callId) {
|
||||
endCall(callId, "remote_hangup");
|
||||
}
|
||||
|
||||
public void handleIncomingBusy(final long callId) {
|
||||
endCall(callId, "remote_busy");
|
||||
}
|
||||
|
||||
// --- Internal helpers ---
|
||||
|
||||
private void sendControlMessage(CallState state, String json) {
|
||||
if (state.controlWriter == null) {
|
||||
logger.debug("Queueing control message for call {} (not yet connected): {}", state.callId, json);
|
||||
state.pendingControlMessages.add(json);
|
||||
return;
|
||||
}
|
||||
state.controlWriter.println(json);
|
||||
}
|
||||
|
||||
private void sendProceed(CallState state, long callId, List<TurnServer> turnServers) {
|
||||
var sb = new StringBuilder();
|
||||
sb.append("{\"type\":\"proceed\",\"callId\":").append(callIdJson(callId));
|
||||
sb.append(",\"hideIp\":false");
|
||||
sb.append(",\"iceServers\":[");
|
||||
for (int i = 0; i < turnServers.size(); i++) {
|
||||
if (i > 0) sb.append(",");
|
||||
var ts = turnServers.get(i);
|
||||
sb.append("{\"username\":\"").append(escapeJson(ts.username())).append("\"");
|
||||
sb.append(",\"password\":\"").append(escapeJson(ts.password())).append("\"");
|
||||
sb.append(",\"urls\":[");
|
||||
for (int j = 0; j < ts.urls().size(); j++) {
|
||||
if (j > 0) sb.append(",");
|
||||
sb.append("\"").append(escapeJson(ts.urls().get(j))).append("\"");
|
||||
}
|
||||
sb.append("]}");
|
||||
}
|
||||
sb.append("]}");
|
||||
sendControlMessage(state, sb.toString());
|
||||
}
|
||||
|
||||
private void spawnMediaTunnel(CallState state) {
|
||||
try {
|
||||
var command = new ArrayList<>(List.of(findTunnelBinary()));
|
||||
// Config is sent via stdin; no --host-audio by default
|
||||
|
||||
var processBuilder = new ProcessBuilder(command);
|
||||
processBuilder.redirectErrorStream(true);
|
||||
var process = processBuilder.start();
|
||||
|
||||
// Write config JSON to stdin
|
||||
var config = buildConfig(state);
|
||||
try (var stdin = process.getOutputStream()) {
|
||||
stdin.write(config.getBytes(StandardCharsets.UTF_8));
|
||||
stdin.flush();
|
||||
}
|
||||
|
||||
state.tunnelProcess = process;
|
||||
|
||||
// Drain subprocess stdout/stderr to prevent pipe buffer deadlock
|
||||
Thread.ofVirtual().name("tunnel-output-" + state.callId).start(() -> {
|
||||
try (var reader = new BufferedReader(
|
||||
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
logger.debug("[tunnel-{}] {}", state.callId, line);
|
||||
}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
});
|
||||
|
||||
// Connect to control socket in background
|
||||
Thread.ofVirtual().name("control-connect-" + state.callId).start(() -> {
|
||||
connectToControlSocket(state);
|
||||
});
|
||||
|
||||
// Monitor process exit
|
||||
process.onExit().thenAcceptAsync(p -> {
|
||||
logger.info("Tunnel for call {} exited with code {}", state.callId, p.exitValue());
|
||||
if (activeCalls.containsKey(state.callId)) {
|
||||
endCall(state.callId, "tunnel_exit");
|
||||
}
|
||||
});
|
||||
|
||||
logger.info("Spawned signal-call-tunnel for call {}", state.callId);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to spawn tunnel for call {}", state.callId, e);
|
||||
endCall(state.callId, "tunnel_spawn_error");
|
||||
}
|
||||
}
|
||||
|
||||
private String findTunnelBinary() {
|
||||
// Check environment variable first
|
||||
var envPath = System.getenv("SIGNAL_CALL_TUNNEL_BIN");
|
||||
if (envPath != null && !envPath.isEmpty()) {
|
||||
return envPath;
|
||||
}
|
||||
|
||||
// Check relative to the signal-cli installation
|
||||
var installDir = System.getProperty("signal.cli.install.dir");
|
||||
if (installDir != null) {
|
||||
var binPath = Path.of(installDir, "bin", "signal-call-tunnel");
|
||||
if (Files.isExecutable(binPath)) {
|
||||
return binPath.toString();
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to PATH
|
||||
return "signal-call-tunnel";
|
||||
}
|
||||
|
||||
private String buildConfig(CallState state) {
|
||||
// Generate control channel authentication token
|
||||
var tokenBytes = new byte[32];
|
||||
new SecureRandom().nextBytes(tokenBytes);
|
||||
state.controlToken = java.util.Base64.getEncoder().encodeToString(tokenBytes);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
sb.append("{");
|
||||
sb.append("\"call_id\":").append(callIdJson(state.callId));
|
||||
sb.append(",\"is_outgoing\":").append(state.isOutgoing);
|
||||
sb.append(",\"control_socket_path\":\"").append(escapeJson(state.controlSocketPath)).append("\"");
|
||||
sb.append(",\"control_token\":\"").append(state.controlToken).append("\"");
|
||||
sb.append(",\"local_device_id\":1");
|
||||
sb.append("}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void connectToControlSocket(CallState state) {
|
||||
var socketPath = Path.of(state.controlSocketPath);
|
||||
var addr = UnixDomainSocketAddress.of(socketPath);
|
||||
|
||||
for (int attempt = 0; attempt < 50; attempt++) {
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
if (!Files.exists(socketPath)) continue;
|
||||
|
||||
var channel = SocketChannel.open(StandardProtocolFamily.UNIX);
|
||||
channel.connect(addr);
|
||||
state.controlChannel = channel;
|
||||
state.controlWriter = new PrintWriter(
|
||||
new OutputStreamWriter(Channels.newOutputStream(channel), StandardCharsets.UTF_8), true);
|
||||
|
||||
// Send authentication token
|
||||
state.controlWriter.println("{\"type\":\"auth\",\"token\":\"" + state.controlToken + "\"}");
|
||||
logger.info("Connected to control socket for call {}", state.callId);
|
||||
|
||||
// Flush any pending control messages
|
||||
for (var msg : state.pendingControlMessages) {
|
||||
state.controlWriter.println(msg);
|
||||
}
|
||||
state.pendingControlMessages.clear();
|
||||
|
||||
// Start reading control events
|
||||
Thread.ofVirtual().name("control-read-" + state.callId).start(() -> {
|
||||
readControlEvents(state);
|
||||
});
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
logger.debug("Control socket connect attempt {} failed: {}", attempt, e.getMessage());
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
}
|
||||
logger.warn("Failed to connect to control socket for call {} after retries", state.callId);
|
||||
}
|
||||
|
||||
private void readControlEvents(CallState state) {
|
||||
try (var reader = new BufferedReader(
|
||||
new InputStreamReader(Channels.newInputStream(state.controlChannel), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
line = line.trim();
|
||||
if (line.isEmpty()) continue;
|
||||
logger.debug("Control event for call {}: {}", state.callId, line);
|
||||
|
||||
try {
|
||||
var json = mapper.readTree(line);
|
||||
var type = json.has("type") ? json.get("type").asText() : "";
|
||||
|
||||
switch (type) {
|
||||
case "ready" -> {
|
||||
if (json.has("inputDeviceName")) {
|
||||
state.inputDeviceName = json.get("inputDeviceName").asText();
|
||||
}
|
||||
if (json.has("outputDeviceName")) {
|
||||
state.outputDeviceName = json.get("outputDeviceName").asText();
|
||||
}
|
||||
logger.debug("Tunnel ready for call {}: input={}, output={}",
|
||||
state.callId, state.inputDeviceName, state.outputDeviceName);
|
||||
}
|
||||
case "sendOffer" -> {
|
||||
var opaqueB64 = json.get("opaque").asText();
|
||||
var opaque = java.util.Base64.getDecoder().decode(opaqueB64);
|
||||
sendOfferViaSignal(state, opaque);
|
||||
}
|
||||
case "sendAnswer" -> {
|
||||
var opaqueB64 = json.get("opaque").asText();
|
||||
var opaque = java.util.Base64.getDecoder().decode(opaqueB64);
|
||||
sendAnswerViaSignal(state, opaque);
|
||||
}
|
||||
case "sendIce" -> {
|
||||
var candidatesArr = json.get("candidates");
|
||||
var opaqueList = new ArrayList<byte[]>();
|
||||
for (var c : candidatesArr) {
|
||||
opaqueList.add(java.util.Base64.getDecoder().decode(c.get("opaque").asText()));
|
||||
}
|
||||
sendIceViaSignal(state, opaqueList);
|
||||
}
|
||||
case "sendHangup" -> {
|
||||
// RingRTC wants us to send a hangup message via Signal protocol.
|
||||
// This is NOT a local state change — local state is handled by stateChange events.
|
||||
var hangupType = json.has("hangupType") ? json.get("hangupType").asText("normal") : "normal";
|
||||
// Skip multi-device hangup types — signal-cli is single-device,
|
||||
// and sending these to the remote peer causes it to terminate the call.
|
||||
if (hangupType.contains("onanotherdevice")) {
|
||||
logger.debug("Ignoring multi-device hangup type: {}", hangupType);
|
||||
} else {
|
||||
sendHangupViaSignal(state, hangupType);
|
||||
}
|
||||
}
|
||||
case "sendBusy" -> {
|
||||
sendBusyViaSignal(state);
|
||||
}
|
||||
case "stateChange" -> {
|
||||
var ringrtcState = json.get("state").asText();
|
||||
var reason = json.has("reason") ? json.get("reason").asText(null) : null;
|
||||
handleStateChange(state, ringrtcState, reason);
|
||||
}
|
||||
case "error" -> {
|
||||
var message = json.has("message") ? json.get("message").asText("unknown") : "unknown";
|
||||
logger.error("Tunnel error for call {}: {}", state.callId, message);
|
||||
endCall(state.callId, "tunnel_error");
|
||||
}
|
||||
default -> {
|
||||
logger.debug("Unknown control event type '{}' for call {}", type, state.callId);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to parse control event JSON for call {}: {}", state.callId, e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.debug("Control read ended for call {}: {}", state.callId, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleStateChange(CallState state, String ringrtcState, String reason) {
|
||||
if (ringrtcState.startsWith("Incoming")) {
|
||||
// Don't downgrade if we've already accepted
|
||||
if (state.state == CallInfo.State.CONNECTING) return;
|
||||
state.state = CallInfo.State.RINGING_INCOMING;
|
||||
} else if (ringrtcState.startsWith("Outgoing")) {
|
||||
state.state = CallInfo.State.RINGING_OUTGOING;
|
||||
} else if ("Ringing".equals(ringrtcState)) {
|
||||
// Tunnel is now ready to accept — flush deferred accept if pending
|
||||
sendAcceptIfReady(state);
|
||||
return;
|
||||
} else if ("Connected".equals(ringrtcState)) {
|
||||
state.state = CallInfo.State.CONNECTED;
|
||||
} else if ("Connecting".equals(ringrtcState)) {
|
||||
state.state = CallInfo.State.RECONNECTING;
|
||||
} else if ("Ended".equals(ringrtcState) || "Rejected".equals(ringrtcState)) {
|
||||
endCall(state.callId, reason != null ? reason : ringrtcState.toLowerCase());
|
||||
return;
|
||||
} else if ("Concluded".equals(ringrtcState)) {
|
||||
// Cleanup, no-op
|
||||
return;
|
||||
}
|
||||
fireCallEvent(state, reason);
|
||||
}
|
||||
|
||||
private void sendAcceptIfReady(CallState state) {
|
||||
if (state.acceptPending && state.controlWriter != null) {
|
||||
state.acceptPending = false;
|
||||
logger.debug("Sending deferred accept for call {}", state.callId);
|
||||
state.controlWriter.println("{\"type\":\"accept\"}");
|
||||
}
|
||||
}
|
||||
|
||||
private void sendOfferViaSignal(CallState state, byte[] opaque) {
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var offerMessage = new org.whispersystems.signalservice.api.messages.calls.OfferMessage(state.callId,
|
||||
org.whispersystems.signalservice.api.messages.calls.OfferMessage.Type.AUDIO_CALL,
|
||||
opaque);
|
||||
var callMessage = org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage.forOffer(
|
||||
offerMessage, null);
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
logger.info("Sent offer via Signal for call {}", state.callId);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to send offer for call {}", state.callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendAnswerViaSignal(CallState state, byte[] opaque) {
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var answerMessage = new org.whispersystems.signalservice.api.messages.calls.AnswerMessage(state.callId, opaque);
|
||||
var callMessage = org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage.forAnswer(
|
||||
answerMessage, null);
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
logger.info("Sent answer via Signal for call {}", state.callId);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to send answer for call {}", state.callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendIceViaSignal(CallState state, List<byte[]> opaqueList) {
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var iceUpdates = opaqueList.stream()
|
||||
.map(opaque -> new org.whispersystems.signalservice.api.messages.calls.IceUpdateMessage(
|
||||
state.callId, opaque))
|
||||
.toList();
|
||||
var callMessage = org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage.forIceUpdates(
|
||||
iceUpdates, null);
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
logger.info("Sent {} ICE candidates via Signal for call {}", opaqueList.size(), state.callId);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to send ICE for call {}", state.callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendBusyViaSignal(CallState state) {
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var busyMessage = new org.whispersystems.signalservice.api.messages.calls.BusyMessage(state.callId);
|
||||
var callMessage = org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage.forBusy(
|
||||
busyMessage, null);
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to send busy for call {}", state.callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendHangupViaSignal(CallState state, String hangupType) {
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var type = switch (hangupType) {
|
||||
case "accepted", "acceptedonanotherdevice" ->
|
||||
org.whispersystems.signalservice.api.messages.calls.HangupMessage.Type.ACCEPTED;
|
||||
case "declined", "declinedonanotherdevice" ->
|
||||
org.whispersystems.signalservice.api.messages.calls.HangupMessage.Type.DECLINED;
|
||||
case "busy", "busyonanotherdevice" ->
|
||||
org.whispersystems.signalservice.api.messages.calls.HangupMessage.Type.BUSY;
|
||||
default -> org.whispersystems.signalservice.api.messages.calls.HangupMessage.Type.NORMAL;
|
||||
};
|
||||
var hangupMessage = new org.whispersystems.signalservice.api.messages.calls.HangupMessage(
|
||||
state.callId, type, 0);
|
||||
var callMessage = org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage.forHangup(
|
||||
hangupMessage, null);
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
logger.info("Sent hangup ({}) via Signal for call {}", hangupType, state.callId);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to send hangup for call {}", state.callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] getRemoteIdentityKey(CallState state) {
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var serviceId = address.getServiceId();
|
||||
var identityInfo = account.getIdentityKeyStore().getIdentityInfo(serviceId);
|
||||
if (identityInfo != null) {
|
||||
return getRawIdentityKeyBytes(identityInfo.getIdentityKey().serialize());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to get remote identity key for call {}", state.callId, e);
|
||||
}
|
||||
logger.warn("Using local identity key as fallback for remote identity key");
|
||||
return getRawIdentityKeyBytes(account.getAciIdentityKeyPair().getPublicKey().serialize());
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip the 0x05 DJB type prefix from a serialized identity key to get the
|
||||
* raw 32-byte Curve25519 public key. Signal Android does this via
|
||||
* WebRtcUtil.getPublicKeyBytes() before passing keys to RingRTC.
|
||||
*/
|
||||
private static byte[] getRawIdentityKeyBytes(byte[] serializedKey) {
|
||||
if (serializedKey.length == 33 && serializedKey[0] == 0x05) {
|
||||
return java.util.Arrays.copyOfRange(serializedKey, 1, serializedKey.length);
|
||||
}
|
||||
return serializedKey;
|
||||
}
|
||||
|
||||
/** Format call ID as unsigned for JSON (tunnel binary expects u64). */
|
||||
private static String callIdJson(long callId) {
|
||||
return Long.toUnsignedString(callId);
|
||||
}
|
||||
|
||||
private static String escapeJson(String s) {
|
||||
if (s == null) return "";
|
||||
return s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "\\r");
|
||||
}
|
||||
|
||||
private void endCall(final long callId, final String reason) {
|
||||
var state = activeCalls.remove(callId);
|
||||
if (state == null) return;
|
||||
|
||||
state.state = CallInfo.State.ENDED;
|
||||
fireCallEvent(state, reason);
|
||||
logger.info("Call {} ended: {}", callId, reason);
|
||||
|
||||
// Send Signal protocol hangup to remote peer (unless they initiated the end)
|
||||
if (!"remote_hangup".equals(reason) && !"rejected".equals(reason) && !"remote_busy".equals(reason)
|
||||
&& !"ringrtc_hangup".equals(reason)) {
|
||||
try {
|
||||
var recipientId = context.getRecipientHelper().resolveRecipient(state.recipientIdentifier);
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var hangupMessage = new org.whispersystems.signalservice.api.messages.calls.HangupMessage(callId,
|
||||
org.whispersystems.signalservice.api.messages.calls.HangupMessage.Type.NORMAL, 0);
|
||||
var callMessage = org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage.forHangup(
|
||||
hangupMessage, null);
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to send hangup to remote for call {}", callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
// Send hangup via control channel before killing process
|
||||
if (state.controlWriter != null) {
|
||||
try {
|
||||
state.controlWriter.println("{\"type\":\"hangup\"}");
|
||||
} catch (Exception e) {
|
||||
logger.debug("Failed to send hangup via control channel", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Close control channel
|
||||
if (state.controlChannel != null) {
|
||||
try {
|
||||
state.controlChannel.close();
|
||||
} catch (IOException e) {
|
||||
logger.debug("Failed to close control channel for call {}", callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
// Kill tunnel process
|
||||
if (state.tunnelProcess != null && state.tunnelProcess.isAlive()) {
|
||||
state.tunnelProcess.destroy();
|
||||
}
|
||||
|
||||
// Clean up socket directory
|
||||
try {
|
||||
Files.deleteIfExists(Path.of(state.controlSocketPath));
|
||||
Files.deleteIfExists(state.socketDir);
|
||||
} catch (IOException e) {
|
||||
logger.debug("Failed to clean up socket directory for call {}", callId, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleRingTimeout(final long callId) {
|
||||
var state = activeCalls.get(callId);
|
||||
if (state == null) return;
|
||||
|
||||
if (state.state == CallInfo.State.RINGING_INCOMING || state.state == CallInfo.State.RINGING_OUTGOING) {
|
||||
logger.info("Call {} ring timeout", callId);
|
||||
try {
|
||||
hangupCall(callId);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to hangup timed-out call {}", callId, e);
|
||||
endCall(callId, "ring_timeout");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static long generateCallId() {
|
||||
return new SecureRandom().nextLong() & Long.MAX_VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
scheduler.shutdownNow();
|
||||
for (var callId : new ArrayList<>(activeCalls.keySet())) {
|
||||
endCall(callId, "shutdown");
|
||||
}
|
||||
}
|
||||
|
||||
// --- Internal call state tracking ---
|
||||
|
||||
static class CallState {
|
||||
|
||||
final long callId;
|
||||
volatile CallInfo.State state;
|
||||
final org.asamk.signal.manager.api.RecipientAddress recipientAddress;
|
||||
final RecipientIdentifier.Single recipientIdentifier;
|
||||
final boolean isOutgoing;
|
||||
final String controlSocketPath;
|
||||
final Path socketDir;
|
||||
volatile String inputDeviceName;
|
||||
volatile String outputDeviceName;
|
||||
volatile Process tunnelProcess;
|
||||
volatile SocketChannel controlChannel;
|
||||
volatile PrintWriter controlWriter;
|
||||
volatile String controlToken;
|
||||
// Raw offer opaque for incoming calls (forwarded to subprocess)
|
||||
volatile byte[] rawOfferOpaque;
|
||||
// Control messages queued before the control channel connects
|
||||
final List<String> pendingControlMessages = java.util.Collections.synchronizedList(new ArrayList<>());
|
||||
// Accept deferred until tunnel reports Ringing state
|
||||
volatile boolean acceptPending = false;
|
||||
|
||||
CallState(
|
||||
long callId,
|
||||
CallInfo.State state,
|
||||
org.asamk.signal.manager.api.RecipientAddress recipientAddress,
|
||||
RecipientIdentifier.Single recipientIdentifier,
|
||||
boolean isOutgoing,
|
||||
String controlSocketPath,
|
||||
Path socketDir
|
||||
) {
|
||||
this.callId = callId;
|
||||
this.state = state;
|
||||
this.recipientAddress = recipientAddress;
|
||||
this.recipientIdentifier = recipientIdentifier;
|
||||
this.isOutgoing = isOutgoing;
|
||||
this.controlSocketPath = controlSocketPath;
|
||||
this.socketDir = socketDir;
|
||||
}
|
||||
|
||||
CallInfo toCallInfo() {
|
||||
return new CallInfo(callId, state, recipientAddress, inputDeviceName, outputDeviceName, isOutgoing);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -23,6 +23,7 @@ public class Context implements AutoCloseable {
|
||||
|
||||
private AccountHelper accountHelper;
|
||||
private AttachmentHelper attachmentHelper;
|
||||
private CallManager callManager;
|
||||
private ContactHelper contactHelper;
|
||||
private GroupHelper groupHelper;
|
||||
private GroupV2Helper groupV2Helper;
|
||||
@ -92,6 +93,10 @@ public class Context implements AutoCloseable {
|
||||
return getOrCreate(() -> attachmentHelper, () -> attachmentHelper = new AttachmentHelper(this));
|
||||
}
|
||||
|
||||
public CallManager getCallManager() {
|
||||
return getOrCreate(() -> callManager, () -> callManager = new CallManager(this));
|
||||
}
|
||||
|
||||
public ContactHelper getContactHelper() {
|
||||
return getOrCreate(() -> contactHelper, () -> contactHelper = new ContactHelper(account));
|
||||
}
|
||||
@ -172,6 +177,9 @@ public class Context implements AutoCloseable {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (callManager != null) {
|
||||
callManager.close();
|
||||
}
|
||||
jobExecutor.close();
|
||||
}
|
||||
|
||||
|
||||
@ -401,9 +401,49 @@ public final class IncomingMessageHandler {
|
||||
longTexts.putAll(syncResults.second());
|
||||
}
|
||||
|
||||
if (content.getCallMessage().isPresent()) {
|
||||
handleCallMessage(content.getCallMessage().get(), sender);
|
||||
}
|
||||
|
||||
return new Pair<>(actions, longTexts);
|
||||
}
|
||||
|
||||
private void handleCallMessage(
|
||||
final org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage callMessage,
|
||||
final org.asamk.signal.manager.storage.recipients.RecipientId sender
|
||||
) {
|
||||
var callManager = context.getCallManager();
|
||||
|
||||
callMessage.getOfferMessage().ifPresent(offer -> {
|
||||
var type = offer.getType() == org.whispersystems.signalservice.api.messages.calls.OfferMessage.Type.VIDEO_CALL
|
||||
? org.asamk.signal.manager.api.MessageEnvelope.Call.Offer.Type.VIDEO_CALL
|
||||
: org.asamk.signal.manager.api.MessageEnvelope.Call.Offer.Type.AUDIO_CALL;
|
||||
callManager.handleIncomingOffer(sender, offer.getId(), type, offer.getOpaque());
|
||||
});
|
||||
|
||||
callMessage.getAnswerMessage().ifPresent(answer ->
|
||||
callManager.handleIncomingAnswer(answer.getId(), answer.getOpaque()));
|
||||
|
||||
callMessage.getIceUpdateMessages().ifPresent(iceUpdates -> {
|
||||
for (var ice : iceUpdates) {
|
||||
callManager.handleIncomingIceCandidate(ice.getId(), ice.getOpaque());
|
||||
}
|
||||
});
|
||||
|
||||
callMessage.getHangupMessage().ifPresent(hangup -> {
|
||||
// Only NORMAL hangups actually end the call. ACCEPTED/DECLINED/BUSY
|
||||
// are multi-device notifications irrelevant for single-device signal-cli.
|
||||
var hangupType = hangup.getType();
|
||||
if (hangupType == org.whispersystems.signalservice.api.messages.calls.HangupMessage.Type.NORMAL
|
||||
|| hangupType == null) {
|
||||
callManager.handleIncomingHangup(hangup.getId());
|
||||
}
|
||||
});
|
||||
|
||||
callMessage.getBusyMessage().ifPresent(busy ->
|
||||
callManager.handleIncomingBusy(busy.getId()));
|
||||
}
|
||||
|
||||
private boolean handlePniSignatureMessage(
|
||||
final SignalServicePniSignatureMessage message,
|
||||
final SignalServiceAddress senderAddress
|
||||
|
||||
@ -19,6 +19,9 @@ package org.asamk.signal.manager.internal;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.AlreadyReceivingException;
|
||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
import org.asamk.signal.manager.api.CallOffer;
|
||||
import org.asamk.signal.manager.api.TurnServer;
|
||||
import org.asamk.signal.manager.api.CaptchaRejectedException;
|
||||
import org.asamk.signal.manager.api.CaptchaRequiredException;
|
||||
import org.asamk.signal.manager.api.Configuration;
|
||||
@ -105,6 +108,12 @@ import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServicePreview;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage;
|
||||
import org.whispersystems.signalservice.api.messages.calls.AnswerMessage;
|
||||
import org.whispersystems.signalservice.api.messages.calls.BusyMessage;
|
||||
import org.whispersystems.signalservice.api.messages.calls.HangupMessage;
|
||||
import org.whispersystems.signalservice.api.messages.calls.IceUpdateMessage;
|
||||
import org.whispersystems.signalservice.api.messages.calls.OfferMessage;
|
||||
import org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage;
|
||||
import org.whispersystems.signalservice.api.messages.multidevice.DeviceInfo;
|
||||
import org.whispersystems.signalservice.api.push.ServiceIdType;
|
||||
import org.whispersystems.signalservice.api.push.exceptions.CdsiResourceExhaustedException;
|
||||
@ -163,6 +172,7 @@ public class ManagerImpl implements Manager {
|
||||
private boolean isReceivingSynchronous;
|
||||
private final Set<ReceiveMessageHandler> weakHandlers = new HashSet<>();
|
||||
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
|
||||
private final Set<CallEventListener> callEventListeners = new HashSet<>();
|
||||
private final List<Runnable> closedListeners = new ArrayList<>();
|
||||
private final List<Runnable> addressChangedListeners = new ArrayList<>();
|
||||
private final CompositeDisposable disposable = new CompositeDisposable();
|
||||
@ -1702,6 +1712,22 @@ public class ManagerImpl implements Manager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCallEventListener(final CallEventListener listener) {
|
||||
synchronized (callEventListeners) {
|
||||
callEventListeners.add(listener);
|
||||
}
|
||||
context.getCallManager().addCallEventListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCallEventListener(final CallEventListener listener) {
|
||||
synchronized (callEventListeners) {
|
||||
callEventListeners.remove(listener);
|
||||
}
|
||||
context.getCallManager().removeCallEventListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream retrieveAttachment(final String id) throws IOException {
|
||||
return context.getAttachmentHelper().retrieveAttachment(id).getStream();
|
||||
@ -1759,6 +1785,132 @@ public class ManagerImpl implements Manager {
|
||||
return streamDetails.getStream();
|
||||
}
|
||||
|
||||
// --- Voice call methods ---
|
||||
|
||||
@Override
|
||||
public CallInfo startCall(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException {
|
||||
return context.getCallManager().startOutgoingCall(recipient);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallInfo acceptCall(final long callId) throws IOException {
|
||||
return context.getCallManager().acceptIncomingCall(callId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hangupCall(final long callId) throws IOException {
|
||||
context.getCallManager().hangupCall(callId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rejectCall(final long callId) throws IOException {
|
||||
context.getCallManager().rejectCall(callId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CallInfo> listActiveCalls() {
|
||||
return context.getCallManager().listActiveCalls();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendCallOffer(
|
||||
final RecipientIdentifier.Single recipient,
|
||||
final CallOffer offer
|
||||
) throws IOException, UnregisteredRecipientException {
|
||||
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
|
||||
final var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var offerMessage = new OfferMessage(offer.callId(),
|
||||
offer.type() == CallOffer.Type.VIDEO ? OfferMessage.Type.VIDEO_CALL : OfferMessage.Type.AUDIO_CALL,
|
||||
offer.opaque());
|
||||
var callMessage = SignalServiceCallMessage.forOffer(offerMessage, null);
|
||||
try {
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException e) {
|
||||
throw new IOException("Untrusted identity for call recipient", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendCallAnswer(
|
||||
final RecipientIdentifier.Single recipient,
|
||||
final long callId,
|
||||
final byte[] answerOpaque
|
||||
) throws IOException, UnregisteredRecipientException {
|
||||
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
|
||||
final var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var answerMessage = new AnswerMessage(callId, answerOpaque);
|
||||
var callMessage = SignalServiceCallMessage.forAnswer(answerMessage, null);
|
||||
try {
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException e) {
|
||||
throw new IOException("Untrusted identity for call recipient", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendIceUpdate(
|
||||
final RecipientIdentifier.Single recipient,
|
||||
final long callId,
|
||||
final List<byte[]> iceCandidates
|
||||
) throws IOException, UnregisteredRecipientException {
|
||||
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
|
||||
final var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var iceUpdates = iceCandidates.stream()
|
||||
.map(opaque -> new IceUpdateMessage(callId, opaque))
|
||||
.toList();
|
||||
var callMessage = SignalServiceCallMessage.forIceUpdates(iceUpdates, null);
|
||||
try {
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException e) {
|
||||
throw new IOException("Untrusted identity for call recipient", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendHangup(
|
||||
final RecipientIdentifier.Single recipient,
|
||||
final long callId,
|
||||
final MessageEnvelope.Call.Hangup.Type type
|
||||
) throws IOException, UnregisteredRecipientException {
|
||||
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
|
||||
final var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var hangupType = switch (type) {
|
||||
case NORMAL -> HangupMessage.Type.NORMAL;
|
||||
case ACCEPTED -> HangupMessage.Type.ACCEPTED;
|
||||
case DECLINED -> HangupMessage.Type.DECLINED;
|
||||
case BUSY -> HangupMessage.Type.BUSY;
|
||||
case NEED_PERMISSION -> HangupMessage.Type.NEED_PERMISSION;
|
||||
};
|
||||
var hangupMessage = new HangupMessage(callId, hangupType, 0);
|
||||
var callMessage = SignalServiceCallMessage.forHangup(hangupMessage, null);
|
||||
try {
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException e) {
|
||||
throw new IOException("Untrusted identity for call recipient", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendBusy(
|
||||
final RecipientIdentifier.Single recipient,
|
||||
final long callId
|
||||
) throws IOException, UnregisteredRecipientException {
|
||||
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
|
||||
final var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
var busyMessage = new BusyMessage(callId);
|
||||
var callMessage = SignalServiceCallMessage.forBusy(busyMessage, null);
|
||||
try {
|
||||
dependencies.getMessageSender().sendCallMessage(address, null, callMessage);
|
||||
} catch (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException e) {
|
||||
throw new IOException("Untrusted identity for call recipient", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TurnServer> getTurnServerInfo() throws IOException {
|
||||
return context.getCallManager().getTurnServers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
Thread thread;
|
||||
@ -1771,6 +1923,12 @@ public class ManagerImpl implements Manager {
|
||||
if (thread != null) {
|
||||
stopReceiveThread(thread);
|
||||
}
|
||||
synchronized (callEventListeners) {
|
||||
for (var listener : callEventListeners) {
|
||||
context.getCallManager().removeCallEventListener(listener);
|
||||
}
|
||||
callEventListeners.clear();
|
||||
}
|
||||
context.close();
|
||||
executor.close();
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
||||
import org.whispersystems.signalservice.api.SignalSessionLock;
|
||||
import org.whispersystems.signalservice.api.account.AccountApi;
|
||||
import org.whispersystems.signalservice.api.attachment.AttachmentApi;
|
||||
import org.whispersystems.signalservice.api.calling.CallingApi;
|
||||
import org.whispersystems.signalservice.api.cds.CdsApi;
|
||||
import org.whispersystems.signalservice.api.certificate.CertificateApi;
|
||||
import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
|
||||
@ -76,6 +77,7 @@ public class SignalDependencies {
|
||||
private StorageServiceApi storageServiceApi;
|
||||
private CertificateApi certificateApi;
|
||||
private AttachmentApi attachmentApi;
|
||||
private CallingApi callingApi;
|
||||
private MessageApi messageApi;
|
||||
private KeysApi keysApi;
|
||||
private GroupsV2Operations groupsV2Operations;
|
||||
@ -255,6 +257,13 @@ public class SignalDependencies {
|
||||
() -> attachmentApi = new AttachmentApi(getAuthenticatedSignalWebSocket(), getPushServiceSocket()));
|
||||
}
|
||||
|
||||
public CallingApi getCallingApi() {
|
||||
return getOrCreate(() -> callingApi,
|
||||
() -> callingApi = new CallingApi(getAuthenticatedSignalWebSocket(),
|
||||
getUnauthenticatedSignalWebSocket(),
|
||||
getPushServiceSocket()));
|
||||
}
|
||||
|
||||
public MessageApi getMessageApi() {
|
||||
return getOrCreate(() -> messageApi,
|
||||
() -> messageApi = new MessageApi(getAuthenticatedSignalWebSocket(),
|
||||
|
||||
32
lib/src/main/proto/rtp_data.proto
Normal file
32
lib/src/main/proto/rtp_data.proto
Normal file
@ -0,0 +1,32 @@
|
||||
// In-call control messages carried over the RTP data channel.
|
||||
// signal-cli hand-codes the parsing in RtpDataProtobuf.java rather than using protoc.
|
||||
syntax = "proto2";
|
||||
|
||||
package rtp_data;
|
||||
|
||||
option java_package = "org.asamk.signal.manager.calling.proto";
|
||||
option java_outer_classname = "RtpDataProtos";
|
||||
|
||||
message Accepted {}
|
||||
|
||||
message Hangup {
|
||||
optional uint32 id = 1;
|
||||
}
|
||||
|
||||
message SenderStatus {
|
||||
optional bool audio_enabled = 1;
|
||||
optional bool video_enabled = 2;
|
||||
optional bool sharing_screen = 3;
|
||||
}
|
||||
|
||||
message Receiver {
|
||||
optional uint32 id = 1;
|
||||
}
|
||||
|
||||
// Top-level RTP data message
|
||||
message Data {
|
||||
optional Accepted accepted = 1;
|
||||
optional Hangup hangup = 2;
|
||||
optional SenderStatus sender_status = 3;
|
||||
optional Receiver receiver = 4;
|
||||
}
|
||||
32
lib/src/main/proto/signaling.proto
Normal file
32
lib/src/main/proto/signaling.proto
Normal file
@ -0,0 +1,32 @@
|
||||
// RingRTC signaling protobuf definitions
|
||||
// These define the structure of the opaque blobs inside Signal call Offer/Answer messages.
|
||||
// signal-cli hand-codes the parsing in SignalingProtobuf.java rather than using protoc.
|
||||
syntax = "proto2";
|
||||
|
||||
package signaling;
|
||||
|
||||
option java_package = "org.asamk.signal.manager.calling.proto";
|
||||
option java_outer_classname = "SignalingProtos";
|
||||
|
||||
message VideoCodec {
|
||||
enum Type {
|
||||
VP8 = 0;
|
||||
H264 = 1;
|
||||
VP9 = 2;
|
||||
}
|
||||
optional Type type = 1;
|
||||
optional uint32 level = 2;
|
||||
}
|
||||
|
||||
message ConnectionParametersV4 {
|
||||
optional bytes public_key = 1; // x25519 public key (32 bytes)
|
||||
optional string ice_ufrag = 2;
|
||||
optional string ice_pwd = 3;
|
||||
repeated VideoCodec receive_video_codecs = 4;
|
||||
optional uint64 max_bitrate_bps = 5;
|
||||
}
|
||||
|
||||
// The top-level opaque blob inside an OfferMessage or AnswerMessage
|
||||
message Opaque {
|
||||
optional ConnectionParametersV4 connection_parameters_v4 = 1;
|
||||
}
|
||||
@ -0,0 +1,464 @@
|
||||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
import org.asamk.signal.manager.api.RecipientAddress;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests for pure functions and state machine logic in CallManager.
|
||||
* Uses reflection to access private static helpers without changing production visibility.
|
||||
*/
|
||||
class CallManagerTest {
|
||||
|
||||
// --- Reflection helpers for private static methods ---
|
||||
|
||||
private static final MethodHandle GET_RAW_IDENTITY_KEY_BYTES;
|
||||
private static final MethodHandle CALL_ID_JSON;
|
||||
private static final MethodHandle ESCAPE_JSON;
|
||||
private static final MethodHandle GENERATE_CALL_ID;
|
||||
|
||||
static {
|
||||
try {
|
||||
var lookup = MethodHandles.privateLookupIn(CallManager.class, MethodHandles.lookup());
|
||||
|
||||
GET_RAW_IDENTITY_KEY_BYTES = lookup.findStatic(CallManager.class, "getRawIdentityKeyBytes",
|
||||
MethodType.methodType(byte[].class, byte[].class));
|
||||
|
||||
CALL_ID_JSON = lookup.findStatic(CallManager.class, "callIdJson",
|
||||
MethodType.methodType(String.class, long.class));
|
||||
|
||||
ESCAPE_JSON = lookup.findStatic(CallManager.class, "escapeJson",
|
||||
MethodType.methodType(String.class, String.class));
|
||||
|
||||
GENERATE_CALL_ID = lookup.findStatic(CallManager.class, "generateCallId",
|
||||
MethodType.methodType(long.class));
|
||||
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new ExceptionInInitializerError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] getRawIdentityKeyBytes(byte[] serializedKey) throws Throwable {
|
||||
return (byte[]) GET_RAW_IDENTITY_KEY_BYTES.invokeExact(serializedKey);
|
||||
}
|
||||
|
||||
private static String callIdJson(long callId) throws Throwable {
|
||||
return (String) CALL_ID_JSON.invokeExact(callId);
|
||||
}
|
||||
|
||||
private static String escapeJson(String s) throws Throwable {
|
||||
return (String) ESCAPE_JSON.invokeExact(s);
|
||||
}
|
||||
|
||||
private static long generateCallId() throws Throwable {
|
||||
return (long) GENERATE_CALL_ID.invokeExact();
|
||||
}
|
||||
|
||||
// --- Helper to create a minimal CallState for state machine tests ---
|
||||
|
||||
private static CallManager.CallState makeCallState(long callId, CallInfo.State initialState) {
|
||||
var address = new RecipientAddress("a1b2c3d4-e5f6-7890-abcd-ef1234567890", null, "+15551234567", null);
|
||||
return new CallManager.CallState(
|
||||
callId,
|
||||
initialState,
|
||||
address,
|
||||
new org.asamk.signal.manager.api.RecipientIdentifier.Number("+15551234567"),
|
||||
true,
|
||||
"/tmp/sc-test/ctrl.sock",
|
||||
Path.of("/tmp/sc-test")
|
||||
);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// getRawIdentityKeyBytes tests
|
||||
// ========================================================================
|
||||
|
||||
@Test
|
||||
void getRawIdentityKeyBytes_strips0x05Prefix() throws Throwable {
|
||||
// 33-byte key with 0x05 DJB type prefix
|
||||
var key33 = new byte[33];
|
||||
key33[0] = 0x05;
|
||||
for (int i = 1; i < 33; i++) key33[i] = (byte) i;
|
||||
|
||||
var result = getRawIdentityKeyBytes(key33);
|
||||
|
||||
assertEquals(32, result.length);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
assertEquals((byte) (i + 1), result[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRawIdentityKeyBytes_already32Bytes() throws Throwable {
|
||||
var key32 = new byte[32];
|
||||
for (int i = 0; i < 32; i++) key32[i] = (byte) (i + 10);
|
||||
|
||||
var result = getRawIdentityKeyBytes(key32);
|
||||
|
||||
assertArrayEquals(key32, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRawIdentityKeyBytes_33BytesWrongPrefix() throws Throwable {
|
||||
// 33 bytes but prefix is NOT 0x05
|
||||
var key33 = new byte[33];
|
||||
key33[0] = 0x07;
|
||||
for (int i = 1; i < 33; i++) key33[i] = (byte) i;
|
||||
|
||||
var result = getRawIdentityKeyBytes(key33);
|
||||
|
||||
// Should return the original key unchanged
|
||||
assertArrayEquals(key33, result);
|
||||
assertEquals(33, result.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRawIdentityKeyBytes_emptyArray() throws Throwable {
|
||||
var empty = new byte[0];
|
||||
var result = getRawIdentityKeyBytes(empty);
|
||||
assertArrayEquals(empty, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRawIdentityKeyBytes_shortArray() throws Throwable {
|
||||
var short5 = new byte[]{0x05, 1, 2};
|
||||
var result = getRawIdentityKeyBytes(short5);
|
||||
// Not 33 bytes, so returned unchanged despite 0x05 prefix
|
||||
assertArrayEquals(short5, result);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// callIdJson tests
|
||||
// ========================================================================
|
||||
|
||||
@Test
|
||||
void callIdJson_zero() throws Throwable {
|
||||
assertEquals("0", callIdJson(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void callIdJson_positiveLong() throws Throwable {
|
||||
assertEquals("8230211930154373276", callIdJson(8230211930154373276L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void callIdJson_negativeLongBecomesUnsigned() throws Throwable {
|
||||
// -1L as unsigned is 2^64 - 1 = 18446744073709551615
|
||||
assertEquals("18446744073709551615", callIdJson(-1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void callIdJson_longMinValueBecomesUnsigned() throws Throwable {
|
||||
// Long.MIN_VALUE as unsigned is 2^63 = 9223372036854775808
|
||||
assertEquals("9223372036854775808", callIdJson(Long.MIN_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void callIdJson_longMaxValue() throws Throwable {
|
||||
assertEquals("9223372036854775807", callIdJson(Long.MAX_VALUE));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// escapeJson tests
|
||||
// ========================================================================
|
||||
|
||||
@Test
|
||||
void escapeJson_null() throws Throwable {
|
||||
assertEquals("", escapeJson(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson_empty() throws Throwable {
|
||||
assertEquals("", escapeJson(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson_noSpecialChars() throws Throwable {
|
||||
assertEquals("hello world", escapeJson("hello world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson_backslash() throws Throwable {
|
||||
assertEquals("path\\\\to\\\\file", escapeJson("path\\to\\file"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson_doubleQuote() throws Throwable {
|
||||
assertEquals("say \\\"hello\\\"", escapeJson("say \"hello\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson_newline() throws Throwable {
|
||||
assertEquals("line1\\nline2", escapeJson("line1\nline2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson_carriageReturn() throws Throwable {
|
||||
assertEquals("line1\\rline2", escapeJson("line1\rline2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJson_allSpecialChars() throws Throwable {
|
||||
assertEquals("a\\\\b\\\"c\\nd\\re", escapeJson("a\\b\"c\nd\re"));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// generateCallId tests
|
||||
// ========================================================================
|
||||
|
||||
@Test
|
||||
void generateCallId_alwaysNonNegative() throws Throwable {
|
||||
for (int i = 0; i < 200; i++) {
|
||||
long id = generateCallId();
|
||||
assertTrue(id >= 0, "generateCallId returned negative: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCallId_producesVariation() throws Throwable {
|
||||
long first = generateCallId();
|
||||
boolean foundDifferent = false;
|
||||
for (int i = 0; i < 20; i++) {
|
||||
if (generateCallId() != first) {
|
||||
foundDifferent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(foundDifferent, "generateCallId returned same value 21 times in a row");
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// handleStateChange state machine tests
|
||||
//
|
||||
// Since handleStateChange is a private instance method requiring a full
|
||||
// CallManager (which needs Context), we test the state transition logic
|
||||
// directly by reproducing its documented rules against CallState.
|
||||
// The rules are:
|
||||
// "Incoming*" -> RINGING_INCOMING (unless already CONNECTING)
|
||||
// "Outgoing*" -> RINGING_OUTGOING
|
||||
// "Ringing" -> triggers deferred accept (no state change)
|
||||
// "Connected" -> CONNECTED
|
||||
// "Connecting"-> RECONNECTING
|
||||
// "Ended"/"Rejected" -> would call endCall (sets ENDED)
|
||||
// "Concluded" -> no-op
|
||||
// ========================================================================
|
||||
|
||||
@Test
|
||||
void stateTransition_incomingToRingingIncoming() {
|
||||
var state = makeCallState(1L, CallInfo.State.IDLE);
|
||||
applyStateTransition(state, "Incoming(Audio)", null);
|
||||
assertEquals(CallInfo.State.RINGING_INCOMING, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_incomingWithMediaType() {
|
||||
var state = makeCallState(1L, CallInfo.State.IDLE);
|
||||
applyStateTransition(state, "Incoming(Video)", null);
|
||||
assertEquals(CallInfo.State.RINGING_INCOMING, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_incomingDoesNotDowngradeFromConnecting() {
|
||||
var state = makeCallState(1L, CallInfo.State.CONNECTING);
|
||||
applyStateTransition(state, "Incoming(Audio)", null);
|
||||
// Must remain CONNECTING, not downgraded to RINGING_INCOMING
|
||||
assertEquals(CallInfo.State.CONNECTING, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_outgoing() {
|
||||
var state = makeCallState(1L, CallInfo.State.IDLE);
|
||||
applyStateTransition(state, "Outgoing(Audio)", null);
|
||||
assertEquals(CallInfo.State.RINGING_OUTGOING, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_connected() {
|
||||
var state = makeCallState(1L, CallInfo.State.CONNECTING);
|
||||
applyStateTransition(state, "Connected", null);
|
||||
assertEquals(CallInfo.State.CONNECTED, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_connectingMapsToReconnecting() {
|
||||
// "Connecting" from RingRTC means ICE reconnection, not initial connect
|
||||
var state = makeCallState(1L, CallInfo.State.CONNECTED);
|
||||
applyStateTransition(state, "Connecting", null);
|
||||
assertEquals(CallInfo.State.RECONNECTING, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_ringingDoesNotChangeState() {
|
||||
var state = makeCallState(1L, CallInfo.State.RINGING_INCOMING);
|
||||
applyStateTransition(state, "Ringing", null);
|
||||
// "Ringing" triggers sendAcceptIfReady but doesn't change state
|
||||
assertEquals(CallInfo.State.RINGING_INCOMING, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_ringSetsAcceptPendingFalseWhenReady() {
|
||||
var state = makeCallState(1L, CallInfo.State.RINGING_INCOMING);
|
||||
state.acceptPending = true;
|
||||
// No controlWriter set, so accept won't actually send but acceptPending stays true
|
||||
// This documents the behavior: without a controlWriter, deferred accept stays pending
|
||||
applyStateTransition(state, "Ringing", null);
|
||||
assertTrue(state.acceptPending, "acceptPending should remain true when controlWriter is null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_concludedIsNoop() {
|
||||
var state = makeCallState(1L, CallInfo.State.CONNECTED);
|
||||
applyStateTransition(state, "Concluded", null);
|
||||
// State should NOT change
|
||||
assertEquals(CallInfo.State.CONNECTED, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_endedSetsEnded() {
|
||||
var state = makeCallState(1L, CallInfo.State.CONNECTED);
|
||||
applyStateTransition(state, "Ended", "Timeout");
|
||||
// endCall would set ENDED (we simulate that since endCall is instance method)
|
||||
assertEquals(CallInfo.State.ENDED, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_rejectedSetsEnded() {
|
||||
var state = makeCallState(1L, CallInfo.State.RINGING_INCOMING);
|
||||
applyStateTransition(state, "Rejected", "BusyOnAnotherDevice");
|
||||
assertEquals(CallInfo.State.ENDED, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_endedWithNullReasonUsesStateName() {
|
||||
var state = makeCallState(1L, CallInfo.State.CONNECTED);
|
||||
// When reason is null, endCall should be called with state name lowercased
|
||||
// We verify state becomes ENDED (the reason defaulting logic is in handleStateChange)
|
||||
applyStateTransition(state, "Ended", null);
|
||||
assertEquals(CallInfo.State.ENDED, state.state);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateTransition_unknownStateIsNoop() {
|
||||
var state = makeCallState(1L, CallInfo.State.CONNECTED);
|
||||
applyStateTransition(state, "SomeUnknownState", null);
|
||||
// No matching branch, state unchanged
|
||||
assertEquals(CallInfo.State.CONNECTED, state.state);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// endCall guard condition tests
|
||||
//
|
||||
// endCall sends a Signal protocol hangup UNLESS the reason indicates the
|
||||
// remote side already knows (remote_hangup, rejected, remote_busy, ringrtc_hangup).
|
||||
// We test this logic directly.
|
||||
// ========================================================================
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"remote_hangup", "rejected", "remote_busy", "ringrtc_hangup"})
|
||||
void endCallGuard_remoteCausesSkipHangup(String reason) {
|
||||
// These reasons should NOT trigger sending a hangup to the remote
|
||||
assertTrue(shouldSkipRemoteHangup(reason));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"local_hangup", "ring_timeout", "tunnel_exit", "tunnel_error", "shutdown"})
|
||||
void endCallGuard_localCausesSendHangup(String reason) {
|
||||
// These reasons SHOULD trigger sending a hangup to the remote
|
||||
assertTrue(shouldSendRemoteHangup(reason));
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// CallState.toCallInfo tests
|
||||
// ========================================================================
|
||||
|
||||
@Test
|
||||
void callState_toCallInfo() {
|
||||
var state = makeCallState(42L, CallInfo.State.CONNECTED);
|
||||
state.inputDeviceName = "test_input";
|
||||
state.outputDeviceName = "test_output";
|
||||
|
||||
var info = state.toCallInfo();
|
||||
|
||||
assertEquals(42L, info.callId());
|
||||
assertEquals(CallInfo.State.CONNECTED, info.state());
|
||||
assertEquals("+15551234567", info.recipient().number().orElse(null));
|
||||
assertTrue(info.isOutgoing());
|
||||
assertEquals("test_input", info.inputDeviceName());
|
||||
assertEquals("test_output", info.outputDeviceName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callState_toCallInfoNullDeviceNames() {
|
||||
var state = makeCallState(1L, CallInfo.State.RINGING_INCOMING);
|
||||
|
||||
var info = state.toCallInfo();
|
||||
|
||||
assertEquals(CallInfo.State.RINGING_INCOMING, info.state());
|
||||
assertEquals(null, info.inputDeviceName());
|
||||
assertEquals(null, info.outputDeviceName());
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// Helpers that reproduce the documented logic from handleStateChange and
|
||||
// endCall, allowing us to verify the state machine rules without needing
|
||||
// a full CallManager instance (which requires Context/SignalAccount/etc).
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Reproduces the state transition logic from CallManager.handleStateChange.
|
||||
* This directly mirrors the production code's branching to verify correctness.
|
||||
*/
|
||||
private static void applyStateTransition(CallManager.CallState state, String ringrtcState, String reason) {
|
||||
if (ringrtcState.startsWith("Incoming")) {
|
||||
if (state.state == CallInfo.State.CONNECTING) return;
|
||||
state.state = CallInfo.State.RINGING_INCOMING;
|
||||
} else if (ringrtcState.startsWith("Outgoing")) {
|
||||
state.state = CallInfo.State.RINGING_OUTGOING;
|
||||
} else if ("Ringing".equals(ringrtcState)) {
|
||||
// Would call sendAcceptIfReady — tested separately
|
||||
return;
|
||||
} else if ("Connected".equals(ringrtcState)) {
|
||||
state.state = CallInfo.State.CONNECTED;
|
||||
} else if ("Connecting".equals(ringrtcState)) {
|
||||
state.state = CallInfo.State.RECONNECTING;
|
||||
} else if ("Ended".equals(ringrtcState) || "Rejected".equals(ringrtcState)) {
|
||||
// Simplified: just set ENDED (production code calls endCall which does cleanup + sets ENDED)
|
||||
state.state = CallInfo.State.ENDED;
|
||||
return;
|
||||
} else if ("Concluded".equals(ringrtcState)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reproduces the endCall guard condition: returns true when a Signal protocol
|
||||
* hangup should NOT be sent to the remote peer.
|
||||
*/
|
||||
private static boolean shouldSkipRemoteHangup(String reason) {
|
||||
return "remote_hangup".equals(reason)
|
||||
|| "rejected".equals(reason)
|
||||
|| "remote_busy".equals(reason)
|
||||
|| "ringrtc_hangup".equals(reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverse of shouldSkipRemoteHangup.
|
||||
*/
|
||||
private static boolean shouldSendRemoteHangup(String reason) {
|
||||
return !shouldSkipRemoteHangup(reason);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class AcceptCallCommand implements JsonRpcLocalCommand {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "acceptCall";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.help("Accept an incoming voice call.");
|
||||
subparser.addArgument("--call-id")
|
||||
.type(long.class)
|
||||
.required(true)
|
||||
.help("The call ID to accept.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(
|
||||
final Namespace ns,
|
||||
final Manager m,
|
||||
final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
final var callIdNumber = ns.get("call-id");
|
||||
if (callIdNumber == null) {
|
||||
throw new UserErrorException("No call ID given");
|
||||
}
|
||||
final long callId = ((Number) callIdNumber).longValue();
|
||||
|
||||
try {
|
||||
var callInfo = m.acceptCall(callId);
|
||||
switch (outputWriter) {
|
||||
case PlainTextWriter writer -> {
|
||||
writer.println("Call accepted:");
|
||||
writer.println(" Call ID: {}", callInfo.callId());
|
||||
writer.println(" State: {}", callInfo.state());
|
||||
writer.println(" Input device: {}", callInfo.inputDeviceName());
|
||||
writer.println(" Output device: {}", callInfo.outputDeviceName());
|
||||
}
|
||||
case JsonWriter writer -> writer.write(new JsonCallInfo(callInfo.callId(),
|
||||
callInfo.state().name(),
|
||||
callInfo.inputDeviceName(),
|
||||
callInfo.outputDeviceName(),
|
||||
"opus",
|
||||
48000,
|
||||
1,
|
||||
20));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to accept call: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonCallInfo(
|
||||
long callId,
|
||||
String state,
|
||||
String inputDeviceName,
|
||||
String outputDeviceName,
|
||||
String codec,
|
||||
int sampleRate,
|
||||
int channels,
|
||||
int ptimeMs
|
||||
) {}
|
||||
}
|
||||
@ -10,18 +10,21 @@ public class Commands {
|
||||
private static final Map<String, SubparserAttacher> commandSubparserAttacher = new TreeMap<>();
|
||||
|
||||
static {
|
||||
addCommand(new AcceptCallCommand());
|
||||
addCommand(new AddDeviceCommand());
|
||||
addCommand(new BlockCommand());
|
||||
addCommand(new DaemonCommand());
|
||||
addCommand(new DeleteLocalAccountDataCommand());
|
||||
addCommand(new FinishChangeNumberCommand());
|
||||
addCommand(new FinishLinkCommand());
|
||||
addCommand(new HangupCallCommand());
|
||||
addCommand(new GetAttachmentCommand());
|
||||
addCommand(new GetAvatarCommand());
|
||||
addCommand(new GetStickerCommand());
|
||||
addCommand(new GetUserStatusCommand());
|
||||
addCommand(new AddStickerPackCommand());
|
||||
addCommand(new JoinGroupCommand());
|
||||
addCommand(new ListCallsCommand());
|
||||
addCommand(new JsonRpcDispatcherCommand());
|
||||
addCommand(new LinkCommand());
|
||||
addCommand(new ListAccountsCommand());
|
||||
@ -32,6 +35,7 @@ public class Commands {
|
||||
addCommand(new ListStickerPacksCommand());
|
||||
addCommand(new QuitGroupCommand());
|
||||
addCommand(new ReceiveCommand());
|
||||
addCommand(new RejectCallCommand());
|
||||
addCommand(new RegisterCommand());
|
||||
addCommand(new RemoveContactCommand());
|
||||
addCommand(new RemoveDeviceCommand());
|
||||
@ -52,6 +56,7 @@ public class Commands {
|
||||
addCommand(new SendTypingCommand());
|
||||
addCommand(new SendUnpinMessageCommand());
|
||||
addCommand(new SetPinCommand());
|
||||
addCommand(new StartCallCommand());
|
||||
addCommand(new SubmitRateLimitChallengeCommand());
|
||||
addCommand(new StartChangeNumberCommand());
|
||||
addCommand(new StartLinkCommand());
|
||||
|
||||
@ -0,0 +1,56 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class HangupCallCommand implements JsonRpcLocalCommand {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "hangupCall";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.help("Hang up an active voice call.");
|
||||
subparser.addArgument("--call-id")
|
||||
.type(long.class)
|
||||
.required(true)
|
||||
.help("The call ID to hang up.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(
|
||||
final Namespace ns,
|
||||
final Manager m,
|
||||
final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
final var callIdNumber = ns.get("call-id");
|
||||
if (callIdNumber == null) {
|
||||
throw new UserErrorException("No call ID given");
|
||||
}
|
||||
final long callId = ((Number) callIdNumber).longValue();
|
||||
|
||||
try {
|
||||
m.hangupCall(callId);
|
||||
switch (outputWriter) {
|
||||
case PlainTextWriter writer -> writer.println("Call {} hung up.", callId);
|
||||
case JsonWriter writer -> writer.write(new JsonResult(callId, "hung_up"));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to hang up call: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonResult(long callId, String status) {}
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ListCallsCommand implements JsonRpcLocalCommand {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "listCalls";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.help("List active voice calls.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(
|
||||
final Namespace ns,
|
||||
final Manager m,
|
||||
final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
var calls = m.listActiveCalls();
|
||||
switch (outputWriter) {
|
||||
case PlainTextWriter writer -> {
|
||||
if (calls.isEmpty()) {
|
||||
writer.println("No active calls.");
|
||||
} else {
|
||||
for (var call : calls) {
|
||||
writer.println("- Call {}:", call.callId());
|
||||
writer.indent(w -> {
|
||||
w.println("State: {}", call.state());
|
||||
w.println("Recipient: {}", call.recipient());
|
||||
w.println("Direction: {}", call.isOutgoing() ? "outgoing" : "incoming");
|
||||
if (call.inputDeviceName() != null) {
|
||||
w.println("Input device: {}", call.inputDeviceName());
|
||||
}
|
||||
if (call.outputDeviceName() != null) {
|
||||
w.println("Output device: {}", call.outputDeviceName());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
case JsonWriter writer -> {
|
||||
var jsonCalls = calls.stream()
|
||||
.map(c -> new JsonCall(c.callId(),
|
||||
c.state().name(),
|
||||
c.recipient().number().orElse(null),
|
||||
c.recipient().uuid().map(java.util.UUID::toString).orElse(null),
|
||||
c.isOutgoing(),
|
||||
c.inputDeviceName(),
|
||||
c.outputDeviceName()))
|
||||
.toList();
|
||||
writer.write(jsonCalls);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonCall(
|
||||
long callId,
|
||||
String state,
|
||||
String number,
|
||||
String uuid,
|
||||
boolean isOutgoing,
|
||||
String inputDeviceName,
|
||||
String outputDeviceName
|
||||
) {}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class RejectCallCommand implements JsonRpcLocalCommand {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "rejectCall";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.help("Reject an incoming voice call.");
|
||||
subparser.addArgument("--call-id")
|
||||
.type(long.class)
|
||||
.required(true)
|
||||
.help("The call ID to reject.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(
|
||||
final Namespace ns,
|
||||
final Manager m,
|
||||
final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
final var callIdNumber = ns.get("call-id");
|
||||
if (callIdNumber == null) {
|
||||
throw new UserErrorException("No call ID given");
|
||||
}
|
||||
final long callId = ((Number) callIdNumber).longValue();
|
||||
|
||||
try {
|
||||
m.rejectCall(callId);
|
||||
switch (outputWriter) {
|
||||
case PlainTextWriter writer -> writer.println("Call {} rejected.", callId);
|
||||
case JsonWriter writer -> writer.write(new JsonResult(callId, "rejected"));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to reject call: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonResult(long callId, String status) {}
|
||||
}
|
||||
@ -0,0 +1,80 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.UnregisteredRecipientException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class StartCallCommand implements JsonRpcLocalCommand {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "startCall";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
subparser.help("Start an outgoing voice call.");
|
||||
subparser.addArgument("recipient").help("Specify the recipient's phone number or UUID.").nargs(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(
|
||||
final Namespace ns,
|
||||
final Manager m,
|
||||
final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
final var recipientStrings = ns.<String>getList("recipient");
|
||||
if (recipientStrings == null || recipientStrings.isEmpty()) {
|
||||
throw new UserErrorException("No recipient given");
|
||||
}
|
||||
|
||||
final var recipient = CommandUtil.getSingleRecipientIdentifier(recipientStrings.getFirst(), m.getSelfNumber());
|
||||
|
||||
try {
|
||||
var callInfo = m.startCall(recipient);
|
||||
switch (outputWriter) {
|
||||
case PlainTextWriter writer -> {
|
||||
writer.println("Call started:");
|
||||
writer.println(" Call ID: {}", callInfo.callId());
|
||||
writer.println(" State: {}", callInfo.state());
|
||||
writer.println(" Input device: {}", callInfo.inputDeviceName());
|
||||
writer.println(" Output device: {}", callInfo.outputDeviceName());
|
||||
}
|
||||
case JsonWriter writer -> writer.write(new JsonCallInfo(callInfo.callId(),
|
||||
callInfo.state().name(),
|
||||
callInfo.inputDeviceName(),
|
||||
callInfo.outputDeviceName(),
|
||||
"opus",
|
||||
48000,
|
||||
1,
|
||||
20));
|
||||
}
|
||||
} catch (UnregisteredRecipientException e) {
|
||||
throw new UserErrorException("Recipient not registered: " + e.getMessage(), e);
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to start call: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonCallInfo(
|
||||
long callId,
|
||||
String state,
|
||||
String inputDeviceName,
|
||||
String outputDeviceName,
|
||||
String codec,
|
||||
int sampleRate,
|
||||
int channels,
|
||||
int ptimeMs
|
||||
) {}
|
||||
}
|
||||
@ -913,6 +913,73 @@ public class DbusManagerImpl implements Manager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCallEventListener(final CallEventListener listener) {
|
||||
// Not supported over DBus
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCallEventListener(final CallEventListener listener) {
|
||||
// Not supported over DBus
|
||||
}
|
||||
|
||||
// --- Voice call methods (not supported over DBus) ---
|
||||
|
||||
@Override
|
||||
public org.asamk.signal.manager.api.CallInfo startCall(final org.asamk.signal.manager.api.RecipientIdentifier.Single recipient) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.asamk.signal.manager.api.CallInfo acceptCall(final long callId) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hangupCall(final long callId) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rejectCall(final long callId) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.List<org.asamk.signal.manager.api.CallInfo> listActiveCalls() {
|
||||
return java.util.List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendCallOffer(final org.asamk.signal.manager.api.RecipientIdentifier.Single recipient, final org.asamk.signal.manager.api.CallOffer offer) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendCallAnswer(final org.asamk.signal.manager.api.RecipientIdentifier.Single recipient, final long callId, final byte[] answerOpaque) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendIceUpdate(final org.asamk.signal.manager.api.RecipientIdentifier.Single recipient, final long callId, final java.util.List<byte[]> iceCandidates) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendHangup(final org.asamk.signal.manager.api.RecipientIdentifier.Single recipient, final long callId, final org.asamk.signal.manager.api.MessageEnvelope.Call.Hangup.Type type) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendBusy(final org.asamk.signal.manager.api.RecipientIdentifier.Single recipient, final long callId) {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.List<org.asamk.signal.manager.api.TurnServer> getTurnServerInfo() {
|
||||
throw new UnsupportedOperationException("Voice calls are not supported over DBus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
synchronized (this) {
|
||||
|
||||
32
src/main/java/org/asamk/signal/json/JsonCallEvent.java
Normal file
32
src/main/java/org/asamk/signal/json/JsonCallEvent.java
Normal file
@ -0,0 +1,32 @@
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
|
||||
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
|
||||
|
||||
public record JsonCallEvent(
|
||||
long callId,
|
||||
String state,
|
||||
@JsonInclude(NON_NULL) String number,
|
||||
@JsonInclude(NON_NULL) String uuid,
|
||||
boolean isOutgoing,
|
||||
@JsonInclude(NON_NULL) String inputDeviceName,
|
||||
@JsonInclude(NON_NULL) String outputDeviceName,
|
||||
@JsonInclude(NON_NULL) String reason
|
||||
) {
|
||||
|
||||
public static JsonCallEvent from(CallInfo callInfo, String reason) {
|
||||
return new JsonCallEvent(
|
||||
callInfo.callId(),
|
||||
callInfo.state().name(),
|
||||
callInfo.recipient().number().orElse(null),
|
||||
callInfo.recipient().aci().orElse(null),
|
||||
callInfo.isOutgoing(),
|
||||
callInfo.inputDeviceName(),
|
||||
callInfo.outputDeviceName(),
|
||||
reason
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -24,6 +24,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -40,6 +41,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||
private final boolean noReceiveOnStart;
|
||||
|
||||
private final Map<Integer, List<Pair<Manager, Manager.ReceiveMessageHandler>>> receiveHandlers = new HashMap<>();
|
||||
private final List<Pair<Manager, Manager.CallEventListener>> callEventHandlers = new ArrayList<>();
|
||||
private SignalJsonRpcCommandHandler commandHandler;
|
||||
|
||||
public SignalJsonRpcDispatcherHandler(
|
||||
@ -62,6 +64,11 @@ public class SignalJsonRpcDispatcherHandler {
|
||||
c.addOnManagerRemovedHandler(this::unsubscribeReceive);
|
||||
}
|
||||
|
||||
for (var m : c.getManagers()) {
|
||||
subscribeCallEvents(m);
|
||||
}
|
||||
c.addOnManagerAddedHandler(this::subscribeCallEvents);
|
||||
|
||||
handleConnection();
|
||||
}
|
||||
|
||||
@ -72,12 +79,33 @@ public class SignalJsonRpcDispatcherHandler {
|
||||
subscribeReceive(m, true);
|
||||
}
|
||||
|
||||
subscribeCallEvents(m);
|
||||
|
||||
final var currentThread = Thread.currentThread();
|
||||
m.addClosedListener(currentThread::interrupt);
|
||||
|
||||
handleConnection();
|
||||
}
|
||||
|
||||
private void subscribeCallEvents(final Manager manager) {
|
||||
Manager.CallEventListener listener = (callInfo, reason) -> {
|
||||
final var params = new ObjectNode(objectMapper.getNodeFactory());
|
||||
params.set("account", params.textNode(manager.getSelfNumber()));
|
||||
params.set("callEvent", objectMapper.valueToTree(
|
||||
org.asamk.signal.json.JsonCallEvent.from(callInfo, reason)));
|
||||
final var jsonRpcRequest = JsonRpcRequest.forNotification("callEvent", params, null);
|
||||
try {
|
||||
jsonRpcSender.sendRequest(jsonRpcRequest);
|
||||
} catch (AssertionError e) {
|
||||
if (e.getCause() instanceof ClosedChannelException) {
|
||||
logger.debug("Call event channel closed, removing listener");
|
||||
}
|
||||
}
|
||||
};
|
||||
manager.addCallEventListener(listener);
|
||||
callEventHandlers.add(new Pair<>(manager, listener));
|
||||
}
|
||||
|
||||
private static final AtomicInteger nextSubscriptionId = new AtomicInteger(0);
|
||||
|
||||
private int subscribeReceive(final Manager manager, boolean internalSubscription) {
|
||||
@ -141,6 +169,10 @@ public class SignalJsonRpcDispatcherHandler {
|
||||
} finally {
|
||||
receiveHandlers.forEach((_subscriptionId, handlers) -> handlers.forEach(this::unsubscribeReceiveHandler));
|
||||
receiveHandlers.clear();
|
||||
for (var pair : callEventHandlers) {
|
||||
pair.first().removeCallEventListener(pair.second());
|
||||
}
|
||||
callEventHandlers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1947,6 +1947,40 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.AcceptCallCommand$JsonCallInfo",
|
||||
"allDeclaredFields": true,
|
||||
"methods": [
|
||||
{
|
||||
"name": "callId",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "channels",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "codec",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "mediaSocketPath",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "ptimeMs",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "sampleRate",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "state",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.FinishLinkCommand$FinishLinkParams",
|
||||
"allDeclaredFields": true,
|
||||
@ -1984,6 +2018,20 @@
|
||||
"allDeclaredMethods": true,
|
||||
"allDeclaredConstructors": true
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.HangupCallCommand$JsonResult",
|
||||
"allDeclaredFields": true,
|
||||
"methods": [
|
||||
{
|
||||
"name": "callId",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "status",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.ListAccountsCommand$JsonAccount",
|
||||
"allDeclaredFields": true,
|
||||
@ -1994,6 +2042,39 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.ListCallsCommand$JsonCall",
|
||||
"allDeclaredFields": true,
|
||||
"methods": [
|
||||
{
|
||||
"name": "callId",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "isOutgoing",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "mediaSocketPath",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "number",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "state",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "uuid",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.ListCallsCommand$JsonCall[]"
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.ListContactsCommand$JsonContact",
|
||||
"allDeclaredFields": true,
|
||||
@ -2159,6 +2240,54 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.RejectCallCommand$JsonResult",
|
||||
"allDeclaredFields": true,
|
||||
"methods": [
|
||||
{
|
||||
"name": "callId",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "status",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.StartCallCommand$JsonCallInfo",
|
||||
"allDeclaredFields": true,
|
||||
"methods": [
|
||||
{
|
||||
"name": "callId",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "channels",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "codec",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "mediaSocketPath",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "ptimeMs",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "sampleRate",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "state",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.asamk.signal.commands.StartLinkCommand$JsonLink",
|
||||
"allDeclaredFields": true,
|
||||
@ -9782,4 +9911,4 @@
|
||||
"bundle": "net.sourceforge.argparse4j.internal.ArgumentParserImpl"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,79 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Verifies that call commands correctly handle call IDs from JSON-RPC,
|
||||
* where Jackson may deserialize large numbers as BigInteger instead of Long.
|
||||
*/
|
||||
class CallCommandParsingTest {
|
||||
|
||||
/**
|
||||
* Simulates what Jackson produces for a JSON-RPC call with a large call ID.
|
||||
* Jackson deserializes numbers that overflow int as BigInteger in untyped maps.
|
||||
*/
|
||||
private static Namespace namespaceWithBigIntegerCallId(long value) {
|
||||
// JsonRpcNamespace converts "call-id" to "callId" lookup
|
||||
return new JsonRpcNamespace(Map.of("callId", BigInteger.valueOf(value)));
|
||||
}
|
||||
|
||||
private static Namespace namespaceWithLongCallId(long value) {
|
||||
return new JsonRpcNamespace(Map.of("callId", value));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hangupCallHandlesBigIntegerCallId() {
|
||||
var ns = namespaceWithBigIntegerCallId(8230211930154373276L);
|
||||
var callIdNumber = ns.get("call-id");
|
||||
long callId = ((Number) callIdNumber).longValue();
|
||||
assertEquals(8230211930154373276L, callId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hangupCallHandlesLongCallId() {
|
||||
var ns = namespaceWithLongCallId(8230211930154373276L);
|
||||
var callIdNumber = ns.get("call-id");
|
||||
long callId = ((Number) callIdNumber).longValue();
|
||||
assertEquals(8230211930154373276L, callId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptCallHandlesBigIntegerCallId() {
|
||||
var ns = namespaceWithBigIntegerCallId(1234567890123456789L);
|
||||
var callIdNumber = ns.get("call-id");
|
||||
long callId = ((Number) callIdNumber).longValue();
|
||||
assertEquals(1234567890123456789L, callId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectCallHandlesBigIntegerCallId() {
|
||||
var ns = namespaceWithBigIntegerCallId(Long.MAX_VALUE);
|
||||
var callIdNumber = ns.get("call-id");
|
||||
long callId = ((Number) callIdNumber).longValue();
|
||||
assertEquals(Long.MAX_VALUE, callId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void camelCaseKeyLookupWorks() {
|
||||
// Verify JsonRpcNamespace maps "call-id" -> "callId"
|
||||
var ns = new JsonRpcNamespace(Map.of("callId", BigInteger.valueOf(42L)));
|
||||
Number result = ns.get("call-id");
|
||||
assertEquals(42L, result.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void smallIntegerCallIdWorks() {
|
||||
// Jackson may produce Integer for small values
|
||||
var ns = new JsonRpcNamespace(Map.of("callId", 42));
|
||||
var callIdNumber = ns.get("call-id");
|
||||
long callId = ((Number) callIdNumber).longValue();
|
||||
assertEquals(42L, callId);
|
||||
}
|
||||
}
|
||||
110
src/test/java/org/asamk/signal/json/JsonCallEventTest.java
Normal file
110
src/test/java/org/asamk/signal/json/JsonCallEventTest.java
Normal file
@ -0,0 +1,110 @@
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
import org.asamk.signal.manager.api.RecipientAddress;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class JsonCallEventTest {
|
||||
|
||||
@Test
|
||||
void fromWithNumberAndUuid() {
|
||||
var recipient = new RecipientAddress("a1b2c3d4-e5f6-7890-abcd-ef1234567890", null, "+15551234567", null);
|
||||
var callInfo = new CallInfo(123L, CallInfo.State.CONNECTED, recipient, "signal_input_123", "signal_output_123", true);
|
||||
|
||||
var event = JsonCallEvent.from(callInfo, null);
|
||||
|
||||
assertEquals(123L, event.callId());
|
||||
assertEquals("CONNECTED", event.state());
|
||||
assertEquals("+15551234567", event.number());
|
||||
assertEquals("a1b2c3d4-e5f6-7890-abcd-ef1234567890", event.uuid());
|
||||
assertTrue(event.isOutgoing());
|
||||
assertEquals("signal_input_123", event.inputDeviceName());
|
||||
assertEquals("signal_output_123", event.outputDeviceName());
|
||||
assertNull(event.reason());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromWithUuidOnly() {
|
||||
var recipient = new RecipientAddress("a1b2c3d4-e5f6-7890-abcd-ef1234567890", null, null, null);
|
||||
var callInfo = new CallInfo(456L, CallInfo.State.RINGING_INCOMING, recipient, "signal_input_456", "signal_output_456", false);
|
||||
|
||||
var event = JsonCallEvent.from(callInfo, null);
|
||||
|
||||
assertEquals(456L, event.callId());
|
||||
assertEquals("RINGING_INCOMING", event.state());
|
||||
assertNull(event.number());
|
||||
assertEquals("a1b2c3d4-e5f6-7890-abcd-ef1234567890", event.uuid());
|
||||
assertFalse(event.isOutgoing());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromWithNumberOnly() {
|
||||
var recipient = new RecipientAddress(null, null, "+15559876543", null);
|
||||
var callInfo = new CallInfo(789L, CallInfo.State.RINGING_OUTGOING, recipient, "signal_input_789", "signal_output_789", true);
|
||||
|
||||
var event = JsonCallEvent.from(callInfo, null);
|
||||
|
||||
assertEquals("+15559876543", event.number());
|
||||
assertNull(event.uuid());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromWithEndedStateAndReason() {
|
||||
var recipient = new RecipientAddress("uuid-1234", null, "+15551111111", null);
|
||||
var callInfo = new CallInfo(101L, CallInfo.State.ENDED, recipient, null, null, false);
|
||||
|
||||
var event = JsonCallEvent.from(callInfo, "remote_hangup");
|
||||
|
||||
assertEquals("ENDED", event.state());
|
||||
assertEquals("remote_hangup", event.reason());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromMapsAllStates() {
|
||||
var recipient = new RecipientAddress("uuid-1234", null, "+15551111111", null);
|
||||
|
||||
for (var state : CallInfo.State.values()) {
|
||||
var callInfo = new CallInfo(1L, state, recipient, "signal_input_1", "signal_output_1", true);
|
||||
var event = JsonCallEvent.from(callInfo, null);
|
||||
assertEquals(state.name(), event.state());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromConnectingState() {
|
||||
var recipient = new RecipientAddress("uuid-5678", null, "+15552222222", null);
|
||||
var callInfo = new CallInfo(200L, CallInfo.State.CONNECTING, recipient, "signal_input_200", "signal_output_200", true);
|
||||
|
||||
var event = JsonCallEvent.from(callInfo, null);
|
||||
|
||||
assertEquals(200L, event.callId());
|
||||
assertEquals("CONNECTING", event.state());
|
||||
assertEquals("signal_input_200", event.inputDeviceName());
|
||||
assertEquals("signal_output_200", event.outputDeviceName());
|
||||
assertTrue(event.isOutgoing());
|
||||
assertNull(event.reason());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromWithVariousEndReasons() {
|
||||
var recipient = new RecipientAddress("uuid-1234", null, "+15551111111", null);
|
||||
|
||||
var reasons = new String[]{"local_hangup", "remote_hangup", "rejected", "remote_busy",
|
||||
"ring_timeout", "ice_failed", "tunnel_exit", "tunnel_error", "shutdown"};
|
||||
|
||||
for (var reason : reasons) {
|
||||
var callInfo = new CallInfo(1L, CallInfo.State.ENDED, recipient, null, null, false);
|
||||
var event = JsonCallEvent.from(callInfo, reason);
|
||||
assertEquals(reason, event.reason());
|
||||
assertEquals("ENDED", event.state());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user