Skip to content

Commit

Permalink
Playback: adapt to VLCKitv4 API
Browse files Browse the repository at this point in the history
  • Loading branch information
fkuehne committed Aug 23, 2024
1 parent 05c8614 commit ae6fa96
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 105 deletions.
1 change: 1 addition & 0 deletions Sources/App/iOS/VLCAppCoordinator.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ - (instancetype)init
self = [super init];
if (self) {
dispatch_async(dispatch_get_main_queue(), ^{
[VLCLibrary setSharedEventsConfiguration:[VLCEventsLegacyConfiguration new]];
[self initializeServices];
});
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/Helpers/Network/URLHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,10 @@ extension VLCURLHandler {
// TODO: This code should probably not live here
func play(url: URL, sub: URL? = nil, completion: ((Bool) -> Void)?) {
let vpc = PlaybackService.sharedInstance()
let mediaList = VLCMediaList(array: [VLCMedia(url: url)])
vpc.playMediaList(mediaList, firstIndex: 0, subtitlesFilePath: sub?.absoluteString, completion: completion)
if let media = VLCMedia(url: url) {
let mediaList = VLCMediaList(array: [media])
vpc.playMediaList(mediaList, firstIndex: 0, subtitlesFilePath: sub?.absoluteString, completion: completion)
}
}

#if os(iOS)
Expand Down
14 changes: 7 additions & 7 deletions Sources/Helpers/Thumbnail Cache/VLCMediaThumbnailerCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import UIKit
return
}

let media = VLCMedia(url: URL(fileURLWithPath: videoURL.removingPercentEncoding!))
if let media = VLCMedia(url: URL(fileURLWithPath: videoURL.removingPercentEncoding!)) {
let thumbnailer = VLCMediaThumbnailer(media: media, andDelegate: self)

let thumbnailer = VLCMediaThumbnailer(media: media, andDelegate: self)
let thumbSize = CGSize(width: 800, height: 600)
thumbnailer.thumbnailWidth = thumbSize.width
thumbnailer.thumbnailHeight = thumbSize.height

let thumbSize = CGSize(width: 800, height: 600)
thumbnailer.thumbnailWidth = thumbSize.width
thumbnailer.thumbnailHeight = thumbSize.height

thumbnailer.fetchThumbnail()
thumbnailer.fetchThumbnail()
}
}

