]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/sout.cpp
553be6aed60d8867b7fd6f0503d78cdeff2eb373
[vlc] / modules / gui / qt4 / dialogs / sout.cpp
1 /*****************************************************************************
2  * sout.cpp : Stream output dialog ( old-style )
3  ****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * Copyright (C) 2007 Société des arts technologiques
6  * Copyright (C) 2007 Savoir-faire Linux
7  *
8  * $Id$
9  *
10  * Authors: Clément Stenac <zorglub@videolan.org>
11  *          Jean-Baptiste Kempf <jb@videolan.org>
12  *          Jean-François Massol <jf.massol -at- gmail.com>
13  *          Pierre-Luc Beaudoin <pierre-luc.beaudoin@savoirfairelinux.com>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * ( at your option ) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include "dialogs/sout.hpp"
34
35 #include <QString>
36 #include <QFileDialog>
37
38 struct streaming_account_t
39 {
40     char *psz_username; /*< username of account */
41     char *psz_password; /*< password of account */
42 };
43
44 struct sout_gui_descr_t
45 {
46     /* Access types */
47     bool b_local;   /*< local access module */
48     bool b_file;    /*< file access module */
49     bool b_http;    /*< http access module */
50     bool b_mms;     /*< mms access module */
51     bool b_rtp;     /*< rtp access module */
52     bool b_udp;     /*< udp access module */
53     bool b_dump;    /*< dump access module */
54     bool b_icecast; /*< icecast access module */
55
56     char *psz_file;     /*< filename */
57     char *psz_http;     /*< HTTP servername or ipaddress */
58     char *psz_mms;      /*< MMS servername or ipaddress */
59     char *psz_rtp;      /*< RTP servername or ipaddress */
60     char *psz_udp;      /*< UDP servername or ipaddress */
61     char *psz_icecast;  /*< Icecast servername or ipaddress*/
62
63     int32_t i_http;     /*< http port number */
64     int32_t i_mms;      /*< mms port number */
65     int32_t i_rtp;      /*< rtp port number */
66     int32_t i_udp;      /*< udp port number */
67     int32_t i_icecast;  /*< icecast port number */
68
69     /* Mux */
70     char *psz_mux;      /*< name of muxer to use in streaming */
71
72     /* Transcode */
73     bool b_soverlay; /*< enable burning overlay in the video */
74     char *psz_vcodec;   /*< video codec to use in transcoding */
75     char *psz_acodec;   /*< audio codec to use in transcoding */
76     char *psz_scodec;   /*< subtitle codec to use in transcoding */
77     int32_t i_vb;       /*< video bitrate to use in transcoding */
78     int32_t i_ab;       /*< audio bitrate to use in transcoding */
79     int32_t i_channels; /*< number of audio channels to use in transcoding */
80     float f_scale;      /*< scaling factor to use in transcoding */
81
82     /* Misc */
83     bool b_sap;   /*< send SAP announcement */
84     bool b_all_es;/*< send all elementary streams from source stream */
85     char *psz_group;    /*< SAP Group name */
86     char *psz_name;     /*< SAP name */
87     int32_t i_ttl;      /*< Time To Live (TTL) for network traversal */
88
89     /* Icecast */
90     char *psz_icecast_mountpoint;/*< path to Icecast mountpoint */
91     struct streaming_account_t sa_icecast;  /*< Icecast account information */
92 };
93
94 SoutDialog* SoutDialog::instance = NULL;
95
96 SoutDialog::SoutDialog( QWidget *parent, intf_thread_t *_p_intf,
97                      bool _transcode_only ) : QVLCDialog( parent,  _p_intf )
98 {
99     setWindowTitle( qtr( "Stream Output" ) );
100
101     b_transcode_only = _transcode_only;
102
103     /* UI stuff */
104     ui.setupUi( this );
105
106     ui.UDPEdit->hide(); ui.UDPLabel->hide();
107     ui.UDPPort->hide(); ui.UDPPortLabel->hide();
108 /* ADD HERE for new profiles */
109 #define ADD_PROFILE( name, shortname ) ui.profileBox->addItem( qtr( name ), QVariant( QString( shortname ) ) );
110     ADD_PROFILE( "Custom" , "Custom" )
111     ADD_PROFILE( "IPod (mp4/aac)", "IPod" )
112     ADD_PROFILE( "XBox", "XBox" )
113     ADD_PROFILE( "Windows (wmv/asf)", "Windows" )
114     ADD_PROFILE( "PSP", "PSP")
115     ADD_PROFILE( "GSM", "GSM" )
116
117 #define ADD_VCODEC( name, fourcc ) ui.vCodecBox->addItem( name, QVariant( fourcc ) );
118     ADD_VCODEC( "MPEG-1", "mp1v" )
119     ADD_VCODEC( "MPEG-2", "mp2v" )
120     ADD_VCODEC( "MPEG-4", "mp4v" )
121     ADD_VCODEC( "DIVX 1" , "DIV1" )
122     ADD_VCODEC( "DIVX 2" , "DIV2" )
123     ADD_VCODEC( "DIVX 3" , "DIV3" )
124     ADD_VCODEC( "H-263", "H263" )
125     ADD_VCODEC( "H-264", "h264" )
126     ADD_VCODEC( "WMV1", "WMV1" )
127     ADD_VCODEC( "WMV2" , "WMV2" )
128     ADD_VCODEC( "M-JPEG", "MJPG" )
129     ADD_VCODEC( "Theora", "theo" )
130
131 #define ADD_ACODEC( name, fourcc ) ui.aCodecBox->addItem( name, QVariant( fourcc ) );
132     ADD_ACODEC( "MPEG Audio", "mpga" )
133     ADD_ACODEC( "MP3", "mp3" )
134     ADD_ACODEC( "MPEG 4 Audio ( AAC )", "mp4a" )
135     ADD_ACODEC( "A52/AC-3", "a52" )
136     ADD_ACODEC( "Vorbis", "vorb" )
137     ADD_ACODEC( "Flac", "flac" )
138     ADD_ACODEC( "Speex", "spx" )
139     ADD_ACODEC( "WAV", "s16l" )
140     ADD_ACODEC( "WMA", "wma" )
141
142 #define ADD_SCALING( factor ) ui.vScaleBox->addItem( factor );
143     ADD_SCALING( "0.25" )
144     ADD_SCALING( "0.5" )
145     ADD_SCALING( "0.75" )
146     ADD_SCALING( "1" )
147     ADD_SCALING( "1.25" )
148     ADD_SCALING( "1.5" )
149     ADD_SCALING( "1.75" )
150     ADD_SCALING( "2" )
151
152     ui.mrlEdit->setToolTip ( qtr( "Stream output string.\n This is automatically generated "
153                                                 "when you change the above settings,\n but you can update it manually." ) ) ;
154
155 //     /* Connect everything to the updateMRL function */
156  #define CB( x ) CONNECT( ui.x, toggled( bool ), this, updateMRL() );
157  #define CT( x ) CONNECT( ui.x, textChanged( const QString ), this, updateMRL() );
158  #define CS( x ) CONNECT( ui.x, valueChanged( int ), this, updateMRL() );
159  #define CC( x ) CONNECT( ui.x, currentIndexChanged( int ), this, updateMRL() );
160 //     /* Output */
161     CB( fileOutput ); CB( HTTPOutput ); CB( localOutput );
162     CB( RTPOutput ); CB( MMSHOutput ); CB( rawInput ); CB( UDPOutput );
163     CT( fileEdit ); CT( HTTPEdit ); CT( RTPEdit ); CT( MMSHEdit ); CT( UDPEdit );
164     CT( IcecastEdit ); CT( IcecastMountpointEdit ); CT( IcecastNamePassEdit );
165     CS( HTTPPort ); CS( RTPPort ); CS( MMSHPort ); CS( UDPPort );
166 //     /* Transcode */
167     CC( vCodecBox ); CC( subsCodecBox ); CC( aCodecBox ) ;
168     CB( transcodeVideo ); CB( transcodeAudio ); CB( transcodeSubs );
169 //     CB( sOverlay );
170     CS( vBitrateSpin ); CS( aBitrateSpin ); CS( aChannelsSpin ); CC( vScaleBox );
171 //     /* Mux */
172     CB( PSMux ); CB( TSMux ); CB( MPEG1Mux ); CB( OggMux ); CB( ASFMux );
173     CB( MP4Mux ); CB( MOVMux ); CB( WAVMux ); CB( RAWMux ); CB( FLVMux );
174 //     /* Misc */
175     CB( soutAll ); CS( ttl ); CT( sapName ); CT( sapGroup );
176 //
177     CONNECT( ui.profileBox, activated( const QString & ), this, setOptions() );
178     CONNECT( ui.fileSelectButton, clicked() , this, fileBrowse()  );
179     CONNECT( ui.transcodeVideo, toggled( bool ), this, setVTranscodeOptions( bool ) );
180     CONNECT( ui.transcodeAudio, toggled( bool ), this, setATranscodeOptions( bool ) );
181     CONNECT( ui.transcodeSubs, toggled( bool ), this, setSTranscodeOptions( bool ) );
182     CONNECT( ui.rawInput, toggled( bool ), this, setRawOptions( bool ) );
183
184     okButton = new QPushButton( qtr( "&Stream" ) );
185     QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ) );
186
187     okButton->setDefault( true );
188     ui.acceptButtonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
189     ui.acceptButtonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
190
191     BUTTONACT( okButton, ok() );
192     BUTTONACT( cancelButton, cancel() );
193
194     if( b_transcode_only ) toggleSout();
195 }
196
197 void SoutDialog::fileBrowse()
198 {
199     ui.tabWidget->setTabEnabled( 0,false );
200     QString fileName = QFileDialog::getSaveFileName( this, qtr( "Save file" ), "",
201         qtr( "Containers (*.ps *.ts *.mpg *.ogg *.asf *.mp4 *.mov *.wav *.raw *.flv)" ) );
202     ui.fileEdit->setText( fileName );
203     updateMRL();
204 }
205
206 void SoutDialog::setVTranscodeOptions( bool b_trans )
207 {
208     ui.vCodecLabel->setEnabled( b_trans );
209     ui.vCodecBox->setEnabled( b_trans );
210     ui.vBitrateLabel->setEnabled( b_trans );
211     ui.vBitrateSpin->setEnabled( b_trans );
212     ui.vScaleLabel->setEnabled( b_trans );
213     ui.vScaleBox->setEnabled( b_trans );
214 }
215
216 void SoutDialog::setATranscodeOptions( bool b_trans )
217 {
218     ui.aCodecLabel->setEnabled( b_trans );
219     ui.aCodecBox->setEnabled( b_trans );
220     ui.aBitrateLabel->setEnabled( b_trans );
221     ui.aBitrateSpin->setEnabled( b_trans );
222     ui.aChannelsLabel->setEnabled( b_trans );
223     ui.aChannelsSpin->setEnabled( b_trans );
224 }
225
226 void SoutDialog::setSTranscodeOptions( bool b_trans )
227 {
228     ui.subsCodecBox->setEnabled( b_trans );
229     ui.subsOverlay->setEnabled( b_trans );
230 }
231
232 void SoutDialog::setRawOptions( bool b_raw )
233 {
234     if( b_raw )
235     {
236         ui.tabWidget->setDisabled( true );
237     }
238     else
239     {
240         SoutDialog::setOptions();
241     }
242 }
243
244 void SoutDialog::setOptions()
245 {
246     QString profileString = ui.profileBox->itemData( ui.profileBox->currentIndex() ).toString();
247     msg_Dbg( p_intf, "Profile Used: %s",  qta( profileString ));
248     int index;
249
250 #define setProfile( muxName, hasVideo, vCodecName, hasAudio, aCodecName ) \
251     { \
252         ui.muxName ##Mux->setChecked( true ); \
253         \
254         ui.transcodeAudio->setChecked( hasAudio ); \
255         index = ui.aCodecBox->findText( vCodecName );  \
256         if( index >= 0 ) ui.aCodecBox->setCurrentIndex( index ); \
257         \
258         ui.transcodeVideo->setChecked( hasVideo ); \
259         index = ui.aCodecBox->findText( vCodecName );  \
260         if( index >=0 ) ui.vCodecBox->setCurrentIndex( index ); \
261     }
262
263     /* ADD HERE the profiles you want and need */
264     /* FIXME */
265     if( profileString == "IPod" ) setProfile( MP4, true, "mp4a", true, "mp4v" )
266     else if( profileString == "XBox" ) setProfile( ASF, true, "wma", true, "WMV2" )
267
268         /* If the profile is not a custom one, then disable the tabWidget */
269         if ( profileString == "Custom" )
270         {
271             ui.tabWidget->setEnabled( true );
272         }
273         else
274         {
275             ui.tabWidget->setDisabled( true );
276         }
277
278     /* Update the MRL !! */
279     updateMRL();
280 }
281
282 void SoutDialog::toggleSout()
283 {
284     //Toggle all the streaming options.
285     TOGGLEV( ui.HTTPOutput ) ; TOGGLEV( ui.RTPOutput ) ; TOGGLEV( ui.MMSHOutput ) ; TOGGLEV( ui.UDPOutput ) ;
286     TOGGLEV( ui.HTTPEdit ) ; TOGGLEV( ui.RTPEdit ) ; TOGGLEV( ui.MMSHEdit ) ; TOGGLEV( ui.UDPEdit ) ;
287     TOGGLEV( ui.HTTPLabel ) ; TOGGLEV( ui.RTPLabel ) ; TOGGLEV( ui.MMSHLabel ) ; TOGGLEV( ui.UDPLabel ) ;
288     TOGGLEV( ui.HTTPPortLabel ) ; TOGGLEV( ui.RTPPortLabel ) ; TOGGLEV( ui.MMSHPortLabel ) ; TOGGLEV( ui.UDPPortLabel )
289     TOGGLEV( ui.HTTPPort ) ; TOGGLEV( ui.RTPPort ) ; TOGGLEV( ui.MMSHPort ) ; TOGGLEV( ui.UDPPort ) ;
290
291     TOGGLEV( ui.sap ); TOGGLEV( ui.sapName );
292     TOGGLEV( ui.sapGroup ); TOGGLEV( ui.sapGroupLabel );
293     TOGGLEV( ui.ttlLabel ); TOGGLEV( ui.ttl );
294
295     if( b_transcode_only ) okButton->setText( "&Save" );
296     else okButton->setText( "&Stream" );
297
298     updateGeometry();
299 }
300
301 void SoutDialog::ok()
302 {
303     mrl = ui.mrlEdit->text();
304     accept();
305 }
306 void SoutDialog::cancel()
307 {
308     mrl = ui.mrlEdit->text();
309     reject();
310 }
311
312 void SoutDialog::updateMRL()
313 {
314     sout_gui_descr_t sout;
315     memset( &sout, 0, sizeof( sout_gui_descr_t ) );
316     unsigned int counter = 0;
317
318     sout.b_local = ui.localOutput->isChecked();
319     sout.b_file = ui.fileOutput->isChecked();
320     sout.b_http = ui.HTTPOutput->isChecked();
321     sout.b_mms = ui.MMSHOutput->isChecked();
322     sout.b_icecast = ui.IcecastOutput->isChecked();
323     sout.b_rtp = ui.RTPOutput->isChecked();
324     sout.b_udp = ui.UDPOutput->isChecked();
325     sout.b_sap = ui.sap->isChecked();
326     sout.b_all_es = ui.soutAll->isChecked();
327     sout.psz_vcodec = strdup( qtu( ui.vCodecBox->itemData( ui.vCodecBox->currentIndex() ).toString() ) );
328     sout.psz_acodec = strdup( qtu( ui.aCodecBox->itemData( ui.aCodecBox->currentIndex() ).toString() ) );
329     sout.psz_scodec = strdup( qtu( ui.subsCodecBox->itemData( ui.subsCodecBox->currentIndex() ).toString() ) );
330     sout.psz_file = strdup( qtu( ui.fileEdit->text() ) );
331     sout.psz_http = strdup( qtu( ui.HTTPEdit->text() ) );
332     sout.psz_mms = strdup( qtu( ui.MMSHEdit->text() ) );
333     sout.psz_rtp = strdup( qtu( ui.RTPEdit->text() ) );
334     sout.psz_udp = strdup( qtu( ui.UDPEdit->text() ) );
335     sout.psz_icecast = strdup( qtu( ui.IcecastEdit->text() ) );
336     sout.sa_icecast.psz_username = strdup( qtu( ui.IcecastNamePassEdit->text() ) );
337     sout.sa_icecast.psz_password = strdup( qtu( ui.IcecastNamePassEdit->text() ) );
338     sout.psz_icecast_mountpoint = strdup( qtu( ui.IcecastMountpointEdit->text() ) );
339     sout.i_http = ui.HTTPPort->value();
340     sout.i_mms = ui.MMSHPort->value();
341     sout.i_rtp = ui.RTPPort->value();
342     sout.i_udp = ui.UDPPort->value();
343     sout.i_icecast = ui.IcecastPort->value();
344     sout.i_ab = ui.aBitrateSpin->value();
345     sout.i_vb = ui.vBitrateSpin->value();
346     sout.i_channels = ui.aChannelsSpin->value();
347     sout.f_scale = atof( qta( ui.vScaleBox->currentText() ) );
348     sout.psz_group = strdup( qtu( ui.sapGroup->text() ) );
349     sout.psz_name = strdup( qtu( ui.sapName->text() ) );
350
351     if ( sout.b_local ) counter++ ;
352     if ( sout.b_file ) counter++ ;
353     if ( sout.b_http ) counter++ ;
354     if ( sout.b_mms ) counter++ ;
355     if ( sout.b_rtp ) counter++ ;
356     if ( sout.b_udp ) counter ++;
357     if ( sout.b_icecast ) counter ++;
358
359 #define SMUX( x, txt ) if( ui.x->isChecked() ) sout.psz_mux = strdup( txt );
360     SMUX( PSMux, "ps" );
361     SMUX( TSMux, "ts" );
362     SMUX( MPEG1Mux, "mpeg" );
363     SMUX( OggMux, "ogg" );
364     SMUX( ASFMux, "asf" );
365     SMUX( MP4Mux, "mp4" );
366     SMUX( MOVMux, "mov" );
367     SMUX( WAVMux, "wav" );
368     SMUX( RAWMux, "raw" );
369     SMUX( FLVMux, "flv" );
370     SMUX( MKVMux, "mkv" );
371
372     bool trans = false;
373     bool more = false;
374
375     if ( ui.transcodeVideo->isChecked() || ui.transcodeAudio->isChecked() )
376     {
377         if ( ui.transcodeVideo->isChecked() )
378         {
379             mrl = ":sout=#transcode{";
380             mrl.append( "vcodec=" );
381             mrl.append( sout.psz_vcodec );
382             mrl.append( "," );
383             mrl.append( "vb=" );
384             mrl.append( QString::number( sout.i_vb,10 ) );
385             mrl.append( "," );
386             mrl.append( "scale=" );
387             mrl.append( QString::number( sout.f_scale ) );
388             trans = true;
389         }
390
391         if ( ui.transcodeAudio->isChecked() )
392         {
393             if ( trans )
394             {
395                 mrl.append( "," );
396             }
397             else
398             {
399                 mrl = ":sout=#transcode{";
400             }
401             mrl.append( "acodec=" );
402             mrl.append( sout.psz_acodec );
403             mrl.append( "," );
404             mrl.append( "ab=" );
405             mrl.append( QString::number( sout.i_ab,10 ) );
406             mrl.append( "," );
407             mrl.append( "channels=" );
408             mrl.append( QString::number( sout.i_channels,10 ) );
409             trans = true;
410         }
411         mrl.append( "}" );
412     }
413
414     if ( sout.b_local || sout.b_file || sout.b_http || sout.b_mms || sout.b_rtp || sout.b_udp )
415     {
416
417 #define ISMORE() if ( more ) mrl.append( "," );
418 #define ATLEASTONE() if ( counter ) mrl.append( "dst=" );
419
420 #define CHECKMUX() \
421        if( sout.psz_mux ) \
422        {                  \
423          mrl.append( ",mux=");\
424          mrl.append( sout.psz_mux ); \
425        }
426
427         if ( trans )
428         {
429             mrl.append( ":" );
430         }
431         else
432         {
433             mrl = ":sout=#";
434         }
435
436         if ( counter )
437         {
438             mrl.append( "duplicate{" );
439         }
440
441         if ( sout.b_local )
442         {
443             ISMORE();
444             ATLEASTONE()
445                 mrl.append( "display" );
446             more = true;
447         }
448
449         if ( sout.b_file )
450         {
451             ISMORE();
452             ATLEASTONE()
453                 mrl.append( "std{access=file" );
454             CHECKMUX();
455             mrl.append( ",dst=" );
456             mrl.append( sout.psz_file );
457             mrl.append( "}" );
458             more = true;
459         }
460
461         if ( sout.b_http )
462         {
463             ISMORE();
464             ATLEASTONE()
465                 mrl.append( "std{access=http" );
466             CHECKMUX();
467             mrl.append( ",dst=" );
468             mrl.append( sout.psz_http );
469             mrl.append( ":" );
470             mrl.append( QString::number( sout.i_http,10 ) );
471             mrl.append( "}" );
472             more = true;
473         }
474
475         if ( sout.b_mms )
476         {
477             ISMORE();
478             ATLEASTONE()
479                 mrl.append( "std{access=mmsh" );
480             CHECKMUX();
481             mrl.append( ",dst=" );
482             mrl.append( sout.psz_mms );
483             mrl.append( ":" );
484             mrl.append( QString::number( sout.i_mms,10 ) );
485             mrl.append( "}" );
486             more = true;
487         }
488
489         if ( sout.b_rtp )
490         {
491             ISMORE();
492             ATLEASTONE()
493                 mrl.append( "rtp{" );
494             CHECKMUX();
495             mrl.append( ",dst=" );
496             mrl.append( sout.psz_rtp );
497             mrl.append( ":" );
498             mrl.append( QString::number( sout.i_rtp,10 ) );
499             mrl.append( "}" );
500             more = true;
501         }
502
503         if ( sout.b_udp )
504         {
505             ISMORE();
506             ATLEASTONE()
507             mrl.append( "std{access=udp" );
508             CHECKMUX();
509             mrl.append( ",dst=" );
510             mrl.append( sout.psz_udp );
511             mrl.append( ":" );
512             mrl.append( QString::number( sout.i_udp,10 ) );
513             if ( sout.b_sap )
514             {
515                 mrl.append( ",sap," );
516                 mrl.append( "group=\"" );
517                 mrl.append( sout.psz_group );
518                 mrl.append( "\"," );
519                 mrl.append( "name=\"" );
520                 mrl.append( sout.psz_name );
521                 mrl.append( "\"" );
522             }
523             mrl.append( "}" );
524             more = true;
525         }
526
527         if( sout.b_icecast )
528         {
529             // TODO
530         }
531
532         if ( counter )
533         {
534             mrl.append( "}" );
535         }
536     }
537
538 #undef CHECKMUX
539
540     if ( sout.b_all_es )
541         mrl.append( ":sout-all" );
542
543     ui.mrlEdit->setText( mrl );
544     free( sout.psz_acodec ); free( sout.psz_vcodec ); free( sout.psz_scodec );
545     free( sout.psz_file );free( sout.psz_http ); free( sout.psz_mms );
546     free( sout.psz_rtp ); free( sout.psz_udp ); free( sout.psz_mux );
547     free( sout.psz_name ); free( sout.psz_group );
548     free( sout.psz_icecast ); free( sout.psz_icecast_mountpoint );
549     free( sout.sa_icecast.psz_password ); free( sout.sa_icecast.psz_username );
550 }