]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/sout.cpp
Qt: step one for Sout Dialog rework. Introduce a kind of progression and gives more...
[vlc] / modules / gui / qt4 / dialogs / sout.cpp
1 /*****************************************************************************
2  * sout.cpp : Stream output dialog ( old-style )
3  ****************************************************************************
4  * Copyright (C) 2007-2008 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
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include "dialogs/sout.hpp"
35 #include "util/qt_dirs.hpp"
36
37 #include <QString>
38 #include <QFileDialog>
39
40 struct streaming_account_t
41 {
42     char *psz_username; /*< username of account */
43     char *psz_password; /*< password of account */
44 };
45
46 struct sout_gui_descr_t
47 {
48     /* Access types */
49     bool b_local;   /*< local access module */
50     bool b_file;    /*< file access module */
51     bool b_http;    /*< http access module */
52     bool b_mms;     /*< mms access module */
53     bool b_rtp;     /*< rtp access module */
54     bool b_udp;     /*< udp access module */
55     bool b_dump;    /*< dump access module */
56     bool b_icecast; /*< icecast access module */
57
58     char *psz_file;     /*< filename */
59     char *psz_http;     /*< HTTP servername or ipaddress */
60     char *psz_mms;      /*< MMS servername or ipaddress */
61     char *psz_rtp;      /*< RTP servername or ipaddress */
62     char *psz_udp;      /*< UDP servername or ipaddress */
63     char *psz_icecast;  /*< Icecast servername or ipaddress*/
64
65     int32_t i_http;     /*< http port number */
66     int32_t i_mms;      /*< mms port number */
67     int32_t i_rtp;      /*< rtp port number */
68     int32_t i_rtp_audio;      /*< rtp port number */
69     int32_t i_rtp_video;      /*< rtp port number */
70     int32_t i_udp;      /*< udp port number */
71     int32_t i_icecast;  /*< icecast port number */
72
73     /* Mux */
74     char *psz_mux;      /*< name of muxer to use in streaming */
75
76     /* Misc */
77     bool b_sap;   /*< send SAP announcement */
78     bool b_all_es;/*< send all elementary streams from source stream */
79     bool b_sout_keep;
80     char *psz_group;    /*< SAP Group name */
81     char *psz_name;     /*< SAP name */
82     int32_t i_ttl;      /*< Time To Live (TTL) for network traversal */
83
84     /* Icecast */
85     char *psz_icecast_mountpoint;/*< path to Icecast mountpoint */
86     struct streaming_account_t sa_icecast;  /*< Icecast account information */
87 };
88
89 SoutDialog* SoutDialog::instance = NULL;
90
91 SoutDialog::SoutDialog( QWidget *parent, intf_thread_t *_p_intf )
92            : QVLCDialog( parent,  _p_intf )
93 {
94     setWindowTitle( qtr( "Stream Output" ) );
95
96     /* UI stuff */
97     ui.setupUi( this );
98
99     changeUDPandRTPmess( false );
100
101 /* ADD HERE for new profiles */
102 #define ADD_PROFILE( name, shortname ) ui.profileBox->addItem( qtr( name ), QVariant( QString( shortname ) ) );
103 /*    ADD_PROFILE( "Custom" , "Custom" )
104     ADD_PROFILE( "Ogg / Theora", "theora" )
105     ADD_PROFILE( "Ogg / Vorbis", "vorbis" )
106     ADD_PROFILE( "MPEG-2", "mpeg2" )
107     ADD_PROFILE( "MP3", "mp3" )
108     ADD_PROFILE( "MPEG-4 audio AAC", "aac" )
109     ADD_PROFILE( "MPEG-4 / DivX", "mp4" )
110     ADD_PROFILE( "H264", "h264" )
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
116 */
117     ui.mrlEdit->setToolTip ( qtr( "Stream output string.\n"
118                 "This is automatically generated "
119                  "when you change the above settings,\n"
120                  "but you can change it manually." ) ) ;
121
122 //     /* Connect everything to the updateMRL function */
123  #define CB( x ) CONNECT( ui.x, toggled( bool ), this, updateMRL() );
124  #define CT( x ) CONNECT( ui.x, textChanged( const QString ), this, updateMRL() );
125  #define CS( x ) CONNECT( ui.x, valueChanged( int ), this, updateMRL() );
126  #define CC( x ) CONNECT( ui.x, currentIndexChanged( int ), this, updateMRL() );
127     /* Output */
128     CB( fileOutput ); CB( HTTPOutput ); CB( localOutput );
129     CB( RTPOutput ); CB( MMSHOutput ); CB( rawInput ); CB( UDPOutput );
130     CT( fileEdit ); CT( HTTPEdit ); CT( RTPEdit ); CT( MMSHEdit ); CT( UDPEdit );
131     CT( IcecastEdit ); CT( IcecastMountpointEdit ); CT( IcecastNamePassEdit );
132     CS( HTTPPort ); CS( RTPPort ); CS( RTPPort2 ); CS( MMSHPort ); CS( UDPPort );
133     /* Misc */
134     CB( soutAll ); CB( soutKeep );  CS( ttl ); CT( sapName ); CT( sapGroup );
135
136 //    CONNECT( ui.profileSelect, optionsChanged(), this, updateMRL() );
137     CONNECT( ui.fileSelectButton, clicked() , this, fileBrowse()  );
138     CONNECT( ui.rawInput, toggled( bool ), this, setRawOptions( bool ) );
139
140     okButton = new QPushButton( qtr( "&Stream" ) );
141     QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ) );
142
143     okButton->setDefault( true );
144     ui.acceptButtonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
145     ui.acceptButtonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
146
147     BUTTONACT( okButton, ok() );
148     BUTTONACT( cancelButton, cancel() );
149
150     CONNECT( ui.UDPOutput, toggled( bool ), this, changeUDPandRTPmess( bool ) );
151     CONNECT( ui.RTPOutput, clicked(bool), this, RTPtoggled( bool ) );
152
153 }
154
155 void SoutDialog::fileBrowse()
156 {
157     QString fileName = QFileDialog::getSaveFileName( this, qtr( "Save file..." ),
158             "", qtr( "Containers (*.ps *.ts *.mpg *.ogg *.asf *.mp4 *.mov *.wav *.raw *.flv)" ) );
159     ui.fileEdit->setText( toNativeSeparators( fileName ) );
160     updateMRL();
161 }
162
163 void SoutDialog::setRawOptions( bool b_raw )
164 {
165     ui.localOutput->setEnabled( !b_raw );
166     ui.HTTPOutput->setEnabled( !b_raw );
167     ui.MMSHOutput->setEnabled( !b_raw );
168     ui.UDPOutput->setEnabled( !b_raw );
169     ui.RTPOutput->setEnabled( !b_raw );
170     ui.IcecastOutput->setEnabled( !b_raw );
171     ui.UDPRTPLabel->setEnabled( !b_raw );
172
173     if( b_raw ) 
174         ;
175 //        ui.tabWidget->setDisabled( true );
176     else
177         setOptions();
178 }
179
180 void SoutDialog::setOptions()
181 {
182 /*    QString profileString =
183         ui.profileBox->itemData( ui.profileBox->currentIndex() ).toString();
184     msg_Dbg( p_intf, "Profile Used: %s",  qtu( profileString )); */
185     int index;
186
187 #define setProfile( muxName, hasVideo, vCodecName, hasAudio, aCodecName ) \
188     { \
189         ui.muxName ##Mux->setChecked( true ); \
190         \
191         ui.transcodeAudio->setChecked( hasAudio ); \
192         index = ui.aCodecBox->findData( aCodecName );  \
193         if( index >= 0 ) ui.aCodecBox->setCurrentIndex( index ); \
194         \
195         ui.transcodeVideo->setChecked( hasVideo ); \
196         index = ui.vCodecBox->findData( vCodecName );  \
197         if( index >=0 ) ui.vCodecBox->setCurrentIndex( index ); \
198     }
199
200     /* ADD HERE the profiles you want and need */
201 /*    if( profileString == "IPod" ) setProfile( MP4, true, "mp4v", true, "mp4a" )
202     else if( profileString == "theora" ) setProfile( Ogg, true, "theo", true, "vorb" )
203     else if( profileString == "vorbis" ) setProfile( Ogg, false, "", true, "vorb" )
204     else if( profileString == "mpeg2" ) setProfile( TS, true, "mp2v", true, "mpga" )
205     else if( profileString == "mp3" ) setProfile( RAW, false, "", true, "mp3" )
206     else if( profileString == "aac" ) setProfile( MP4, false, "", true, "mp4a" )
207     else if( profileString == "mp4" ) setProfile( MP4, true, "mp4v", true, "mp4a" )
208     else if( profileString == "h264" ) setProfile( TS, true, "h264", true, "mp4a" )
209     else if( profileString == "XBox" ) setProfile( ASF, true, "WMV2", true, "wma" )
210     else if( profileString == "Windows" ) setProfile( ASF, true, "WMV2", true, "wma" )
211     else if( profileString == "PSP" ) setProfile( MP4, true, "mp4v", true, "mp4a" )*/
212
213         /* If the profile is not a custom one, then disable the tabWidget */
214       /*  if ( profileString == "Custom" )
215             ui.tabWidget->setEnabled( true );
216         else
217             ui.tabWidget->setDisabled( true );
218
219     /* Update the MRL !! */
220     updateMRL();
221 }
222
223 void SoutDialog::changeUDPandRTPmess( bool b_udp )
224 {
225     ui.RTPEdit->setVisible( !b_udp );
226     ui.RTPLabel->setVisible( !b_udp );
227     ui.RTPPort->setVisible( !b_udp );
228     ui.RTPPortLabel->setVisible( !b_udp );
229     ui.UDPEdit->setVisible( b_udp );
230     ui.UDPLabel->setVisible( b_udp );
231     ui.UDPPortLabel->setText( b_udp ? qtr( "Port:") : qtr( "Audio Port:" ) );
232     ui.RTPPort2->setVisible( !b_udp );
233     ui.RTPPortLabel2->setVisible( !b_udp );
234 }
235
236 void SoutDialog::RTPtoggled( bool b_en )
237 {
238     if( !b_en )
239     {
240         if( ui.RTPPort->value() == ui.UDPPort->value() )
241         {
242             ui.UDPPort->setValue( ui.UDPPort->value() + 1 );
243         }
244
245         while( ui.RTPPort2->value() == ui.UDPPort->value() ||
246                 ui.RTPPort2->value() == ui.RTPPort->value() )
247         {
248             ui.RTPPort2->setValue( ui.RTPPort2->value() + 1 );
249         }
250     }
251     ui.sap->setEnabled( b_en );
252     ui.RTPLabel->setEnabled( b_en );
253     ui.RTPEdit->setEnabled( b_en );
254     ui.UDPOutput->setEnabled( b_en );
255     ui.UDPRTPLabel->setEnabled( b_en );
256     ui.UDPEdit->setEnabled( b_en );
257     ui.UDPPort->setEnabled( b_en );
258     ui.UDPPortLabel->setEnabled( b_en );
259     ui.RTPPort2->setEnabled( b_en );
260     ui.RTPPortLabel2->setEnabled( b_en );
261 }
262
263 void SoutDialog::ok()
264 {
265     mrl = ui.mrlEdit->toPlainText();
266     accept();
267 }
268
269 void SoutDialog::cancel()
270 {
271     mrl.clear();
272     reject();
273 }
274
275 void SoutDialog::updateMRL()
276 {
277     sout_gui_descr_t sout;
278     memset( &sout, 0, sizeof( sout_gui_descr_t ) );
279     unsigned int counter = 0;
280
281     sout.b_local = ui.localOutput->isChecked();
282     sout.b_file = ui.fileOutput->isChecked();
283     sout.b_http = ui.HTTPOutput->isChecked();
284     sout.b_mms = ui.MMSHOutput->isChecked();
285     sout.b_icecast = ui.IcecastOutput->isChecked();
286     sout.b_rtp = ui.RTPOutput->isChecked();
287     sout.b_udp = ui.UDPOutput->isChecked();
288     sout.b_dump = ui.rawInput->isChecked();
289     sout.b_sap = ui.sap->isChecked();
290     sout.b_all_es = ui.soutAll->isChecked();
291     sout.b_sout_keep = ui.soutKeep->isChecked();
292 /*    sout.psz_vcodec = strdup( qtu( ui.vCodecBox->itemData( ui.vCodecBox->currentIndex() ).toString() ) );
293     sout.psz_acodec = strdup( qtu( ui.aCodecBox->itemData( ui.aCodecBox->currentIndex() ).toString() ) );
294     sout.psz_scodec = strdup( qtu( ui.subsCodecBox->itemData( ui.subsCodecBox->currentIndex() ).toString() ) );*/
295     sout.psz_file = strdup( qtu( ui.fileEdit->text() ) );
296     sout.psz_http = strdup( qtu( ui.HTTPEdit->text() ) );
297     sout.psz_mms = strdup( qtu( ui.MMSHEdit->text() ) );
298     sout.psz_rtp = strdup( qtu( ui.RTPEdit->text() ) );
299     sout.psz_udp = strdup( qtu( ui.UDPEdit->text() ) );
300     sout.psz_icecast = strdup( qtu( ui.IcecastEdit->text() ) );
301     sout.sa_icecast.psz_username = strdup( qtu( ui.IcecastNamePassEdit->text() ) );
302     sout.sa_icecast.psz_password = strdup( qtu( ui.IcecastNamePassEdit->text() ) );
303     sout.psz_icecast_mountpoint = strdup( qtu( ui.IcecastMountpointEdit->text() ) );
304     sout.i_http = ui.HTTPPort->value();
305     sout.i_mms = ui.MMSHPort->value();
306     sout.i_rtp = ui.RTPPort->value();
307     sout.i_rtp_audio = sout.i_udp = ui.UDPPort->value();
308     sout.i_rtp_video = ui.RTPPort2->value();
309     sout.i_icecast = ui.IcecastPort->value();
310 /*    sout.i_ab = ui.aBitrateSpin->value();
311     sout.i_vb = ui.vBitrateSpin->value();
312     sout.i_channels = ui.aChannelsSpin->value();
313     sout.f_scale = atof( qtu( ui.vScaleBox->currentText() ) ); */
314     sout.psz_group = strdup( qtu( ui.sapGroup->text() ) );
315     sout.psz_name = strdup( qtu( ui.sapName->text() ) );
316
317     if ( sout.b_local ) counter++ ;
318     if ( sout.b_file ) counter++ ;
319     if ( sout.b_http ) counter++ ;
320     if ( sout.b_mms ) counter++ ;
321     if ( sout.b_rtp ) counter++ ;
322     if ( sout.b_udp ) counter ++;
323     if ( sout.b_icecast ) counter ++;
324
325     sout.psz_mux = strdup( qtu( ui.profileSelect->getMux() ) );
326
327     bool trans = false;
328     bool more = false;
329
330     SoutMrl smrl( ":sout=#" );
331
332     /* Special case for demuxdump */
333     if ( sout.b_file && sout.b_dump )
334     {
335         mrl = ":demux=dump :demuxdump-file=";
336         mrl.append( qfu( sout.psz_file ) );
337     }
338     else {
339     if( !ui.profileSelect->getTranscode().isEmpty() )
340     {
341         smrl.begin( ui.profileSelect->getTranscode() );
342         smrl.end();
343     }
344
345     /* Protocol output */
346     if ( sout.b_local || sout.b_file || sout.b_http ||
347          sout.b_mms || sout.b_rtp || sout.b_udp || sout.b_icecast )
348     {
349         if( counter > 1 )
350             smrl.begin( "duplicate" );
351
352 #define ADD(m) do { if( counter > 1 ) { \
353                 smrl.option( "dst", m.getMrl() ); \
354             } else { \
355                 smrl.begin( m.getMrl() ); \
356                 smrl.end(); \
357             } } while(0)
358
359         if ( sout.b_local )
360         {
361             SoutMrl m;
362             m.begin( "display" );
363             m.end();
364
365             ADD( m );
366             more = true;
367         }
368
369         if ( sout.b_file )
370         {
371             SoutMrl m;
372
373             m.begin( "std" );
374             m.option( "access", "file" );
375             if( sout.psz_mux )
376                 m.option( "mux", qfu( sout.psz_mux ) );
377             m.option( "dst", qfu( sout.psz_file ) );
378             m.end();
379
380             ADD( m );
381             more = true;
382         }
383
384         if ( sout.b_http )
385         {
386             SoutMrl m;
387
388             m.begin( "std" );
389             m.option(  "access", "http" );
390             if( sout.psz_mux )
391                 m.option( "mux", qfu( sout.psz_mux ) );
392             m.option( "dst", qfu( sout.psz_http ), sout.i_http );
393             m.end();
394
395             ADD( m );
396             more = true;
397         }
398
399         if ( sout.b_mms )
400         {
401             SoutMrl m;
402
403             m.begin( "std" );
404             m.option(  "access", "mmsh" );
405             m.option( "mux", "asfh" );
406             m.option( "dst", qfu( sout.psz_mms ), sout.i_mms );
407             m.end();
408
409             ADD( m );
410             more = true;
411         }
412
413         if ( sout.b_rtp )
414         {
415             SoutMrl m;
416             if ( sout.b_udp )
417             {
418                 m.begin( "std" );
419                 m.option(  "access", "udp" );
420                 if( sout.psz_mux )
421                     m.option( "mux", qfu( sout.psz_mux ) );
422                 m.option( "dst", qfu( sout.psz_udp ), sout.i_udp );
423             }
424             else
425             {
426                 m.begin( "rtp" );
427
428                 if( sout.psz_rtp && *sout.psz_rtp )
429                     m.option( "dst", qfu( sout.psz_rtp ) );
430                 if( sout.psz_mux )
431                     m.option( "mux", qfu( sout.psz_mux ) );
432
433                 m.option( "port", sout.i_rtp );
434                 if( !sout.psz_mux || strncmp( sout.psz_mux, "ts", 2 ) )
435                 {
436                     m.option( "port-audio", sout.i_rtp_audio );
437                     m.option( "port-video", sout.i_rtp_video );
438                 }
439             }
440
441             /* SAP */
442             if ( sout.b_sap )
443             {
444                 m.option( "sap" );
445                 m.option( "group", qfu( sout.psz_group ) );
446                 m.option( "name", qfu( sout.psz_name ) );
447             }
448
449             m.end();
450             ADD( m );
451             more = true;
452         }
453
454         if( sout.b_icecast )
455         {
456             SoutMrl m;
457             QString url;
458
459             url = qfu(sout.sa_icecast.psz_username) + "@"
460                 + qfu( sout.psz_icecast )
461                 + ":" + QString::number( sout.i_icecast, 10 )
462                 + "/" + qfu( sout.psz_icecast_mountpoint );
463
464             m.begin( "std" );
465             m.option( "access", "shout" );
466             m.option( "mux", "ogg" );
467             m.option( "dst", url );
468             m.end();
469
470             ADD( m );
471             more = true;
472         }
473
474         if ( counter )
475             smrl.end();
476
477         mrl = smrl.getMrl();
478     }
479     }
480
481     if ( sout.b_all_es )
482         mrl.append( " :sout-all" );
483
484     if ( sout.b_sout_keep )
485         mrl.append( " :sout-keep" );
486
487     ui.mrlEdit->setPlainText( mrl );
488     free( sout.psz_file );free( sout.psz_http ); free( sout.psz_mms );
489     free( sout.psz_rtp ); free( sout.psz_udp ); free( sout.psz_mux );
490     free( sout.psz_name ); free( sout.psz_group );
491     free( sout.psz_icecast ); free( sout.psz_icecast_mountpoint );
492     free( sout.sa_icecast.psz_password ); free( sout.sa_icecast.psz_username );
493 }
494
495