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 <stdio.h>
25 
26 #include <avmedia/mediawindow.hxx>
27 #include "mediawindow_impl.hxx"
28 #include "mediamisc.hxx"
29 #include "mediawindow.hrc"
30 #include <tools/urlobj.hxx>
31 #include <vcl/msgbox.hxx>
32 #include <unotools/pathoptions.hxx>
33 #include <sfx2/filedlghelper.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/media/XManager.hpp>
37 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
38 
39 #define AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME 3.0
40 
41 using namespace ::com::sun::star;
42 
43 namespace avmedia {
44 
45 // ---------------
46 // - MediaWindow -
47 // ---------------
48 
MediaWindow(Window * parent,bool bInternalMediaControl)49 MediaWindow::MediaWindow( Window* parent, bool bInternalMediaControl ) :
50     mpImpl( new priv::MediaWindowImpl( parent, this, bInternalMediaControl ) )
51 {
52     mpImpl->Show();
53 }
54 
55 // -------------------------------------------------------------------------
56 
~MediaWindow()57 MediaWindow::~MediaWindow()
58 {
59     mpImpl->cleanUp();
60     delete mpImpl;
61     mpImpl = NULL;
62 }
63 
64 // -------------------------------------------------------------------------
65 
setURL(const::rtl::OUString & rURL)66 void MediaWindow::setURL( const ::rtl::OUString& rURL )
67 {
68     if( mpImpl )
69         mpImpl->setURL( rURL );
70 }
71 
72 // -------------------------------------------------------------------------
73 
getURL() const74 const ::rtl::OUString& MediaWindow::getURL() const
75 {
76     return mpImpl->getURL();
77 }
78 
79 // -------------------------------------------------------------------------
80 
isValid() const81 bool MediaWindow::isValid() const
82 {
83     return( mpImpl != NULL && mpImpl->isValid() );
84 }
85 
86 // -------------------------------------------------------------------------
87 
MouseMove(const MouseEvent &)88 void MediaWindow::MouseMove( const MouseEvent& /* rMEvt */ )
89 {
90 }
91 
92 // ---------------------------------------------------------------------
93 
MouseButtonDown(const MouseEvent &)94 void MediaWindow::MouseButtonDown( const MouseEvent& /* rMEvt */ )
95 {
96 }
97 
98 // ---------------------------------------------------------------------
99 
MouseButtonUp(const MouseEvent &)100 void MediaWindow::MouseButtonUp( const MouseEvent& /* rMEvt */ )
101 {
102 }
103 
104 // -------------------------------------------------------------------------
105 
KeyInput(const KeyEvent &)106 void MediaWindow::KeyInput( const KeyEvent& /* rKEvt */ )
107 {
108 }
109 
110 // -------------------------------------------------------------------------
111 
KeyUp(const KeyEvent &)112 void MediaWindow::KeyUp( const KeyEvent& /* rKEvt */ )
113 {
114 }
115 
116 // -------------------------------------------------------------------------
117 
Command(const CommandEvent &)118 void MediaWindow::Command( const CommandEvent& /* rCEvt */ )
119 {
120 }
121 
122 // -------------------------------------------------------------------------
123 
AcceptDrop(const AcceptDropEvent &)124 sal_Int8 MediaWindow::AcceptDrop( const AcceptDropEvent& /* rEvt */ )
125 {
126     return 0;
127 }
128 
129 // -------------------------------------------------------------------------
130 
ExecuteDrop(const ExecuteDropEvent &)131 sal_Int8 MediaWindow::ExecuteDrop( const ExecuteDropEvent& /* rEvt */ )
132 {
133     return 0;
134 }
135 
136 // -------------------------------------------------------------------------
137 
StartDrag(sal_Int8,const Point &)138 void MediaWindow::StartDrag( sal_Int8 /* nAction */, const Point& /* rPosPixel */ )
139 {
140 }
141 
142 // -------------------------------------------------------------------------
143 
hasPreferredSize() const144 bool MediaWindow::hasPreferredSize() const
145 {
146     return( mpImpl != NULL && mpImpl->hasPreferredSize() );
147 }
148 
149 // -------------------------------------------------------------------------
150 
getPreferredSize() const151 Size MediaWindow::getPreferredSize() const
152 {
153     return mpImpl->getPreferredSize();
154 }
155 
156 // -------------------------------------------------------------------------
157 
setPosSize(const Rectangle & rNewRect)158 void MediaWindow::setPosSize( const Rectangle& rNewRect )
159 {
160     if( mpImpl )
161     {
162         mpImpl->setPosSize( rNewRect );
163     }
164 }
165 
166 // -------------------------------------------------------------------------
167 
getPosSize() const168 Rectangle MediaWindow::getPosSize() const
169 {
170     return Rectangle( mpImpl->GetPosPixel(), mpImpl->GetSizePixel() );
171 }
172 
173 // -------------------------------------------------------------------------
174 
setPointer(const Pointer & rPointer)175 void MediaWindow::setPointer( const Pointer& rPointer )
176 {
177     if( mpImpl )
178         mpImpl->setPointer( rPointer );
179 }
180 
181 // -------------------------------------------------------------------------
182 
getPointer() const183 const Pointer& MediaWindow::getPointer() const
184 {
185     return mpImpl->getPointer();
186 }
187 
188 // -------------------------------------------------------------------------
189 
setZoom(::com::sun::star::media::ZoomLevel eLevel)190 bool MediaWindow::setZoom( ::com::sun::star::media::ZoomLevel eLevel )
191 {
192     return( mpImpl != NULL && mpImpl->setZoom( eLevel ) );
193 }
194 
195 // -------------------------------------------------------------------------
196 
getZoom() const197 ::com::sun::star::media::ZoomLevel MediaWindow::getZoom() const
198 {
199     return mpImpl->getZoom();
200 }
201 
202 // -------------------------------------------------------------------------
203 
start()204 bool MediaWindow::start()
205 {
206     return( mpImpl != NULL && mpImpl->start() );
207 }
208 
209 // -------------------------------------------------------------------------
210 
stop()211 void MediaWindow::stop()
212 {
213     if( mpImpl )
214         mpImpl->stop();
215 }
216 
217 // -------------------------------------------------------------------------
218 
isPlaying() const219 bool MediaWindow::isPlaying() const
220 {
221     return( mpImpl != NULL && mpImpl->isPlaying() );
222 }
223 
224 // -------------------------------------------------------------------------
225 
getDuration() const226 double MediaWindow::getDuration() const
227 {
228     return mpImpl->getDuration();
229 }
230 
231 // -------------------------------------------------------------------------
232 
setMediaTime(double fTime)233 void MediaWindow::setMediaTime( double fTime )
234 {
235     if( mpImpl )
236         mpImpl->setMediaTime( fTime );
237 }
238 
239 // -------------------------------------------------------------------------
240 
getMediaTime() const241 double MediaWindow::getMediaTime() const
242 {
243     return mpImpl->getMediaTime();
244 }
245 
246 // -------------------------------------------------------------------------
247 
setStopTime(double fTime)248 void MediaWindow::setStopTime( double fTime )
249 {
250     if( mpImpl )
251         mpImpl->setStopTime( fTime );
252 }
253 
254 // -------------------------------------------------------------------------
255 
getStopTime() const256 double MediaWindow::getStopTime() const
257 {
258     return mpImpl->getStopTime();
259 }
260 
261 // -------------------------------------------------------------------------
262 
setRate(double fRate)263 void MediaWindow::setRate( double fRate )
264 {
265     if( mpImpl )
266         mpImpl->setRate( fRate );
267 }
268 
269 // -------------------------------------------------------------------------
270 
getRate() const271 double MediaWindow::getRate() const
272 {
273     return mpImpl->getRate();
274 }
275 
276 // -------------------------------------------------------------------------
277 
setPlaybackLoop(bool bSet)278 void MediaWindow::setPlaybackLoop( bool bSet )
279 {
280     if( mpImpl )
281         mpImpl->setPlaybackLoop( bSet );
282 }
283 
284 // -------------------------------------------------------------------------
285 
isPlaybackLoop() const286 bool MediaWindow::isPlaybackLoop() const
287 {
288     return mpImpl->isPlaybackLoop();
289 }
290 
291 // -------------------------------------------------------------------------
292 
setMute(bool bSet)293 void MediaWindow::setMute( bool bSet )
294 {
295     if( mpImpl )
296         mpImpl->setMute( bSet );
297 }
298 
299 // -------------------------------------------------------------------------
300 
isMute() const301 bool MediaWindow::isMute() const
302 {
303     return mpImpl->isMute();
304 }
305 
306 // -------------------------------------------------------------------------
307 
updateMediaItem(MediaItem & rItem) const308 void MediaWindow::updateMediaItem( MediaItem& rItem ) const
309 {
310     if( mpImpl )
311         mpImpl->updateMediaItem( rItem );
312 }
313 
314 // -------------------------------------------------------------------------
315 
executeMediaItem(const MediaItem & rItem)316 void MediaWindow::executeMediaItem( const MediaItem& rItem )
317 {
318     if( mpImpl )
319         mpImpl->executeMediaItem( rItem );
320 }
321 
322 // -------------------------------------------------------------------------
323 
show()324 void MediaWindow::show()
325 {
326     if( mpImpl )
327         mpImpl->Show();
328 }
329 
330 // -------------------------------------------------------------------------
331 
hide()332 void MediaWindow::hide()
333 {
334     if( mpImpl )
335         mpImpl->Hide();
336 }
337 
338 // -------------------------------------------------------------------------
339 
enable()340 void MediaWindow::enable()
341 {
342     if( mpImpl )
343         mpImpl->Enable();
344 }
345 
346 // -------------------------------------------------------------------------
347 
disable()348 void MediaWindow::disable()
349 {
350     if( mpImpl )
351         mpImpl->Disable();
352 }
353 
354 // -------------------------------------------------------------------------
355 
getWindow() const356 Window* MediaWindow::getWindow() const
357 {
358     return mpImpl;
359 }
360 
361 // -------------------------------------------------------------------------
362 
getMediaFilters(FilterNameVector & rFilterNameVector)363 void MediaWindow::getMediaFilters( FilterNameVector& rFilterNameVector )
364 {
365     static const char* pFilters[] = {   "AIF Audio", "aif;aiff",
366                                         "AU Audio", "au",
367                                         "AVI", "avi",
368                                         "CD Audio", "cda",
369                                         "FLAC Audio", "flac",
370                                         "Flash Video", "flv",
371                                         "Matroska Media", "mkv",
372                                         "MIDI Audio", "mid;midi",
373                                         "MPEG Audio", "mp2;mp3;mpa;m4a",
374                                         "MPEG Video", "mpg;mpeg;mpv;mp4",
375                                         "Ogg bitstream", "ogg;oga;ogv",
376                                         "Quicktime Video", "mov",
377                                         "Vivo Video", "viv",
378                                         "WAVE Audio", "wav",
379                                         "Windows Media Video", "wmv" };
380 
381     unsigned int i;
382 	for( i = 0; i < ( sizeof( pFilters ) / sizeof( char* ) ); i += 2 )
383     {
384         rFilterNameVector.push_back( ::std::make_pair< ::rtl::OUString, ::rtl::OUString >(
385                                         ::rtl::OUString::createFromAscii( pFilters[ i ] ),
386                                         ::rtl::OUString::createFromAscii( pFilters[ i + 1 ] ) ) );
387     }
388 }
389 
390 // -------------------------------------------------------------------------
391 
executeMediaURLDialog(Window *,::rtl::OUString & rURL,bool bInsertDialog)392 bool MediaWindow::executeMediaURLDialog( Window* /* pParent */, ::rtl::OUString& rURL, bool bInsertDialog )
393 {
394     ::sfx2::FileDialogHelper        aDlg( com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE, 0 );
395     static const ::rtl::OUString    aWildcard( RTL_CONSTASCII_USTRINGPARAM( "*." ) );
396     FilterNameVector                aFilters;
397     const ::rtl::OUString           aSeparator( RTL_CONSTASCII_USTRINGPARAM( ";" ) );
398     ::rtl::OUString                 aAllTypes;
399 
400     aDlg.SetTitle( AVMEDIA_RESID( bInsertDialog ? AVMEDIA_STR_INSERTMEDIA_DLG : AVMEDIA_STR_OPENMEDIA_DLG ) );
401 
402     getMediaFilters( aFilters );
403 
404 	unsigned int i;
405     for( i = 0; i < aFilters.size(); ++i )
406     {
407         for( sal_Int32 nIndex = 0; nIndex >= 0; )
408         {
409             if( !aAllTypes.isEmpty() )
410                 aAllTypes += aSeparator;
411 
412             ( aAllTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
413         }
414     }
415 
416     // add filter for all media types
417     aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_MEDIAFILES ), aAllTypes );
418 
419     for( i = 0; i < aFilters.size(); ++i )
420     {
421         ::rtl::OUString aTypes;
422 
423         for( sal_Int32 nIndex = 0; nIndex >= 0; )
424         {
425             if( !aTypes.isEmpty() )
426                 aTypes += aSeparator;
427 
428             ( aTypes += aWildcard ) += aFilters[ i ].second.getToken( 0, ';', nIndex );
429         }
430 
431         // add single filters
432         aDlg.AddFilter( aFilters[ i ].first, aTypes );
433     }
434 
435     // add filter for all types
436     aDlg.AddFilter( AVMEDIA_RESID( AVMEDIA_STR_ALL_FILES ), String( RTL_CONSTASCII_USTRINGPARAM( "*.*" ) ) );
437 
438     if( aDlg.Execute() == ERRCODE_NONE )
439     {
440         const INetURLObject aURL( aDlg.GetPath() );
441         rURL = aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS );
442     }
443     else if( !rURL.isEmpty() )
444         rURL = ::rtl::OUString();
445 
446     return !rURL.isEmpty();
447 }
448 
449 // -------------------------------------------------------------------------
450 
executeFormatErrorBox(Window * pParent)451 void MediaWindow::executeFormatErrorBox( Window* pParent )
452 {
453     ErrorBox aErrBox( pParent, AVMEDIA_RESID( AVMEDIA_ERR_URL ) );
454 
455     aErrBox.Execute();
456 }
457 
458 // -------------------------------------------------------------------------
459 
isMediaURL(const::rtl::OUString & rURL,bool bDeep,Size * pPreferredSizePixel)460 bool MediaWindow::isMediaURL( const ::rtl::OUString& rURL, bool bDeep, Size* pPreferredSizePixel )
461 {
462     const INetURLObject aURL( rURL );
463     bool                bRet = false;
464 
465     if( aURL.GetProtocol() != INET_PROT_NOT_VALID )
466     {
467         if( bDeep || pPreferredSizePixel )
468         {
469             try
470             {
471                 sal_Bool bIsJavaBasedMediaWindow;
472     	        uno::Reference< media::XPlayer > xPlayer( priv::MediaWindowImpl::createPlayer(
473         	                                                aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ),
474                                                             bIsJavaBasedMediaWindow ) );
475 
476             	if( xPlayer.is() )
477                 {
478                     bRet = true;
479 
480                     if( pPreferredSizePixel )
481                     {
482                         const awt::Size aAwtSize( xPlayer->getPreferredPlayerWindowSize() );
483 
484                         pPreferredSizePixel->Width() = aAwtSize.Width;
485                         pPreferredSizePixel->Height() = aAwtSize.Height;
486                     }
487                 }
488             }
489             catch( ... )
490             {
491             }
492         }
493         else
494         {
495             FilterNameVector        aFilters;
496             const ::rtl::OUString   aExt( aURL.getExtension() );
497 
498             getMediaFilters( aFilters );
499 
500 			unsigned int i;
501             for( i = 0; ( i < aFilters.size() ) && !bRet; ++i )
502             {
503                 for( sal_Int32 nIndex = 0; nIndex >= 0 && !bRet; )
504                 {
505                     if( aExt.equalsIgnoreAsciiCase( aFilters[ i ].second.getToken( 0, ';', nIndex ) ) )
506                         bRet = true;
507                 }
508             }
509         }
510     }
511 
512     return bRet;
513 }
514 
515 // -------------------------------------------------------------------------
516 
createPlayer(const::rtl::OUString & rURL)517 uno::Reference< media::XPlayer > MediaWindow::createPlayer( const ::rtl::OUString& rURL )
518 {
519     sal_Bool bJavaBased = sal_False;
520     return priv::MediaWindowImpl::createPlayer( rURL, bJavaBased );
521 }
522 
523 // -------------------------------------------------------------------------
524 
grabFrame(const::rtl::OUString & rURL,bool bAllowToCreateReplacementGraphic,double fMediaTime)525 uno::Reference< graphic::XGraphic > MediaWindow::grabFrame( const ::rtl::OUString& rURL,
526                                                             bool bAllowToCreateReplacementGraphic,
527                                                             double fMediaTime )
528 {
529     uno::Reference< media::XPlayer >    xPlayer( createPlayer( rURL ) );
530     uno::Reference< graphic::XGraphic > xRet;
531     ::std::auto_ptr< Graphic >          apGraphic;
532 
533     if( xPlayer.is() )
534     {
535         uno::Reference< media::XFrameGrabber > xGrabber( xPlayer->createFrameGrabber() );
536 
537         if( xGrabber.is() )
538         {
539             if( AVMEDIA_FRAMEGRABBER_DEFAULTFRAME == fMediaTime )
540                 fMediaTime = AVMEDIA_FRAMEGRABBER_DEFAULTFRAME_MEDIATIME;
541 
542             if( fMediaTime >= xPlayer->getDuration() )
543                 fMediaTime = ( xPlayer->getDuration() * 0.5 );
544 
545             xRet = xGrabber->grabFrame( fMediaTime );
546         }
547 
548         if( !xRet.is() && bAllowToCreateReplacementGraphic )
549         {
550             awt::Size aPrefSize( xPlayer->getPreferredPlayerWindowSize() );
551 
552             if( !aPrefSize.Width && !aPrefSize.Height )
553             {
554                 const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_AUDIOLOGO ) );
555                 apGraphic.reset( new Graphic( aBmpEx ) );
556             }
557         }
558     }
559 
560     if( !xRet.is() && !apGraphic.get() && bAllowToCreateReplacementGraphic )
561     {
562         const BitmapEx aBmpEx( AVMEDIA_RESID( AVMEDIA_BMP_EMPTYLOGO ) );
563         apGraphic.reset( new Graphic( aBmpEx ) );
564     }
565 
566     if( apGraphic.get() )
567         xRet = apGraphic->GetXGraphic();
568 
569     return xRet;
570 }
571 
572 } // namespace avmedia
573 
574