1
Runt
2025-06-27 bf9e4680eb466bcb7c9cb1bef567252bb1f2bb7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
//  LiveActivity.swift
//  LiveProject
//
//  Created by 倪路朋 on 6/25/25.
//
 
import Foundation
import SwiftUI
import WrappingHStack
 
struct LiveActivity: View {
    @State private var pixelBuffer: CVPixelBuffer?
    
    
    @State private var showDeviceDialog = false
    
    @State private var streamRate = Float(9/16.0);
    @State private var mainSize: CGSize = .init(width: 100, height: 100)
    
    @State private var devices = [DeviceInfo(name: "相机", type: .CAMERA(), deviceId: "相机"),
                                  DeviceInfo(name: "话筒", type: .MICROPHONE(),deviceId: "话筒"),
                                  DeviceInfo(name: "系统", type: .SYSTEM(),deviceId : "系统")]
    
    var body: some View {
        ZStack{
            Color.clear
                .ignoresSafeArea() // 填满全屏
            VStack{
                VideoRendererView(renderer: MetalRenderer()).background(Color.black).frame(width: mainSize.width,height:mainSize.height)
                Spacer()
            }.border(Color.blue)
            VStack{
                Spacer()
                BottomBtns().frame(alignment: .bottom).border(Color.green)
            }
            if showDeviceDialog {
                DialogDevices()
            }
        }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
            .background(
                GeometryReader { geometry in
                    Color.clear
                        .onAppear {
                            updateWindowSize(width: Int(geometry.size.width), height: Int(geometry.size.height))
                        }
                        .onChange(of: geometry.size) { newSize in
                            updateWindowSize(width: Int(newSize.width), height: Int(newSize.height))
                        }
                })
            .border(Color.red)
            .onDisappear {
                print("onDisappear 视图消失了!")
                
            }.onAppear {
                print("onAppear 视图出现了!")
            }
    }
    
    func updateWindowSize(width:Int,height:Int){
        var rate : Float = Float(width)/Float(height);
        if(rate != streamRate) {
            var mainWidth = 0;
            var mainHeight = 0;
            if(rate < streamRate){
                mainWidth = width;
                if(9.0/16 == streamRate){
                    mainHeight = (mainWidth / 9 * 16);
                }else{
                    mainHeight = (mainWidth / 16 * 9);
                }
            }else{
                mainHeight = height;
                if(9.0 / 16 == streamRate){
                    mainWidth = (mainHeight / 16 * 9);
                }else{
                    mainWidth = (mainHeight / 9 * 16);
                }
            }
            if(mainSize.width != CGFloat(mainWidth) || mainSize.height != CGFloat(mainHeight)){
                mainSize = .init(width: mainWidth, height: mainHeight);
            }
        }else{
            if(mainSize.width != CGFloat(width) || mainSize.height != CGFloat(height)){
                mainSize = .init(width: width, height: height);
            }
        }
        //Log.w(TAG , "onSizeChanged: ${mainWindowSize.value}" , )
    }
    
    func DialogDevices() -> some View{
        ZStack{
            Color.black.opacity(0.4)
                .edgesIgnoringSafeArea(.all)
                .onTapGesture {
                    withAnimation {
                        showDeviceDialog = false
                    }
                }
            VStack {
                Spacer()
                VStack(spacing: 20) {
                    Spacer().frame(height:40)
                    FlowLayout(devices){ device in
                        MButton(text:device.name){
                            
                        }
                    }
                    .padding()
                    .animation(.default, value: devices)
                }
                .frame(maxWidth: .infinity)
                .padding()
                .background(Color.white)
                .cornerRadius(20)
                .transition(.move(edge: .bottom))
            }
            .zIndex(1)
        }
    }
    
    func BottomBtns() -> some View{
        VStack{
        
            HStack(){
                MButton(icon: IconPortrait()){
                    
                }
                MButton(text: "30帧"){
                    
                }
                MButton(valid: .INVALID,text: "+"){
                    
                }
            }
            HStack{
                LButton(text: "设备"){
                    print("Click 设备 button")
                    withAnimation{
                        showDeviceDialog.toggle()
                    }
                }
                LButton(text: "RTMP"){
                    
                }
                /*flLButton(text: "文件"){
                    
                }*/
                LButton(text: "文本"){
                    
                }
            }
            HStack{
                Text("编码正常").font(Font.system(size: 14)).foregroundColor(Color.init("ColorGreen"))
                Text("推流正常").font(Font.system(size: 14)).foregroundColor(Color.init("ColorGreen"))
                Text("内存正常").font(Font.system(size: 14)).foregroundColor(Color.init("ColorGreen"))
            }
        }
    }
}
 
 
struct LiveActivity_BottomBtns_Previews: PreviewProvider{
    static var previews: some View {
        LiveActivity();
    }
    
}