[{"data":1,"prerenderedAt":3544},["ShallowReactive",2],{"page-/blog/22":3},{"id":4,"title":5,"body":6,"created":3528,"description":3529,"extension":3530,"image":3531,"meta":3532,"navigation":367,"path":3533,"seo":3534,"short":3535,"stem":3536,"tags":3537,"updated":3528,"__hash__":3543},"content/blog/22/index.md","Two-way audio on a Xiaomi Xiaofang iSC5 with fang-hacks and Frigate",{"type":7,"value":8,"toc":3516},"minimark",[9,14,36,50,53,69,78,83,107,114,118,137,140,163,166,189,203,212,247,250,261,264,273,281,302,311,314,318,321,452,463,467,487,494,497,517,531,538,592,599,606,610,639,653,662,666,673,679,709,728,737,2922,2926,2936,2950,2960,3304,3314,3323,3330,3336,3340,3343,3352,3376,3379,3388,3400,3403,3410,3414,3417,3456,3459,3463,3512],[10,11,13],"h1",{"id":12},"making-a-xiaofang-talk-back","Making a Xiaofang talk back",[15,16,17,18,22,23,30,31,35],"p",{},"The small white Xiaomi Xiaofang (model iSC5, ",[19,20,21],"code",{},"isa.camera.isc5",") can be a local RTSP camera, and it can play sound through its speaker. The path is ",[24,25,29],"a",{"href":26,"rel":27},"https://github.com/samtap/fang-hacks",[28],"nofollow","fang-hacks"," on the Sonix SN98600, plus a tiny RTSP proxy in front of go2rtc. It is a different camera from the Yi and Tuya units in the ",[24,32,34],{"href":33},"/blog/21","previous two-way audio guide",".",[37,38,39],"blockquote",{},[15,40,41,42,45,46,49],{},"This SoC is Sonix. The Dafang procedure (Ingenic, ",[19,43,44],{},"cfw.bin"," renamed to ",[19,47,48],{},"demo.bin",", RTSP on port 8554, flashing with the SETUP button) can brick an iSC5. SETUP is only for stock pairing and for the official Sonix downgrade below.",[15,51,52],{},"Frigate shows the microphone button when three things are true at once:",[54,55,56,63,66],"ol",{},[57,58,59,60,35],"li",{},"The go2rtc producer advertises ",[19,61,62],{},"audio, sendonly",[57,64,65],{},"The page is HTTPS.",[57,67,68],{},"The live view is WebRTC. MSE has no microphone.",[15,70,71,72,77],{},"The stock fang-hacks RTSP server does not advertise a return channel. The proxy adds one. The speaker itself is a userspace player from fang-hacks issue ",[24,73,76],{"href":74,"rel":75},"https://github.com/samtap/fang-hacks/issues/305",[28],"#305",". No firmware patch is required for the audio.",[79,80,82],"h2",{"id":81},"what-you-need","What you need",[84,85,86,89,92,95,98,101],"ul",{},[57,87,88],{},"A Xiaofang iSC5 on firmware that can run fang-hacks. 3.2.x cannot. 3.0.3.56 can.",[57,90,91],{},"A microSD with a small FAT32 partition and an ext2 partition. Shell scripts must be Unix LF.",[57,93,94],{},"A 5 V charger, 2 A preferred. A PC USB port does not hold this camera up. The micro-USB plug is easy to knock loose.",[57,96,97],{},"2.4 GHz Wi-Fi only.",[57,99,100],{},"Frigate 0.18 with the bundled go2rtc, reached over HTTPS.",[57,102,103,106],{},[19,104,105],{},"CAMERA_IP"," in every command below is the camera address on your LAN.",[15,108,109,110,113],{},"The speaker on this model is quiet. A blinking rear LED does not always mean it is offline. Confirm with DHCP, ARP, or HTTP. There is no boot beep. A beep when the card is inserted means ",[19,111,112],{},"snx_autorun.sh"," ran.",[79,115,117],{"id":116},"step-1-put-fang-hacks-on-a-firmware-that-runs-it","Step 1: put fang-hacks on a firmware that runs it",[15,119,120,121,126,127,130,131,133,134,35],{},"Download the 0.2.0 SD image from the ",[24,122,125],{"href":123,"rel":124},"https://github.com/samtap/fang-hacks/releases/tag/0.2.0",[28],"fang-hacks releases",". The FAT partition gets ",[19,128,129],{},"bootstrap/"," and ",[19,132,112],{},". The ext2 partition gets ",[19,135,136],{},"data/",[15,138,139],{},"On the FAT partition, write the Wi-Fi client files with no trailing newline:",[141,142,147],"pre",{"className":143,"code":144,"language":145,"meta":146,"style":146},"language-sh shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","printf '%s' 'YOUR_SSID' > .wifissid\nprintf '%s' 'YOUR_PASSWORD' > .wifipasswd\n","sh","",[19,148,149,157],{"__ignoreMap":146},[150,151,154],"span",{"class":152,"line":153},"line",1,[150,155,156],{},"printf '%s' 'YOUR_SSID' > .wifissid\n",[150,158,160],{"class":152,"line":159},2,[150,161,162],{},"printf '%s' 'YOUR_PASSWORD' > .wifipasswd\n",[15,164,165],{},"In the fang-hacks config, stay local:",[141,167,171],{"className":168,"code":169,"language":170,"meta":146,"style":146},"language-ini shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","HACKS_ENABLED=1\nDISABLE_CLOUD=1\nNETWORK_MODE=1\n","ini",[19,172,173,178,183],{"__ignoreMap":146},[150,174,175],{"class":152,"line":153},[150,176,177],{},"HACKS_ENABLED=1\n",[150,179,180],{"class":152,"line":159},[150,181,182],{},"DISABLE_CLOUD=1\n",[150,184,186],{"class":152,"line":185},3,[150,187,188],{},"NETWORK_MODE=1\n",[15,190,191,192,195,196,198,199,202],{},"Firmware 3.2.x mounts the card and logs ",[19,193,194],{},"SD can Insert",", then never runs ",[19,197,112],{},". The SD log is XOR 0x09. If it says ",[19,200,201],{},"3.2.2_112"," (or another 3.2.x), downgrade before expecting RTSP.",[15,204,205,206,211],{},"The official SD downgrade is documented in the ",[24,207,210],{"href":208,"rel":209},"https://github.com/samtap/fang-hacks/wiki/HowTo:-Flash-original-Xiaomi-firmware-from-sdcard-%28factory-reset%29",[28],"fang-hacks wiki",":",[54,213,214,221,231,234,244],{},[57,215,216,217,220],{},"Unpack ",[19,218,219],{},"upd_isa.camera.isc5.bin.extracted"," from firmware 3.0.3.56 onto the FAT partition.",[57,222,223,224,227,228,35],{},"Rename ",[19,225,226],{},"0.elf"," to ",[19,229,230],{},"FIRMWARE_660R.bin",[57,232,233],{},"Insert the card, hold SETUP, and apply power. Keep the USB plug seated. A power cut during the flash can brick the camera. It reboots by itself after a couple of minutes.",[57,235,236,237,239,240,243],{},"Delete ",[19,238,230],{}," once RTSP works, so a later SETUP press cannot flash again. Leave the ",[19,241,242],{},".cramfs"," files where they are.",[57,245,246],{},"Factory state needs pairing again. Hold SETUP and show a fang-hacks Wi-Fi QR.",[15,248,249],{},"Hot-plug the card. Power on with no card, insert it while the camera is already running, wait for the insert beep, then boot again with the card left in. Booting with the card already seated does not copy the Wi-Fi files. The upstream README says to wait until the blue LED stops flashing. On the unit this was written from, the LED kept blinking after the camera already answered ping, so check the network.",[15,251,252,253,256,257,260],{},"Open ",[19,254,255],{},"http://CAMERA_IP/cgi-bin/status"," and apply the hack once. Boa does not answer that CGI until ",[19,258,259],{},"fang_hacks.sh"," exits, and the script drops Wi-Fi while it reboots. A client timeout with an empty body is normal. Wait for the camera to come back. The page should say the script is already installed. Do not start a second Apply.",[15,262,263],{},"RTSP is the hardware encoder:",[141,265,267],{"className":143,"code":266,"language":145,"meta":146,"style":146},"snx_rtsp_server -W 1920 -H 1080 -Q 10 -b 4096 -a\n",[19,268,269],{"__ignoreMap":146},[150,270,271],{"class":152,"line":153},[150,272,266],{},[141,274,279],{"className":275,"code":277,"language":278,"meta":146},[276],"language-text","rtsp://CAMERA_IP/unicast\n","text",[19,280,277],{"__ignoreMap":146},[15,282,283,284,287,288,130,291,294,295,298,299,35],{},"Port 554. Keep ",[19,285,286],{},"-a",". That flag is the microphone towards the client. The SDP has H.264 on ",[19,289,290],{},"track1",[19,292,293],{},"pcm_alaw"," at 8 kHz on ",[19,296,297],{},"track2",". There is no ",[19,300,301],{},"a=sendonly",[141,303,305],{"className":143,"code":304,"language":145,"meta":146,"style":146},"ffprobe -rtsp_transport tcp -i rtsp://CAMERA_IP/unicast\n",[19,306,307],{"__ignoreMap":146},[150,308,309],{"class":152,"line":153},[150,310,304],{},[15,312,313],{},"While 1080p was streaming, this SoC sat around load 0.05 with about 17 MB free of 36 MB. The H.264 encoder is hardware. A software RTSP server does not fit. A return audio path does.",[79,315,317],{"id":316},"step-2-video-in-frigate","Step 2: video in Frigate",[15,319,320],{},"Point go2rtc at the camera. Frigate then reads go2rtc, same as any other camera:",[141,322,326],{"className":323,"code":324,"language":325,"meta":146,"style":146},"language-yaml shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","go2rtc:\n  streams:\n    xiaofang:\n      - rtsp://CAMERA_IP/unicast#tcp\n\ncameras:\n  xiaofang:\n    enabled: true\n    ffmpeg:\n      inputs:\n        - path: rtsp://127.0.0.1:8554/xiaofang\n          roles:\n            - detect\n            - record\n","yaml",[19,327,328,338,345,352,362,369,377,385,397,405,413,427,435,444],{"__ignoreMap":146},[150,329,330,334],{"class":152,"line":153},[150,331,333],{"class":332},"swJcz","go2rtc",[150,335,337],{"class":336},"sMK4o",":\n",[150,339,340,343],{"class":152,"line":159},[150,341,342],{"class":332},"  streams",[150,344,337],{"class":336},[150,346,347,350],{"class":152,"line":185},[150,348,349],{"class":332},"    xiaofang",[150,351,337],{"class":336},[150,353,355,358],{"class":152,"line":354},4,[150,356,357],{"class":336},"      -",[150,359,361],{"class":360},"sfazB"," rtsp://CAMERA_IP/unicast#tcp\n",[150,363,365],{"class":152,"line":364},5,[150,366,368],{"emptyLinePlaceholder":367},true,"\n",[150,370,372,375],{"class":152,"line":371},6,[150,373,374],{"class":332},"cameras",[150,376,337],{"class":336},[150,378,380,383],{"class":152,"line":379},7,[150,381,382],{"class":332},"  xiaofang",[150,384,337],{"class":336},[150,386,388,391,393],{"class":152,"line":387},8,[150,389,390],{"class":332},"    enabled",[150,392,211],{"class":336},[150,394,396],{"class":395},"sfNiH"," true\n",[150,398,400,403],{"class":152,"line":399},9,[150,401,402],{"class":332},"    ffmpeg",[150,404,337],{"class":336},[150,406,408,411],{"class":152,"line":407},10,[150,409,410],{"class":332},"      inputs",[150,412,337],{"class":336},[150,414,416,419,422,424],{"class":152,"line":415},11,[150,417,418],{"class":336},"        -",[150,420,421],{"class":332}," path",[150,423,211],{"class":336},[150,425,426],{"class":360}," rtsp://127.0.0.1:8554/xiaofang\n",[150,428,430,433],{"class":152,"line":429},12,[150,431,432],{"class":332},"          roles",[150,434,337],{"class":336},[150,436,438,441],{"class":152,"line":437},13,[150,439,440],{"class":336},"            -",[150,442,443],{"class":360}," detect\n",[150,445,447,449],{"class":152,"line":446},14,[150,448,440],{"class":336},[150,450,451],{"class":360}," record\n",[15,453,454,455,458,459,462],{},"Leave the camera enabled. An ",[19,456,457],{},"enabled: false"," left over from when the camera was offline shows 0 fps in Frigate while ",[19,460,461],{},"ffprobe"," still gets a picture.",[79,464,466],{"id":465},"step-3-the-speaker-without-a-new-firmware","Step 3: the speaker, without a new firmware",[15,468,469,472,473,478,479,482,483,486],{},[19,470,471],{},"snx_rtsp_server"," does not accept return audio. The player from the ",[24,474,477],{"href":475,"rel":476},"https://github.com/samtap/fang-hacks/files/1673257/snx_audio.zip",[28],"#305 zip"," does. ",[19,480,481],{},"snx_audio_playback"," is a 32-bit ARM ELF. The good file is exactly 30617 bytes and starts with ",[19,484,485],{},"\\x7fELF",". A short or truncated copy will not play.",[15,488,489,490,493],{},"Copy it to the ext2 partition, for example ",[19,491,492],{},"/media/mmcblk0p2/data/usr/bin/snx_audio_playback",". The volume helper ships with fang-hacks.",[15,495,496],{},"Open the speaker, set the volume, then play raw PCM from a file or from stdin:",[141,498,500],{"className":143,"code":499,"language":145,"meta":146,"style":146},"gpio_ms1 -n 7 -m 1 -v 1\n/media/mmcblk0p2/data/test/audio/snx_audio_vol_ctl -d spk -v 15\nsnx_audio_playback -d s16 -s 32k -f /tmp/test.wav\n",[19,501,502,507,512],{"__ignoreMap":146},[150,503,504],{"class":152,"line":153},[150,505,506],{},"gpio_ms1 -n 7 -m 1 -v 1\n",[150,508,509],{"class":152,"line":159},[150,510,511],{},"/media/mmcblk0p2/data/test/audio/snx_audio_vol_ctl -d spk -v 15\n",[150,513,514],{"class":152,"line":185},[150,515,516],{},"snx_audio_playback -d s16 -s 32k -f /tmp/test.wav\n",[15,518,519,522,523,526,527,530],{},[19,520,521],{},"-d s16 -s 32k"," means signed 16-bit little-endian, mono, 32 kHz. That is 64000 bytes per second. Guides that resample this player to 16 kHz are aimed at a different binary. A two-second file returned ",[19,524,525],{},"PLAY_RC:0"," here while ",[19,528,529],{},"snx_rtsp_server -a"," kept sending microphone RTP. The GPIO dance from issue #310 was not required.",[15,532,533,534,537],{},"A listener for later, saved as ",[19,535,536],{},"/media/mmcblk0p2/data/etc/scripts/22-speaker"," (mode 755, LF), next to the other numbered scripts:",[141,539,541],{"className":143,"code":540,"language":145,"meta":146,"style":146},"#!/bin/sh\n# Raw speaker backchannel: s16le 32 kHz mono on TCP 11032.\nif netstat -ltn 2>/dev/null | grep -q ':11032 '; then\n  exit 0\nfi\ngpio_ms1 -n 7 -m 1 -v 1\n/media/mmcblk0p2/data/test/audio/snx_audio_vol_ctl -d spk -v 15 >/dev/null 2>&1\n/media/mmcblk0p2/data/usr/bin/tcpsvd -E 0.0.0.0 11032 \\\n  /media/mmcblk0p2/data/usr/bin/snx_audio_playback -d s16 -s 32k -f - \\\n  >/tmp/spk.log 2>&1 &\n",[19,542,543,548,553,558,563,568,572,577,582,587],{"__ignoreMap":146},[150,544,545],{"class":152,"line":153},[150,546,547],{},"#!/bin/sh\n",[150,549,550],{"class":152,"line":159},[150,551,552],{},"# Raw speaker backchannel: s16le 32 kHz mono on TCP 11032.\n",[150,554,555],{"class":152,"line":185},[150,556,557],{},"if netstat -ltn 2>/dev/null | grep -q ':11032 '; then\n",[150,559,560],{"class":152,"line":354},[150,561,562],{},"  exit 0\n",[150,564,565],{"class":152,"line":364},[150,566,567],{},"fi\n",[150,569,570],{"class":152,"line":371},[150,571,506],{},[150,573,574],{"class":152,"line":379},[150,575,576],{},"/media/mmcblk0p2/data/test/audio/snx_audio_vol_ctl -d spk -v 15 >/dev/null 2>&1\n",[150,578,579],{"class":152,"line":387},[150,580,581],{},"/media/mmcblk0p2/data/usr/bin/tcpsvd -E 0.0.0.0 11032 \\\n",[150,583,584],{"class":152,"line":399},[150,585,586],{},"  /media/mmcblk0p2/data/usr/bin/snx_audio_playback -d s16 -s 32k -f - \\\n",[150,588,589],{"class":152,"line":407},[150,590,591],{},"  >/tmp/spk.log 2>&1 &\n",[15,593,594,595,598],{},"Anything that connects and writes PCM is played. There is no password. Do not forward port 11032. A TCP connect wakes the player, so do not probe the port just to see if it is open. Check with ",[19,596,597],{},"netstat"," on the camera.",[15,600,601,602,605],{},"This boot script was installed and the listener was started by hand. It has not been proven across a camera reboot. After the next power cycle, ",[19,603,604],{},"netstat -ltn | grep 11032"," on the camera is the check. If the port is missing, run the script once.",[79,607,609],{"id":608},"step-4-why-frigate-018-will-not-take-an-exec-backchannel","Step 4: why Frigate 0.18 will not take an exec backchannel",[15,611,612,613,616,617,619,620,619,623,626,627,630,631,634,635,638],{},"go2rtc can attach a return channel with an ",[19,614,615],{},"exec:"," source. Frigate 0.18 deletes ",[19,618,615],{},", ",[19,621,622],{},"echo:",[19,624,625],{},"expr:",", and ",[19,628,629],{},"${VAR}"," sources while it builds ",[19,632,633],{},"/dev/shm/go2rtc.yaml",", unless ",[19,636,637],{},"GO2RTC_ALLOW_ARBITRARY_EXEC"," is set. That switch is remote code execution on the Frigate host. Leave it off, especially if the UI has no auth and port 1984 is published.",[15,640,641,642,644,645,648,649,652],{},"A wrapper that injected the ",[19,643,615],{}," line after ",[19,646,647],{},"create_config.py"," still failed the UI. The producer had no ",[19,650,651],{},"medias",", so Frigate never drew the microphone. The default pipe codec is PCML at 16 kHz, which is also the wrong rate for this player.",[15,654,655,656,658,659,661],{},"The live view looks at the active producer's ",[19,657,651],{}," and shows the button only when that string contains ",[19,660,62],{},". The stream list is cached for about a minute. Reload the page after the producer changes.",[79,663,665],{"id":664},"step-5-a-local-rtsp-proxy-that-adds-the-missing-track","Step 5: a local RTSP proxy that adds the missing track",[15,667,668,669,672],{},"The proxy listens on ",[19,670,671],{},"127.0.0.1:8556"," inside the Frigate container. It forwards the camera and appends an ONVIF-style track:",[141,674,677],{"className":675,"code":676,"language":278,"meta":146},[276],"m=audio 0 RTP/AVP 8\na=rtpmap:8 PCMA/8000\na=sendonly\na=control:track3\n",[19,678,676],{"__ignoreMap":146},[15,680,681,684,685,688,689,692,693,696,697,700,701,704,705,708],{},[19,682,683],{},"SETUP"," for ",[19,686,687],{},"track3"," is answered locally and is not sent to the camera. Talk RTP is G.711 a-law. Each decoded sample is repeated four times (8 kHz to 32 kHz) and written to ",[19,690,691],{},"tcp://CAMERA_IP:11032"," with the container ffmpeg. On the Frigate 0.18 image that binary is ",[19,694,695],{},"/usr/lib/ffmpeg/8.0/bin/ffmpeg",", not on ",[19,698,699],{},"PATH",". Video RTP is copied. RTP is held until the ",[19,702,703],{},"PLAY"," response, otherwise ",[19,706,707],{},"$"," interleaved bytes land in the RTSP status line.",[15,710,711,712,715,716,719,720,723,724,727],{},"Rewrite only the ",[19,713,714],{},"rtsp://CAMERA_IP"," prefix. Replacing the bare IP also corrupts the SDP ",[19,717,718],{},"o="," line. Drop every old ",[19,721,722],{},"Content-Length"," and write one new length after the extra track. The last header does not always have a trailing CRLF, and a regex that requires ",[19,725,726],{},"\\r\\n"," will leave a duplicate length.",[15,729,730,731,734,735,35],{},"Save this as ",[19,732,733],{},"/config/xiaofang_talk_proxy.py"," (mode 755, LF). Change ",[19,736,105],{},[141,738,742],{"className":739,"code":740,"language":741,"meta":146,"style":146},"language-python shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","#!/usr/bin/env python3\n\"\"\"RTSP proxy for a Sonix Xiaofang (fang-hacks).\n\nForwards the camera stream and adds an ONVIF-style sendonly PCMA track.\nTalk audio is resampled to s16le 32 kHz and sent to the speaker on TCP 11032.\nVideo packets are copied through. This process must stay up for that camera.\n\nChange CAMERA_IP before use. Frigate 0.18 strips exec: sources, so this\nproxy is what makes the live-view microphone appear.\n\"\"\"\nimport queue\nimport re\nimport socket\nimport subprocess\nimport threading\n\nCAMERA_IP = \"192.168.1.50\"\nLISTEN = (\"127.0.0.1\", 8556)\nCAMERA = (CAMERA_IP, 554)\nPROXY_HOST = \"127.0.0.1:8556\"\nCAMERA_HOST = CAMERA_IP\nFFMPEG = \"/usr/lib/ffmpeg/8.0/bin/ffmpeg\"\nSPEAKER = \"tcp://%s:11032\" % CAMERA_IP\n\n_alaw = None\n\n\ndef alaw_table():\n    global _alaw\n    if _alaw is not None:\n        return _alaw\n    table = []\n    for i in range(256):\n        a = i ^ 0x55\n        quant = a & 0x0F\n        seg = (a & 0x70) >> 4\n        if seg == 0:\n            sample = (quant \u003C\u003C 4) + 8\n        else:\n            sample = ((quant \u003C\u003C 4) + 0x108) \u003C\u003C (seg - 1)\n        if not (a & 0x80):\n            sample = -sample\n        table.append(sample)\n    _alaw = table\n    return table\n\n\ndef rtp_payload(packet):\n    if len(packet) \u003C 12:\n        return b\"\"\n    cc = packet[0] & 0x0F\n    header = 12 + 4 * cc\n    if packet[0] & 0x10:\n        if len(packet) \u003C header + 4:\n            return b\"\"\n        ext = int.from_bytes(packet[header + 2 : header + 4], \"big\")\n        header += 4 + ext * 4\n    if len(packet) \u003C header:\n        return b\"\"\n    return packet[header:]\n\n\nclass Speaker:\n    def __init__(self):\n        self.proc = None\n        self.lock = threading.Lock()\n        self.table = alaw_table()\n        self.chunks = queue.Queue(maxsize=25)\n        self.alive = True\n        self.thread = threading.Thread(target=self._run, daemon=True)\n        self.thread.start()\n\n    def write_alaw(self, payload):\n        if not payload or not self.alive:\n            return\n        pcm = bytearray()\n        table = self.table\n        for byte in payload:\n            sample = table[byte]\n            pcm += sample.to_bytes(2, \"little\", signed=True) * 4\n        try:\n            self.chunks.put_nowait(bytes(pcm))\n        except queue.Full:\n            return\n\n    def _run(self):\n        while self.alive:\n            try:\n                pcm = self.chunks.get(timeout=0.5)\n            except queue.Empty:\n                continue\n            with self.lock:\n                proc = self._ensure()\n                if proc is None:\n                    continue\n                try:\n                    proc.stdin.write(pcm)\n                    proc.stdin.flush()\n                except (BrokenPipeError, OSError):\n                    self._stop_locked()\n\n    def _ensure(self):\n        if self.proc is not None and self.proc.poll() is None:\n            return self.proc\n        self.proc = subprocess.Popen(\n            [\n                FFMPEG,\n                \"-hide_banner\",\n                \"-loglevel\",\n                \"error\",\n                \"-fflags\",\n                \"nobuffer\",\n                \"-probesize\",\n                \"32\",\n                \"-analyzeduration\",\n                \"0\",\n                \"-f\",\n                \"alaw\",\n                \"-ar\",\n                \"8000\",\n                \"-ac\",\n                \"1\",\n                \"-i\",\n                \"pipe:0\",\n                \"-f\",\n                \"s16le\",\n                \"-ar\",\n                \"32000\",\n                \"-ac\",\n                \"1\",\n                SPEAKER,\n            ],\n            stdin=subprocess.PIPE,\n            stdout=subprocess.DEVNULL,\n            stderr=subprocess.DEVNULL,\n        )\n        return self.proc\n\n    def _stop_locked(self):\n        proc = self.proc\n        self.proc = None\n        if proc is None:\n            return\n        try:\n            if proc.stdin:\n                proc.stdin.close()\n        except OSError:\n            pass\n        try:\n            proc.wait(timeout=1)\n        except subprocess.TimeoutExpired:\n            proc.kill()\n\n    def close(self):\n        self.alive = False\n        with self.lock:\n            self._stop_locked()\n\n\ndef read_some(sock, buf):\n    chunk = sock.recv(8192)\n    if not chunk:\n        raise ConnectionError(\"closed\")\n    return buf + chunk\n\n\ndef pop_item(sock, buf):\n    while True:\n        if not buf:\n            buf = read_some(sock, buf)\n            continue\n        if buf[0:1] == b\"$\":\n            if len(buf) \u003C 4:\n                buf = read_some(sock, buf)\n                continue\n            length = int.from_bytes(buf[2:4], \"big\")\n            need = 4 + length\n            while len(buf) \u003C need:\n                buf = read_some(sock, buf)\n            packet = buf[1:need]\n            return (\"rtp\", packet[0], packet[3:]), buf[need:]\n        header_end = buf.find(b\"\\r\\n\\r\\n\")\n        if header_end \u003C 0:\n            buf = read_some(sock, buf)\n            continue\n        header = buf[: header_end + 4]\n        match = re.search(br\"(?im)^content-length:\\s*(\\d+)\\s*$\", header)\n        body_len = int(match.group(1)) if match else 0\n        total = header_end + 4 + body_len\n        while len(buf) \u003C total:\n            buf = read_some(sock, buf)\n        return (\"rtsp\", buf[:total]), buf[total:]\n\n\ndef content_length(message):\n    match = re.search(br\"(?im)^content-length:\\s*(\\d+)\\s*$\", message)\n    return int(match.group(1)) if match else 0\n\n\ndef rewrite_urls(blob):\n    return blob.replace(\n        b\"rtsp://\" + CAMERA_IP.encode(),\n        b\"rtsp://\" + PROXY_HOST.encode(),\n    )\n\n\ndef rewrite_response(message):\n    head, _, body = message.partition(b\"\\r\\n\\r\\n\")\n    lines = [rewrite_urls(line) for line in head.split(b\"\\r\\n\")]\n    lines = [line for line in lines if not line.lower().startswith(b\"content-length:\")]\n    if b\"m=video\" not in body and b\"application/sdp\" not in head.lower():\n        text = b\"\\r\\n\".join(lines)\n        if body:\n            text += b\"\\r\\n\\r\\n\" + rewrite_urls(body)\n        elif message.endswith(b\"\\r\\n\\r\\n\"):\n            text += b\"\\r\\n\\r\\n\"\n        return text, False\n    body = rewrite_urls(body)\n    if b\"a=control:track3\" not in body:\n        extra = (\n            b\"m=audio 0 RTP/AVP 8\\r\\n\"\n            b\"a=rtpmap:8 PCMA/8000\\r\\n\"\n            b\"a=sendonly\\r\\n\"\n            b\"a=control:track3\\r\\n\"\n        )\n        if body and not body.endswith(b\"\\r\\n\"):\n            body += b\"\\r\\n\"\n        body += extra\n    lines.append(b\"Content-Length: \" + str(len(body)).encode())\n    return b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\" + body, True\n\n\ndef session_of(message):\n    match = re.search(br\"(?im)^session:\\s*([^;\\r\\n]+)\", message)\n    return match.group(1).strip() if match else None\n\n\ndef local_setup(request, session):\n    cseq = re.search(br\"(?im)^cseq:\\s*(\\d+)\", request)\n    inter = re.search(br\"interleaved=(\\d+)-(\\d+)\", request, re.I)\n    cseq_n = cseq.group(1) if cseq else b\"1\"\n    if inter is None:\n        transport = b\"RTP/AVP/TCP;unicast;interleaved=4-5\"\n        channels = (4, 5)\n    else:\n        transport = b\"RTP/AVP/TCP;unicast;interleaved=%s-%s\" % (inter.group(1), inter.group(2))\n        channels = (int(inter.group(1)), int(inter.group(2)))\n    sid = session or b\"xf-talk\"\n    response = (\n        b\"RTSP/1.0 200 OK\\r\\n\"\n        b\"CSeq: \" + cseq_n + b\"\\r\\n\"\n        b\"Session: \" + sid + b\"\\r\\n\"\n        b\"Transport: \" + transport + b\"\\r\\n\"\n        b\"\\r\\n\"\n    )\n    return response, channels\n\n\ndef rewrite_request(message):\n    return message.replace(PROXY_HOST.encode(), CAMERA_HOST.encode())\n\n\ndef is_track3(message):\n    first = message.split(b\"\\r\\n\", 1)[0]\n    return first.startswith(b\"SETUP \") and b\"track3\" in first\n\n\ndef serve(client):\n    upstream = socket.create_connection(CAMERA, timeout=8)\n    client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)\n    upstream.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)\n    client.settimeout(None)\n    upstream.settimeout(None)\n    speaker = Speaker()\n    state = {\n        \"session\": None,\n        \"back\": set(),\n        \"lock\": threading.Lock(),\n        \"hold_rtp\": True,\n        \"held\": [],\n    }\n\n    def send_client(data):\n        with state[\"lock\"]:\n            client.sendall(data)\n\n    def client_loop():\n        buf = b\"\"\n        try:\n            while True:\n                item, buf = pop_item(client, buf)\n                if item[0] == \"rtp\":\n                    channel, payload = item[1], item[2]\n                    if channel in state[\"back\"]:\n                        speaker.write_alaw(rtp_payload(payload))\n                    else:\n                        upstream.sendall(b\"$\" + bytes((channel,)) + len(payload).to_bytes(2, \"big\") + payload)\n                    continue\n                message = item[1]\n                if is_track3(message):\n                    response, channels = local_setup(message, state[\"session\"])\n                    state[\"back\"].update(channels)\n                    send_client(response)\n                    continue\n                if message.startswith(b\"PLAY \"):\n                    state[\"hold_rtp\"] = True\n                upstream.sendall(rewrite_request(message))\n        except (ConnectionError, OSError):\n            pass\n        finally:\n            speaker.close()\n            try:\n                upstream.shutdown(socket.SHUT_RDWR)\n            except OSError:\n                pass\n\n    def camera_loop():\n        buf = b\"\"\n        try:\n            while True:\n                item, buf = pop_item(upstream, buf)\n                if item[0] == \"rtp\":\n                    if state[\"hold_rtp\"]:\n                        if len(state[\"held\"]) \u003C 200:\n                            state[\"held\"].append(item)\n                        continue\n                    channel, payload = item[1], item[2]\n                    send_client(b\"$\" + bytes((channel,)) + len(payload).to_bytes(2, \"big\") + payload)\n                    continue\n                message = item[1]\n                sid = session_of(message)\n                if sid:\n                    state[\"session\"] = sid\n                rewritten, _added = rewrite_response(message)\n                send_client(rewritten)\n                play_ok = message.startswith(b\"RTSP/1.0 200\") and (\n                    b\"RTP-Info:\" in message or b\"\\r\\nRange:\" in message\n                )\n                if state[\"hold_rtp\"] and play_ok:\n                    state[\"hold_rtp\"] = False\n                    held = state[\"held\"]\n                    state[\"held\"] = []\n                    for held_item in held:\n                        channel, payload = held_item[1], held_item[2]\n                        send_client(b\"$\" + bytes((channel,)) + len(payload).to_bytes(2, \"big\") + payload)\n        except (ConnectionError, OSError):\n            pass\n        finally:\n            try:\n                client.shutdown(socket.SHUT_RDWR)\n            except OSError:\n                pass\n\n    threading.Thread(target=camera_loop, daemon=True).start()\n    client_loop()\n    try:\n        client.close()\n    except OSError:\n        pass\n    try:\n        upstream.close()\n    except OSError:\n        pass\n\n\ndef main():\n    server = socket.socket()\n    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n    server.bind(LISTEN)\n    server.listen(2)\n    print(\"xiaofang talk proxy on %s:%s\" % LISTEN, flush=True)\n    while True:\n        client, addr = server.accept()\n        print(\"client\", addr, flush=True)\n        threading.Thread(target=serve, args=(client,), daemon=True).start()\n\n\nif __name__ == \"__main__\":\n    main()\n","python",[19,743,744,749,754,758,763,768,773,777,782,787,792,797,802,807,812,818,823,829,835,841,847,853,859,865,870,876,881,886,892,898,904,910,916,922,928,934,940,946,952,958,964,970,976,982,988,994,999,1004,1010,1016,1022,1028,1034,1040,1046,1052,1058,1064,1070,1075,1081,1086,1091,1097,1103,1109,1115,1121,1127,1133,1139,1145,1150,1156,1162,1168,1174,1180,1186,1192,1198,1204,1210,1216,1221,1226,1232,1238,1244,1250,1256,1262,1268,1274,1280,1286,1292,1298,1304,1310,1316,1321,1327,1333,1339,1345,1351,1357,1363,1369,1375,1381,1387,1393,1399,1405,1411,1417,1423,1429,1435,1441,1447,1453,1459,1464,1470,1475,1481,1486,1491,1497,1503,1509,1515,1521,1527,1533,1538,1544,1550,1555,1561,1566,1571,1577,1583,1589,1595,1600,1606,1612,1618,1623,1629,1635,1641,1647,1652,1657,1663,1669,1675,1681,1687,1692,1697,1703,1709,1715,1721,1727,1733,1739,1745,1750,1756,1762,1768,1773,1779,1785,1791,1797,1802,1807,1813,1819,1825,1831,1837,1842,1848,1853,1858,1864,1870,1876,1881,1886,1892,1898,1904,1910,1916,1921,1926,1932,1938,1944,1950,1956,1962,1968,1974,1980,1986,1992,1998,2004,2010,2016,2022,2028,2034,2039,2045,2051,2057,2063,2069,2074,2079,2085,2091,2097,2102,2107,2113,2119,2125,2131,2137,2143,2149,2155,2161,2167,2173,2179,2185,2191,2197,2203,2209,2214,2220,2225,2230,2236,2242,2247,2252,2258,2264,2270,2275,2280,2286,2292,2298,2304,2310,2316,2322,2328,2334,2340,2346,2352,2358,2364,2369,2375,2381,2387,2392,2398,2404,2409,2415,2421,2427,2433,2439,2445,2451,2457,2462,2468,2474,2480,2486,2492,2497,2503,2509,2515,2521,2526,2532,2538,2543,2549,2555,2561,2566,2572,2577,2582,2587,2593,2598,2604,2610,2616,2622,2627,2633,2638,2643,2649,2655,2661,2667,2673,2679,2685,2691,2697,2703,2709,2715,2721,2727,2733,2738,2743,2748,2753,2759,2764,2769,2774,2780,2786,2792,2798,2804,2810,2815,2821,2826,2831,2836,2841,2847,2853,2859,2865,2871,2877,2882,2888,2894,2900,2905,2910,2916],{"__ignoreMap":146},[150,745,746],{"class":152,"line":153},[150,747,748],{},"#!/usr/bin/env python3\n",[150,750,751],{"class":152,"line":159},[150,752,753],{},"\"\"\"RTSP proxy for a Sonix Xiaofang (fang-hacks).\n",[150,755,756],{"class":152,"line":185},[150,757,368],{"emptyLinePlaceholder":367},[150,759,760],{"class":152,"line":354},[150,761,762],{},"Forwards the camera stream and adds an ONVIF-style sendonly PCMA track.\n",[150,764,765],{"class":152,"line":364},[150,766,767],{},"Talk audio is resampled to s16le 32 kHz and sent to the speaker on TCP 11032.\n",[150,769,770],{"class":152,"line":371},[150,771,772],{},"Video packets are copied through. This process must stay up for that camera.\n",[150,774,775],{"class":152,"line":379},[150,776,368],{"emptyLinePlaceholder":367},[150,778,779],{"class":152,"line":387},[150,780,781],{},"Change CAMERA_IP before use. Frigate 0.18 strips exec: sources, so this\n",[150,783,784],{"class":152,"line":399},[150,785,786],{},"proxy is what makes the live-view microphone appear.\n",[150,788,789],{"class":152,"line":407},[150,790,791],{},"\"\"\"\n",[150,793,794],{"class":152,"line":415},[150,795,796],{},"import queue\n",[150,798,799],{"class":152,"line":429},[150,800,801],{},"import re\n",[150,803,804],{"class":152,"line":437},[150,805,806],{},"import socket\n",[150,808,809],{"class":152,"line":446},[150,810,811],{},"import subprocess\n",[150,813,815],{"class":152,"line":814},15,[150,816,817],{},"import threading\n",[150,819,821],{"class":152,"line":820},16,[150,822,368],{"emptyLinePlaceholder":367},[150,824,826],{"class":152,"line":825},17,[150,827,828],{},"CAMERA_IP = \"192.168.1.50\"\n",[150,830,832],{"class":152,"line":831},18,[150,833,834],{},"LISTEN = (\"127.0.0.1\", 8556)\n",[150,836,838],{"class":152,"line":837},19,[150,839,840],{},"CAMERA = (CAMERA_IP, 554)\n",[150,842,844],{"class":152,"line":843},20,[150,845,846],{},"PROXY_HOST = \"127.0.0.1:8556\"\n",[150,848,850],{"class":152,"line":849},21,[150,851,852],{},"CAMERA_HOST = CAMERA_IP\n",[150,854,856],{"class":152,"line":855},22,[150,857,858],{},"FFMPEG = \"/usr/lib/ffmpeg/8.0/bin/ffmpeg\"\n",[150,860,862],{"class":152,"line":861},23,[150,863,864],{},"SPEAKER = \"tcp://%s:11032\" % CAMERA_IP\n",[150,866,868],{"class":152,"line":867},24,[150,869,368],{"emptyLinePlaceholder":367},[150,871,873],{"class":152,"line":872},25,[150,874,875],{},"_alaw = None\n",[150,877,879],{"class":152,"line":878},26,[150,880,368],{"emptyLinePlaceholder":367},[150,882,884],{"class":152,"line":883},27,[150,885,368],{"emptyLinePlaceholder":367},[150,887,889],{"class":152,"line":888},28,[150,890,891],{},"def alaw_table():\n",[150,893,895],{"class":152,"line":894},29,[150,896,897],{},"    global _alaw\n",[150,899,901],{"class":152,"line":900},30,[150,902,903],{},"    if _alaw is not None:\n",[150,905,907],{"class":152,"line":906},31,[150,908,909],{},"        return _alaw\n",[150,911,913],{"class":152,"line":912},32,[150,914,915],{},"    table = []\n",[150,917,919],{"class":152,"line":918},33,[150,920,921],{},"    for i in range(256):\n",[150,923,925],{"class":152,"line":924},34,[150,926,927],{},"        a = i ^ 0x55\n",[150,929,931],{"class":152,"line":930},35,[150,932,933],{},"        quant = a & 0x0F\n",[150,935,937],{"class":152,"line":936},36,[150,938,939],{},"        seg = (a & 0x70) >> 4\n",[150,941,943],{"class":152,"line":942},37,[150,944,945],{},"        if seg == 0:\n",[150,947,949],{"class":152,"line":948},38,[150,950,951],{},"            sample = (quant \u003C\u003C 4) + 8\n",[150,953,955],{"class":152,"line":954},39,[150,956,957],{},"        else:\n",[150,959,961],{"class":152,"line":960},40,[150,962,963],{},"            sample = ((quant \u003C\u003C 4) + 0x108) \u003C\u003C (seg - 1)\n",[150,965,967],{"class":152,"line":966},41,[150,968,969],{},"        if not (a & 0x80):\n",[150,971,973],{"class":152,"line":972},42,[150,974,975],{},"            sample = -sample\n",[150,977,979],{"class":152,"line":978},43,[150,980,981],{},"        table.append(sample)\n",[150,983,985],{"class":152,"line":984},44,[150,986,987],{},"    _alaw = table\n",[150,989,991],{"class":152,"line":990},45,[150,992,993],{},"    return table\n",[150,995,997],{"class":152,"line":996},46,[150,998,368],{"emptyLinePlaceholder":367},[150,1000,1002],{"class":152,"line":1001},47,[150,1003,368],{"emptyLinePlaceholder":367},[150,1005,1007],{"class":152,"line":1006},48,[150,1008,1009],{},"def rtp_payload(packet):\n",[150,1011,1013],{"class":152,"line":1012},49,[150,1014,1015],{},"    if len(packet) \u003C 12:\n",[150,1017,1019],{"class":152,"line":1018},50,[150,1020,1021],{},"        return b\"\"\n",[150,1023,1025],{"class":152,"line":1024},51,[150,1026,1027],{},"    cc = packet[0] & 0x0F\n",[150,1029,1031],{"class":152,"line":1030},52,[150,1032,1033],{},"    header = 12 + 4 * cc\n",[150,1035,1037],{"class":152,"line":1036},53,[150,1038,1039],{},"    if packet[0] & 0x10:\n",[150,1041,1043],{"class":152,"line":1042},54,[150,1044,1045],{},"        if len(packet) \u003C header + 4:\n",[150,1047,1049],{"class":152,"line":1048},55,[150,1050,1051],{},"            return b\"\"\n",[150,1053,1055],{"class":152,"line":1054},56,[150,1056,1057],{},"        ext = int.from_bytes(packet[header + 2 : header + 4], \"big\")\n",[150,1059,1061],{"class":152,"line":1060},57,[150,1062,1063],{},"        header += 4 + ext * 4\n",[150,1065,1067],{"class":152,"line":1066},58,[150,1068,1069],{},"    if len(packet) \u003C header:\n",[150,1071,1073],{"class":152,"line":1072},59,[150,1074,1021],{},[150,1076,1078],{"class":152,"line":1077},60,[150,1079,1080],{},"    return packet[header:]\n",[150,1082,1084],{"class":152,"line":1083},61,[150,1085,368],{"emptyLinePlaceholder":367},[150,1087,1089],{"class":152,"line":1088},62,[150,1090,368],{"emptyLinePlaceholder":367},[150,1092,1094],{"class":152,"line":1093},63,[150,1095,1096],{},"class Speaker:\n",[150,1098,1100],{"class":152,"line":1099},64,[150,1101,1102],{},"    def __init__(self):\n",[150,1104,1106],{"class":152,"line":1105},65,[150,1107,1108],{},"        self.proc = None\n",[150,1110,1112],{"class":152,"line":1111},66,[150,1113,1114],{},"        self.lock = threading.Lock()\n",[150,1116,1118],{"class":152,"line":1117},67,[150,1119,1120],{},"        self.table = alaw_table()\n",[150,1122,1124],{"class":152,"line":1123},68,[150,1125,1126],{},"        self.chunks = queue.Queue(maxsize=25)\n",[150,1128,1130],{"class":152,"line":1129},69,[150,1131,1132],{},"        self.alive = True\n",[150,1134,1136],{"class":152,"line":1135},70,[150,1137,1138],{},"        self.thread = threading.Thread(target=self._run, daemon=True)\n",[150,1140,1142],{"class":152,"line":1141},71,[150,1143,1144],{},"        self.thread.start()\n",[150,1146,1148],{"class":152,"line":1147},72,[150,1149,368],{"emptyLinePlaceholder":367},[150,1151,1153],{"class":152,"line":1152},73,[150,1154,1155],{},"    def write_alaw(self, payload):\n",[150,1157,1159],{"class":152,"line":1158},74,[150,1160,1161],{},"        if not payload or not self.alive:\n",[150,1163,1165],{"class":152,"line":1164},75,[150,1166,1167],{},"            return\n",[150,1169,1171],{"class":152,"line":1170},76,[150,1172,1173],{},"        pcm = bytearray()\n",[150,1175,1177],{"class":152,"line":1176},77,[150,1178,1179],{},"        table = self.table\n",[150,1181,1183],{"class":152,"line":1182},78,[150,1184,1185],{},"        for byte in payload:\n",[150,1187,1189],{"class":152,"line":1188},79,[150,1190,1191],{},"            sample = table[byte]\n",[150,1193,1195],{"class":152,"line":1194},80,[150,1196,1197],{},"            pcm += sample.to_bytes(2, \"little\", signed=True) * 4\n",[150,1199,1201],{"class":152,"line":1200},81,[150,1202,1203],{},"        try:\n",[150,1205,1207],{"class":152,"line":1206},82,[150,1208,1209],{},"            self.chunks.put_nowait(bytes(pcm))\n",[150,1211,1213],{"class":152,"line":1212},83,[150,1214,1215],{},"        except queue.Full:\n",[150,1217,1219],{"class":152,"line":1218},84,[150,1220,1167],{},[150,1222,1224],{"class":152,"line":1223},85,[150,1225,368],{"emptyLinePlaceholder":367},[150,1227,1229],{"class":152,"line":1228},86,[150,1230,1231],{},"    def _run(self):\n",[150,1233,1235],{"class":152,"line":1234},87,[150,1236,1237],{},"        while self.alive:\n",[150,1239,1241],{"class":152,"line":1240},88,[150,1242,1243],{},"            try:\n",[150,1245,1247],{"class":152,"line":1246},89,[150,1248,1249],{},"                pcm = self.chunks.get(timeout=0.5)\n",[150,1251,1253],{"class":152,"line":1252},90,[150,1254,1255],{},"            except queue.Empty:\n",[150,1257,1259],{"class":152,"line":1258},91,[150,1260,1261],{},"                continue\n",[150,1263,1265],{"class":152,"line":1264},92,[150,1266,1267],{},"            with self.lock:\n",[150,1269,1271],{"class":152,"line":1270},93,[150,1272,1273],{},"                proc = self._ensure()\n",[150,1275,1277],{"class":152,"line":1276},94,[150,1278,1279],{},"                if proc is None:\n",[150,1281,1283],{"class":152,"line":1282},95,[150,1284,1285],{},"                    continue\n",[150,1287,1289],{"class":152,"line":1288},96,[150,1290,1291],{},"                try:\n",[150,1293,1295],{"class":152,"line":1294},97,[150,1296,1297],{},"                    proc.stdin.write(pcm)\n",[150,1299,1301],{"class":152,"line":1300},98,[150,1302,1303],{},"                    proc.stdin.flush()\n",[150,1305,1307],{"class":152,"line":1306},99,[150,1308,1309],{},"                except (BrokenPipeError, OSError):\n",[150,1311,1313],{"class":152,"line":1312},100,[150,1314,1315],{},"                    self._stop_locked()\n",[150,1317,1319],{"class":152,"line":1318},101,[150,1320,368],{"emptyLinePlaceholder":367},[150,1322,1324],{"class":152,"line":1323},102,[150,1325,1326],{},"    def _ensure(self):\n",[150,1328,1330],{"class":152,"line":1329},103,[150,1331,1332],{},"        if self.proc is not None and self.proc.poll() is None:\n",[150,1334,1336],{"class":152,"line":1335},104,[150,1337,1338],{},"            return self.proc\n",[150,1340,1342],{"class":152,"line":1341},105,[150,1343,1344],{},"        self.proc = subprocess.Popen(\n",[150,1346,1348],{"class":152,"line":1347},106,[150,1349,1350],{},"            [\n",[150,1352,1354],{"class":152,"line":1353},107,[150,1355,1356],{},"                FFMPEG,\n",[150,1358,1360],{"class":152,"line":1359},108,[150,1361,1362],{},"                \"-hide_banner\",\n",[150,1364,1366],{"class":152,"line":1365},109,[150,1367,1368],{},"                \"-loglevel\",\n",[150,1370,1372],{"class":152,"line":1371},110,[150,1373,1374],{},"                \"error\",\n",[150,1376,1378],{"class":152,"line":1377},111,[150,1379,1380],{},"                \"-fflags\",\n",[150,1382,1384],{"class":152,"line":1383},112,[150,1385,1386],{},"                \"nobuffer\",\n",[150,1388,1390],{"class":152,"line":1389},113,[150,1391,1392],{},"                \"-probesize\",\n",[150,1394,1396],{"class":152,"line":1395},114,[150,1397,1398],{},"                \"32\",\n",[150,1400,1402],{"class":152,"line":1401},115,[150,1403,1404],{},"                \"-analyzeduration\",\n",[150,1406,1408],{"class":152,"line":1407},116,[150,1409,1410],{},"                \"0\",\n",[150,1412,1414],{"class":152,"line":1413},117,[150,1415,1416],{},"                \"-f\",\n",[150,1418,1420],{"class":152,"line":1419},118,[150,1421,1422],{},"                \"alaw\",\n",[150,1424,1426],{"class":152,"line":1425},119,[150,1427,1428],{},"                \"-ar\",\n",[150,1430,1432],{"class":152,"line":1431},120,[150,1433,1434],{},"                \"8000\",\n",[150,1436,1438],{"class":152,"line":1437},121,[150,1439,1440],{},"                \"-ac\",\n",[150,1442,1444],{"class":152,"line":1443},122,[150,1445,1446],{},"                \"1\",\n",[150,1448,1450],{"class":152,"line":1449},123,[150,1451,1452],{},"                \"-i\",\n",[150,1454,1456],{"class":152,"line":1455},124,[150,1457,1458],{},"                \"pipe:0\",\n",[150,1460,1462],{"class":152,"line":1461},125,[150,1463,1416],{},[150,1465,1467],{"class":152,"line":1466},126,[150,1468,1469],{},"                \"s16le\",\n",[150,1471,1473],{"class":152,"line":1472},127,[150,1474,1428],{},[150,1476,1478],{"class":152,"line":1477},128,[150,1479,1480],{},"                \"32000\",\n",[150,1482,1484],{"class":152,"line":1483},129,[150,1485,1440],{},[150,1487,1489],{"class":152,"line":1488},130,[150,1490,1446],{},[150,1492,1494],{"class":152,"line":1493},131,[150,1495,1496],{},"                SPEAKER,\n",[150,1498,1500],{"class":152,"line":1499},132,[150,1501,1502],{},"            ],\n",[150,1504,1506],{"class":152,"line":1505},133,[150,1507,1508],{},"            stdin=subprocess.PIPE,\n",[150,1510,1512],{"class":152,"line":1511},134,[150,1513,1514],{},"            stdout=subprocess.DEVNULL,\n",[150,1516,1518],{"class":152,"line":1517},135,[150,1519,1520],{},"            stderr=subprocess.DEVNULL,\n",[150,1522,1524],{"class":152,"line":1523},136,[150,1525,1526],{},"        )\n",[150,1528,1530],{"class":152,"line":1529},137,[150,1531,1532],{},"        return self.proc\n",[150,1534,1536],{"class":152,"line":1535},138,[150,1537,368],{"emptyLinePlaceholder":367},[150,1539,1541],{"class":152,"line":1540},139,[150,1542,1543],{},"    def _stop_locked(self):\n",[150,1545,1547],{"class":152,"line":1546},140,[150,1548,1549],{},"        proc = self.proc\n",[150,1551,1553],{"class":152,"line":1552},141,[150,1554,1108],{},[150,1556,1558],{"class":152,"line":1557},142,[150,1559,1560],{},"        if proc is None:\n",[150,1562,1564],{"class":152,"line":1563},143,[150,1565,1167],{},[150,1567,1569],{"class":152,"line":1568},144,[150,1570,1203],{},[150,1572,1574],{"class":152,"line":1573},145,[150,1575,1576],{},"            if proc.stdin:\n",[150,1578,1580],{"class":152,"line":1579},146,[150,1581,1582],{},"                proc.stdin.close()\n",[150,1584,1586],{"class":152,"line":1585},147,[150,1587,1588],{},"        except OSError:\n",[150,1590,1592],{"class":152,"line":1591},148,[150,1593,1594],{},"            pass\n",[150,1596,1598],{"class":152,"line":1597},149,[150,1599,1203],{},[150,1601,1603],{"class":152,"line":1602},150,[150,1604,1605],{},"            proc.wait(timeout=1)\n",[150,1607,1609],{"class":152,"line":1608},151,[150,1610,1611],{},"        except subprocess.TimeoutExpired:\n",[150,1613,1615],{"class":152,"line":1614},152,[150,1616,1617],{},"            proc.kill()\n",[150,1619,1621],{"class":152,"line":1620},153,[150,1622,368],{"emptyLinePlaceholder":367},[150,1624,1626],{"class":152,"line":1625},154,[150,1627,1628],{},"    def close(self):\n",[150,1630,1632],{"class":152,"line":1631},155,[150,1633,1634],{},"        self.alive = False\n",[150,1636,1638],{"class":152,"line":1637},156,[150,1639,1640],{},"        with self.lock:\n",[150,1642,1644],{"class":152,"line":1643},157,[150,1645,1646],{},"            self._stop_locked()\n",[150,1648,1650],{"class":152,"line":1649},158,[150,1651,368],{"emptyLinePlaceholder":367},[150,1653,1655],{"class":152,"line":1654},159,[150,1656,368],{"emptyLinePlaceholder":367},[150,1658,1660],{"class":152,"line":1659},160,[150,1661,1662],{},"def read_some(sock, buf):\n",[150,1664,1666],{"class":152,"line":1665},161,[150,1667,1668],{},"    chunk = sock.recv(8192)\n",[150,1670,1672],{"class":152,"line":1671},162,[150,1673,1674],{},"    if not chunk:\n",[150,1676,1678],{"class":152,"line":1677},163,[150,1679,1680],{},"        raise ConnectionError(\"closed\")\n",[150,1682,1684],{"class":152,"line":1683},164,[150,1685,1686],{},"    return buf + chunk\n",[150,1688,1690],{"class":152,"line":1689},165,[150,1691,368],{"emptyLinePlaceholder":367},[150,1693,1695],{"class":152,"line":1694},166,[150,1696,368],{"emptyLinePlaceholder":367},[150,1698,1700],{"class":152,"line":1699},167,[150,1701,1702],{},"def pop_item(sock, buf):\n",[150,1704,1706],{"class":152,"line":1705},168,[150,1707,1708],{},"    while True:\n",[150,1710,1712],{"class":152,"line":1711},169,[150,1713,1714],{},"        if not buf:\n",[150,1716,1718],{"class":152,"line":1717},170,[150,1719,1720],{},"            buf = read_some(sock, buf)\n",[150,1722,1724],{"class":152,"line":1723},171,[150,1725,1726],{},"            continue\n",[150,1728,1730],{"class":152,"line":1729},172,[150,1731,1732],{},"        if buf[0:1] == b\"$\":\n",[150,1734,1736],{"class":152,"line":1735},173,[150,1737,1738],{},"            if len(buf) \u003C 4:\n",[150,1740,1742],{"class":152,"line":1741},174,[150,1743,1744],{},"                buf = read_some(sock, buf)\n",[150,1746,1748],{"class":152,"line":1747},175,[150,1749,1261],{},[150,1751,1753],{"class":152,"line":1752},176,[150,1754,1755],{},"            length = int.from_bytes(buf[2:4], \"big\")\n",[150,1757,1759],{"class":152,"line":1758},177,[150,1760,1761],{},"            need = 4 + length\n",[150,1763,1765],{"class":152,"line":1764},178,[150,1766,1767],{},"            while len(buf) \u003C need:\n",[150,1769,1771],{"class":152,"line":1770},179,[150,1772,1744],{},[150,1774,1776],{"class":152,"line":1775},180,[150,1777,1778],{},"            packet = buf[1:need]\n",[150,1780,1782],{"class":152,"line":1781},181,[150,1783,1784],{},"            return (\"rtp\", packet[0], packet[3:]), buf[need:]\n",[150,1786,1788],{"class":152,"line":1787},182,[150,1789,1790],{},"        header_end = buf.find(b\"\\r\\n\\r\\n\")\n",[150,1792,1794],{"class":152,"line":1793},183,[150,1795,1796],{},"        if header_end \u003C 0:\n",[150,1798,1800],{"class":152,"line":1799},184,[150,1801,1720],{},[150,1803,1805],{"class":152,"line":1804},185,[150,1806,1726],{},[150,1808,1810],{"class":152,"line":1809},186,[150,1811,1812],{},"        header = buf[: header_end + 4]\n",[150,1814,1816],{"class":152,"line":1815},187,[150,1817,1818],{},"        match = re.search(br\"(?im)^content-length:\\s*(\\d+)\\s*$\", header)\n",[150,1820,1822],{"class":152,"line":1821},188,[150,1823,1824],{},"        body_len = int(match.group(1)) if match else 0\n",[150,1826,1828],{"class":152,"line":1827},189,[150,1829,1830],{},"        total = header_end + 4 + body_len\n",[150,1832,1834],{"class":152,"line":1833},190,[150,1835,1836],{},"        while len(buf) \u003C total:\n",[150,1838,1840],{"class":152,"line":1839},191,[150,1841,1720],{},[150,1843,1845],{"class":152,"line":1844},192,[150,1846,1847],{},"        return (\"rtsp\", buf[:total]), buf[total:]\n",[150,1849,1851],{"class":152,"line":1850},193,[150,1852,368],{"emptyLinePlaceholder":367},[150,1854,1856],{"class":152,"line":1855},194,[150,1857,368],{"emptyLinePlaceholder":367},[150,1859,1861],{"class":152,"line":1860},195,[150,1862,1863],{},"def content_length(message):\n",[150,1865,1867],{"class":152,"line":1866},196,[150,1868,1869],{},"    match = re.search(br\"(?im)^content-length:\\s*(\\d+)\\s*$\", message)\n",[150,1871,1873],{"class":152,"line":1872},197,[150,1874,1875],{},"    return int(match.group(1)) if match else 0\n",[150,1877,1879],{"class":152,"line":1878},198,[150,1880,368],{"emptyLinePlaceholder":367},[150,1882,1884],{"class":152,"line":1883},199,[150,1885,368],{"emptyLinePlaceholder":367},[150,1887,1889],{"class":152,"line":1888},200,[150,1890,1891],{},"def rewrite_urls(blob):\n",[150,1893,1895],{"class":152,"line":1894},201,[150,1896,1897],{},"    return blob.replace(\n",[150,1899,1901],{"class":152,"line":1900},202,[150,1902,1903],{},"        b\"rtsp://\" + CAMERA_IP.encode(),\n",[150,1905,1907],{"class":152,"line":1906},203,[150,1908,1909],{},"        b\"rtsp://\" + PROXY_HOST.encode(),\n",[150,1911,1913],{"class":152,"line":1912},204,[150,1914,1915],{},"    )\n",[150,1917,1919],{"class":152,"line":1918},205,[150,1920,368],{"emptyLinePlaceholder":367},[150,1922,1924],{"class":152,"line":1923},206,[150,1925,368],{"emptyLinePlaceholder":367},[150,1927,1929],{"class":152,"line":1928},207,[150,1930,1931],{},"def rewrite_response(message):\n",[150,1933,1935],{"class":152,"line":1934},208,[150,1936,1937],{},"    head, _, body = message.partition(b\"\\r\\n\\r\\n\")\n",[150,1939,1941],{"class":152,"line":1940},209,[150,1942,1943],{},"    lines = [rewrite_urls(line) for line in head.split(b\"\\r\\n\")]\n",[150,1945,1947],{"class":152,"line":1946},210,[150,1948,1949],{},"    lines = [line for line in lines if not line.lower().startswith(b\"content-length:\")]\n",[150,1951,1953],{"class":152,"line":1952},211,[150,1954,1955],{},"    if b\"m=video\" not in body and b\"application/sdp\" not in head.lower():\n",[150,1957,1959],{"class":152,"line":1958},212,[150,1960,1961],{},"        text = b\"\\r\\n\".join(lines)\n",[150,1963,1965],{"class":152,"line":1964},213,[150,1966,1967],{},"        if body:\n",[150,1969,1971],{"class":152,"line":1970},214,[150,1972,1973],{},"            text += b\"\\r\\n\\r\\n\" + rewrite_urls(body)\n",[150,1975,1977],{"class":152,"line":1976},215,[150,1978,1979],{},"        elif message.endswith(b\"\\r\\n\\r\\n\"):\n",[150,1981,1983],{"class":152,"line":1982},216,[150,1984,1985],{},"            text += b\"\\r\\n\\r\\n\"\n",[150,1987,1989],{"class":152,"line":1988},217,[150,1990,1991],{},"        return text, False\n",[150,1993,1995],{"class":152,"line":1994},218,[150,1996,1997],{},"    body = rewrite_urls(body)\n",[150,1999,2001],{"class":152,"line":2000},219,[150,2002,2003],{},"    if b\"a=control:track3\" not in body:\n",[150,2005,2007],{"class":152,"line":2006},220,[150,2008,2009],{},"        extra = (\n",[150,2011,2013],{"class":152,"line":2012},221,[150,2014,2015],{},"            b\"m=audio 0 RTP/AVP 8\\r\\n\"\n",[150,2017,2019],{"class":152,"line":2018},222,[150,2020,2021],{},"            b\"a=rtpmap:8 PCMA/8000\\r\\n\"\n",[150,2023,2025],{"class":152,"line":2024},223,[150,2026,2027],{},"            b\"a=sendonly\\r\\n\"\n",[150,2029,2031],{"class":152,"line":2030},224,[150,2032,2033],{},"            b\"a=control:track3\\r\\n\"\n",[150,2035,2037],{"class":152,"line":2036},225,[150,2038,1526],{},[150,2040,2042],{"class":152,"line":2041},226,[150,2043,2044],{},"        if body and not body.endswith(b\"\\r\\n\"):\n",[150,2046,2048],{"class":152,"line":2047},227,[150,2049,2050],{},"            body += b\"\\r\\n\"\n",[150,2052,2054],{"class":152,"line":2053},228,[150,2055,2056],{},"        body += extra\n",[150,2058,2060],{"class":152,"line":2059},229,[150,2061,2062],{},"    lines.append(b\"Content-Length: \" + str(len(body)).encode())\n",[150,2064,2066],{"class":152,"line":2065},230,[150,2067,2068],{},"    return b\"\\r\\n\".join(lines) + b\"\\r\\n\\r\\n\" + body, True\n",[150,2070,2072],{"class":152,"line":2071},231,[150,2073,368],{"emptyLinePlaceholder":367},[150,2075,2077],{"class":152,"line":2076},232,[150,2078,368],{"emptyLinePlaceholder":367},[150,2080,2082],{"class":152,"line":2081},233,[150,2083,2084],{},"def session_of(message):\n",[150,2086,2088],{"class":152,"line":2087},234,[150,2089,2090],{},"    match = re.search(br\"(?im)^session:\\s*([^;\\r\\n]+)\", message)\n",[150,2092,2094],{"class":152,"line":2093},235,[150,2095,2096],{},"    return match.group(1).strip() if match else None\n",[150,2098,2100],{"class":152,"line":2099},236,[150,2101,368],{"emptyLinePlaceholder":367},[150,2103,2105],{"class":152,"line":2104},237,[150,2106,368],{"emptyLinePlaceholder":367},[150,2108,2110],{"class":152,"line":2109},238,[150,2111,2112],{},"def local_setup(request, session):\n",[150,2114,2116],{"class":152,"line":2115},239,[150,2117,2118],{},"    cseq = re.search(br\"(?im)^cseq:\\s*(\\d+)\", request)\n",[150,2120,2122],{"class":152,"line":2121},240,[150,2123,2124],{},"    inter = re.search(br\"interleaved=(\\d+)-(\\d+)\", request, re.I)\n",[150,2126,2128],{"class":152,"line":2127},241,[150,2129,2130],{},"    cseq_n = cseq.group(1) if cseq else b\"1\"\n",[150,2132,2134],{"class":152,"line":2133},242,[150,2135,2136],{},"    if inter is None:\n",[150,2138,2140],{"class":152,"line":2139},243,[150,2141,2142],{},"        transport = b\"RTP/AVP/TCP;unicast;interleaved=4-5\"\n",[150,2144,2146],{"class":152,"line":2145},244,[150,2147,2148],{},"        channels = (4, 5)\n",[150,2150,2152],{"class":152,"line":2151},245,[150,2153,2154],{},"    else:\n",[150,2156,2158],{"class":152,"line":2157},246,[150,2159,2160],{},"        transport = b\"RTP/AVP/TCP;unicast;interleaved=%s-%s\" % (inter.group(1), inter.group(2))\n",[150,2162,2164],{"class":152,"line":2163},247,[150,2165,2166],{},"        channels = (int(inter.group(1)), int(inter.group(2)))\n",[150,2168,2170],{"class":152,"line":2169},248,[150,2171,2172],{},"    sid = session or b\"xf-talk\"\n",[150,2174,2176],{"class":152,"line":2175},249,[150,2177,2178],{},"    response = (\n",[150,2180,2182],{"class":152,"line":2181},250,[150,2183,2184],{},"        b\"RTSP/1.0 200 OK\\r\\n\"\n",[150,2186,2188],{"class":152,"line":2187},251,[150,2189,2190],{},"        b\"CSeq: \" + cseq_n + b\"\\r\\n\"\n",[150,2192,2194],{"class":152,"line":2193},252,[150,2195,2196],{},"        b\"Session: \" + sid + b\"\\r\\n\"\n",[150,2198,2200],{"class":152,"line":2199},253,[150,2201,2202],{},"        b\"Transport: \" + transport + b\"\\r\\n\"\n",[150,2204,2206],{"class":152,"line":2205},254,[150,2207,2208],{},"        b\"\\r\\n\"\n",[150,2210,2212],{"class":152,"line":2211},255,[150,2213,1915],{},[150,2215,2217],{"class":152,"line":2216},256,[150,2218,2219],{},"    return response, channels\n",[150,2221,2223],{"class":152,"line":2222},257,[150,2224,368],{"emptyLinePlaceholder":367},[150,2226,2228],{"class":152,"line":2227},258,[150,2229,368],{"emptyLinePlaceholder":367},[150,2231,2233],{"class":152,"line":2232},259,[150,2234,2235],{},"def rewrite_request(message):\n",[150,2237,2239],{"class":152,"line":2238},260,[150,2240,2241],{},"    return message.replace(PROXY_HOST.encode(), CAMERA_HOST.encode())\n",[150,2243,2245],{"class":152,"line":2244},261,[150,2246,368],{"emptyLinePlaceholder":367},[150,2248,2250],{"class":152,"line":2249},262,[150,2251,368],{"emptyLinePlaceholder":367},[150,2253,2255],{"class":152,"line":2254},263,[150,2256,2257],{},"def is_track3(message):\n",[150,2259,2261],{"class":152,"line":2260},264,[150,2262,2263],{},"    first = message.split(b\"\\r\\n\", 1)[0]\n",[150,2265,2267],{"class":152,"line":2266},265,[150,2268,2269],{},"    return first.startswith(b\"SETUP \") and b\"track3\" in first\n",[150,2271,2273],{"class":152,"line":2272},266,[150,2274,368],{"emptyLinePlaceholder":367},[150,2276,2278],{"class":152,"line":2277},267,[150,2279,368],{"emptyLinePlaceholder":367},[150,2281,2283],{"class":152,"line":2282},268,[150,2284,2285],{},"def serve(client):\n",[150,2287,2289],{"class":152,"line":2288},269,[150,2290,2291],{},"    upstream = socket.create_connection(CAMERA, timeout=8)\n",[150,2293,2295],{"class":152,"line":2294},270,[150,2296,2297],{},"    client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)\n",[150,2299,2301],{"class":152,"line":2300},271,[150,2302,2303],{},"    upstream.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)\n",[150,2305,2307],{"class":152,"line":2306},272,[150,2308,2309],{},"    client.settimeout(None)\n",[150,2311,2313],{"class":152,"line":2312},273,[150,2314,2315],{},"    upstream.settimeout(None)\n",[150,2317,2319],{"class":152,"line":2318},274,[150,2320,2321],{},"    speaker = Speaker()\n",[150,2323,2325],{"class":152,"line":2324},275,[150,2326,2327],{},"    state = {\n",[150,2329,2331],{"class":152,"line":2330},276,[150,2332,2333],{},"        \"session\": None,\n",[150,2335,2337],{"class":152,"line":2336},277,[150,2338,2339],{},"        \"back\": set(),\n",[150,2341,2343],{"class":152,"line":2342},278,[150,2344,2345],{},"        \"lock\": threading.Lock(),\n",[150,2347,2349],{"class":152,"line":2348},279,[150,2350,2351],{},"        \"hold_rtp\": True,\n",[150,2353,2355],{"class":152,"line":2354},280,[150,2356,2357],{},"        \"held\": [],\n",[150,2359,2361],{"class":152,"line":2360},281,[150,2362,2363],{},"    }\n",[150,2365,2367],{"class":152,"line":2366},282,[150,2368,368],{"emptyLinePlaceholder":367},[150,2370,2372],{"class":152,"line":2371},283,[150,2373,2374],{},"    def send_client(data):\n",[150,2376,2378],{"class":152,"line":2377},284,[150,2379,2380],{},"        with state[\"lock\"]:\n",[150,2382,2384],{"class":152,"line":2383},285,[150,2385,2386],{},"            client.sendall(data)\n",[150,2388,2390],{"class":152,"line":2389},286,[150,2391,368],{"emptyLinePlaceholder":367},[150,2393,2395],{"class":152,"line":2394},287,[150,2396,2397],{},"    def client_loop():\n",[150,2399,2401],{"class":152,"line":2400},288,[150,2402,2403],{},"        buf = b\"\"\n",[150,2405,2407],{"class":152,"line":2406},289,[150,2408,1203],{},[150,2410,2412],{"class":152,"line":2411},290,[150,2413,2414],{},"            while True:\n",[150,2416,2418],{"class":152,"line":2417},291,[150,2419,2420],{},"                item, buf = pop_item(client, buf)\n",[150,2422,2424],{"class":152,"line":2423},292,[150,2425,2426],{},"                if item[0] == \"rtp\":\n",[150,2428,2430],{"class":152,"line":2429},293,[150,2431,2432],{},"                    channel, payload = item[1], item[2]\n",[150,2434,2436],{"class":152,"line":2435},294,[150,2437,2438],{},"                    if channel in state[\"back\"]:\n",[150,2440,2442],{"class":152,"line":2441},295,[150,2443,2444],{},"                        speaker.write_alaw(rtp_payload(payload))\n",[150,2446,2448],{"class":152,"line":2447},296,[150,2449,2450],{},"                    else:\n",[150,2452,2454],{"class":152,"line":2453},297,[150,2455,2456],{},"                        upstream.sendall(b\"$\" + bytes((channel,)) + len(payload).to_bytes(2, \"big\") + payload)\n",[150,2458,2460],{"class":152,"line":2459},298,[150,2461,1285],{},[150,2463,2465],{"class":152,"line":2464},299,[150,2466,2467],{},"                message = item[1]\n",[150,2469,2471],{"class":152,"line":2470},300,[150,2472,2473],{},"                if is_track3(message):\n",[150,2475,2477],{"class":152,"line":2476},301,[150,2478,2479],{},"                    response, channels = local_setup(message, state[\"session\"])\n",[150,2481,2483],{"class":152,"line":2482},302,[150,2484,2485],{},"                    state[\"back\"].update(channels)\n",[150,2487,2489],{"class":152,"line":2488},303,[150,2490,2491],{},"                    send_client(response)\n",[150,2493,2495],{"class":152,"line":2494},304,[150,2496,1285],{},[150,2498,2500],{"class":152,"line":2499},305,[150,2501,2502],{},"                if message.startswith(b\"PLAY \"):\n",[150,2504,2506],{"class":152,"line":2505},306,[150,2507,2508],{},"                    state[\"hold_rtp\"] = True\n",[150,2510,2512],{"class":152,"line":2511},307,[150,2513,2514],{},"                upstream.sendall(rewrite_request(message))\n",[150,2516,2518],{"class":152,"line":2517},308,[150,2519,2520],{},"        except (ConnectionError, OSError):\n",[150,2522,2524],{"class":152,"line":2523},309,[150,2525,1594],{},[150,2527,2529],{"class":152,"line":2528},310,[150,2530,2531],{},"        finally:\n",[150,2533,2535],{"class":152,"line":2534},311,[150,2536,2537],{},"            speaker.close()\n",[150,2539,2541],{"class":152,"line":2540},312,[150,2542,1243],{},[150,2544,2546],{"class":152,"line":2545},313,[150,2547,2548],{},"                upstream.shutdown(socket.SHUT_RDWR)\n",[150,2550,2552],{"class":152,"line":2551},314,[150,2553,2554],{},"            except OSError:\n",[150,2556,2558],{"class":152,"line":2557},315,[150,2559,2560],{},"                pass\n",[150,2562,2564],{"class":152,"line":2563},316,[150,2565,368],{"emptyLinePlaceholder":367},[150,2567,2569],{"class":152,"line":2568},317,[150,2570,2571],{},"    def camera_loop():\n",[150,2573,2575],{"class":152,"line":2574},318,[150,2576,2403],{},[150,2578,2580],{"class":152,"line":2579},319,[150,2581,1203],{},[150,2583,2585],{"class":152,"line":2584},320,[150,2586,2414],{},[150,2588,2590],{"class":152,"line":2589},321,[150,2591,2592],{},"                item, buf = pop_item(upstream, buf)\n",[150,2594,2596],{"class":152,"line":2595},322,[150,2597,2426],{},[150,2599,2601],{"class":152,"line":2600},323,[150,2602,2603],{},"                    if state[\"hold_rtp\"]:\n",[150,2605,2607],{"class":152,"line":2606},324,[150,2608,2609],{},"                        if len(state[\"held\"]) \u003C 200:\n",[150,2611,2613],{"class":152,"line":2612},325,[150,2614,2615],{},"                            state[\"held\"].append(item)\n",[150,2617,2619],{"class":152,"line":2618},326,[150,2620,2621],{},"                        continue\n",[150,2623,2625],{"class":152,"line":2624},327,[150,2626,2432],{},[150,2628,2630],{"class":152,"line":2629},328,[150,2631,2632],{},"                    send_client(b\"$\" + bytes((channel,)) + len(payload).to_bytes(2, \"big\") + payload)\n",[150,2634,2636],{"class":152,"line":2635},329,[150,2637,1285],{},[150,2639,2641],{"class":152,"line":2640},330,[150,2642,2467],{},[150,2644,2646],{"class":152,"line":2645},331,[150,2647,2648],{},"                sid = session_of(message)\n",[150,2650,2652],{"class":152,"line":2651},332,[150,2653,2654],{},"                if sid:\n",[150,2656,2658],{"class":152,"line":2657},333,[150,2659,2660],{},"                    state[\"session\"] = sid\n",[150,2662,2664],{"class":152,"line":2663},334,[150,2665,2666],{},"                rewritten, _added = rewrite_response(message)\n",[150,2668,2670],{"class":152,"line":2669},335,[150,2671,2672],{},"                send_client(rewritten)\n",[150,2674,2676],{"class":152,"line":2675},336,[150,2677,2678],{},"                play_ok = message.startswith(b\"RTSP/1.0 200\") and (\n",[150,2680,2682],{"class":152,"line":2681},337,[150,2683,2684],{},"                    b\"RTP-Info:\" in message or b\"\\r\\nRange:\" in message\n",[150,2686,2688],{"class":152,"line":2687},338,[150,2689,2690],{},"                )\n",[150,2692,2694],{"class":152,"line":2693},339,[150,2695,2696],{},"                if state[\"hold_rtp\"] and play_ok:\n",[150,2698,2700],{"class":152,"line":2699},340,[150,2701,2702],{},"                    state[\"hold_rtp\"] = False\n",[150,2704,2706],{"class":152,"line":2705},341,[150,2707,2708],{},"                    held = state[\"held\"]\n",[150,2710,2712],{"class":152,"line":2711},342,[150,2713,2714],{},"                    state[\"held\"] = []\n",[150,2716,2718],{"class":152,"line":2717},343,[150,2719,2720],{},"                    for held_item in held:\n",[150,2722,2724],{"class":152,"line":2723},344,[150,2725,2726],{},"                        channel, payload = held_item[1], held_item[2]\n",[150,2728,2730],{"class":152,"line":2729},345,[150,2731,2732],{},"                        send_client(b\"$\" + bytes((channel,)) + len(payload).to_bytes(2, \"big\") + payload)\n",[150,2734,2736],{"class":152,"line":2735},346,[150,2737,2520],{},[150,2739,2741],{"class":152,"line":2740},347,[150,2742,1594],{},[150,2744,2746],{"class":152,"line":2745},348,[150,2747,2531],{},[150,2749,2751],{"class":152,"line":2750},349,[150,2752,1243],{},[150,2754,2756],{"class":152,"line":2755},350,[150,2757,2758],{},"                client.shutdown(socket.SHUT_RDWR)\n",[150,2760,2762],{"class":152,"line":2761},351,[150,2763,2554],{},[150,2765,2767],{"class":152,"line":2766},352,[150,2768,2560],{},[150,2770,2772],{"class":152,"line":2771},353,[150,2773,368],{"emptyLinePlaceholder":367},[150,2775,2777],{"class":152,"line":2776},354,[150,2778,2779],{},"    threading.Thread(target=camera_loop, daemon=True).start()\n",[150,2781,2783],{"class":152,"line":2782},355,[150,2784,2785],{},"    client_loop()\n",[150,2787,2789],{"class":152,"line":2788},356,[150,2790,2791],{},"    try:\n",[150,2793,2795],{"class":152,"line":2794},357,[150,2796,2797],{},"        client.close()\n",[150,2799,2801],{"class":152,"line":2800},358,[150,2802,2803],{},"    except OSError:\n",[150,2805,2807],{"class":152,"line":2806},359,[150,2808,2809],{},"        pass\n",[150,2811,2813],{"class":152,"line":2812},360,[150,2814,2791],{},[150,2816,2818],{"class":152,"line":2817},361,[150,2819,2820],{},"        upstream.close()\n",[150,2822,2824],{"class":152,"line":2823},362,[150,2825,2803],{},[150,2827,2829],{"class":152,"line":2828},363,[150,2830,2809],{},[150,2832,2834],{"class":152,"line":2833},364,[150,2835,368],{"emptyLinePlaceholder":367},[150,2837,2839],{"class":152,"line":2838},365,[150,2840,368],{"emptyLinePlaceholder":367},[150,2842,2844],{"class":152,"line":2843},366,[150,2845,2846],{},"def main():\n",[150,2848,2850],{"class":152,"line":2849},367,[150,2851,2852],{},"    server = socket.socket()\n",[150,2854,2856],{"class":152,"line":2855},368,[150,2857,2858],{},"    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n",[150,2860,2862],{"class":152,"line":2861},369,[150,2863,2864],{},"    server.bind(LISTEN)\n",[150,2866,2868],{"class":152,"line":2867},370,[150,2869,2870],{},"    server.listen(2)\n",[150,2872,2874],{"class":152,"line":2873},371,[150,2875,2876],{},"    print(\"xiaofang talk proxy on %s:%s\" % LISTEN, flush=True)\n",[150,2878,2880],{"class":152,"line":2879},372,[150,2881,1708],{},[150,2883,2885],{"class":152,"line":2884},373,[150,2886,2887],{},"        client, addr = server.accept()\n",[150,2889,2891],{"class":152,"line":2890},374,[150,2892,2893],{},"        print(\"client\", addr, flush=True)\n",[150,2895,2897],{"class":152,"line":2896},375,[150,2898,2899],{},"        threading.Thread(target=serve, args=(client,), daemon=True).start()\n",[150,2901,2903],{"class":152,"line":2902},376,[150,2904,368],{"emptyLinePlaceholder":367},[150,2906,2908],{"class":152,"line":2907},377,[150,2909,368],{"emptyLinePlaceholder":367},[150,2911,2913],{"class":152,"line":2912},378,[150,2914,2915],{},"if __name__ == \"__main__\":\n",[150,2917,2919],{"class":152,"line":2918},379,[150,2920,2921],{},"    main()\n",[79,2923,2925],{"id":2924},"step-6-point-go2rtc-at-the-proxy-without-editing-the-camera-url-every-boot","Step 6: point go2rtc at the proxy without editing the camera URL every boot",[15,2927,2928,2929,2931,2932,2935],{},"The go2rtc s6 service deletes ",[19,2930,633],{},", regenerates it from your config, then execs ",[19,2933,2934],{},"/config/go2rtc"," when that path is executable. A Python wrapper there can start the proxy and rewrite the generated URL, then replace itself with the real go2rtc binary.",[15,2937,2938,2939,2942,2943,2946,2947,2949],{},"Your ",[19,2940,2941],{},"config.yaml"," keeps the direct camera URL from Step 2. The wrapper swaps that one string for ",[19,2944,2945],{},"rtsp://127.0.0.1:8556/unicast#tcp"," in the generated file. Delete ",[19,2948,2934],{}," and restart the service to go back to the direct stream. Talk disappears with it. If the proxy dies while go2rtc stays up, that camera's picture goes black until the service starts the wrapper again.",[15,2951,730,2952,2954,2955,2957,2958,35],{},[19,2953,2934],{}," (mode 755, LF shebang). The same ",[19,2956,105],{}," must match the URL in ",[19,2959,2941],{},[141,2961,2963],{"className":739,"code":2962,"language":741,"meta":146,"style":146},"#!/usr/bin/env python3\n\"\"\"Start the Xiaofang talk proxy, point go2rtc at it, then run real go2rtc.\n\nFrigate 0.18 removes exec: sources unless arbitrary exec is enabled. Leave\nthat switch off. The proxy adds the sendonly audio track the live view needs\nfor the microphone button.\n\nInstall this file as /config/go2rtc inside the Frigate container and chmod 755.\nThe s6 run script uses it instead of /usr/local/go2rtc/bin/go2rtc when it is\nexecutable. CAMERA_IP must match the URL in config.yaml.\n\"\"\"\nimport os\nimport socket\nimport subprocess\nimport sys\nimport time\n\nCAMERA_IP = \"192.168.1.50\"\nREAL = \"/usr/local/go2rtc/bin/go2rtc\"\nCFG = \"/dev/shm/go2rtc.yaml\"\nPROXY = \"/config/xiaofang_talk_proxy.py\"\nDIRECT = \"rtsp://%s/unicast#tcp\" % CAMERA_IP\nVIA_PROXY = \"rtsp://127.0.0.1:8556/unicast#tcp\"\n\n\ndef proxy_up():\n    try:\n        sock = socket.create_connection((\"127.0.0.1\", 8556), timeout=0.4)\n    except OSError:\n        return False\n    sock.close()\n    return True\n\n\ndef start_proxy():\n    if proxy_up():\n        print(\"[xiaofang-talk] proxy already up\", flush=True)\n        return True\n    log = open(\"/tmp/xiaofang-talk.log\", \"ab\", buffering=0)\n    subprocess.Popen(\n        [\"python3\", PROXY],\n        stdout=log,\n        stderr=log,\n        start_new_session=True,\n    )\n    for _ in range(30):\n        if proxy_up():\n            print(\"[xiaofang-talk] proxy started\", flush=True)\n            return True\n        time.sleep(0.1)\n    print(\"[xiaofang-talk] proxy failed, keeping direct RTSP\", flush=True)\n    return False\n\n\ndef patch(use_proxy):\n    if not os.path.isfile(CFG):\n        print(\"[xiaofang-talk] no go2rtc yaml\", flush=True)\n        return\n    text = open(CFG, encoding=\"utf-8\").read()\n    if use_proxy and DIRECT in text:\n        text = text.replace(DIRECT, VIA_PROXY)\n        open(CFG, \"w\", encoding=\"utf-8\").write(text)\n        print(\"[xiaofang-talk] stream pointed at proxy\", flush=True)\n        return\n    if use_proxy and VIA_PROXY in text:\n        print(\"[xiaofang-talk] stream already uses proxy\", flush=True)\n        return\n    print(\"[xiaofang-talk] direct stream left unchanged\", flush=True)\n\n\npatch(start_proxy())\nos.execv(REAL, [REAL, *sys.argv[1:]])\n",[19,2964,2965,2969,2974,2978,2983,2988,2993,2997,3002,3007,3012,3016,3021,3025,3029,3034,3039,3043,3047,3052,3057,3062,3067,3072,3076,3080,3085,3089,3094,3098,3103,3108,3113,3117,3121,3126,3131,3136,3141,3146,3151,3156,3161,3166,3171,3175,3180,3185,3190,3195,3200,3205,3210,3214,3218,3223,3228,3233,3238,3243,3248,3253,3258,3263,3267,3272,3277,3281,3286,3290,3294,3299],{"__ignoreMap":146},[150,2966,2967],{"class":152,"line":153},[150,2968,748],{},[150,2970,2971],{"class":152,"line":159},[150,2972,2973],{},"\"\"\"Start the Xiaofang talk proxy, point go2rtc at it, then run real go2rtc.\n",[150,2975,2976],{"class":152,"line":185},[150,2977,368],{"emptyLinePlaceholder":367},[150,2979,2980],{"class":152,"line":354},[150,2981,2982],{},"Frigate 0.18 removes exec: sources unless arbitrary exec is enabled. Leave\n",[150,2984,2985],{"class":152,"line":364},[150,2986,2987],{},"that switch off. The proxy adds the sendonly audio track the live view needs\n",[150,2989,2990],{"class":152,"line":371},[150,2991,2992],{},"for the microphone button.\n",[150,2994,2995],{"class":152,"line":379},[150,2996,368],{"emptyLinePlaceholder":367},[150,2998,2999],{"class":152,"line":387},[150,3000,3001],{},"Install this file as /config/go2rtc inside the Frigate container and chmod 755.\n",[150,3003,3004],{"class":152,"line":399},[150,3005,3006],{},"The s6 run script uses it instead of /usr/local/go2rtc/bin/go2rtc when it is\n",[150,3008,3009],{"class":152,"line":407},[150,3010,3011],{},"executable. CAMERA_IP must match the URL in config.yaml.\n",[150,3013,3014],{"class":152,"line":415},[150,3015,791],{},[150,3017,3018],{"class":152,"line":429},[150,3019,3020],{},"import os\n",[150,3022,3023],{"class":152,"line":437},[150,3024,806],{},[150,3026,3027],{"class":152,"line":446},[150,3028,811],{},[150,3030,3031],{"class":152,"line":814},[150,3032,3033],{},"import sys\n",[150,3035,3036],{"class":152,"line":820},[150,3037,3038],{},"import time\n",[150,3040,3041],{"class":152,"line":825},[150,3042,368],{"emptyLinePlaceholder":367},[150,3044,3045],{"class":152,"line":831},[150,3046,828],{},[150,3048,3049],{"class":152,"line":837},[150,3050,3051],{},"REAL = \"/usr/local/go2rtc/bin/go2rtc\"\n",[150,3053,3054],{"class":152,"line":843},[150,3055,3056],{},"CFG = \"/dev/shm/go2rtc.yaml\"\n",[150,3058,3059],{"class":152,"line":849},[150,3060,3061],{},"PROXY = \"/config/xiaofang_talk_proxy.py\"\n",[150,3063,3064],{"class":152,"line":855},[150,3065,3066],{},"DIRECT = \"rtsp://%s/unicast#tcp\" % CAMERA_IP\n",[150,3068,3069],{"class":152,"line":861},[150,3070,3071],{},"VIA_PROXY = \"rtsp://127.0.0.1:8556/unicast#tcp\"\n",[150,3073,3074],{"class":152,"line":867},[150,3075,368],{"emptyLinePlaceholder":367},[150,3077,3078],{"class":152,"line":872},[150,3079,368],{"emptyLinePlaceholder":367},[150,3081,3082],{"class":152,"line":878},[150,3083,3084],{},"def proxy_up():\n",[150,3086,3087],{"class":152,"line":883},[150,3088,2791],{},[150,3090,3091],{"class":152,"line":888},[150,3092,3093],{},"        sock = socket.create_connection((\"127.0.0.1\", 8556), timeout=0.4)\n",[150,3095,3096],{"class":152,"line":894},[150,3097,2803],{},[150,3099,3100],{"class":152,"line":900},[150,3101,3102],{},"        return False\n",[150,3104,3105],{"class":152,"line":906},[150,3106,3107],{},"    sock.close()\n",[150,3109,3110],{"class":152,"line":912},[150,3111,3112],{},"    return True\n",[150,3114,3115],{"class":152,"line":918},[150,3116,368],{"emptyLinePlaceholder":367},[150,3118,3119],{"class":152,"line":924},[150,3120,368],{"emptyLinePlaceholder":367},[150,3122,3123],{"class":152,"line":930},[150,3124,3125],{},"def start_proxy():\n",[150,3127,3128],{"class":152,"line":936},[150,3129,3130],{},"    if proxy_up():\n",[150,3132,3133],{"class":152,"line":942},[150,3134,3135],{},"        print(\"[xiaofang-talk] proxy already up\", flush=True)\n",[150,3137,3138],{"class":152,"line":948},[150,3139,3140],{},"        return True\n",[150,3142,3143],{"class":152,"line":954},[150,3144,3145],{},"    log = open(\"/tmp/xiaofang-talk.log\", \"ab\", buffering=0)\n",[150,3147,3148],{"class":152,"line":960},[150,3149,3150],{},"    subprocess.Popen(\n",[150,3152,3153],{"class":152,"line":966},[150,3154,3155],{},"        [\"python3\", PROXY],\n",[150,3157,3158],{"class":152,"line":972},[150,3159,3160],{},"        stdout=log,\n",[150,3162,3163],{"class":152,"line":978},[150,3164,3165],{},"        stderr=log,\n",[150,3167,3168],{"class":152,"line":984},[150,3169,3170],{},"        start_new_session=True,\n",[150,3172,3173],{"class":152,"line":990},[150,3174,1915],{},[150,3176,3177],{"class":152,"line":996},[150,3178,3179],{},"    for _ in range(30):\n",[150,3181,3182],{"class":152,"line":1001},[150,3183,3184],{},"        if proxy_up():\n",[150,3186,3187],{"class":152,"line":1006},[150,3188,3189],{},"            print(\"[xiaofang-talk] proxy started\", flush=True)\n",[150,3191,3192],{"class":152,"line":1012},[150,3193,3194],{},"            return True\n",[150,3196,3197],{"class":152,"line":1018},[150,3198,3199],{},"        time.sleep(0.1)\n",[150,3201,3202],{"class":152,"line":1024},[150,3203,3204],{},"    print(\"[xiaofang-talk] proxy failed, keeping direct RTSP\", flush=True)\n",[150,3206,3207],{"class":152,"line":1030},[150,3208,3209],{},"    return False\n",[150,3211,3212],{"class":152,"line":1036},[150,3213,368],{"emptyLinePlaceholder":367},[150,3215,3216],{"class":152,"line":1042},[150,3217,368],{"emptyLinePlaceholder":367},[150,3219,3220],{"class":152,"line":1048},[150,3221,3222],{},"def patch(use_proxy):\n",[150,3224,3225],{"class":152,"line":1054},[150,3226,3227],{},"    if not os.path.isfile(CFG):\n",[150,3229,3230],{"class":152,"line":1060},[150,3231,3232],{},"        print(\"[xiaofang-talk] no go2rtc yaml\", flush=True)\n",[150,3234,3235],{"class":152,"line":1066},[150,3236,3237],{},"        return\n",[150,3239,3240],{"class":152,"line":1072},[150,3241,3242],{},"    text = open(CFG, encoding=\"utf-8\").read()\n",[150,3244,3245],{"class":152,"line":1077},[150,3246,3247],{},"    if use_proxy and DIRECT in text:\n",[150,3249,3250],{"class":152,"line":1083},[150,3251,3252],{},"        text = text.replace(DIRECT, VIA_PROXY)\n",[150,3254,3255],{"class":152,"line":1088},[150,3256,3257],{},"        open(CFG, \"w\", encoding=\"utf-8\").write(text)\n",[150,3259,3260],{"class":152,"line":1093},[150,3261,3262],{},"        print(\"[xiaofang-talk] stream pointed at proxy\", flush=True)\n",[150,3264,3265],{"class":152,"line":1099},[150,3266,3237],{},[150,3268,3269],{"class":152,"line":1105},[150,3270,3271],{},"    if use_proxy and VIA_PROXY in text:\n",[150,3273,3274],{"class":152,"line":1111},[150,3275,3276],{},"        print(\"[xiaofang-talk] stream already uses proxy\", flush=True)\n",[150,3278,3279],{"class":152,"line":1117},[150,3280,3237],{},[150,3282,3283],{"class":152,"line":1123},[150,3284,3285],{},"    print(\"[xiaofang-talk] direct stream left unchanged\", flush=True)\n",[150,3287,3288],{"class":152,"line":1129},[150,3289,368],{"emptyLinePlaceholder":367},[150,3291,3292],{"class":152,"line":1135},[150,3293,368],{"emptyLinePlaceholder":367},[150,3295,3296],{"class":152,"line":1141},[150,3297,3298],{},"patch(start_proxy())\n",[150,3300,3301],{"class":152,"line":1147},[150,3302,3303],{},"os.execv(REAL, [REAL, *sys.argv[1:]])\n",[15,3305,3306,3307,3310,3311,3313],{},"Restart only go2rtc. ",[19,3308,3309],{},"s6-svc"," is not on ",[19,3312,699],{}," in this image. The restart blips every live view for a few seconds.",[141,3315,3317],{"className":143,"code":3316,"language":145,"meta":146,"style":146},"docker exec frigate /command/s6-svc -r /run/s6-rc/servicedirs/go2rtc\n",[19,3318,3319],{"__ignoreMap":146},[150,3320,3321],{"class":152,"line":153},[150,3322,3316],{},[15,3324,3325,3326,3329],{},"The container name on a stock Compose file is often ",[19,3327,3328],{},"frigate",". Use the name you actually have. The log should contain:",[141,3331,3334],{"className":3332,"code":3333,"language":278,"meta":146},[276],"[WARN] Using go2rtc binary from '/config/go2rtc'\n[xiaofang-talk] proxy started\n[xiaofang-talk] stream pointed at proxy\n",[19,3335,3333],{"__ignoreMap":146},[79,3337,3339],{"id":3338},"step-7-check-it-without-holding-the-speaker-open","Step 7: check it without holding the speaker open",[15,3341,3342],{},"DESCRIBE the proxy from inside the container:",[141,3344,3346],{"className":143,"code":3345,"language":145,"meta":146,"style":146},"docker exec frigate python3 -c \"import socket; s=socket.create_connection(('127.0.0.1',8556),timeout=8); s.sendall(b'DESCRIBE rtsp://127.0.0.1:8556/unicast RTSP/1.0\\r\\nCSeq: 1\\r\\nAccept: application/sdp\\r\\n\\r\\n'); print(s.recv(8000).decode(errors='replace'))\"\n",[19,3347,3348],{"__ignoreMap":146},[150,3349,3350],{"class":152,"line":153},[150,3351,3345],{},[15,3353,3354,3355,619,3357,3360,3361,3364,3365,3367,3368,626,3370,3372,3373,35],{},"Expect one ",[19,3356,722],{},[19,3358,3359],{},"Content-Base"," on ",[19,3362,3363],{},"rtsp://127.0.0.1:8556/unicast/",", H.264 ",[19,3366,290],{},", PCMA ",[19,3369,297],{},[19,3371,301],{}," with ",[19,3374,3375],{},"a=control:track3",[15,3377,3378],{},"Then the producer:",[141,3380,3382],{"className":143,"code":3381,"language":145,"meta":146,"style":146},"docker exec frigate python3 -c \"import json,urllib.request; d=json.load(urllib.request.urlopen('http://127.0.0.1:1984/api/streams', timeout=8)); print(d['xiaofang']['producers'])\"\n",[19,3383,3384],{"__ignoreMap":146},[150,3385,3386],{"class":152,"line":153},[150,3387,3381],{},[15,3389,3390,3391,619,3394,626,3397,35],{},"One producer should list ",[19,3392,3393],{},"video, recvonly, H264",[19,3395,3396],{},"audio, recvonly, PCMA/8000",[19,3398,3399],{},"audio, sendonly, PCMA/8000",[15,3401,3402],{},"Reload the Frigate UI over HTTPS, open the WebRTC live view, and use the microphone icon. Give the speaker a second. It is quiet.",[15,3404,3405,3406,3409],{},"An optional file test does make a noise. Skip it unless you want to hear the speaker. Generate a short PCM tone and send it with ffmpeg at 32 kHz, or post a PCMA file to the go2rtc API as a temporary source. An empty ",[19,3407,3408],{},"src="," on that API can remove sources. Stop the ffmpeg that is writing to port 11032 when you have heard enough, and leave the proxy process running.",[79,3411,3413],{"id":3412},"what-was-verified","What was verified",[15,3415,3416],{},"On the camera this was built on:",[84,3418,3419,3422,3427,3439,3445,3450,3453],{},[57,3420,3421],{},"Official downgrade from 3.2.2_112 to 3.0.3.56, then fang-hacks 0.2.0, cloud apps stopped.",[57,3423,3424,3426],{},[19,3425,461],{}," got a real 1920x1080 frame plus pcm_alaw. Frigate detect ran at 4 fps.",[57,3428,3429,3430,3432,3433,3435,3436,3438],{},"A 2 second file on ",[19,3431,481],{}," returned ",[19,3434,525],{}," while RTSP ",[19,3437,286],{}," stayed up.",[57,3440,3441,3442,3444],{},"Proxy DESCRIBE carried a single length and ",[19,3443,687],{}," sendonly. SETUP and PLAY returned 200, and RTP followed the PLAY response.",[57,3446,3447,3448,35],{},"go2rtc reported ",[19,3449,3399],{},[57,3451,3452],{},"A PCMA test tone came out of the speaker. It was stopped on purpose.",[57,3454,3455],{},"The next morning the picture was still at about 4 fps, the sendonly track was still advertised, and nothing was writing to port 11032.",[15,3457,3458],{},"Still open: the boot script across a camera reboot, and a person confirming the browser microphone in the live view. The tone test is not that confirmation.",[79,3460,3462],{"id":3461},"checklist","Checklist",[54,3464,3465,3468,3473,3481,3486,3491,3501,3506,3509],{},[57,3466,3467],{},"The camera is a Sonix iSC5 and the firmware is 3.0.3.56, not 3.2.x.",[57,3469,3470,3472],{},[19,3471,255],{}," says the fang-hacks script is installed.",[57,3474,3475,3478,3479,35],{},[19,3476,3477],{},"ffprobe rtsp://CAMERA_IP/unicast"," shows H.264 and pcm_alaw. The server command still has ",[19,3480,286],{},[57,3482,3483,3485],{},[19,3484,481],{}," is 30617 bytes and port 11032 is listening on the camera.",[57,3487,3488,3490],{},[19,3489,2934],{}," is the wrapper, executable, LF shebang.",[57,3492,3493,3494,3496,3497,3360,3499,35],{},"DESCRIBE on ",[19,3495,671],{}," includes ",[19,3498,301],{},[19,3500,687],{},[57,3502,3503,3504,35],{},"The go2rtc producer medias include ",[19,3505,3399],{},[57,3507,3508],{},"The UI is HTTPS and the live view is WebRTC.",[57,3510,3511],{},"Port 11032 is not forwarded off the LAN.",[3513,3514,3515],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .sfNiH, html code.shiki .sfNiH{--shiki-light:#FF5370;--shiki-default:#FF9CAC;--shiki-dark:#FF9CAC}",{"title":146,"searchDepth":153,"depth":159,"links":3517},[3518,3519,3520,3521,3522,3523,3524,3525,3526,3527],{"id":81,"depth":159,"text":82},{"id":116,"depth":159,"text":117},{"id":316,"depth":159,"text":317},{"id":465,"depth":159,"text":466},{"id":608,"depth":159,"text":609},{"id":664,"depth":159,"text":665},{"id":2924,"depth":159,"text":2925},{"id":3338,"depth":159,"text":3339},{"id":3412,"depth":159,"text":3413},{"id":3461,"depth":159,"text":3462},"2026-11-30","The small white Xiaomi Xiaofang (model iSC5, isa.camera.isc5) can be a local RTSP camera, and it can play sound through its speaker. The path is fang-hacks on the Sonix SN98600, plus a tiny RTSP proxy in front of go2rtc. It is a different camera from the Yi and Tuya units in the previous two-way audio guide.","md","./cover.jpg",{},"/blog/22",{"title":5,"description":3529},"The classic Xiaofang is a Sonix camera, not a Dafang. Downgrade the firmware, serve RTSP with fang-hacks, and make Frigate 0.18 talk without turning on arbitrary exec.","blog/22/index",[3538,333,3539,3540,3541,3542],"Frigate","RTSP","Home Automation","Xiaomi","Linux","C22oOzyfX3rVOP_MV614IfFF3LN7KSF6A7sacFjCXRA",1796033550719]