forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
macos: gui: Add Picture in Picture feature
This adds system picture in picture support using private AVKit's PIPViewController. A new button to enter picture in picture mode is added in the player controls overlay.
- Loading branch information
1 parent
e981199
commit f697fef
Showing
10 changed files
with
258 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/***************************************************************************** | ||
* PIPSPI.h: Picture in Picture private API | ||
***************************************************************************** | ||
* Copyright (C) 2024 VLC authors and VideoLAN | ||
* | ||
* Authors: Maxime Chapelet <umxprime at videolabs dot io> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. | ||
*****************************************************************************/ | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@protocol PIPViewControllerDelegate; | ||
|
||
@interface PIPViewController : NSViewController | ||
|
||
@property (nonatomic, weak, nullable) id<PIPViewControllerDelegate> delegate; | ||
@property (nonatomic, weak, nullable) NSWindow *replacementWindow; | ||
@property (nonatomic) NSRect replacementRect; | ||
@property (nonatomic) bool playing; | ||
@property (nonatomic) bool userCanResize; | ||
@property (nonatomic) NSSize aspectRatio; | ||
|
||
- (void)presentViewControllerAsPictureInPicture:(NSViewController *)viewController; | ||
|
||
@end | ||
|
||
@protocol PIPViewControllerDelegate <NSObject> | ||
@optional | ||
- (BOOL)pipShouldClose:(PIPViewController *)pip; | ||
- (void)pipWillClose:(PIPViewController *)pip; | ||
- (void)pipDidClose:(PIPViewController *)pip; | ||
- (void)pipActionPlay:(PIPViewController *)pip; | ||
- (void)pipActionPause:(PIPViewController *)pip; | ||
- (void)pipActionStop:(PIPViewController *)pip; | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
/***************************************************************************** | ||
* VLCMainVideoViewController.m: MacOS X interface module | ||
***************************************************************************** | ||
* Copyright (C) 2023 VLC authors and VideoLAN | ||
* Copyright (C) 2024 VLC authors and VideoLAN | ||
* | ||
* Authors: Claudio Cambra <[email protected]> | ||
* Maxime Chapelet <umxprime at videolabs dot io> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
|
@@ -47,11 +48,44 @@ | |
|
||
#import <vlc_common.h> | ||
|
||
@interface VLCMainVideoViewController() | ||
#import "private/PIPSPI.h" | ||
|
||
@interface PIPVoutViewController : NSViewController | ||
@end | ||
|
||
@implementation PIPVoutViewController | ||
|
||
- (void)setView:(NSView *)view { | ||
[super setView:view]; | ||
} | ||
|
||
- (void)viewDidLoad { | ||
[super viewDidLoad]; | ||
} | ||
|
||
- (void)viewWillAppear { | ||
[super viewWillAppear]; | ||
|
||
if (self.view.superview) { | ||
[self.view.superview.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES; | ||
[self.view.superview.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES; | ||
[self.view.superview.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES; | ||
[self.view.superview.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES; | ||
} | ||
} | ||
|
||
- (void)viewDidAppear { | ||
[super viewDidAppear]; | ||
} | ||
@end | ||
|
||
@interface VLCMainVideoViewController() <PIPViewControllerDelegate> | ||
{ | ||
NSTimer *_hideControlsTimer; | ||
NSLayoutConstraint *_returnButtonBottomConstraint; | ||
NSLayoutConstraint *_playlistButtonBottomConstraint; | ||
PIPViewController *_pipViewController; | ||
PIPVoutViewController *_voutViewController; | ||
|
||
BOOL _isFadingIn; | ||
} | ||
|
@@ -86,6 +120,15 @@ - (instancetype)init | |
selector:@selector(shouldShowControls:) | ||
name:VLCVideoWindowShouldShowFullscreenController | ||
object:nil]; | ||
[notificationCenter addObserver:self | ||
selector:@selector(pictureInPictureChanged:) | ||
name:VLCPlayerPictureInPictureChanged | ||
object:nil]; | ||
|
||
Class PIPViewControllerClass = NSClassFromString(@"PIPViewController"); | ||
_pipViewController = [[PIPViewControllerClass alloc] init]; | ||
_pipViewController.delegate = self; | ||
_pipViewController.userCanResize = true; | ||
} | ||
return self; | ||
} | ||
|
@@ -456,6 +499,37 @@ - (void)updateLibraryControls | |
[_overlayView setNeedsDisplay:YES]; | ||
} | ||
|
||
- (void)pictureInPictureChanged:(VLCPlayerController *)playerController { | ||
if (_voutViewController) | ||
return; | ||
[self.view.window orderOut:self.view.window]; | ||
_voutViewController = [PIPVoutViewController new]; | ||
_voutViewController.view = _voutView; | ||
VLCPlayerController * const controller = | ||
VLCMain.sharedInstance.playlistController.playerController; | ||
_pipViewController.playing = controller.playerState == VLC_PLAYER_STATE_PLAYING; | ||
|
||
VLCInputItem *item = controller.currentMedia; | ||
input_item_t * const p_input = item.vlcInputItem; | ||
vlc_mutex_lock(&p_input->lock); | ||
const struct input_item_es *item_es; | ||
vlc_vector_foreach_ref(item_es, &p_input->es_vec) | ||
{ | ||
if (item_es->es.i_cat != VIDEO_ES) | ||
continue; | ||
const video_format_t *fmt = &item_es->es.video; | ||
unsigned int width = fmt->i_visible_width; | ||
unsigned int height = fmt->i_visible_height; | ||
if (fmt->i_sar_num && fmt->i_sar_den) | ||
height = (height * fmt->i_sar_den) / fmt->i_sar_num; | ||
_pipViewController.aspectRatio = CGSizeMake(width, height); | ||
break; | ||
} | ||
vlc_mutex_unlock(&p_input->lock); | ||
_pipViewController.title = self.view.window.title; | ||
[_pipViewController presentViewControllerAsPictureInPicture:_voutViewController]; | ||
} | ||
|
||
- (IBAction)togglePlaylist:(id)sender | ||
{ | ||
VLCLibraryWindow * const libraryWindow = (VLCLibraryWindow*)self.view.window; | ||
|
@@ -471,5 +545,48 @@ - (IBAction)returnToLibrary:(id)sender | |
[libraryWindow disableVideoPlaybackAppearance]; | ||
} | ||
} | ||
#pragma mark - PIPViewControllerDelegate | ||
|
||
- (BOOL)pipShouldClose:(PIPViewController *)pip { | ||
return YES; | ||
} | ||
|
||
- (void)pipWillClose:(PIPViewController *)pip { | ||
[_voutView removeFromSuperview]; | ||
[_voutContainingView addSubview:_voutView]; | ||
[_voutContainingView.topAnchor constraintEqualToAnchor:_voutView.topAnchor].active = YES; | ||
[_voutContainingView.bottomAnchor constraintEqualToAnchor:_voutView.bottomAnchor].active = YES; | ||
[_voutContainingView.leftAnchor constraintEqualToAnchor:_voutView.leftAnchor].active = YES; | ||
[_voutContainingView.rightAnchor constraintEqualToAnchor:_voutView.rightAnchor].active = YES; | ||
_voutViewController = nil; | ||
pip.replacementWindow = self.view.window; | ||
pip.replacementRect = self.voutContainingView.frame; | ||
} | ||
|
||
- (void)pipDidClose:(PIPViewController *)pip { | ||
[self.view.window orderFront:self.view.window]; | ||
} | ||
|
||
- (void)pipActionPlay:(PIPViewController *)pip { | ||
VLCPlayerController * const controller = | ||
VLCMain.sharedInstance.playlistController.playerController; | ||
if (controller.playerState == VLC_PLAYER_STATE_PAUSED) { | ||
[controller resume]; | ||
} else { | ||
[controller start]; | ||
} | ||
} | ||
|
||
- (void)pipActionStop:(PIPViewController *)pip { | ||
VLCPlayerController * const controller = | ||
VLCMain.sharedInstance.playlistController.playerController; | ||
[controller pause]; | ||
} | ||
|
||
- (void)pipActionPause:(PIPViewController *)pip { | ||
VLCPlayerController * const controller = | ||
VLCMain.sharedInstance.playlistController.playerController; | ||
[controller pause]; | ||
} | ||
|
||
@end |