Improve network state updates

This commit is contained in:
Arkadiusz Fal 2022-06-25 01:32:21 +02:00
parent 634b0187ed
commit 19014cc540
5 changed files with 16 additions and 85 deletions

View File

@ -589,5 +589,5 @@ final class AVPlayerBackend: PlayerBackend {
func stopControlsUpdates() {} func stopControlsUpdates() {}
func setNeedsDrawing(_: Bool) {} func setNeedsDrawing(_: Bool) {}
func setSize(_: Double, _: Double) {} func setSize(_: Double, _: Double) {}
func updateNetworkState() {} func setNeedsNetworkStateUpdates() {}
} }

View File

@ -7,6 +7,7 @@ import SwiftUI
final class MPVBackend: PlayerBackend { final class MPVBackend: PlayerBackend {
static var controlsUpdateInterval = 0.5 static var controlsUpdateInterval = 0.5
static var networkStateUpdateInterval = 0.1
private var logger = Logger(label: "mpv-backend") private var logger = Logger(label: "mpv-backend")
@ -27,7 +28,7 @@ final class MPVBackend: PlayerBackend {
} }
self.controls?.isLoadingVideo = self.isLoadingVideo self.controls?.isLoadingVideo = self.isLoadingVideo
self.updateNetworkState() self.setNeedsNetworkStateUpdates()
if !self.isLoadingVideo { if !self.isLoadingVideo {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
@ -40,7 +41,7 @@ final class MPVBackend: PlayerBackend {
}} }}
var isPlaying = true { didSet { var isPlaying = true { didSet {
updateNetworkState() networkStateTimer.resume()
if isPlaying { if isPlaying {
startClientUpdates() startClientUpdates()
@ -73,6 +74,7 @@ final class MPVBackend: PlayerBackend {
var client: MPVClient! { didSet { client.backend = self } } var client: MPVClient! { didSet { client.backend = self } }
private var clientTimer: RepeatingTimer! private var clientTimer: RepeatingTimer!
private var networkStateTimer: RepeatingTimer!
private var handleEOF = false private var handleEOF = false
private var onFileLoaded: (() -> Void)? private var onFileLoaded: (() -> Void)?
@ -117,6 +119,9 @@ final class MPVBackend: PlayerBackend {
clientTimer = .init(timeInterval: Self.controlsUpdateInterval) clientTimer = .init(timeInterval: Self.controlsUpdateInterval)
clientTimer.eventHandler = getClientUpdates clientTimer.eventHandler = getClientUpdates
networkStateTimer = .init(timeInterval: Self.networkStateUpdateInterval)
networkStateTimer.eventHandler = updateNetworkState
} }
typealias AreInIncreasingOrder = (Stream, Stream) -> Bool typealias AreInIncreasingOrder = (Stream, Stream) -> Bool
@ -396,12 +401,12 @@ final class MPVBackend: PlayerBackend {
onFileLoaded = nil onFileLoaded = nil
case MPV_EVENT_PAUSE: case MPV_EVENT_PAUSE:
updateNetworkState() networkStateTimer.resume()
case MPV_EVENT_UNPAUSE: case MPV_EVENT_UNPAUSE:
isLoadingVideo = false isLoadingVideo = false
isSeeking = false isSeeking = false
updateNetworkState() networkStateTimer.resume()
case MPV_EVENT_SEEK: case MPV_EVENT_SEEK:
isSeeking = true isSeeking = true
@ -466,15 +471,12 @@ final class MPVBackend: PlayerBackend {
networkState.bufferingState = client.bufferingState networkState.bufferingState = client.bufferingState
} }
if networkState.needsUpdates { if !networkState.needsUpdates {
dispatchNetworkUpdate() networkStateTimer.suspend()
} }
} }
func dispatchNetworkUpdate() { func setNeedsNetworkStateUpdates() {
print("dispatching network update") networkStateTimer.resume()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
self?.updateNetworkState()
}
} }
} }

View File

@ -52,7 +52,7 @@ protocol PlayerBackend {
func startControlsUpdates() func startControlsUpdates()
func stopControlsUpdates() func stopControlsUpdates()
func updateNetworkState() func setNeedsNetworkStateUpdates()
func setNeedsDrawing(_ needsDrawing: Bool) func setNeedsDrawing(_ needsDrawing: Bool)
func setSize(_ width: Double, _ height: Double) func setSize(_ width: Double, _ height: Double)

View File

@ -67,7 +67,7 @@ final class PlayerModel: ObservableObject {
@Published var returnYouTubeDislike = ReturnYouTubeDislikeAPI() @Published var returnYouTubeDislike = ReturnYouTubeDislikeAPI()
@Published var isSeeking = false { didSet { @Published var isSeeking = false { didSet {
backend.updateNetworkState() backend.setNeedsNetworkStateUpdates()
}} }}
#if os(iOS) #if os(iOS)

View File

@ -122,19 +122,6 @@ struct VideoDetails: View {
var body: some View { var body: some View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Group { Group {
// Group {
// subscriptionsSection
// .border(.red, width: 4)
//
// .onChange(of: video) { video in
// if let video = video {
// subscribed = subscriptions.isSubscribing(video.channel.id)
// }
// }
// }
// .padding(.top, 4)
// .padding(.horizontal)
HStack(spacing: 4) { HStack(spacing: 4) {
pageButton("Info", "info.circle", .info) pageButton("Info", "info.circle", .info)
pageButton("Chapters", "bookmark", .chapters) pageButton("Chapters", "bookmark", .chapters)
@ -188,64 +175,6 @@ struct VideoDetails: View {
accounts.app.supportsUserPlaylists && accounts.signedIn accounts.app.supportsUserPlaylists && accounts.signedIn
} }
var subscriptionsSection: some View {
Group {
if let video = video {
HStack(alignment: .center) {
HStack(spacing: 10) {
Group {
// ZStack(alignment: .bottomTrailing) {
// authorAvatar
//
// if subscribed {
// Image(systemName: "star.circle.fill")
// .background(Color.background)
// .clipShape(Circle())
// .foregroundColor(.secondary)
// }
// }
// VStack(alignment: .leading, spacing: 4) {
// Text(video.title)
// .font(.system(size: 11))
// .fontWeight(.bold)
//
// HStack(spacing: 4) {
// Text(video.channel.name)
//
// if let subscribers = video.channel.subscriptionsString {
// Text("")
// .foregroundColor(.secondary)
// .opacity(0.3)
//
// Text("\(subscribers) subscribers")
// }
// }
// .foregroundColor(.secondary)
// .font(.caption2)
// }
}
}
.contentShape(RoundedRectangle(cornerRadius: 12))
.contextMenu {
if let video = video {
Button(action: {
NavigationModel.openChannel(
video.channel,
player: player,
recents: recents,
navigation: navigation
)
}) {
Label("\(video.channel.name) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
}
}
}
}
}
}
}
var publishedDateSection: some View { var publishedDateSection: some View {
Group { Group {
if let video = player.currentVideo { if let video = player.currentVideo {