Compare commits

..

1 Commits

Author SHA1 Message Date
Toni Förster
e62010d5d5 improvements to MPVGLView
- add Pixel Buffer Object to (PBO)
- add some debug logging
- add scissor testing
- add dirty region checking

Signed-off-by: Toni Förster <toni.foerster@gmail.com>
2024-09-14 15:04:17 +02:00
5 changed files with 90 additions and 67 deletions

View File

@@ -23,14 +23,15 @@ final class MPVBackend: PlayerBackend {
var stream: Stream? var stream: Stream?
var video: Video? var video: Video?
var captions: Captions? { var captions: Captions? { didSet {
didSet { guard let captions else {
Task { if client?.areSubtitlesAdded == true {
await handleCaptionsChange() client?.removeSubs()
} }
return
} }
} addSubTrack(captions.url)
}}
var currentTime: CMTime? var currentTime: CMTime?
var loadedVideo = false var loadedVideo = false
@@ -616,14 +617,10 @@ final class MPVBackend: PlayerBackend {
} }
func addSubTrack(_ url: URL) { func addSubTrack(_ url: URL) {
Task { if client?.areSubtitlesAdded == true {
if let areSubtitlesAdded = client?.areSubtitlesAdded { client?.removeSubs()
if await areSubtitlesAdded() {
await client?.removeSubs()
}
}
await client?.addSubTrack(url)
} }
client?.addSubTrack(url)
} }
func setVideoToAuto() { func setVideoToAuto() {
@@ -687,17 +684,6 @@ final class MPVBackend: PlayerBackend {
} }
} }
private func handleCaptionsChange() async {
guard let captions else {
if let isSubtitlesAdded = client?.areSubtitlesAdded, await isSubtitlesAdded() {
await client?.removeSubs()
}
return
}
addSubTrack(captions.url)
}
private func handlePropertyChange(_ name: String, _ property: mpv_event_property) { private func handlePropertyChange(_ name: String, _ property: mpv_event_property) {
switch name { switch name {
case "pause": case "pause":

View File

@@ -349,17 +349,23 @@ final class MPVClient: ObservableObject {
return Int(fps.rounded()) return Int(fps.rounded())
} }
func areSubtitlesAdded() async -> Bool { var areSubtitlesAdded: Bool {
guard !mpv.isNil else { return false } guard !mpv.isNil else { return false }
let trackCount = await Task(operation: { getInt("track-list/count") }).value // Retrieve the number of tracks
let trackCount = getInt("track-list/count")
guard trackCount > 0 else { return false } guard trackCount > 0 else { return false }
for index in 0 ..< trackCount { for index in 0 ..< trackCount {
if let trackType = await Task(operation: { getString("track-list/\(index)/type") }).value, trackType == "sub" { // Get the type of each track
if let trackType = getString("track-list/\(index)/type"), trackType == "sub" {
// Check if the subtitle track is currently selected
let selected = getInt("track-list/\(index)/selected")
if selected == 1 {
return true return true
} }
} }
}
return false return false
} }
@@ -533,16 +539,12 @@ final class MPVClient: ObservableObject {
command("video-add", args: [url.absoluteString]) command("video-add", args: [url.absoluteString])
} }
func addSubTrack(_ url: URL) async { func addSubTrack(_ url: URL) {
await Task {
command("sub-add", args: [url.absoluteString]) command("sub-add", args: [url.absoluteString])
}.value
} }
func removeSubs() async { func removeSubs() {
await Task {
command("sub-remove") command("sub-remove")
}.value
} }
func setVideoToAuto() { func setVideoToAuto() {

View File

@@ -4,7 +4,7 @@
<dict> <dict>
<key>com.apple.security.application-groups</key> <key>com.apple.security.application-groups</key>
<array> <array>
<string>group.78Z5H3M6RJ.stream.yattee.app.urlbookmarks</string> <string>group.ZYMM2HKXY2.yattee.app.url</string>
</array> </array>
</dict> </dict>
</plist> </plist>

View File

@@ -5,8 +5,6 @@ struct ControlsOverlay: View {
@ObservedObject private var player = PlayerModel.shared @ObservedObject private var player = PlayerModel.shared
private var model = PlayerControlsModel.shared private var model = PlayerControlsModel.shared
@State private var availableCaptions: [Captions] = []
@State private var isLoadingCaptions = true
@State private var contentSize: CGSize = .zero @State private var contentSize: CGSize = .zero
@Default(.showMPVPlaybackStats) private var showMPVPlaybackStats @Default(.showMPVPlaybackStats) private var showMPVPlaybackStats
@@ -337,6 +335,7 @@ struct ControlsOverlay: View {
Image(systemName: "text.bubble") Image(systemName: "text.bubble")
if let captions = captionsBinding.wrappedValue, if let captions = captionsBinding.wrappedValue,
let language = LanguageCodes(rawValue: captions.code) let language = LanguageCodes(rawValue: captions.code)
{ {
Text("\(language.description.capitalized) (\(language.rawValue))") Text("\(language.description.capitalized) (\(language.rawValue))")
.foregroundColor(.accentColor) .foregroundColor(.accentColor)
@@ -381,16 +380,17 @@ struct ControlsOverlay: View {
.contextMenu { .contextMenu {
Button("Disabled") { captionsBinding.wrappedValue = nil } Button("Disabled") { captionsBinding.wrappedValue = nil }
ForEach(availableCaptions) { caption in ForEach(player.currentVideo?.captions ?? []) { caption in
Button(caption.description) { captionsBinding.wrappedValue = caption } Button(caption.description) { captionsBinding.wrappedValue = caption }
} }
Button("Cancel", role: .cancel) {} Button("Cancel", role: .cancel) {}
} }
#endif #endif
} }
@ViewBuilder private var captionsPicker: some View { @ViewBuilder private var captionsPicker: some View {
let captions = availableCaptions let captions = player.currentVideo?.captions ?? []
Picker("Captions", selection: captionsBinding) { Picker("Captions", selection: captionsBinding) {
if captions.isEmpty { if captions.isEmpty {
Text("Not available").tag(Captions?.none) Text("Not available").tag(Captions?.none)
@@ -402,31 +402,6 @@ struct ControlsOverlay: View {
} }
} }
.disabled(captions.isEmpty) .disabled(captions.isEmpty)
.onAppear {
loadCaptions()
}
}
private func loadCaptions() {
isLoadingCaptions = true
// Fetch captions asynchronously
Task {
let fetchedCaptions = await fetchCaptions()
await MainActor.run {
// Update state on the main thread
self.availableCaptions = fetchedCaptions
self.isLoadingCaptions = false
}
}
}
private func fetchCaptions() async -> [Captions] {
// Access currentVideo from the main actor context
await MainActor.run {
// Safely access the main actor-isolated currentVideo property
player.currentVideo?.captions ?? []
}
} }
private var captionsBinding: Binding<Captions?> { private var captionsBinding: Binding<Captions?> {

View File

@@ -11,6 +11,7 @@ final class MPVOGLView: GLKView {
var mpvGL: UnsafeMutableRawPointer? var mpvGL: UnsafeMutableRawPointer?
var queue = DispatchQueue(label: "stream.yattee.opengl", qos: .userInteractive) var queue = DispatchQueue(label: "stream.yattee.opengl", qos: .userInteractive)
var needsDrawing = true var needsDrawing = true
private var dirtyRegion: CGRect?
override init(frame: CGRect) { override init(frame: CGRect) {
guard let context = EAGLContext(api: .openGLES2) else { guard let context = EAGLContext(api: .openGLES2) else {
@@ -85,6 +86,7 @@ final class MPVOGLView: GLKView {
@objc private func updateFrame() { @objc private func updateFrame() {
// Trigger the drawing process if needed // Trigger the drawing process if needed
if needsDrawing { if needsDrawing {
markRegionAsDirty(bounds)
setNeedsDisplay() setNeedsDisplay()
} }
} }
@@ -100,16 +102,60 @@ final class MPVOGLView: GLKView {
glClear(UInt32(GL_COLOR_BUFFER_BIT)) glClear(UInt32(GL_COLOR_BUFFER_BIT))
} }
// Function to set a dirty region when a part of the screen changes
func markRegionAsDirty(_ region: CGRect) {
if dirtyRegion == nil {
dirtyRegion = region
} else {
// Expand the dirty region to include the new region
dirtyRegion = dirtyRegion!.union(region)
}
}
// Logic to decide if only part of the screen needs updating
private func needsPartialUpdate() -> Bool {
// Check if there is a defined dirty region that needs updating
if let dirtyRegion, !dirtyRegion.isEmpty {
// Set up glScissor based on dirtyRegion coordinates
glScissor(GLint(dirtyRegion.origin.x), GLint(dirtyRegion.origin.y), GLsizei(dirtyRegion.width), GLsizei(dirtyRegion.height))
return true
}
return false
}
// Call this function when you know the entire screen needs updating
private func clearDirtyRegion() {
dirtyRegion = nil
}
override func draw(_: CGRect) { override func draw(_: CGRect) {
guard needsDrawing, let mpvGL else { return } guard needsDrawing, let mpvGL else { return }
// Ensure the correct context is set
guard EAGLContext.setCurrent(context) else {
logger.error("Failed to set current OpenGL context.")
return
}
// Bind the default framebuffer // Bind the default framebuffer
glGetIntegerv(UInt32(GL_FRAMEBUFFER_BINDING), &defaultFBO!) glGetIntegerv(UInt32(GL_FRAMEBUFFER_BINDING), &defaultFBO!)
// Ensure the framebuffer is valid
guard defaultFBO != nil && defaultFBO! != 0 else {
logger.error("Invalid framebuffer ID.")
return
}
// Get the current viewport dimensions // Get the current viewport dimensions
var dims: [GLint] = [0, 0, 0, 0] var dims: [GLint] = [0, 0, 0, 0]
glGetIntegerv(GLenum(GL_VIEWPORT), &dims) glGetIntegerv(GLenum(GL_VIEWPORT), &dims)
// Check if we need partial updates
if needsPartialUpdate() {
logger.info("Performing partial update with scissor test.")
glEnable(GLenum(GL_SCISSOR_TEST))
}
// Set up the OpenGL FBO data // Set up the OpenGL FBO data
var data = mpv_opengl_fbo( var data = mpv_opengl_fbo(
fbo: Int32(defaultFBO!), fbo: Int32(defaultFBO!),
@@ -129,10 +175,24 @@ final class MPVOGLView: GLKView {
mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: flipPtr), mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: flipPtr),
mpv_render_param() mpv_render_param()
] ]
mpv_render_context_render(OpaquePointer(mpvGL), &params) // Call the render function and check for errors
let result = mpv_render_context_render(OpaquePointer(mpvGL), &params)
if result < 0 {
logger.error("mpv_render_context_render() failed with error code: \(result)")
} else {
logger.info("mpv_render_context_render() called successfully.")
} }
} }
} }
// Disable the scissor test after rendering if it was enabled
if needsPartialUpdate() {
glDisable(GLenum(GL_SCISSOR_TEST))
}
// Clear dirty region after drawing
clearDirtyRegion()
}
} }
extension Notification.Name { extension Notification.Name {