Runt
2025-07-04 acf8e83cbf106b4350536d54eb46379dd86a623c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
//  LiveActivity.swift
//  LiveProject
//
//  Created by 倪路朋 on 6/25/25.
//
 
import UIKit
import AVFoundation
import SwiftUI
import MetalKit
 
struct LiveActivity: View {
    
    @State private var showDeviceDialog = false
    @State private var showInputDialog = false
    
    @State private var streamRate = Float(9/16.0);
    @State private var fpsState = 30;
    @State private var mainSize: CGSize = .init(width: 100, height: 100)
    
    @State private var displaySize : CGSize = .zero;
    
    @State private var devices = [DeviceInfo(name: "相机", type: .CAMERA, deviceId: UUID().uuidString,icon: Icons.CAMERA),
                                  DeviceInfo(name: "话筒", type: .MICROPHONE,deviceId: UUID().uuidString,icon: Icons.MIC),
                                  DeviceInfo(name: "系统", type: .SYSTEM,deviceId : UUID().uuidString,icon: Icons.PORTRAIT)]
 
    
    @StateObject private var mViewModel = LiveViewModel()
    
    var body: some View {
        ZStack{
            Color.clear
                .ignoresSafeArea() // 填满全屏
            VStack{
                VideoRendererView(pixelBuffer: $mViewModel.pixelBuffer).background(Color.black).frame(width: mainSize.width,height:mainSize.height)
                Spacer()
            }.border(Color.blue)
            VStack{
                Spacer()
                BottomBtns().frame(alignment: .bottom).border(Color.green)
            }
            if showInputDialog{
                DialogInput()
            }
        }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
            .background(
                GeometryReader { geometry in
                    Color.clear
                        .onAppear {
                            displaySize = geometry.size;
                            updateWindowSize()
                            print("displaySize:\(displaySize)")
                        }
                        .onChange(of: geometry.size) { newSize in
                            displaySize = newSize;
                            updateWindowSize()
                            print("displaySize:\(displaySize)")
                        }
                })
            .border(Color.red)
            .onDisappear {
                print("onDisappear 视图消失了!")
                
            }.onAppear {
                print("onAppear 视图出现了!")
            }
    }
    
    func updateWindowSize(){
        var rate : Float = Float(displaySize.width)/Float(displaySize.height);
        if(rate != streamRate) {
            var mainWidth = 0.0;
            var mainHeight = 0.0;
            if(rate < streamRate){
                mainWidth = displaySize.width;
                if(9.0/16 == streamRate){
                    mainHeight = (mainWidth / 9.0 * 16);
                }else{
                    mainHeight = (mainWidth / 16.0 * 9);
                }
            }else{
                mainHeight = displaySize.height;
                if(9.0 / 16 == streamRate){
                    mainWidth = (mainHeight / 16.0 * 9);
                }else{
                    mainWidth = (mainHeight / 9.0 * 16);
                }
            }
            if(mainSize.width != CGFloat(mainWidth) || mainSize.height != CGFloat(mainHeight)){
                mainSize = .init(width: mainWidth, height: mainHeight);
            }
        }else{
            if(mainSize.width != displaySize.width || mainSize.height != displaySize.height){
                mainSize = .init(width: displaySize.width, height: displaySize.height);
            }
        }
        print("updateWindow:\(mainSize)")
    }
    
    func DialogInput(onCancel:() ->Void = {},onConfirm:() -> Void = {}) -> some View{
        ZStack{
            Color.black.opacity(0.4)
                .edgesIgnoringSafeArea(.all)
                .onTapGesture {
                    withAnimation {
                        showInputDialog = false
                    }
                }
            VStack {
                VStack(alignment: .leading, spacing: 40) {
                    Text("请输入直播地址")
                        .font(Font.system(size: 20))
                    LTextField().environmentObject(LText())
                    HStack{
                     Spacer()
                        Button(action:{
                            showInputDialog.toggle();
                        }){
                            Text("取消")
                                .font(Font.system(size: 20))
                                .foregroundColor(Color.gray)
                        }
                        
                        Spacer().frame(width: 30)
                        Button(action:{
                            showInputDialog.toggle();
                        }){
                            Text("确认")
                                .font(Font.system(size: 20))
                                .foregroundColor(Color.colorTextLink)
                        }
                        
                    }
                }
                .padding(30)
                .background(Color.white)
                .cornerRadius(20)
                .transition(.move(edge: .bottom))
            }
            .padding(60)
            .zIndex(1)
            .animation(.default, value: devices)
        }
    }
    
    func DialogDevices() -> some View{
        VStack{
            VStack(spacing: 20) {
                Spacer().frame(height:20)
                FlowLayout(){
                    
                    ForEach(devices, id: \.self) { device in
                        MButton(icon: device.icon,text: device.name){
                            mViewModel.newWindowAction(device: device){ status in
                                withAnimation{
                                    showDeviceDialog = false;
                                }
                            }
                            print("\(device.name) click")
                        }
                    }
                }
            }
            .frame(maxWidth: .infinity,alignment:.leading)
            .padding()
        }
        .frame(maxHeight: .infinity,alignment:.topLeading)
    }
    
    func BottomBtns() -> some View{
        VStack{
        
            HStack(){
                //横竖屏控制
                MButton(icon:streamRate == (9/16.0) ? Icons.PORTRAIT : Icons.LANDSCAPE ){
                    streamRate = streamRate == (9/16.0) ? (16/9.0) : (9/16.0)
                    updateWindowSize()
                }
                // fps 控制
                MButton(text: "\(fpsState)帧"){
                    fpsState = fpsState == 30 ? 60 : 30;
                }
                //添加推流地址
                MButton(valid: .INVALID,text: "+"){
                    
                }
            }
            HStack{
                LButton(text: "设备"){
                    print("Click 设备 button")
                    showDeviceDialog.toggle()
                }.sheet(isPresented:$showDeviceDialog, content: {
                    VStack {
                        ScrollView {
                            DialogDevices()
                        }
                    }
                    .presentationDetents([.height(200),.medium])
                })
                LButton(text: "RTMP"){
                    print("Click RTMP button")
                    withAnimation{
                        showInputDialog.toggle()
                    }
                }
                /*flLButton(text: "文件"){
                    
                }*/
                LButton(text: "文本"){
                    print("Click 文本 button")
                    withAnimation{
                        showInputDialog.toggle()
                    }
                }
            }
            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();
    }
    
}