1 /*****************************************************************************
2 r playlistinfo.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include "playlistinfo.h"
34 /*****************************************************************************
35 * VLCPlaylistInfo Implementation
36 *****************************************************************************/
38 @implementation VLCInfo
40 static VLCInfo *_o_sharedInstance = nil;
42 + (VLCInfo *)sharedInstance
44 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
49 if( _o_sharedInstance )
55 _o_sharedInstance = [super init];
57 if( _o_sharedInstance != nil )
60 o_statUpdateTimer = nil;
61 [self updatePanelWithItem: NULL];
62 rootItem = [[VLCInfoTreeItem alloc] init];
66 return _o_sharedInstance;
71 [o_info_window setExcludedFromWindowsMenu: YES];
72 [o_info_window setFloatingPanel: NO];
74 [o_info_window setTitle: _NS("Media Information")];
75 [o_uri_lbl setStringValue: _NS("Location")];
76 [o_title_lbl setStringValue: _NS("Title")];
77 [o_author_lbl setStringValue: _NS("Artist")];
78 [o_saveMetaData_btn setStringValue: _NS("Save Metadata" )];
80 [[o_tab_view tabViewItemAtIndex: 0] setLabel: _NS("General")];
81 [[o_tab_view tabViewItemAtIndex: 1] setLabel: _NS("Codec Details")];
82 [[o_tab_view tabViewItemAtIndex: 2] setLabel: _NS("Statistics")];
83 [o_tab_view selectTabViewItemAtIndex: 0];
85 /* constants defined in vlc_meta.h */
86 [o_genre_lbl setStringValue: _NS(VLC_META_GENRE)];
87 [o_copyright_lbl setStringValue: _NS(VLC_META_COPYRIGHT)];
88 [o_collection_lbl setStringValue: _NS(VLC_META_ALBUM)];
89 [o_seqNum_lbl setStringValue: _NS(VLC_META_TRACK_NUMBER)];
90 [o_description_lbl setStringValue: _NS(VLC_META_DESCRIPTION)];
91 [o_date_lbl setStringValue: _NS(VLC_META_DATE)];
92 [o_language_lbl setStringValue: _NS(VLC_META_LANGUAGE)];
93 [o_nowPlaying_lbl setStringValue: _NS(VLC_META_NOW_PLAYING)];
94 [o_publisher_lbl setStringValue: _NS(VLC_META_PUBLISHER)];
97 [o_input_box setTitle: _NS("Input")];
98 [o_read_bytes_lbl setStringValue: _NS("Read at media")];
99 [o_input_bitrate_lbl setStringValue: _NS("Input bitrate")];
100 [o_demux_bytes_lbl setStringValue: _NS("Demuxed")];
101 [o_demux_bitrate_lbl setStringValue: _NS("Stream bitrate")];
103 [o_video_box setTitle: _NS("Video")];
104 [o_video_decoded_lbl setStringValue: _NS("Decoded blocks")];
105 [o_displayed_lbl setStringValue: _NS("Displayed frames")];
106 [o_lost_frames_lbl setStringValue: _NS("Lost frames")];
107 [o_fps_lbl setStringValue: _NS("Frames per Second")];
109 [o_sout_box setTitle: _NS("Streaming")];
110 [o_sent_packets_lbl setStringValue: _NS("Sent packets")];
111 [o_sent_bytes_lbl setStringValue: _NS("Sent bytes")];
112 [o_sent_bitrate_lbl setStringValue: _NS("Send rate")];
114 [o_audio_box setTitle: _NS("Audio")];
115 [o_audio_decoded_lbl setStringValue: _NS("Decoded blocks")];
116 [o_played_abuffers_lbl setStringValue: _NS("Played buffers")];
117 [o_lost_abuffers_lbl setStringValue: _NS("Lost buffers")];
119 [o_info_window setInitialFirstResponder: o_uri_txt];
120 [o_info_window setDelegate: self];
122 /* We may be awoken from nib way after initialisation
123 * Update ourselves */
124 [self updatePanelWithItem:p_item];
129 /* make sure that the timer is released in any case */
130 if( o_statUpdateTimer && [o_statUpdateTimer isValid] )
131 [o_statUpdateTimer invalidate];
135 [o_statUpdateTimer release];
136 o_statUpdateTimer = nil;
141 /* make sure that the timer is released in any case */
142 if( o_statUpdateTimer && [o_statUpdateTimer isValid] )
143 [o_statUpdateTimer invalidate];
145 [o_statUpdateTimer release];
147 if( p_item ) vlc_gc_decref( p_item );
152 - (void)windowDidBecomeKey:(NSNotification *)notification
154 BOOL b_stats = config_GetInt(VLCIntf, "stats");
157 if( o_statUpdateTimer )
159 [o_statUpdateTimer invalidate];
160 [o_statUpdateTimer release];
161 o_statUpdateTimer = nil;
163 o_statUpdateTimer = [NSTimer scheduledTimerWithTimeInterval: 1
164 target: self selector: @selector(updateStatistics:)
165 userInfo: nil repeats: YES];
166 [o_statUpdateTimer fire];
167 [o_statUpdateTimer retain];
171 - (BOOL)windowShouldClose:(id)sender
173 if( [o_statUpdateTimer isValid] )
174 [o_statUpdateTimer invalidate];
176 if( o_statUpdateTimer )
177 [o_statUpdateTimer release];
178 o_statUpdateTimer = nil;
184 BOOL b_stats = config_GetInt(VLCIntf, "stats");
187 if( [o_tab_view numberOfTabViewItems] > 2 )
188 [o_tab_view removeTabViewItem: [o_tab_view tabViewItemAtIndex: 2]];
191 [self initMediaPanelStats];
192 [o_info_window makeKeyAndOrderFront: self];
195 - (void)initMediaPanelStats
197 //Initializing Input Variables
198 [o_read_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
199 [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
200 [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
201 [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
203 //Initializing Video Variables
204 [o_video_decoded_txt setIntValue:0];
205 [o_displayed_txt setIntValue:0];
206 [o_lost_frames_txt setIntValue:0];
207 [o_fps_txt setFloatValue:0];
209 //Initializing Output Variables
210 [o_sent_packets_txt setIntValue: 0];
211 [o_sent_bytes_txt setStringValue: [NSString stringWithFormat:@"%8.0f KiB", (float)0]];
212 [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:@"%6.0f kb/s", (float)0]];
214 //Initializing Audio Variables
215 [o_audio_decoded_txt setIntValue:0];
216 [o_played_abuffers_txt setIntValue: 0];
217 [o_lost_abuffers_txt setIntValue: 0];
221 - (void)updatePanelWithItem:(input_item_t *)_p_item;
223 if( _p_item != p_item )
225 if( p_item ) vlc_gc_decref( p_item );
226 [o_saveMetaData_btn setEnabled: NO];
227 if( _p_item ) vlc_gc_incref( _p_item );
235 [self setMeta: "" forLabel: o_##foo##_txt];
249 [o_image_well setImage: [NSImage imageNamed: @"noart.png"]];
253 if( !input_item_IsPreparsed( p_item ) )
255 playlist_PreparseEnqueue( pl_Get( VLCIntf ), p_item );
259 char * psz_url = input_item_GetURI( p_item );
260 [o_uri_txt setStringValue: [NSString stringWithUTF8String: psz_url ? psz_url : "" ]];
263 /* fill title info */
264 char * psz_title = input_item_GetTitle( p_item );
266 psz_title = input_item_GetName( p_item );
267 [o_title_txt setStringValue: [NSString stringWithUTF8String: psz_title ? : "" ]];
270 #define SET( foo, bar ) \
271 char *psz_##foo = input_item_Get##bar ( p_item ); \
272 [self setMeta: psz_##foo forLabel: o_##foo##_txt]; \
273 FREENULL( psz_##foo );
275 /* fill the other fields */
276 SET( author, Artist );
277 SET( collection, Album );
278 SET( seqNum, TrackNum );
280 SET( copyright, Copyright );
281 SET( publisher, Publisher );
282 SET( nowPlaying, NowPlaying );
283 SET( language, Language );
285 SET( description, Description );
291 psz_meta = input_item_GetArtURL( p_item );
293 /* FIXME Can also be attachment:// */
294 if( psz_meta && strncmp( psz_meta, "attachment://", 13 ) )
295 o_image = [[NSImage alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithUTF8String: psz_meta]]];
297 o_image = [[NSImage imageNamed: @"noart.png"] retain];
298 [o_image_well setImage: o_image];
300 FREENULL( psz_meta );
303 /* reload the advanced table */
305 [o_outline_view reloadData];
307 /* update the stats once to display p_item change faster */
308 [self updateStatistics: nil];
311 - (void)setMeta: (char *)psz_meta forLabel: (id)theItem
313 if( psz_meta != NULL && *psz_meta)
314 [theItem setStringValue: [NSString stringWithUTF8String:psz_meta]];
316 [theItem setStringValue: @""];
319 - (void)updateStatistics:(NSTimer*)theTimer
321 if( !p_item || !p_item->p_stats )
323 [self initMediaPanelStats];
327 vlc_mutex_lock( &p_item->p_stats->lock );
330 [o_read_bytes_txt setStringValue: [NSString stringWithFormat:
331 @"%8.0f KiB", (float)(p_item->p_stats->i_read_bytes)/1024]];
332 [o_input_bitrate_txt setStringValue: [NSString stringWithFormat:
333 @"%6.0f kb/s", (float)(p_item->p_stats->f_input_bitrate)*8000]];
334 [o_demux_bytes_txt setStringValue: [NSString stringWithFormat:
335 @"%8.0f KiB", (float)(p_item->p_stats->i_demux_read_bytes)/1024]];
336 [o_demux_bitrate_txt setStringValue: [NSString stringWithFormat:
337 @"%6.0f kb/s", (float)(p_item->p_stats->f_demux_bitrate)*8000]];
340 [o_video_decoded_txt setIntValue: p_item->p_stats->i_decoded_video];
341 [o_displayed_txt setIntValue: p_item->p_stats->i_displayed_pictures];
342 [o_lost_frames_txt setIntValue: p_item->p_stats->i_lost_pictures];
344 /* FIXME: input_Control( p_item, INPUT_GET_VIDEO_FPS, &f_fps ); */
345 [o_fps_txt setFloatValue: f_fps];
348 [o_sent_packets_txt setIntValue: p_item->p_stats->i_sent_packets];
349 [o_sent_bytes_txt setStringValue: [NSString stringWithFormat: @"%8.0f KiB",
350 (float)(p_item->p_stats->i_sent_bytes)/1024]];
351 [o_sent_bitrate_txt setStringValue: [NSString stringWithFormat:
352 @"%6.0f kb/s", (float)(p_item->p_stats->f_send_bitrate*8)*1000]];
355 [o_audio_decoded_txt setIntValue: p_item->p_stats->i_decoded_audio];
356 [o_played_abuffers_txt setIntValue: p_item->p_stats->i_played_abuffers];
357 [o_lost_abuffers_txt setIntValue: p_item->p_stats->i_lost_abuffers];
359 vlc_mutex_unlock( &p_item->p_stats->lock );
362 - (IBAction)metaFieldChanged:(id)sender
364 [o_saveMetaData_btn setEnabled: YES];
367 - (IBAction)saveMetaData:(id)sender
369 if( !p_item ) goto error;
371 #define utf8( o_blub ) \
372 [[o_blub stringValue] UTF8String]
374 input_item_SetName( p_item, utf8( o_title_txt ) );
375 input_item_SetTitle( p_item, utf8( o_title_txt ) );
376 input_item_SetArtist( p_item, utf8( o_author_txt ) );
377 input_item_SetAlbum( p_item, utf8( o_collection_txt ) );
378 input_item_SetGenre( p_item, utf8( o_genre_txt ) );
379 input_item_SetTrackNum( p_item, utf8( o_seqNum_txt ) );
380 input_item_SetDate( p_item, utf8( o_date_txt ) );
381 input_item_SetCopyright( p_item, utf8( o_copyright_txt ) );
382 input_item_SetPublisher( p_item, utf8( o_publisher_txt ) );
383 input_item_SetDescription( p_item, utf8( o_description_txt ) );
384 input_item_SetLanguage( p_item, utf8( o_language_txt ) );
386 playlist_t * p_playlist = pl_Get( VLCIntf );
387 input_item_WriteMeta( VLC_OBJECT(p_playlist), p_item );
389 var_SetBool( p_playlist, "intf-change", true );
390 [self updatePanelWithItem: p_item];
392 [o_saveMetaData_btn setEnabled: NO];
396 NSRunAlertPanel(_NS("Error while saving meta"),
397 _NS("VLC was unable to save the meta data."),
398 _NS("OK"), nil, nil);
401 - (IBAction)downloadCoverArt:(id)sender
403 playlist_t * p_playlist = pl_Get( VLCIntf );
404 if( p_item) playlist_AskForArtEnqueue( p_playlist, p_item );
407 - (input_item_t *)item
409 if( p_item ) vlc_gc_incref( p_item );
415 @implementation VLCInfo (NSMenuValidation)
417 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
419 BOOL bEnabled = TRUE;
421 if( [[o_mi title] isEqualToString: _NS("Information")] )
423 return ![[[VLCMain sharedInstance] playlist] isSelectionEmpty];
431 @implementation VLCInfo (NSTableDataSource)
433 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
435 return (item == nil) ? [rootItem numberOfChildren] : [item numberOfChildren];
438 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
439 return ([item numberOfChildren] > 0);
442 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
444 return (item == nil) ? [rootItem childAtIndex:index] : (id)[item childAtIndex:index];
447 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
449 if ([[tableColumn identifier] isEqualToString:@"0"])
451 return (item == nil) ? @"" : (id)[item name];
455 return (item == nil) ? @"" : (id)[item value];
461 @implementation VLCInfoTreeItem
463 #define IsALeafNode ((id)-1)
465 - (id)initWithName: (NSString *)o_item_name value: (NSString *)o_item_value ID: (int)i_id
466 parent:(VLCInfoTreeItem *)o_parent_item
472 o_name = [o_item_name copy];
473 o_value = [o_item_value copy];
475 o_parent = o_parent_item;
476 p_item = [[[VLCMain sharedInstance] info] item];
484 return [self initWithName:@"main" value:@"" ID:-1 parent:nil];
489 if( o_children != IsALeafNode ) [o_children release];
492 if( p_item ) vlc_gc_decref( p_item );
496 /* Creates and returns the array of children
497 * Loads children incrementally */
498 - (NSArray *)children
500 if( !p_item ) return nil;
502 if (o_children == NULL)
506 if( i_object_id == -1 )
508 vlc_mutex_lock( &p_item->lock );
509 o_children = [[NSMutableArray alloc] initWithCapacity: p_item->i_categories];
510 for (i = 0 ; i < p_item->i_categories ; i++)
512 NSString * name = [NSString stringWithUTF8String: p_item->pp_categories[i]->psz_name];
513 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:@"" ID:i parent:self];
515 [o_children addObject:item];
517 vlc_mutex_unlock( &p_item->lock );
519 else if( o_parent->i_object_id == -1 )
521 vlc_mutex_lock( &p_item->lock );
522 info_category_t * cat = p_item->pp_categories[i_object_id];
523 o_children = [[NSMutableArray alloc] initWithCapacity: cat->i_infos];
524 for (i = 0 ; i < cat->i_infos ; i++)
526 NSString * name = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_name];
527 NSString * value = [NSString stringWithUTF8String: cat->pp_infos[i]->psz_value ? : ""];
528 VLCInfoTreeItem * item = [[VLCInfoTreeItem alloc] initWithName:name value:value ID:i parent:self];
530 [o_children addObject:item];
532 vlc_mutex_unlock( &p_item->lock );
536 o_children = IsALeafNode;
544 return [[o_name retain] autorelease];
549 return [[o_value retain] autorelease];
554 input_item_t * oldItem = p_item;
555 p_item = [[[VLCMain sharedInstance] info] item];
556 if( oldItem && oldItem != p_item ) vlc_gc_decref( oldItem );
558 [o_children release];
562 - (VLCInfoTreeItem *)childAtIndex:(NSUInteger)i_index {
563 return [[self children] objectAtIndex:i_index];
566 - (int)numberOfChildren {
568 id i_tmp = [self children];
569 return ( i_tmp == IsALeafNode ) ? (-1) : (int)[i_tmp count];