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