1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <com/sun/star/awt/SystemPointer.hpp>
25 #include <com/sun/star/awt/PosSize.hpp>
26 
27 #include "window.hxx"
28 #include "player.hxx"
29 
30 using namespace ::com::sun::star;
31 
32 
33 namespace avmedia { namespace macavf {
34 
35 // ---------------
36 // - Window -
37 // ---------------
38 
39 Window::Window( const uno::Reference< lang::XMultiServiceFactory >& i_rxMgr, Player& i_rPlayer, NSView* i_pParentView )
40 :   mxMgr( i_rxMgr )
41 ,   maListeners( maMutex )
42 ,   meZoomLevel( media::ZoomLevel_NOT_AVAILABLE )
43 ,   mrPlayer( i_rPlayer )
44 ,   mnPointerType( awt::SystemPointer::ARROW )
45 ,   mpView( i_pParentView )
46 ,   mpPlayerLayer( NULL )
47 {
48     OSL_TRACE ("Constructing an avmedia::macavf::Window");
49     if( !mpView ) // sanity check
50         return;
51 
52     // check the media asset for video content
53     AVPlayer* pAVPlayer = mrPlayer.getAVPlayer();
54     AVAsset* pMovie = [[pAVPlayer currentItem] asset];
55     const int nVideoCount = [pMovie tracksWithMediaType:AVMediaTypeVideo].count;
56     const int nAudioCount = [pMovie tracksWithMediaType:AVMediaTypeAudio].count;
57     OSL_TRACE( "Found %d video and %d audio tracks.", nVideoCount, nAudioCount );
58     (void)nAudioCount;
59     if( nVideoCount <= 0 )
60         return;
61 
62     // setup the AVPlayerLayer
63     [pAVPlayer retain];
64     [pAVPlayer pause];
65     mpPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:pAVPlayer];
66     [mpPlayerLayer retain];
67     [mpPlayerLayer setFrame:[mpView frame]];
68     [mpPlayerLayer setHidden:YES];
69     [mpPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
70     [mpPlayerLayer addObserver:getObserver() forKeyPath:@"readyForDisplay" options:0 context:this];
71 
72     // setup the target view
73     [mpView setWantsLayer:YES];
74     [mpView.layer addSublayer:mpPlayerLayer];
75 }
76 
77 // ------------------------------------------------------------------------------
78 
79 Window::~Window()
80 {
81     [mpPlayerLayer removeObserver:getObserver() forKeyPath:@"readyForDisplay"];
82     [mpPlayerLayer release];
83 }
84 
85 // ------------------------------------------------------------------------------
86 
87 bool Window::create( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments )
88 {
89     return true;
90 }
91 
92 // ------------------------------------------------------------------------------
93 
94 bool Window::handleObservation( NSString* pKeyPath )
95 {
96     OSL_TRACE( "AVPlayer::handleObservation key=\"%s\"", [pKeyPath UTF8String]);
97     const BOOL bReadyForDisplay = [mpPlayerLayer isReadyForDisplay];
98     [mpPlayerLayer setHidden:!bReadyForDisplay];
99     return true;
100 }
101 
102 // XPlayerWindow
103 // ------------------------------------------------------------------------------
104 
105 void SAL_CALL Window::update()
106     throw (uno::RuntimeException)
107 {}
108 
109 // ------------------------------------------------------------------------------
110 
111 sal_Bool SAL_CALL Window::setZoomLevel( media::ZoomLevel eZoomLevel )
112     throw (uno::RuntimeException)
113 {
114     return false;
115 }
116 
117 // ------------------------------------------------------------------------------
118 
119 media::ZoomLevel SAL_CALL Window::getZoomLevel(  )
120     throw (uno::RuntimeException)
121 {
122     return meZoomLevel;
123 }
124 
125 // ------------------------------------------------------------------------------
126 
127 void SAL_CALL Window::setPointerType( sal_Int32 nPointerType )
128     throw (uno::RuntimeException)
129 {
130     mnPointerType = nPointerType;
131 }
132 
133 // XWindow
134 // ------------------------------------------------------------------------------
135 
136 void SAL_CALL Window::setPosSize( sal_Int32 X, sal_Int32 Y, sal_Int32 Width, sal_Int32 Height, sal_Int16 Flags )
137     throw (uno::RuntimeException)
138 {
139     OSL_TRACE( "AVWindow::setPosSize( %dx%d%+d%+d)", (int)Width,(int)Height,(int)X,(int)Y);//######
140     if( !mpView )
141         return;
142     NSRect aRect = [mpView frame];
143     // NOTE: if( (Flags & awt::PosSize::WIDTH) )
144     aRect.size.width = Width;
145     // NOTE: if( (Flags & awt::PosSize::HEIGHT) )
146     aRect.size.height = Height;
147 
148     [mpView setFrameSize: aRect.size];
149     [mpPlayerLayer setFrame: [mpView frame]];
150 }
151 
152 // ------------------------------------------------------------------------------
153 
154 awt::Rectangle SAL_CALL Window::getPosSize()
155     throw (uno::RuntimeException)
156 {
157     awt::Rectangle aRet;
158 
159     NSRect aRect = [mpView frame];
160     aRet.X = aRet.Y = 0;
161     aRet.Width = aRect.size.width;
162     aRet.Height = aRect.size.height;
163 
164     return aRet;
165 }
166 
167 // ------------------------------------------------------------------------------
168 
169 void SAL_CALL Window::setVisible( sal_Bool bVisible )
170     throw (uno::RuntimeException)
171 {
172     OSL_TRACE ("Window::setVisible(%d)", bVisible);
173 }
174 
175 // ------------------------------------------------------------------------------
176 
177 void SAL_CALL Window::setEnable( sal_Bool bEnable )
178     throw (uno::RuntimeException)
179 {
180     OSL_TRACE ("Window::setEnable(%d)", bEnable);
181 }
182 
183 // ------------------------------------------------------------------------------
184 
185 void SAL_CALL Window::setFocus()
186     throw (uno::RuntimeException)
187 {
188     OSL_TRACE ("Window::setFocus");
189 }
190 
191 // ------------------------------------------------------------------------------
192 
193 void SAL_CALL Window::addWindowListener( const uno::Reference< awt::XWindowListener >& xListener )
194     throw (uno::RuntimeException)
195 {
196     maListeners.addInterface( getCppuType( &xListener ), xListener );
197 }
198 
199 // ------------------------------------------------------------------------------
200 
201 void SAL_CALL Window::removeWindowListener( const uno::Reference< awt::XWindowListener >& xListener )
202     throw (uno::RuntimeException)
203 {
204     maListeners.removeInterface( getCppuType( &xListener ), xListener );
205 }
206 
207 // ------------------------------------------------------------------------------
208 
209 void SAL_CALL Window::addFocusListener( const uno::Reference< awt::XFocusListener >& xListener )
210     throw (uno::RuntimeException)
211 {
212     maListeners.addInterface( getCppuType( &xListener ), xListener );
213 }
214 
215 // ------------------------------------------------------------------------------
216 
217 void SAL_CALL Window::removeFocusListener( const uno::Reference< awt::XFocusListener >& xListener )
218     throw (uno::RuntimeException)
219 {
220     maListeners.removeInterface( getCppuType( &xListener ), xListener );
221 }
222 
223 // ------------------------------------------------------------------------------
224 
225 void SAL_CALL Window::addKeyListener( const uno::Reference< awt::XKeyListener >& xListener )
226     throw (uno::RuntimeException)
227 {
228     maListeners.addInterface( getCppuType( &xListener ), xListener );
229 }
230 
231 // ------------------------------------------------------------------------------
232 
233 void SAL_CALL Window::removeKeyListener( const uno::Reference< awt::XKeyListener >& xListener )
234     throw (uno::RuntimeException)
235 {
236     maListeners.removeInterface( getCppuType( &xListener ), xListener );
237 }
238 
239 // ------------------------------------------------------------------------------
240 
241 void SAL_CALL Window::addMouseListener( const uno::Reference< awt::XMouseListener >& xListener )
242     throw (uno::RuntimeException)
243 {
244     maListeners.addInterface( getCppuType( &xListener ), xListener );
245 }
246 
247 // ------------------------------------------------------------------------------
248 
249 void SAL_CALL Window::removeMouseListener( const uno::Reference< awt::XMouseListener >& xListener )
250     throw (uno::RuntimeException)
251 {
252     maListeners.removeInterface( getCppuType( &xListener ), xListener );
253 }
254 
255 // ------------------------------------------------------------------------------
256 
257 void SAL_CALL Window::addMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener )
258     throw (uno::RuntimeException)
259 {
260     maListeners.addInterface( getCppuType( &xListener ), xListener );
261 }
262 
263 // ------------------------------------------------------------------------------
264 
265 void SAL_CALL Window::removeMouseMotionListener( const uno::Reference< awt::XMouseMotionListener >& xListener )
266     throw (uno::RuntimeException)
267 {
268     maListeners.removeInterface( getCppuType( &xListener ), xListener );
269 }
270 
271 // ------------------------------------------------------------------------------
272 
273 void SAL_CALL Window::addPaintListener( const uno::Reference< awt::XPaintListener >& xListener )
274     throw (uno::RuntimeException)
275 {
276     maListeners.addInterface( getCppuType( &xListener ), xListener );
277 }
278 
279 // ------------------------------------------------------------------------------
280 
281 void SAL_CALL Window::removePaintListener( const uno::Reference< awt::XPaintListener >& xListener )
282     throw (uno::RuntimeException)
283 {
284     maListeners.removeInterface( getCppuType( &xListener ), xListener );
285 }
286 
287 
288 // XComponent
289 // ------------------------------------------------------------------------------
290 
291 void SAL_CALL Window::dispose(  )
292     throw (uno::RuntimeException)
293 {
294 }
295 
296 // ------------------------------------------------------------------------------
297 
298 void SAL_CALL Window::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
299     throw (uno::RuntimeException)
300 {
301     maListeners.addInterface( getCppuType( &xListener ), xListener );
302 }
303 
304 // ------------------------------------------------------------------------------
305 
306 void SAL_CALL Window::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
307     throw (uno::RuntimeException)
308 {
309     maListeners.removeInterface( getCppuType( &xListener ), xListener );
310 }
311 
312 // XServiceInfo
313 // ------------------------------------------------------------------------------
314 
315 ::rtl::OUString SAL_CALL Window::getImplementationName(  )
316     throw (uno::RuntimeException)
317 {
318     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_MACAVF_WINDOW_IMPLEMENTATIONNAME ) );
319 }
320 
321 // ------------------------------------------------------------------------------
322 
323 sal_Bool SAL_CALL Window::supportsService( const ::rtl::OUString& ServiceName )
324     throw (uno::RuntimeException)
325 {
326     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_MACAVF_WINDOW_SERVICENAME ) );
327 }
328 
329 // ------------------------------------------------------------------------------
330 
331 uno::Sequence< ::rtl::OUString > SAL_CALL Window::getSupportedServiceNames(  )
332     throw (uno::RuntimeException)
333 {
334     uno::Sequence< ::rtl::OUString > aRet(1);
335     aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_MACAVF_WINDOW_SERVICENAME ) );
336 
337     return aRet;
338 }
339 
340 } // namespace macavf
341 } // namespace avmedia
342 
343