//
|
// 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();
|
}
|
|
}
|