|
3 | 3 |
|
4 | 4 | from django.contrib.postgres.fields import JSONField |
5 | 5 | from django.db import models |
| 6 | +from django.db.models import Q |
6 | 7 |
|
7 | 8 | from .basemodel import BaseModel |
8 | 9 |
|
9 | 10 |
|
| 11 | +def _positive_stat_counter(val): |
| 12 | + try: |
| 13 | + return float(val) > 0 |
| 14 | + except (TypeError, ValueError): |
| 15 | + return False |
| 16 | + |
| 17 | + |
| 18 | +def _candidate_pair_stats_indicate_established(conn): |
| 19 | + """ |
| 20 | + RTC candidate-pair stats (merged into parsed stats connection block): require |
| 21 | + succeeded plus a signal that the pair is actually in use, not only reported. |
| 22 | + """ |
| 23 | + if not isinstance(conn, dict) or conn.get('state') != 'succeeded': |
| 24 | + return False |
| 25 | + if conn.get('nominated') is True: |
| 26 | + return True |
| 27 | + if conn.get('selected') is True: |
| 28 | + return True |
| 29 | + if _positive_stat_counter(conn.get('bytesSent')) or _positive_stat_counter(conn.get('bytesReceived')): |
| 30 | + return True |
| 31 | + if conn.get('currentRoundTripTime') is not None: |
| 32 | + return True |
| 33 | + return False |
| 34 | + |
| 35 | + |
| 36 | +def _stats_payload_indicates_established(data): |
| 37 | + """ |
| 38 | + True only when parsed stats show ICE completion or media counters advancing — |
| 39 | + not the first getStats() poll right after RTCPeerConnection creation (those rows |
| 40 | + still use type=stats and carry minimal / empty connection snapshots). |
| 41 | + """ |
| 42 | + if not isinstance(data, dict): |
| 43 | + return False |
| 44 | + |
| 45 | + conn = data.get('connection') |
| 46 | + if isinstance(conn, dict): |
| 47 | + if _candidate_pair_stats_indicate_established(conn): |
| 48 | + return True |
| 49 | + if _positive_stat_counter(conn.get('bytesReceived')) or _positive_stat_counter( |
| 50 | + conn.get('bytesSent') |
| 51 | + ): |
| 52 | + return True |
| 53 | + local_ct = (conn.get('local') or {}).get('candidateType') |
| 54 | + remote_ct = (conn.get('remote') or {}).get('candidateType') |
| 55 | + if local_ct and remote_ct: |
| 56 | + return True |
| 57 | + |
| 58 | + def media_blocks_have_traffic(block): |
| 59 | + if not isinstance(block, dict): |
| 60 | + return False |
| 61 | + for direction in ('inbound', 'outbound'): |
| 62 | + reports = block.get(direction) |
| 63 | + if not isinstance(reports, list): |
| 64 | + continue |
| 65 | + for report in reports: |
| 66 | + if not isinstance(report, dict): |
| 67 | + continue |
| 68 | + if ( |
| 69 | + _positive_stat_counter(report.get('bytesReceived')) |
| 70 | + or _positive_stat_counter(report.get('bytesSent')) |
| 71 | + or _positive_stat_counter(report.get('packetsReceived')) |
| 72 | + or _positive_stat_counter(report.get('packetsSent')) |
| 73 | + ): |
| 74 | + return True |
| 75 | + return False |
| 76 | + |
| 77 | + for kind in ('audio', 'video'): |
| 78 | + if media_blocks_have_traffic(data.get(kind)): |
| 79 | + return True |
| 80 | + |
| 81 | + remote = data.get('remote') |
| 82 | + if isinstance(remote, dict): |
| 83 | + for kind in ('audio', 'video'): |
| 84 | + if media_blocks_have_traffic(remote.get(kind)): |
| 85 | + return True |
| 86 | + |
| 87 | + # Per-track stats rows (StatsView.save_event for each track) — flat RTP-ish dict |
| 88 | + if ( |
| 89 | + _positive_stat_counter(data.get('bytesReceived')) |
| 90 | + or _positive_stat_counter(data.get('bytesSent')) |
| 91 | + or _positive_stat_counter(data.get('packetsReceived')) |
| 92 | + or _positive_stat_counter(data.get('packetsSent')) |
| 93 | + ): |
| 94 | + return True |
| 95 | + |
| 96 | + return False |
| 97 | + |
| 98 | + |
10 | 99 | ISSUES = { |
11 | 100 | # ERRORS |
12 | 101 | 'no_media_access': { |
@@ -189,9 +278,31 @@ def check_end_session(session): |
189 | 278 | # we're looking if all connections are in an unfinished state |
190 | 279 | connection_bad_state = ['new', 'connecting', 'failed'] |
191 | 280 | connections = session.connections.all() |
| 281 | + # Only stats batches that actually show ICE/RTP progress (not an empty early poll). |
| 282 | + connection_ids_with_establishing_stats = set() |
| 283 | + for ev in session.events.filter(type='stats').exclude(connection_id=None).only( |
| 284 | + 'connection_id', 'data' |
| 285 | + ): |
| 286 | + if ev.data and _stats_payload_indicates_established(ev.data): |
| 287 | + connection_ids_with_establishing_stats.add(ev.connection_id) |
| 288 | + # If the peer leaves, the PC often ends in failed while rows still look "unfinished". |
| 289 | + # Once we logged connected/completed, do not treat as never-connected. |
| 290 | + connection_ids_ever_established = set( |
| 291 | + session.events.filter( |
| 292 | + Q(type='onconnectionstatechange', data='connected') |
| 293 | + | Q(type='oniceconnectionstatechange', data='connected') |
| 294 | + | Q(type='oniceconnectionstatechange', data='completed'), |
| 295 | + ) |
| 296 | + .exclude(connection_id=None) |
| 297 | + .values_list('connection_id', flat=True) |
| 298 | + ) |
| 299 | + established_ids = connection_ids_with_establishing_stats | connection_ids_ever_established |
192 | 300 | # if the user had at least one connection |
193 | 301 | if len(connections) > 0: |
194 | | - bad_conns = [c for c in connections if c.state in connection_bad_state] |
| 302 | + bad_conns = [ |
| 303 | + c for c in connections |
| 304 | + if c.state in connection_bad_state and c.id not in established_ids |
| 305 | + ] |
195 | 306 |
|
196 | 307 | if len(connections) == len(bad_conns): |
197 | 308 | Issue( |
|
0 commit comments