[vlc-commits] macosx: iterate on internal tracks API

Felix Paul Kühne git at videolan.org
Sat Mar 30 22:05:17 CET 2019


vlc | branch: master | Felix Paul Kühne <felix at feepk.net> | Sat Mar 30 21:54:33 2019 +0100| [21e28d61ffa7a0d55d6ae66dba66ceefff885433] | committer: Felix Paul Kühne

macosx: iterate on internal tracks API

This way, it is simpler and can no longer break in case the tracks listing changes while iterating

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=21e28d61ffa7a0d55d6ae66dba66ceefff885433
---

 modules/gui/macosx/menus/VLCMainMenu.m            | 21 ++-----
 modules/gui/macosx/playlist/VLCPlayerController.h | 33 +++-------
 modules/gui/macosx/playlist/VLCPlayerController.m | 73 ++++++++---------------
 3 files changed, 41 insertions(+), 86 deletions(-)

diff --git a/modules/gui/macosx/menus/VLCMainMenu.m b/modules/gui/macosx/menus/VLCMainMenu.m
index 8c6d848b0c..16b632add8 100644
--- a/modules/gui/macosx/menus/VLCMainMenu.m
+++ b/modules/gui/macosx/menus/VLCMainMenu.m
@@ -867,27 +867,18 @@
 #pragma mark - track handling
 - (void)updateTrackHandlingMenus:(NSNotification *)aNotification
 {
-    size_t numberOfTracks = _playerController.numberOfAudioTracks;
-    NSMutableArray *tracks = [[NSMutableArray alloc] initWithCapacity:numberOfTracks];
-    for (size_t x = 0; x < numberOfTracks; x++) {
-        [tracks addObject:[_playerController audioTrackAtIndex:x]];
-    }
+    NSArray *tracks = _playerController.audioTracks;
+    NSUInteger numberOfTracks = tracks.count;
     [self rebuildTracksMenu:_audiotrackMenu withMetadata:tracks count:numberOfTracks category:AUDIO_ES];
     _audiotrack.enabled = numberOfTracks > 0 ? YES : NO;
 
-    numberOfTracks = _playerController.numberOfVideoTracks;
-    tracks = [[NSMutableArray alloc] initWithCapacity:numberOfTracks];
-    for (size_t x = 0; x < numberOfTracks; x++) {
-        [tracks addObject:[_playerController videoTrackAtIndex:x]];
-    }
+    tracks = _playerController.videoTracks;
+    numberOfTracks = tracks.count;
     [self rebuildTracksMenu:_videotrackMenu withMetadata:tracks count:numberOfTracks category:VIDEO_ES];
     _videotrack.enabled = numberOfTracks > 0 ? YES : NO;
 
-    numberOfTracks = _playerController.numberOfSubtitleTracks;
-    tracks = [[NSMutableArray alloc] initWithCapacity:numberOfTracks];
-    for (size_t x = 0; x < numberOfTracks; x++) {
-        [tracks addObject:[_playerController subtitleTrackAtIndex:x]];
-    }
+    tracks = _playerController.subtitleTracks;
+    numberOfTracks = tracks.count;
     [self rebuildTracksMenu:_subtitle_tracksMenu withMetadata:tracks count:numberOfTracks category:SPU_ES];
     _subtitle_track.enabled = numberOfTracks > 0 ? YES : NO;
 }
diff --git a/modules/gui/macosx/playlist/VLCPlayerController.h b/modules/gui/macosx/playlist/VLCPlayerController.h
index 59760defad..b50c08f72e 100644
--- a/modules/gui/macosx/playlist/VLCPlayerController.h
+++ b/modules/gui/macosx/playlist/VLCPlayerController.h
@@ -640,37 +640,22 @@ extern NSString *VLCPlayerMuteChanged;
 - (void)selectNextTrackForCategory:(enum es_format_category_e)category;
 
 /**
- * the number of audio tracks
- * @note listen to VLCPlayerTrackListChanged to be notified if this value changes
+ * an array holding instances of VLCTrackMetaData describing the available audio tracks for the current media
+ * @note listen to VLCPlayerTrackListChanged to be notified of changes to the list
  */
- at property (readonly) size_t numberOfAudioTracks;
+ at property (readonly, nullable) NSArray<VLCTrackMetaData *>* audioTracks;
 
 /**
- * get a metadata copy of a certain audio track at @param index
+ * an array holding instances of VLCTrackMetaData describing the available video tracks for the current media
+ * @note listen to VLCPlayerTrackListChanged to be notified of changes to the list
  */
-- (VLCTrackMetaData *)audioTrackAtIndex:(size_t)index;
+ at property (readonly, nullable) NSArray<VLCTrackMetaData *>* videoTracks;
 
 /**
- * the number of video tracks
- * @note listen to VLCPlayerTrackListChanged to be notified if this value changes
+ * an array holding instances of VLCTrackMetaData describing the available subtitle tracks for the current media
+ * @note listen to VLCPlayerTrackListChanged to be notified of changes to the list
  */
