[vlc-commits] macosx/library: generate thumbnails as needed and update view representation once done

Felix Paul Kühne git at videolan.org
Mon Apr 29 19:26:58 CEST 2019


vlc | branch: master | Felix Paul Kühne <felix at feepk.net> | Sat Apr 27 19:38:40 2019 +0200| [1680fed9b1e6da3066f4c05ec991f4370c61eef1] | committer: Felix Paul Kühne

macosx/library: generate thumbnails as needed and update view representation once done

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

 .../macosx/library/VLCLibraryCollectionViewItem.h  |  3 +
 .../macosx/library/VLCLibraryCollectionViewItem.m  | 64 ++++++++++++++++++++++
 modules/gui/macosx/library/VLCLibraryController.h  |  4 +-
 modules/gui/macosx/library/VLCLibraryController.m  | 18 +++++-
 modules/gui/macosx/library/VLCLibraryDataSource.m  | 18 +-----
 modules/gui/macosx/library/VLCLibraryModel.h       |  1 +
 modules/gui/macosx/library/VLCLibraryModel.m       | 32 ++++++++++-
 7 files changed, 118 insertions(+), 22 deletions(-)

diff --git a/modules/gui/macosx/library/VLCLibraryCollectionViewItem.h b/modules/gui/macosx/library/VLCLibraryCollectionViewItem.h
index bf79d58055..6c3cc45cab 100644
--- a/modules/gui/macosx/library/VLCLibraryCollectionViewItem.h
+++ b/modules/gui/macosx/library/VLCLibraryCollectionViewItem.h
@@ -27,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
 extern NSString *VLCLibraryCellIdentifier;
 
 @class VLCImageView;
+ at class VLCMediaLibraryMediaItem;
 
 @interface VLCLibraryCollectionViewItem : NSCollectionViewItem
 
@@ -36,6 +37,8 @@ extern NSString *VLCLibraryCellIdentifier;
 @property (readwrite, assign) IBOutlet NSButton *playInstantlyButton;
 @property (readwrite, assign) IBOutlet NSButton *addToPlaylistButton;
 
+ at property (readwrite, assign, nonatomic) VLCMediaLibraryMediaItem *representedMediaItem;
+
 - (IBAction)playInstantly:(id)sender;
 - (IBAction)addToPlaylist:(id)sender;
 
diff --git a/modules/gui/macosx/library/VLCLibraryCollectionViewItem.m b/modules/gui/macosx/library/VLCLibraryCollectionViewItem.m
index 1b100678ca..0da62b8fa7 100644
--- a/modules/gui/macosx/library/VLCLibraryCollectionViewItem.m
+++ b/modules/gui/macosx/library/VLCLibraryCollectionViewItem.m
@@ -24,6 +24,10 @@
 
 #import "main/VLCMain.h"
 #import "library/VLCLibraryController.h"
+#import "library/VLCLibraryModel.h"
+#import "library/VLCLibraryDataTypes.h"
+#import "views/VLCImageView.h"
+#import "extensions/NSString+Helpers.h"
 
 NSString *VLCLibraryCellIdentifier = @"VLCLibraryCellIdentifier";
 
@@ -35,6 +39,66 @@ NSString *VLCLibraryCellIdentifier = @"VLCLibraryCellIdentifier";
 
 @implementation VLCLibraryCollectionViewItem
 
