|
14 | 14 | ] |
15 | 15 | """@private""" |
16 | 16 |
|
17 | | -# Calls that dial a phone (SIP CreateSIPParticipant with wait_until_answered and |
18 | | -# TransferSIPParticipant; WhatsApp AcceptWhatsAppCall with wait_until_answered) |
19 | | -# take longer than a normal request. |
20 | | -DIAL_TIMEOUT = 30.0 |
| 17 | +# Ring window (seconds) assumed when a request doesn't set ringing_timeout; |
| 18 | +# matches the server default. A dialing request must outlast it. |
| 19 | +DEFAULT_RINGING_TIMEOUT = 30.0 |
21 | 20 | """@private""" |
22 | 21 |
|
23 | 22 | # A dialing request must outlast the ringing window, or it would abort before |
24 | 23 | # the call can be answered. Keep the request timeout at least this many seconds |
25 | | -# above the request's ringing_timeout. |
| 24 | +# above the ringing timeout. |
26 | 25 | RINGING_TIMEOUT_MARGIN = 2.0 |
27 | 26 | """@private""" |
28 | 27 |
|
29 | 28 |
|
| 29 | +def pin_ringing_timeout(request: DialRequest) -> None: |
| 30 | + """Set the ring window explicitly on a dialing request when the caller left it |
| 31 | + unset, so the derived request timeout doesn't depend on the server's default |
| 32 | + (which could change out from under us). |
| 33 | +
|
| 34 | + @private |
| 35 | + """ |
| 36 | + if not request.HasField("ringing_timeout"): |
| 37 | + request.ringing_timeout.seconds = int(DEFAULT_RINGING_TIMEOUT) |
| 38 | + |
| 39 | + |
30 | 40 | def dial_timeout(user_timeout: Optional[float], request: DialRequest) -> float: |
31 | | - """Request timeout (seconds) for a phone-dialing call: the user-supplied |
32 | | - value (or the dial default) raised, when needed, to stay at least |
33 | | - RINGING_TIMEOUT_MARGIN above the request's ringing_timeout. |
| 41 | + """Request timeout (seconds) for a phone-dialing call: the ring window plus a |
| 42 | + margin, so the request doesn't abort before the call can be answered. The |
| 43 | + ring window is the request's ringing_timeout when set, else |
| 44 | + DEFAULT_RINGING_TIMEOUT. A longer user_timeout is honored; a shorter one is |
| 45 | + raised to the floor. |
34 | 46 |
|
35 | 47 | @private |
36 | 48 | """ |
37 | | - effective = user_timeout if user_timeout else DIAL_TIMEOUT |
38 | 49 | if request.HasField("ringing_timeout"): |
39 | | - effective = max(effective, request.ringing_timeout.seconds + RINGING_TIMEOUT_MARGIN) |
40 | | - return effective |
| 50 | + ring: float = request.ringing_timeout.seconds |
| 51 | + else: |
| 52 | + ring = DEFAULT_RINGING_TIMEOUT |
| 53 | + floor = ring + RINGING_TIMEOUT_MARGIN |
| 54 | + return max(user_timeout if user_timeout else floor, floor) |
0 commit comments