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