+- (instancetype)initWithNibName:(NSNibName)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
+{
+    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
+    if (self) {
+        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaItemUpdated:) name:VLCLibraryModelMediaItemUpdated object:nil];
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+#pragma mark - view representation
+
+- (void)setRepresentedMediaItem:(VLCMediaLibraryMediaItem *)representedMediaItem
+{
+    if (!_libraryController) {
+        _libraryController = [[VLCMain sharedInstance] libraryController];
+    }
+
+    _representedMediaItem = representedMediaItem;
+    [self updateRepresentation];
+}
+
+- (void)mediaItemUpdated:(NSNotification *)aNotification
+{
+    VLCMediaLibraryMediaItem *updatedMediaItem = aNotification.object;
+    if (updatedMediaItem.libraryID == _representedMediaItem.libraryID) {
+        [self updateRepresentation];
+    }
+}
+
+- (void)updateRepresentation
+{
+    if (_representedMediaItem == nil) {
+        _mediaTitleTextField.stringValue = @"";
+        _durationTextField.stringValue = [NSString stringWithTime:0];
+        _mediaImageView.image = [NSImage imageNamed: @"noart.png"];
+        return;
+    }
+
+    _mediaTitleTextField.stringValue = _representedMediaItem.title;
+    _durationTextField.stringValue = [NSString stringWithTime:_representedMediaItem.duration / 1000];
+
+    NSImage *image;
+    if (_representedMediaItem.artworkGenerated) {
+        image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:_representedMediaItem.artworkMRL]];
+    } else {
+        [_libraryController attemptToGenerateThumbnailForMediaItem:_representedMediaItem];
+    }
+    if (!image) {
+        image = [NSImage imageNamed: @"noart.png"];
+    }
+    _mediaImageView.image = image;
+}
+
+#pragma mark - actions
+
 - (IBAction)playInstantly:(id)sender
 {
     if (!_libraryController) {
diff --git a/modules/gui/macosx/library/VLCLibraryController.h b/modules/gui/macosx/library/VLCLibraryController.h
index ad2b45adfc..a3fada8079 100644
--- a/modules/gui/macosx/library/VLCLibraryController.h
+++ b/modules/gui/macosx/library/VLCLibraryController.h
@@ -23,6 +23,7 @@
 #import <Foundation/Foundation.h>
 
 @class VLCLibraryModel;
+ at class VLCMediaLibraryMediaItem;
 
 NS_ASSUME_NONNULL_BEGIN
 
@@ -30,8 +31,9 @@ NS_ASSUME_NONNULL_BEGIN
 
 @property (readonly) VLCLibraryModel *libraryModel;
 
-- (void)appendItemAtIndexPathToPlaylist:(NSIndexPath *)indexPath playImmediately:(BOOL)playImmediately;
+- (int)appendItemAtIndexPathToPlaylist:(NSIndexPath *)indexPath playImmediately:(BOOL)playImmediately;
 - (void)showItemAtIndexPathInFinder:(NSIndexPath *)indexPath;
+- (int)attemptToGenerateThumbnailForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem;
 
 @end
 
diff --git a/modules/gui/macosx/library/VLCLibraryController.m b/modules/gui/macosx/library/VLCLibraryController.m
index 891c64257b..01ed5f42ab 100644
--- a/modules/gui/macosx/library/VLCLibraryController.m
+++ b/modules/gui/macosx/library/VLCLibraryController.m
@@ -58,6 +58,9 @@
                                       selector:@selector(playbackStateChanged:)
                                           name:VLCPlayerStateChanged
                                         object:nil];
+        dispatch_async(dispatch_get_main_queue(), ^{
+            [self lazyLoad];
+        });
     }
     return self;
 }
@@ -68,6 +71,11 @@
     _p_libraryInstance = NULL;
 }
 
+- (void)lazyLoad
+{
+    [self applicationWillEnterBackground:nil];
+}
+
 - (void)applicationWillEnterBackground:(NSNotification *)aNotification
 {
     vlc_ml_resume_background(_p_libraryInstance);
@@ -88,12 +96,13 @@
     }
 }
 
-- (void)appendItemAtIndexPathToPlaylist:(NSIndexPath *)indexPath playImmediately:(BOOL)playImmediately
+- (int)appendItemAtIndexPathToPlaylist:(NSIndexPath *)indexPath playImmediately:(BOOL)playImmediately
 {
     VLCMediaLibraryMediaItem *mediaItem = [self.libraryModel mediaItemAtIndexPath:indexPath];
     input_item_t *p_inputItem = vlc_ml_get_input_item(_p_libraryInstance, mediaItem.libraryID);
-    [[[VLCMain sharedInstance] playlistController] addInputItem:p_inputItem atPosition:-1 startPlayback:playImmediately];
+    int ret = [[[VLCMain sharedInstance] playlistController] addInputItem:p_inputItem atPosition:-1 startPlayback:playImmediately];
     input_item_Release(p_inputItem);
+    return ret;
 }
 
 - (void)showItemAtIndexPathInFinder:(NSIndexPath *)indexPath
@@ -112,4 +121,9 @@
     }
 }
 
+- (int)attemptToGenerateThumbnailForMediaItem:(VLCMediaLibraryMediaItem *)mediaItem
+{
+    return vlc_ml_media_generate_thumbnail(_p_libraryInstance, mediaItem.libraryID);
+}
+
 @end
diff --git a/modules/gui/macosx/library/VLCLibraryDataSource.m b/modules/gui/macosx/library/VLCLibraryDataSource.m
index d5629aca7b..ae49c686da 100644
--- a/modules/gui/macosx/library/VLCLibraryDataSource.m
+++ b/modules/gui/macosx/library/VLCLibraryDataSource.m
@@ -24,10 +24,6 @@
 
 #import "library/VLCLibraryCollectionViewItem.h"
 #import "library/VLCLibraryModel.h"
-#import "library/VLCLibraryDataTypes.h"
-
-#import "views/VLCImageView.h"
-#import "extensions/NSString+Helpers.h"
 
 @implementation VLCLibraryDataSource
 
@@ -69,19 +65,7 @@
             break;
     }
 
