import subprocess
import psutil
import time
import os
import tkinter as tk
from tkinter import messagebox

# 基础配置
SERVER_IP = "192.168.1.100"
MEDIAMTX_PATH = r"D:\ScreenCast\mediamtx\mediamtx.exe"
FFMPEG_PATH = r"D:\ScreenCast\ffmpeg\bin\ffmpeg.exe"
# 32台终端IP列表（替换为实际终端IP）
TERMINAL_IPS = [f"192.168.1.{i}" for i in range(11, 43)]  # 示例：192.168.1.11~43

# 停止所有相关进程
def stop_all():
    # 停止MediaMTX
    for proc in psutil.process_iter(['name']):
        if proc.info['name'] in ['mediamtx.exe', 'ffmpeg.exe']:
            proc.kill()
    # 停止终端Chrome（可选）
    for ip in TERMINAL_IPS:
        try:
            subprocess.run(f"ssh {ip} taskkill /f /im chrome.exe", shell=True, capture_output=True)
        except:
            pass
    messagebox.showinfo("提示", "所有服务已停止！")

# 模式1：1台投屏→31台接收
def mode1():
    # 启动投屏源推流（本地执行push_screen.py）
    subprocess.Popen(["python", r"D:\ScreenCast\push_screen.py"], creationflags=subprocess.CREATE_NEW_CONSOLE)
    time.sleep(5)
    # 批量启动31台终端接收（排除投屏源IP，比如192.168.1.10）
    for ip in TERMINAL_IPS:
        if ip != "192.168.1.10":  # 投屏源IP
            # 远程执行auto_receive.py（需开启终端SSH/共享脚本）
            subprocess.run(f"ssh {ip} python D:\ScreenCast\auto_receive.py screen", shell=True)
    messagebox.showinfo("提示", "模式1已启动：1台投屏→31台接收！")

# 模式2：投屏+第三方会议（投屏流→31台+HDMI输出到矩阵）
def mode2():
    # 启动投屏推流
    subprocess.Popen([
        FFMPEG_PATH,
        "-f", "gdigrab", "-framerate", "30", "-i", "desktop",
        "-c:v", "libx264", "-preset", "ultrafast", "-tune", "zerolatency",
        "-f", "rtsp", f"rtsp://{SERVER_IP}:8554/screen"
    ], creationflags=subprocess.CREATE_NEW_CONSOLE)
    # 启动HDMI输出到矩阵（mpv需提前解压到D:\ScreenCast）
    subprocess.Popen([
        r"D:\ScreenCast\mpv.exe",
        f"rtsp://{SERVER_IP}:8554/screen",
        "--vo=direct3d", "--screen=1", "--fullscreen"
    ], creationflags=subprocess.CREATE_NEW_CONSOLE)
    # 批量启动31台终端接收
    for ip in TERMINAL_IPS:
        if ip != "192.168.1.10":
            subprocess.run(f"ssh {ip} python D:\ScreenCast\auto_receive.py screen", shell=True)
    messagebox.showinfo("提示", "模式2已启动：投屏→31台+HDMI输出到会议！")

# 模式3：会议流→32台接收（采集卡采集会议流）
def mode3():
    # 采集卡采集矩阵的会议流，推到MediaMTX
    subprocess.Popen([
        FFMPEG_PATH,
        "-f", "dshow", "-i", "video=采集卡设备名称",
        "-c:v", "libx264", "-preset", "ultrafast", "-tune", "zerolatency",
        "-f", "rtsp", f"rtsp://{SERVER_IP}:8554/meeting"
    ], creationflags=subprocess.CREATE_NEW_CONSOLE)
    # 批量启动32台终端接收会议流
    for ip in TERMINAL_IPS:
        subprocess.run(f"ssh {ip} python D:\ScreenCast\auto_receive.py meeting", shell=True)
    messagebox.showinfo("提示", "模式3已启动：会议流→32台接收！")

# 主界面
def main():
    root = tk.Tk()
    root.title("会议投屏运维总控")
    root.geometry("400x300")
    
    btn1 = tk.Button(root, text="模式1：1台投屏→31台接收", command=mode1, width=30, height=2)
    btn1.pack(pady=10)
    
    btn2 = tk.Button(root, text="模式2：投屏+HDMI输出到矩阵", command=mode2, width=30, height=2)
    btn2.pack(pady=10)
    
    btn3 = tk.Button(root, text="模式3：会议流→32台接收", command=mode3, width=30, height=2)
    btn3.pack(pady=10)
    
    btn4 = tk.Button(root, text="停止所有服务", command=stop_all, width=30, height=2)
    btn4.pack(pady=10)
    
    root.mainloop()

if __name__ == "__main__":
    main()