Приоритет вызова
This commit is contained in:
parent
be71d05a31
commit
8200884827
@ -18,6 +18,7 @@ API_PORT = 18500
|
||||
SSRC = 269484035
|
||||
RTP_PAYLOAD_TYPE = 8
|
||||
PACKET_DURATION_MS = 20
|
||||
CALL_MODIFIER = "priority" # можно заменить на 'emergency', 'broadcast', и др.
|
||||
|
||||
|
||||
def log(msg):
|
||||
@ -44,14 +45,14 @@ def get_location_initialize_command():
|
||||
return [{"identifier": "location:0/initialization_request", "payload": {}}]
|
||||
|
||||
|
||||
def get_transmit_command(promise_id, ssrc, target, source):
|
||||
def get_transmit_command(promise_id, ssrc, target, source, modifier=CALL_MODIFIER):
|
||||
return [{
|
||||
"identifier": "call:1/transmit_request",
|
||||
"payload": {
|
||||
"promise_id": promise_id,
|
||||
"source": source,
|
||||
"target": target,
|
||||
"modifier": "not",
|
||||
"modifier": modifier,
|
||||
"voip_ssrc": ssrc
|
||||
}
|
||||
}]
|
||||
@ -73,6 +74,20 @@ async def send_messages(websocket, message):
|
||||
await websocket.close()
|
||||
|
||||
|
||||
async def wait_for_own_tx(event_id, source_id, participant_events, timeout=2):
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < timeout:
|
||||
for payload in participant_events:
|
||||
if (
|
||||
payload.get("composite_call_id") == event_id
|
||||
and payload.get("state") == "own_tx"
|
||||
and payload.get("source") == source_id
|
||||
):
|
||||
return True
|
||||
await asyncio.sleep(0.1)
|
||||
return False
|
||||
|
||||
|
||||
async def receive_messages(websocket, auth_dict):
|
||||
try:
|
||||
promise_id = 2
|
||||
@ -82,6 +97,8 @@ async def receive_messages(websocket, auth_dict):
|
||||
source_id = auth_dict.get("client_id")
|
||||
waiting_for_grant = False
|
||||
current_voice_call = None
|
||||
granted_event_id = None
|
||||
participant_events = []
|
||||
|
||||
async for message in websocket:
|
||||
message_from_json = json.loads(message)
|
||||
@ -104,26 +121,38 @@ async def receive_messages(websocket, auth_dict):
|
||||
elif identifier == "location:0/position_broadcast" and auth_dict.get("wlocationsystem"):
|
||||
await save_location(message_item["payload"])
|
||||
|
||||
elif identifier == "call:1/participant_state_broadcast":
|
||||
participant_events.append(message_item["payload"])
|
||||
|
||||
elif identifier == "core/promise_response" and message_item["payload"].get("identifier") == "call:1/transmit_response":
|
||||
payload = message_item["payload"]
|
||||
if payload["state"] == "granted" and current_voice_call:
|
||||
log("Разрешение получено. Начинаем трансляцию...")
|
||||
await set_voice_call_sent(current_voice_call)
|
||||
await start_audio_stream(
|
||||
current_voice_call.alarm_file.audio_file.path,
|
||||
auth_dict["radiusip_host_ws"],
|
||||
voip_port,
|
||||
ssrc
|
||||
)
|
||||
await send_messages(websocket, get_promise_cancel_command(promise_id))
|
||||
log("Трансляция завершена и вызов остановлен.")
|
||||
granted_event_id = payload.get("event_id")
|
||||
log("Разрешение получено. Ожидаем подтверждение own_tx...")
|
||||
confirmed = await wait_for_own_tx(granted_event_id, source_id, participant_events)
|
||||
if confirmed:
|
||||
log("own_tx подтвержден. Начинаем трансляцию...")
|
||||
await set_voice_call_sent(current_voice_call)
|
||||
await start_audio_stream(
|
||||
current_voice_call.alarm_file.audio_file.path,
|
||||
auth_dict["radiusip_host_ws"],
|
||||
voip_port,
|
||||
ssrc
|
||||
)
|
||||
await send_messages(websocket, get_promise_cancel_command(promise_id))
|
||||
log("Трансляция завершена и вызов остановлен.")
|
||||
else:
|
||||
log("own_tx не получен. Передача не начата. Отмена...")
|
||||
await send_messages(websocket, get_promise_cancel_command(promise_id))
|
||||
promise_id += 1
|
||||
waiting_for_grant = False
|
||||
current_voice_call = None
|
||||
participant_events.clear()
|
||||
elif payload["state"] == "faulted":
|
||||
log("[ОШИБКА] Передача отклонена сервером.")
|
||||
waiting_for_grant = False
|
||||
current_voice_call = None
|
||||
participant_events.clear()
|
||||
|
||||
elif call_mapi_initialized and auth_dict.get("plasystem") and not waiting_for_grant:
|
||||
voice_call = await get_voice_calls()
|
||||
@ -131,7 +160,13 @@ async def receive_messages(websocket, auth_dict):
|
||||
log("Запрос на отправку аудио...")
|
||||
await send_messages(
|
||||
websocket,
|
||||
get_transmit_command(promise_id, ssrc, auth_dict["raduisip_group"], source_id)
|
||||
get_transmit_command(
|
||||
promise_id,
|
||||
ssrc,
|
||||
auth_dict["raduisip_group"],
|
||||
source_id,
|
||||
modifier=CALL_MODIFIER
|
||||
)
|
||||
)
|
||||
current_voice_call = voice_call
|
||||
waiting_for_grant = True
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user