-    VLCMediaLibraryMediaItem *mediaItem = mediaArray[indexPath.item];
-
-    viewItem.mediaTitleTextField.stringValue = mediaItem.title;
-    viewItem.durationTextField.stringValue = [NSString stringWithTime:mediaItem.duration / 1000];
-
-    NSImage *image;
-    if (mediaItem.artworkGenerated) {
-        image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:mediaItem.artworkMRL]];
-    }
-    if (!image) {
-        image = [NSImage imageNamed: @"noart.png"];
-    }
-    viewItem.mediaImageView.image = image;
+    viewItem.representedMediaItem = mediaArray[indexPath.item];
 
     return viewItem;
 }
diff --git a/modules/gui/macosx/library/VLCLibraryModel.h b/modules/gui/macosx/library/VLCLibraryModel.h
index 555147a578..7ff90a31a2 100644
--- a/modules/gui/macosx/library/VLCLibraryModel.h
+++ b/modules/gui/macosx/library/VLCLibraryModel.h
@@ -35,6 +35,7 @@ typedef NS_ENUM(NSInteger, VLCLibraryMode) {
 
 extern NSString *VLCLibraryModelAudioMediaListUpdated;
 extern NSString *VLCLibraryModelVideoMediaListUpdated;
+extern NSString *VLCLibraryModelMediaItemUpdated;
 
 @interface VLCLibraryModel : NSObject
 
diff --git a/modules/gui/macosx/library/VLCLibraryModel.m b/modules/gui/macosx/library/VLCLibraryModel.m
index e6a8639f47..a21d38c3d4 100644
--- a/modules/gui/macosx/library/VLCLibraryModel.m
+++ b/modules/gui/macosx/library/VLCLibraryModel.m
@@ -26,6 +26,7 @@
 
 NSString *VLCLibraryModelAudioMediaListUpdated = @"VLCLibraryModelAudioMediaListUpdated";
 NSString *VLCLibraryModelVideoMediaListUpdated = @"VLCLibraryModelVideoMediaListUpdated";
+NSString *VLCLibraryModelMediaItemUpdated = @"VLCLibraryModelMediaItemUpdated";
 
 @interface VLCLibraryModel ()
 {
@@ -39,6 +40,7 @@ NSString *VLCLibraryModelVideoMediaListUpdated = @"VLCLibraryModelVideoMediaList
 
 - (void)updateCachedListOfAudioMedia;
 - (void)updateCachedListOfVideoMedia;
+- (void)mediaItemWasUpdated:(VLCMediaLibraryMediaItem *)mediaItem;
 
 @end
 
@@ -66,6 +68,15 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
 
             });
             break;
+        case VLC_ML_EVENT_MEDIA_THUMBNAIL_GENERATED:
+            if (p_event->media_thumbnail_generated.b_success) {
+                VLCMediaLibraryMediaItem *mediaItem = [[VLCMediaLibraryMediaItem alloc] initWithMediaItem:(struct vlc_ml_media_t *)p_event->media_thumbnail_generated.p_media];
+                dispatch_async(dispatch_get_main_queue(), ^{
+                    VLCLibraryModel *libraryModel = (__bridge VLCLibraryModel *)p_data;
+                    [libraryModel mediaItemWasUpdated:mediaItem];
+                });
+            }
+            break;
         default:
             break;
     }
@@ -79,18 +90,32 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
     if (self) {
         _p_mediaLibrary = library;
         _p_eventCallback = vlc_ml_event_register_callback(_p_mediaLibrary, libraryCallback, (__bridge void *)self);
-        _defaultNotificationCenter = [[NSNotificationCenter alloc] init];
+        _defaultNotificationCenter = [NSNotificationCenter defaultCenter];
+        [_defaultNotificationCenter addObserver:self
+                                       selector:@selector(applicationWillTerminate:)
+                                           name:NSApplicationWillTerminateNotification
+                                         object:nil];
     }
     return self;
 }
 
-- (void)dealloc
+- (void)applicationWillTerminate:(NSNotification *)aNotification
 {
     if (_p_eventCallback) {
         vlc_ml_event_unregister_callback(_p_mediaLibrary, _p_eventCallback);
     }
 }
 
+- (void)dealloc
+{
+    [_defaultNotificationCenter removeObserver:self];
+}
+
+- (void)mediaItemWasUpdated:(VLCMediaLibraryMediaItem *)mediaItem
+{
+    [_defaultNotificationCenter postNotificationName:VLCLibraryModelMediaItemUpdated object:mediaItem];
+}
+
 - (size_t)numberOfAudioMedia
 {
     if (_cachedAudioMedia) {
@@ -134,6 +159,9 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
 - (void)updateCachedListOfVideoMedia
 {
     vlc_ml_media_list_t *p_media_list = vlc_ml_list_video_media(_p_mediaLibrary, NULL);
+    if (p_media_list == NULL) {
+        return;
+    }
     NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:p_media_list->i_nb_items];
     for (size_t x = 0; x < p_media_list->i_nb_items; x++) {
         VLCMediaLibraryMediaItem *mediaItem = [[VLCMediaLibraryMediaItem alloc] initWithMediaItem:&p_media_list->p_items[x]];



More information about the vlc-commits mailing list