import threading import time import ctypes from ctypes import wintypes # Load avrt.dll and declare prototypes avrt = ctypes.WinDLL('avrt.dll') AvSetMmThreadCharacteristicsW = avrt.AvSetMmThreadCharacteristicsW AvSetMmThreadCharacteristicsW.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(wintypes.DWORD)] AvSetMmThreadCharacteristicsW.restype = wintypes.HANDLE AvRevertMmThreadCharacteristics = avrt.AvRevertMmThreadCharacteristics AvRevertMmThreadCharacteristics.argtypes = [wintypes.HANDLE] AvRevertMmThreadCharacteristics.restype = wintypes.BOOL def media_thread(): # Register this thread with MMCSS as "Audio" (or "Pro Audio" for low-latency) task_index = wintypes.DWORD(0) handle = AvSetMmThreadCharacteristicsW("Audio", ctypes.byref(task_index)) # or: handle = AvSetMmThreadCharacteristicsW("Pro Audio", ctypes.byref(task_index)) if not handle: print("Failed to register thread with MMCSS") return print("Media thread registered with MMCSS, task index =", task_index.value) try: # Simulate continuous media playback forever chunk_num = 0 while True: # Your real-time audio/video processing would go here print("Playing media chunk", chunk_num) chunk_num += 1 time.sleep(0.1) # Simulate 100ms media chunks except KeyboardInterrupt: print(" Stopping media playback...") finally: # Revert when stopping (handles Ctrl+C gracefully) AvRevertMmThreadCharacteristics(handle) print("Media thread reverted from MMCSS") if __name__ == "__main__": print("Starting infinite media simulation thread. Press Ctrl+C to stop.") t = threading.Thread(target=media_thread, name="MediaThread", daemon=True) t.start() try: # Keep main thread alive to allow Ctrl+C while True: time.sleep(1) except KeyboardInterrupt: print(" Main thread interrupted.")