]> git.sesse.net Git - vlc/blob - extras/MacOSX/Framework/Sources/VLCVideoView.m
extra/MacOSX/Framework: Initial import of the Mac OS X Framework.
[vlc] / extras / MacOSX / Framework / Sources / VLCVideoView.m
1 /*****************************************************************************
2  * VLCVideoView.h: VLC.framework VLCVideoView implementation
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #import <VLC/VLCVideoView.h>
26 #import "VLCLibrary.h"
27 #import "VLCEventManager.h"
28
29 /* Libvlc */
30 #include <vlc/vlc.h>
31 #include <vlc/libvlc.h>
32
33 /* Notification */
34 NSString * VLCVideoDidChangeVolume                  = @"VLCVideoDidChangeVolume";
35 NSString * VLCVideoDidChangeTime                    = @"VLCVideoDidChangeTime";
36 NSString * VLCVideoDidChangeCurrentlyPlayingItem    = @"VLCVideoDidChangeCurrentlyPlayingItem";
37 NSString * VLCVideoDidPause                         = @"VLCVideoDidPause";
38 NSString * VLCVideoDidPlay                          = @"VLCVideoDidPlay";
39 NSString * VLCVideoDidStop                          = @"VLCVideoDidStop";
40
41 static void HandleMediaListPlayerCurrentPlayingItemChanged( const libvlc_event_t * event, void * self )
42 {
43     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
44                                      withDelegateMethod: @selector(volumeDidChangeCurrentPlayingItem:)
45                                      withNotificationName: VLCVideoDidChangeCurrentlyPlayingItem];
46 }
47
48 static void HandleMediaListPlayerPaused( const libvlc_event_t * event, void * self )
49 {
50     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
51                                      withDelegateMethod: @selector(videoDidPause:)
52                                      withNotificationName: VLCVideoDidPause];
53 }
54
55 static void HandleMediaListPlayerPlayed( const libvlc_event_t * event, void * self )
56 {
57     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
58                                      withDelegateMethod: @selector(videoDidPlay:)
59                                      withNotificationName: VLCVideoDidPlay];
60 }
61
62 static void HandleMediaListPlayerStopped( const libvlc_event_t * event, void * self )
63 {
64     [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
65                                      withDelegateMethod: @selector(videoDidStop:)
66                                      withNotificationName: VLCVideoDidStop];
67 }
68
69 @implementation VLCVideoView
70 - (id)initWithFrame:(NSRect)frameRect
71 {
72     if (self = [super initWithFrame: frameRect])
73     {
74         libvlc_exception_t p_e;
75         libvlc_exception_init( &p_e );
76         p_mi = libvlc_media_instance_new( [VLCLibrary sharedInstance], &p_e );
77         quit_on_exception( &p_e );
78         p_mlp = libvlc_media_list_player_new( [VLCLibrary sharedInstance], &p_e );
79         quit_on_exception( &p_e );
80         libvlc_media_list_player_set_media_instance( p_mlp, p_mi, &p_e );
81         quit_on_exception( &p_e );
82         libvlc_media_instance_set_drawable( p_mi, (libvlc_drawable_t)self, &p_e );
83         quit_on_exception( &p_e );
84
85         libvlc_event_manager_t * p_em = libvlc_media_list_event_manager( p_mlp, &p_e );
86         //libvlc_event_attach( p_em, libvlc_MediaListPlayerCurrentPlayingItemChanged, HandleMediaListPlayerCurrentPlayingItemChanged, self, &p_e );
87         //libvlc_event_attach( p_em, libvlc_MediaListPlayerPaused,                    HandleMediaListPlayerPaused,  self, &p_e );
88         //libvlc_event_attach( p_em, libvlc_MediaListPlayerPlayed,                    HandleMediaListPlayerPlayed,  self, &p_e );
89         //libvlc_event_attach( p_em, libvlc_MediaListPlayerStopped,                   HandleMediaListPlayerStopped, self, &p_e );
90         quit_on_exception( &p_e );
91
92         delegate = nil;
93         playlist = nil;
94
95         [self setStretchesVideo: NO];
96         [self setAutoresizesSubviews: YES];
97     }
98     return self;
99 }
100
101 - (void)dealloc
102 {
103     libvlc_media_instance_release( p_mi );
104
105     if (delegate)
106     {
107         [delegate release];
108         delegate = nil;
109     }
110     [super dealloc];
111 }
112
113 - (void)setPlaylist: (VLCPlaylist *)newPlaylist
114 {
115     libvlc_exception_t p_e;
116     libvlc_exception_init( &p_e );
117
118     if (playlist)
119         [playlist release];
120
121     playlist = [newPlaylist retain];
122
123     libvlc_media_list_player_set_media_list( p_mlp, [playlist libVLCMediaList], &p_e );
124     quit_on_exception( &p_e );
125 }
126
127 - (VLCPlaylist *)playlist
128 {
129     return playlist;
130 }
131
132 /* Play */
133 - (void)play
134 {
135     libvlc_exception_t p_e;
136     libvlc_exception_init( &p_e );
137     libvlc_media_list_player_play( p_mlp, &p_e );
138     quit_on_exception( &p_e );
139 }
140
141 - (void)playItemAtIndex:(int) i
142 {
143     libvlc_exception_t p_e;
144     libvlc_exception_init( &p_e );
145     libvlc_media_list_player_play_item_at_index( p_mlp, i, &p_e );
146     quit_on_exception( &p_e );
147 }
148
149 - (void)playMedia:(VLCMedia *) media
150 {
151     libvlc_exception_t p_e;
152     libvlc_exception_init( &p_e );
153     libvlc_media_list_player_play_item( p_mlp, [media libVLCMediaDescriptor], &p_e );
154     quit_on_exception( &p_e );
155 }
156
157 - (void)pause
158 {
159     libvlc_exception_t p_e;
160     libvlc_exception_init( &p_e );
161     //libvlc_media_list_player_pause( p_mlp, &p_e );
162     quit_on_exception( &p_e );
163 }
164
165 - (void)setCurrentTime:(VLCTime *)timeObj
166 {
167     
168 }
169
170 /* State */
171 - (BOOL)isPlaying
172 {
173     //libvlc_media_list_player_is_playing( p_mlp, &p_e );
174     //quit_on_exception( &p_e );
175     return FALSE;
176 }
177
178 - (BOOL)isPaused
179 {
180     return FALSE;
181 }
182
183 - (VLCTime *)currentTime
184 {
185     return NULL;
186 }
187
188 - (id)currentPlaylistItem
189 {
190     return NULL;
191 }
192
193 /* Video output property */
194 - (void)setStretchesVideo:(BOOL)flag
195 {
196     stretchVideo = flag;
197 }
198
199 - (BOOL)stretchesVideo
200 {
201     return stretchVideo;
202 }
203
204 /* Fullscreen */
205 - (void)enterFullscreen
206 {
207
208 }
209
210 - (void)leaveFullscreen
211 {
212
213 }
214
215 /* Delegate */
216 - (void)setDelegate: (id)newDelegate
217 {
218     if (delegate)
219         [delegate release];
220     delegate = [newDelegate retain];
221 }
222
223 - (id)delegate
224 {
225     return delegate;
226 }
227 @end
228
229 @implementation VLCVideoView (NSViewSubclass)
230 - (void)drawRect:(NSRect)aRect
231 {
232     [self lockFocus];
233     [[NSColor blackColor] set];
234     NSRectFill(aRect);
235     [self unlockFocus];
236 }
237 - (BOOL)preservesContentDuringLiveResize
238 {
239     return NO;//YES;
240 }
241 - (BOOL)isOpaque
242 {
243     return YES;
244 }
245 @end
246
247 @interface VLCOpenGLVoutView : NSView
248  /* This is part of the hack to avoid a warning in -[VLCVideoView addVoutSubview:] */
249 - (void)detachFromVout;
250 @end
251
252 @implementation VLCVideoView (VLCOpenGLVoutEmbedding)
253 /* This is called by the libvlc module 'macosx' as soon as there is one vout
254  * available */
255 - (void)addVoutSubview:(id)aView
256 {
257     if( [[self subviews] count] )
258     {
259         /* XXX: This is a hack until core gets fixed */
260         int i;
261         for( i = 0; i < [[self subviews] count]; i++ )
262         {
263             [[[self subviews] objectAtIndex: i] detachFromVout];
264             [[[self subviews] objectAtIndex: i] retain];
265             [[[self subviews] objectAtIndex: i] removeFromSuperview];
266         }
267     }
268     [self addSubview: aView];
269     [aView setFrame: [self bounds]];
270     [aView setAutoresizingMask:NSViewHeightSizable|NSViewWidthSizable];
271 }
272 @end