// MARK: - VLCMediaThumbnailer data source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ extension VLCFavoriteListViewController: UITableViewDelegate, UITableViewDataSou
serverBrowser = VLCNetworkServerBrowserVLCMedia.sftpNetworkServerBrowser(withLogin: login)
}
} else {
let media = VLCMedia(url: favorite.url)
serverBrowser = VLCNetworkServerBrowserVLCMedia(media: media)
if let media = VLCMedia(url: favorite.url) {
serverBrowser = VLCNetworkServerBrowserVLCMedia(media: media)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ extension VLCFavoriteListViewController {
serverBrowser = VLCNetworkServerBrowserVLCMedia.sftpNetworkServerBrowser(withLogin: login)
}
} else {
let media = VLCMedia(url: favorite.url)
serverBrowser = VLCNetworkServerBrowserVLCMedia(media: media)
if let media = VLCMedia(url: favorite.url) {
serverBrowser = VLCNetworkServerBrowserVLCMedia(media: media)
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/Playback/Control/VLCPlaybackService.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ extern NSString *const VLCPlaybackServicePlaybackDidMoveOnToNextItem;
@class VLCMLMedia;
@class VLCPlayerDisplayController;
@class VLCPlaybackServiceAdjustFilter;
@class VLCMediaPlayerTitleDescription;
@class VLCMediaPlayerChapterDescription;

@protocol VLCPlaybackServiceDelegate <NSObject>
@optional
Expand Down Expand Up @@ -86,7 +88,9 @@ NS_SWIFT_NAME(PlaybackService)
@property (readonly) NSInteger indexOfCurrentAudioTrack;
@property (readonly) NSInteger indexOfCurrentSubtitleTrack;
@property (readonly) NSInteger indexOfCurrentTitle;
@property (readonly, nullable) VLCMediaPlayerTitleDescription *currentTitleDescription;
@property (readonly) NSInteger indexOfCurrentChapter;
@property (readonly, nullable) VLCMediaPlayerChapterDescription *currentChapterDescription;
@property (readonly) NSInteger numberOfVideoTracks;
@property (readonly) NSInteger numberOfAudioTracks;
@property (readonly) NSInteger numberOfVideoSubtitlesIndexes;
Expand Down Expand Up @@ -130,8 +134,8 @@ NS_SWIFT_NAME(PlaybackService)

- (NSString *)audioTrackNameAtIndex:(NSInteger)index;
- (NSString *)videoSubtitleNameAtIndex:(NSInteger)index;
- (NSDictionary *)titleDescriptionsDictAtIndex:(NSInteger)index;
- (NSDictionary *)chapterDescriptionsDictAtIndex:(NSInteger)index;
- (nullable VLCMediaPlayerTitleDescription *)titleDescriptionAtIndex:(NSInteger)index;
- (nullable VLCMediaPlayerChapterDescription *)chapterDescriptionAtIndex:(NSInteger)index;
- (void)selectAudioTrackAtIndex:(NSInteger)index;
- (void)selectVideoSubtitleAtIndex:(NSInteger)index;
- (void)selectTitleAtIndex:(NSInteger)index;
Expand Down
123 changes: 83 additions & 40 deletions Sources/Playback/Control/VLCPlaybackService.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ @interface VLCPlaybackService () <VLCMediaPlayerDelegate, VLCMediaDelegate, VLCM
VLCMediaListPlayer *_listPlayer;
BOOL _shouldResumePlaying;
BOOL _sessionWillRestart;
VLCMediaPlayerTrack *videoTrack;

NSString *_pathToExternalSubtitlesFile;
int _itemInMediaListToBePlayedFirst;
Expand Down Expand Up @@ -466,6 +467,7 @@ - (void)restoreAudioAndSubtitleTrack
if (media.isNew)
return;

#if LIBVLC_VERSION_MAJOR == 3
SInt64 audioIndex = media.audioTrackIndex;
NSArray *audioTrackIndexes = _mediaPlayer.audioTrackIndexes;
if (audioIndex >= 0 && audioIndex < audioTrackIndexes.count) {
Expand All @@ -482,6 +484,25 @@ - (void)restoreAudioAndSubtitleTrack
} else {
[self selectVideoSubtitleAtIndex:media.subtitleTrackIndex];
}
#else
BOOL disableSubtitles = [[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingDisableSubtitles];

NSArray *audioTracks = _mediaPlayer.audioTracks;
if (media.audioTrackIndex < audioTracks.count) {
VLCMediaPlayerTrack *track = audioTracks[media.audioTrackIndex];
track.selectedExclusively = YES;
}

if (disableSubtitles) {
[_mediaPlayer deselectAllTextTracks];
} else {
NSArray *subtitlesTracks = _mediaPlayer.textTracks;
if (media.subtitleTrackIndex < subtitlesTracks.count) {
VLCMediaPlayerTrack *track = subtitlesTracks[media.subtitleTrackIndex];
track.selectedExclusively = YES;
}
}
#endif
}
}
#endif
Expand Down Expand Up @@ -545,7 +566,7 @@ - (BOOL)currentMediaHasTrackToChooseFrom
{
/* allow track selection if there is more than 1 audio track or if there is video because even if
* there is only video, there will always be the option to download additional subtitles */
return [[_mediaPlayer audioTrackIndexes] count] > 2 || [[_mediaPlayer videoTrackIndexes] count] >= 1;
return [[_mediaPlayer audioTracks] count] > 2 || [[_mediaPlayer videoTracks] count] >= 1;
}

- (BOOL)isSeekable
Expand Down Expand Up @@ -627,44 +648,66 @@ - (void)toggleRepeatMode

- (NSInteger)indexOfCurrentAudioTrack
{
return [_mediaPlayer.audioTrackIndexes indexOfObject:@(_mediaPlayer.currentAudioTrackIndex)];
NSArray *audioTracks = [_mediaPlayer audioTracks];
for (VLCMediaPlayerTrack *track in audioTracks) {
if (track.isSelected) {
return [audioTracks indexOfObject:track];
}
}
return 0;
}

- (NSInteger)indexOfCurrentSubtitleTrack
{
return [_mediaPlayer.videoSubTitlesIndexes indexOfObject:@(_mediaPlayer.currentVideoSubTitleIndex)];
NSArray *textTracks = [_mediaPlayer textTracks];
for (VLCMediaPlayerTrack *track in textTracks) {
if (track.isSelected) {
return [textTracks indexOfObject:track];
}
}
return 0;
}

- (VLCMediaPlayerChapterDescription *)currentChapterDescription
{
return _mediaPlayer.currentChapterDescription;
}

- (NSInteger)indexOfCurrentChapter
{
return _mediaPlayer.currentChapterIndex;
}

- (VLCMediaPlayerTitleDescription *)currentTitleDescription
{
return _mediaPlayer.currentTitleDescription;
}

- (NSInteger)indexOfCurrentTitle
{
return _mediaPlayer.currentTitleIndex;
}

- (NSInteger)numberOfVideoTracks
{
return [_mediaPlayer numberOfVideoTracks];
return [[_mediaPlayer videoTracks] count];
}

- (NSInteger)numberOfAudioTracks
{
#if TARGET_OS_IOS
return [_mediaPlayer numberOfAudioTracks] + 1;
#if TARGET_OS_TV
return [_mediaPlayer audioTracks];
#else
return [_mediaPlayer numberOfAudioTracks];
return [[_mediaPlayer audioTracks] count] + 1;
#endif
}

- (NSInteger)numberOfVideoSubtitlesIndexes
{
#if TARGET_OS_IOS
return [_mediaPlayer numberOfSubtitlesTracks] + 2;
#if TARGET_OS_TV
return [[_mediaPlayer textTracks] count] + 1;
#else
return [_mediaPlayer numberOfSubtitlesTracks] + 1;
return [[_mediaPlayer textTracks] count] + 2;
#endif
}

Expand All @@ -680,59 +723,62 @@ - (NSInteger)numberOfChaptersForCurrentTitle

- (NSString *)videoSubtitleNameAtIndex:(NSInteger)index
{
NSArray *videoSubTitlesNames = _mediaPlayer.videoSubTitlesNames;
NSInteger count = videoSubTitlesNames.count;
NSArray *textTracks = [_mediaPlayer textTracks];
NSInteger count = textTracks.count;

if (index == count) {
return NSLocalizedString(@"SELECT_SUBTITLE_FROM_FILES", nil);
} else if (index < count) {
return videoSubTitlesNames[index];
VLCMediaPlayerTrack *track = textTracks[index];
return track.trackName;
}
return @"";
}

- (NSString *)audioTrackNameAtIndex:(NSInteger)index
{
NSArray *audioTrackNames = _mediaPlayer.audioTrackNames;
NSInteger count = audioTrackNames.count;
NSArray *audioTracks = _mediaPlayer.audioTracks;
NSInteger count = audioTracks.count;

if (index == count) {
return NSLocalizedString(@"SELECT_AUDIO_FROM_FILES", nil);
} else if (index < count) {
return audioTrackNames[index];
VLCMediaPlayerTrack *track = audioTracks[index];
return track.trackName;
}
return @"";
}

- (NSDictionary *)titleDescriptionsDictAtIndex:(NSInteger)index
- (VLCMediaPlayerTitleDescription *)titleDescriptionAtIndex:(NSInteger)index
{
if (index >= 0 && index < _mediaPlayer.titleDescriptions.count)
return _mediaPlayer.titleDescriptions[index];
return [NSDictionary dictionary];
return nil;
}

- (NSDictionary *)chapterDescriptionsDictAtIndex:(NSInteger)index
- (VLCMediaPlayerChapterDescription *)chapterDescriptionAtIndex:(NSInteger)index
{
NSArray *chapterDescriptions = [_mediaPlayer chapterDescriptionsOfTitle:_mediaPlayer.currentTitleIndex];
if (index >= 0 && index < chapterDescriptions.count)
return chapterDescriptions[index];
return [NSDictionary dictionary];
return nil;
}

- (void)selectAudioTrackAtIndex:(NSInteger)index
{
NSArray *audioTrackIndexes = _mediaPlayer.audioTrackIndexes;
if (index >= 0 && index < audioTrackIndexes.count) {
//we can cast this cause we won't have more than 2 million audiotracks
_mediaPlayer.currentAudioTrackIndex = [audioTrackIndexes[index] intValue];
NSArray *audioTracks = [_mediaPlayer audioTracks];
if (index >= 0 && index < audioTracks.count) {
VLCMediaPlayerTrack *track = audioTracks[index];
track.selected = YES;
}
}

- (void)selectVideoSubtitleAtIndex:(NSInteger)index
{
NSArray *videoSubTitlesIndexes = _mediaPlayer.videoSubTitlesIndexes;
if (index >= 0 && index < videoSubTitlesIndexes.count) {
_mediaPlayer.currentVideoSubTitleIndex = [videoSubTitlesIndexes[index] intValue];
NSArray *textTracks = [_mediaPlayer textTracks];
if (index >= 0 && index < [textTracks count]) {
VLCMediaPlayerTrack *track = textTracks[index];
track.selected = YES;
}
}

Expand Down Expand Up @@ -772,10 +818,8 @@ - (void)setAudioPassthrough:(BOOL)shouldPass
_mediaPlayer.audio.passthrough = shouldPass;
}

- (void)mediaPlayerStateChanged:(NSNotification *)aNotification
- (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
{
VLCMediaPlayerState currentState = _mediaPlayer.state;

switch (currentState) {
case VLCMediaPlayerStateBuffering: {
/* attach delegate */
Expand Down Expand Up @@ -828,12 +872,7 @@ - (void)mediaPlayerStateChanged:(NSNotification *)aNotification
_sessionWillRestart = NO;
[self stopPlayback];
} break;
#if LIBVLC_VERSION_MAJOR == 3
case VLCMediaPlayerStateEnded: {
#endif
#if LIBVLC_VERSION_MAJOR == 4
case VLCMediaPlayerStateStopping: {
#endif
#if TARGET_OS_IOS
[self savePlaybackState];
#endif
Expand Down Expand Up @@ -1171,7 +1210,8 @@ - (void)applyAspectRatio

- (void)setVideoTrackEnabled:(BOOL)enabled
{
if (!enabled)
// FIXME: check if this hack is still possible with v4
/* if (!enabled)
_mediaPlayer.currentVideoTrackIndex = -1;
else if (_mediaPlayer.currentVideoTrackIndex == -1) {
NSArray *videoTrackIndexes = _mediaPlayer.videoTrackIndexes;
Expand All @@ -1181,7 +1221,7 @@ - (void)setVideoTrackEnabled:(BOOL)enabled
break;
}
}
}
}*/
}

- (void)setVideoOutputView:(UIView *)videoOutputView
Expand Down Expand Up @@ -1614,7 +1654,7 @@ - (void)recoverPlaybackState
- (void)disableSubtitlesIfNeeded
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingDisableSubtitles]) {
_mediaPlayer.currentVideoSubTitleIndex = -1;
[_mediaPlayer deselectAllTextTracks];
}
}

Expand Down Expand Up @@ -1657,14 +1697,14 @@ - (void)applicationDidEnterBackground:(NSNotification *)notification
_preBackgroundWrapperView = _videoOutputViewWrapper;

#if TARGET_OS_IOS
if (!_renderer && _mediaPlayer.audioTrackIndexes.count > 0 && [_mediaPlayer isPlaying])
if (!_renderer && _mediaPlayer.audioTracks.count > 0 && [_mediaPlayer isPlaying])
[self setVideoTrackEnabled:false];

if (_renderer) {
[_backgroundDummyPlayer play];
}
#else
if (_mediaPlayer.audioTrackIndexes.count > 0 && [_mediaPlayer isPlaying])
if ([[_mediaPlayer audioTracks] count] > 0 && [_mediaPlayer isPlaying])
[self setVideoTrackEnabled:false];
#endif
}
Expand All @@ -1682,9 +1722,12 @@ - (void)applicationWillEnterForeground:(NSNotification *)notification
}
#endif

/*
// FIXME: fix this hack
if (_mediaPlayer.currentVideoTrackIndex == -1) {
[self setVideoTrackEnabled:true];
}
*/

if (_shouldResumePlaying) {
_shouldResumePlaying = NO;
Expand Down
Loading

0 comments on commit ae6fa96

Please sign in to comment.