- at property (readonly) size_t numberOfVideoTracks;
-
-/**
- * get a metadata copy of a certain video track at @param index
- */
-- (VLCTrackMetaData *)videoTrackAtIndex:(size_t)index;
-
-/**
- * the number of subtitle tracks
- * @note listen to VLCPlayerTrackListChanged to be notified if this value changes
- */
- at property (readonly) size_t numberOfSubtitleTracks;
-
-/**
- * get a metadata copy of a certain subtitle track at @param index
- */
-- (VLCTrackMetaData *)subtitleTrackAtIndex:(size_t)index;
+ at property (readonly, nullable) NSArray<VLCTrackMetaData *>* subtitleTracks;
 
 #pragma mark - video output properties
 
diff --git a/modules/gui/macosx/playlist/VLCPlayerController.m b/modules/gui/macosx/playlist/VLCPlayerController.m
index 886b4a04b2..a4308243a7 100644
--- a/modules/gui/macosx/playlist/VLCPlayerController.m
+++ b/modules/gui/macosx/playlist/VLCPlayerController.m
@@ -1302,66 +1302,45 @@ static const struct vlc_player_aout_cbs player_aout_callbacks = {
     vlc_player_Unlock(_p_player);
 }
 
-- (size_t)numberOfAudioTracks
+- (NSArray<VLCTrackMetaData *> *)tracksForCategory:(enum es_format_category_e)category
 {
-    size_t ret = 0;
-    vlc_player_Lock(_p_player);
-    ret = vlc_player_GetTrackCount(_p_player, AUDIO_ES);
-    vlc_player_Unlock(_p_player);
-    return ret;
-}
-- (VLCTrackMetaData *)audioTrackAtIndex:(size_t)index
-{
-    VLCTrackMetaData *trackMetadata = [[VLCTrackMetaData alloc] init];
-    vlc_player_Lock(_p_player);
-    const struct vlc_player_track *track = vlc_player_GetTrackAt(_p_player, AUDIO_ES, index);
-    trackMetadata.esID = track->es_id;
-    trackMetadata.name = toNSStr(track->name);
-    trackMetadata.selected = track->selected;
-    vlc_player_Unlock(_p_player);
-    return trackMetadata;
-}
+    size_t numberOfTracks = 0;
+    NSMutableArray *tracks;
 
-- (size_t)numberOfVideoTracks
-{
-    size_t ret = 0;
     vlc_player_Lock(_p_player);
-    ret = vlc_player_GetTrackCount(_p_player, VIDEO_ES);
+    numberOfTracks = vlc_player_GetTrackCount(_p_player, category);
+    if (numberOfTracks == 0) {
+        vlc_player_Unlock(_p_player);
+        return nil;
+    }
+
+    tracks = [[NSMutableArray alloc] initWithCapacity:numberOfTracks];
+    for (size_t x = 0; x < numberOfTracks; x++) {
+        VLCTrackMetaData *trackMetadata = [[VLCTrackMetaData alloc] init];
+        const struct vlc_player_track *track = vlc_player_GetTrackAt(_p_player, category, x);
+        trackMetadata.esID = track->es_id;
+        trackMetadata.name = toNSStr(track->name);
+        trackMetadata.selected = track->selected;
+        [tracks addObject:trackMetadata];
+    }
     vlc_player_Unlock(_p_player);
-    return ret;
+
+    return [tracks copy];
 }
 
-- (VLCTrackMetaData *)videoTrackAtIndex:(size_t)index
+- (NSArray<VLCTrackMetaData *> *)audioTracks
 {
-    VLCTrackMetaData *trackMetadata = [[VLCTrackMetaData alloc] init];
-    vlc_player_Lock(_p_player);
-    const struct vlc_player_track *track = vlc_player_GetTrackAt(_p_player, VIDEO_ES, index);
-    trackMetadata.esID = track->es_id;
-    trackMetadata.name = toNSStr(track->name);
-    trackMetadata.selected = track->selected;
-    vlc_player_Unlock(_p_player);
-    return trackMetadata;
+    return [self tracksForCategory:AUDIO_ES];
 }
 
-- (size_t)numberOfSubtitleTracks
+- (NSArray<VLCTrackMetaData *> *)videoTracks
 {
-    size_t ret = 0;
-    vlc_player_Lock(_p_player);
-    ret = vlc_player_GetTrackCount(_p_player, SPU_ES);
-    vlc_player_Unlock(_p_player);
-    return ret;
+    return [self tracksForCategory:VIDEO_ES];
 }
 
-- (VLCTrackMetaData *)subtitleTrackAtIndex:(size_t)index
+- (NSArray<VLCTrackMetaData *> *)subtitleTracks
 {
-    VLCTrackMetaData *trackMetadata = [[VLCTrackMetaData alloc] init];
-    vlc_player_Lock(_p_player);
-    const struct vlc_player_track *track = vlc_player_GetTrackAt(_p_player, SPU_ES, index);
-    trackMetadata.esID = track->es_id;
-    trackMetadata.name = toNSStr(track->name);
-    trackMetadata.selected = track->selected;
-    vlc_player_Unlock(_p_player);
-    return trackMetadata;
+    return [self tracksForCategory:SPU_ES];
 }
 
 - (void)ABLoopStateChanged:(enum vlc_player_abloop)abLoopState



More information about the vlc-commits mailing list