]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/sout.cpp
Qt: Profiles Editor for the transcoding.
[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                      bool _transcode_only ) : QVLCDialog( parent,  _p_intf )
93 {
94     setWindowTitle( qtr( "Stream Output" ) );
95
96     b_transcode_only = _transcode_only;
97
98     /* UI stuff */
99     ui.setupUi( this );
100
101     changeUDPandRTPmess( false );
102
103 /* ADD HERE for new profiles */
104 #define ADD_PROFILE( name, shortname ) ui.profileBox->addItem( qtr( name ), QVariant( QString( shortname ) ) );
105 /*    ADD_PROFILE( "Custom" , "Custom" )
106     ADD_PROFILE( "Ogg / Theora", "theora" )
107     ADD_PROFILE( "Ogg / Vorbis", "vorbis" )
108     ADD_PROFILE( "MPEG-2", "mpeg2" )
109     ADD_PROFILE( "MP3", "mp3" )
110     ADD_PROFILE( "MPEG-4 audio AAC", "aac" )
111     ADD_PROFILE( "MPEG-4 / DivX", "mp4" )
112     ADD_PROFILE( "H264", "h264" )
113     ADD_PROFILE( "IPod (mp4/aac)", "IPod" )
114     ADD_PROFILE( "XBox", "XBox" )
115     ADD_PROFILE( "Windows (wmv/asf)", "Windows" )
116     ADD_PROFILE( "PSP", "PSP")
117
118 */
119     ui.mrlEdit->setToolTip ( qtr( "Stream output string.\n"
120                 "This is automatically generated "
121                  "when you change the above settings,\n"
122                  "but you can change it manually." ) ) ;
123
124 //     /* Connect everything to the updateMRL function */
125  #define CB( x ) CONNECT( ui.x, toggled( bool ), this, updateMRL() );
126  #define CT( x ) CONNECT( ui.x, textChanged( const QString ), this, updateMRL() );
127  #define CS( x ) CONNECT( ui.x, valueChanged( int ), this, updateMRL() );
128  #define CC( x ) CONNECT( ui.x, currentIndexChanged( int ), this, updateMRL() );
129     /* Output */
130     CB( fileOutput ); CB( HTTPOutput ); CB( localOutput );
131     CB( RTPOutput ); CB( MMSHOutput ); CB( rawInput ); CB( UDPOutput );
132     CT( fileEdit ); CT( HTTPEdit ); CT( RTPEdit ); CT( MMSHEdit ); CT( UDPEdit );
133     CT( IcecastEdit ); CT( IcecastMountpointEdit ); CT( IcecastNamePassEdit );
134     CS( HTTPPort ); CS( RTPPort ); CS( RTPPort2 ); CS( MMSHPort ); CS( UDPPort );
135     /* Misc */
136     CB( soutAll ); CB( soutKeep );  CS( ttl ); CT( sapName ); CT( sapGroup );
137
138 //    CONNECT( ui.profileSelect, optionsChanged(), this, updateMRL() );
139     CONNECT( ui.fileSelectButton, clicked() , this, fileBrowse()  );
140     CONNECT( ui.rawInput, toggled( bool ), this, setRawOptions( bool ) );
141
142     okButton = new QPushButton( qtr( "&Stream" ) );
143     QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ) );
144
145     okButton->setDefault( true );
146     ui.acceptButtonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
147     ui.acceptButtonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
148
149     BUTTONACT( okButton, ok() );
150     BUTTONACT( cancelButton, cancel() );
151
152     CONNECT( ui.UDPOutput, toggled( bool ), this, changeUDPandRTPmess( bool ) );
153     CONNECT( ui.RTPOutput, clicked(bool), this, RTPtoggled( bool ) );
154
155     if( b_transcode_only ) toggleSout();
156 }
157
158 void SoutDialog::fileBrowse()
159 {
160     QString fileName = QFileDialog::getSaveFileName( this, qtr( "Save file..." ),
161             "", qtr( "Containers (*.ps *.ts *.mpg *.ogg *.asf *.mp4 *.mov *.wav *.raw *.flv)" ) );
162     ui.fileEdit->setText( toNativeSeparators( fileName ) );
163     updateMRL();
164 }
165
166 void SoutDialog::setRawOptions( bool b_raw )
167 {
168     ui.localOutput->setEnabled( !b_raw );
169     ui.HTTPOutput->setEnabled( !b_raw );
170     ui.MMSHOutput->setEnabled( !b_raw );
171     ui.UDPOutput->setEnabled( !b_raw );
172     ui.RTPOutput->setEnabled( !b_raw );
173     ui.IcecastOutput->setEnabled( !b_raw );
174     ui.UDPRTPLabel->setEnabled( !b_raw );
175
176     if( b_raw ) 
177         ;
178 //        ui.tabWidget->setDisabled( true );
179     else
180         setOptions();
181 }
182
183 void SoutDialog::setOptions()
184 {
185 /*    QString profileString =
186         ui.profileBox->itemData( ui.profileBox->currentIndex() ).toString();
187     msg_Dbg( p_intf, "Profile Used: %s",  qtu( profileString )); */
188     int index;
189
190 #define setProfile( muxName, hasVideo, vCodecName, hasAudio, aCodecName ) \
191     { \
192         ui.muxName ##Mux->setChecked( true ); \
193         \
194         ui.transcodeAudio->setChecked( hasAudio ); \
195         index = ui.aCodecBox->findData( aCodecName );  \
196         if( index >= 0 ) ui.aCodecBox->setCurrentIndex( index ); \
197         \
198         ui.transcodeVideo->setChecked( hasVideo ); \
199         index = ui.vCodecBox->findData( vCodecName );  \
200         if( index >=0 ) ui.vCodecBox->setCurrentIndex( index ); \
201     }
202
203     /* ADD HERE the profiles you want and need */
204 /*    if( profileString == "IPod" ) setProfile( MP4, true, "mp4v", true, "mp4a" )
205     else if( profileString == "theora" ) setProfile( Ogg, true, "theo", true, "vorb" )
206     else if( profileString == "vorbis" ) setProfile( Ogg, false, "", true, "vorb" )
207     else if( profileString == "mpeg2" ) setProfile( TS, true, "mp2v", true, "mpga" )
208     else if( profileString == "mp3" ) setProfile( RAW, false, "", true, "mp3" )
209     else if( profileString == "aac" ) setProfile( MP4, false, "", true, "mp4a" )
210     else if( profileString == "mp4" ) setProfile( MP4, true, "mp4v", true, "mp4a" )
211     else if( profileString == "h264" ) setProfile( TS, true, "h264", true, "mp4a" )
212     else if( profileString == "XBox" ) setProfile( ASF, true, "WMV2", true, "wma" )
213     else if( profileString == "Windows" ) setProfile( ASF, true, "WMV2", true, "wma" )
214     else if( profileString == "PSP" ) setProfile( MP4, true, "mp4v", true, "mp4a" )*/
215
216         /* If the profile is not a custom one, then disable the tabWidget */
217       /*  if ( profileString == "Custom" )
218             ui.tabWidget->setEnabled( true );
219         else
220             ui.tabWidget->setDisabled( true );
221
222     /* Update the MRL !! */
223     updateMRL();
224 }
225
226 void SoutDialog::toggleSout()
227 {
228     //Toggle all the streaming options.
229 #define HIDEORSHOW(x) if( b_transcode_only ) x->hide(); else x->show();
230     HIDEORSHOW( ui.HTTPOutput ) ; HIDEORSHOW( ui.RTPOutput ) ; HIDEORSHOW( ui.MMSHOutput ) ; HIDEORSHOW( ui.UDPOutput ) ;
231     HIDEORSHOW( ui.HTTPEdit ) ; HIDEORSHOW( ui.RTPEdit ) ; HIDEORSHOW( ui.MMSHEdit ) ; HIDEORSHOW( ui.UDPEdit ) ;
232     HIDEORSHOW( ui.HTTPLabel ) ; HIDEORSHOW( ui.RTPLabel ) ; HIDEORSHOW( ui.MMSHLabel ) ; HIDEORSHOW( ui.UDPLabel ) ;
233     HIDEORSHOW( ui.HTTPPortLabel ) ; HIDEORSHOW( ui.RTPPortLabel ) ; HIDEORSHOW( ui.MMSHPortLabel ) ; HIDEORSHOW( ui.UDPPortLabel )
234     HIDEORSHOW( ui.HTTPPort ) ; HIDEORSHOW( ui.RTPPort ) ; HIDEORSHOW( ui.MMSHPort ) ; HIDEORSHOW( ui.UDPPort ) ; HIDEORSHOW( ui.RTPPortLabel2 ); HIDEORSHOW( ui.RTPPort2 ); HIDEORSHOW( ui.UDPRTPLabel )
235
236     HIDEORSHOW( ui.sap ); HIDEORSHOW( ui.sapName );
237     HIDEORSHOW( ui.sapGroup ); HIDEORSHOW( ui.sapGroupLabel );
238     HIDEORSHOW( ui.ttlLabel ); HIDEORSHOW( ui.ttl );
239     HIDEORSHOW( ui.soutKeep );
240
241     HIDEORSHOW( ui.IcecastOutput ); HIDEORSHOW( ui.IcecastEdit );
242     HIDEORSHOW( ui.IcecastNamePassEdit ); HIDEORSHOW( ui.IcecastMountpointEdit );
243     HIDEORSHOW( ui.IcecastPort ); HIDEORSHOW( ui.IcecastLabel );
244     HIDEORSHOW( ui.IcecastPortLabel );
245     HIDEORSHOW( ui.IcecastMountpointLabel ); HIDEORSHOW( ui.IcecastNameLabel );
246 #undef HIDEORSHOW
247
248     if( b_transcode_only ) okButton->setText( "&Save" );
249     else okButton->setText( "&Stream" );
250
251     setMinimumHeight( 500 );
252     resize( width(), sizeHint().height() );
253 }
254
255 void SoutDialog::changeUDPandRTPmess( bool b_udp )
256 {
257     ui.RTPEdit->setVisible( !b_udp );
258     ui.RTPLabel->setVisible( !b_udp );
259     ui.RTPPort->setVisible( !b_udp );
260     ui.RTPPortLabel->setVisible( !b_udp );
261     ui.UDPEdit->setVisible( b_udp );
262     ui.UDPLabel->setVisible( b_udp );
263     ui.UDPPortLabel->setText( b_udp ? qtr( "Port:") : qtr( "Audio Port:" ) );
264     ui.RTPPort2->setVisible( !b_udp );
265     ui.RTPPortLabel2->setVisible( !b_udp );
266 }
267
268 void SoutDialog::RTPtoggled( bool b_en )
269 {
270     if( !b_en )
271     {
272         if( ui.RTPPort->value() == ui.UDPPort->value() )
273         {
274             ui.UDPPort->setValue( ui.UDPPort->value() + 1 );
275         }
276
277         while( ui.RTPPort2->value() == ui.UDPPort->value() ||
278                 ui.RTPPort2->value() == ui.RTPPort->value() )
279         {
280             ui.RTPPort2->setValue( ui.RTPPort2->value() + 1 );
281         }
282     }
283     ui.sap->setEnabled( b_en );
284     ui.RTPLabel->setEnabled( b_en );
285     ui.RTPEdit->setEnabled( b_en );
286     ui.UDPOutput->setEnabled( b_en );
287     ui.UDPRTPLabel->setEnabled( b_en );
288     ui.UDPEdit->setEnabled( b_en );
289     ui.UDPPort->setEnabled( b_en );
290     ui.UDPPortLabel->setEnabled( b_en );
291     ui.RTPPort2->setEnabled( b_en );
292     ui.RTPPortLabel2->setEnabled( b_en );
293 }
294
295 void SoutDialog::ok()
296 {
297     mrl = ui.mrlEdit->text();
298     accept();
299 }
300
301 void SoutDialog::cancel()
302 {
303     mrl.clear();
304     reject();
305 }
306
307 void SoutDialog::updateMRL()
308 {
309     sout_gui_descr_t sout;
310     memset( &sout, 0, sizeof( sout_gui_descr_t ) );
311     unsigned int counter = 0;
312
313     sout.b_local = ui.localOutput->isChecked();
314     sout.b_file = ui.fileOutput->isChecked();
315     sout.b_http = ui.HTTPOutput->isChecked();
316     sout.b_mms = ui.MMSHOutput->isChecked();
317     sout.b_icecast = ui.IcecastOutput->isChecked();
318     sout.b_rtp = ui.RTPOutput->isChecked();
319     sout.b_udp = ui.UDPOutput->isChecked();
320     sout.b_dump = ui.rawInput->isChecked();
321     sout.b_sap = ui.sap->isChecked();
322     sout.b_all_es = ui.soutAll->isChecked();
323     sout.b_sout_keep = ui.soutKeep->isChecked();
324 /*    sout.psz_vcodec = strdup( qtu( ui.vCodecBox->itemData( ui.vCodecBox->currentIndex() ).toString() ) );
325     sout.psz_acodec = strdup( qtu( ui.aCodecBox->itemData( ui.aCodecBox->currentIndex() ).toString() ) );
326     sout.psz_scodec = strdup( qtu( ui.subsCodecBox->itemData( ui.subsCodecBox->currentIndex() ).toString() ) );*/
327     sout.psz_file = strdup( qtu( ui.fileEdit->text() ) );
328     sout.psz_http = strdup( qtu( ui.HTTPEdit->text() ) );
329     sout.psz_mms = strdup( qtu( ui.MMSHEdit->text() ) );
330     sout.psz_rtp = strdup( qtu( ui.RTPEdit->text() ) );
331     sout.psz_udp = strdup( qtu( ui.UDPEdit->text() ) );
332     sout.psz_icecast = strdup( qtu( ui.IcecastEdit->text() ) );
333     sout.sa_icecast.psz_username = strdup( qtu( ui.IcecastNamePassEdit->text() ) );
334     sout.sa_icecast.psz_password = strdup( qtu( ui.IcecastNamePassEdit->text() ) );
335     sout.psz_icecast_mountpoint = strdup( qtu( ui.IcecastMountpointEdit->text() ) );
336     sout.i_http = ui.HTTPPort->value();
337     sout.i_mms = ui.MMSHPort->value();
338     sout.i_rtp = ui.RTPPort->value();
339     sout.i_rtp_audio = sout.i_udp = ui.UDPPort->value();
340     sout.i_rtp_video = ui.RTPPort2->value();
341     sout.i_icecast = ui.IcecastPort->value();
342 /*    sout.i_ab = ui.aBitrateSpin->value();
343     sout.i_vb = ui.vBitrateSpin->value();
344     sout.i_channels = ui.aChannelsSpin->value();
345     sout.f_scale = atof( qtu( ui.vScaleBox->currentText() ) ); */
346     sout.psz_group = strdup( qtu( ui.sapGroup->text() ) );
347     sout.psz_name = strdup( qtu( ui.sapName->text() ) );
348
349     if ( sout.b_local ) counter++ ;
350     if ( sout.b_file ) counter++ ;
351     if ( sout.b_http ) counter++ ;
352     if ( sout.b_mms ) counter++ ;
353     if ( sout.b_rtp ) counter++ ;
354     if ( sout.b_udp ) counter ++;
355     if ( sout.b_icecast ) counter ++;
356
357     sout.psz_mux = strdup( qtu( ui.profileSelect->getMux() ) );
358
359     bool trans = false;
360     bool more = false;
361
362     SoutMrl smrl( ":sout=#" );
363
364     /* Special case for demuxdump */
365     if ( sout.b_file && sout.b_dump )
366     {
367         mrl = ":demux=dump :demuxdump-file=";
368         mrl.append( qfu( sout.psz_file ) );
369     }
370     else {
371     if( !ui.profileSelect->getTranscode().isEmpty() )
372     {
373         smrl.begin( ui.profileSelect->getTranscode() );
374         smrl.end();
375     }
376
377     /* Protocol output */
378     if ( sout.b_local || sout.b_file || sout.b_http ||
379          sout.b_mms || sout.b_rtp || sout.b_udp || sout.b_icecast )
380     {
381         if( counter > 1 )
382             smrl.begin( "duplicate" );
383
384 #define ADD(m) do { if( counter > 1 ) { \
385                 smrl.option( "dst", m.getMrl() ); \
386             } else { \
387                 smrl.begin( m.getMrl() ); \
388                 smrl.end(); \
389             } } while(0)
390
391         if ( sout.b_local )
392         {
393             SoutMrl m;
394             m.begin( "display" );
395             m.end();
396
397             ADD( m );
398             more = true;
399         }
400
401         if ( sout.b_file )
402         {
403             SoutMrl m;
404
405             m.begin( "std" );
406             m.option( "access", "file" );
407             if( sout.psz_mux )
408                 m.option( "mux", qfu( sout.psz_mux ) );
409             m.option( "dst", qfu( sout.psz_file ) );
410             m.end();
411
412             ADD( m );
413             more = true;
414         }
415
416         if ( sout.b_http )
417         {
418             SoutMrl m;
419
420             m.begin( "std" );
421             m.option(  "access", "http" );
422             if( sout.psz_mux )
423                 m.option( "mux", qfu( sout.psz_mux ) );
424             m.option( "dst", qfu( sout.psz_http ), sout.i_http );
425             m.end();
426
427             ADD( m );
428             more = true;
429         }
430
431         if ( sout.b_mms )
432         {
433             SoutMrl m;
434
435             m.begin( "std" );
436             m.option(  "access", "mmsh" );
437             m.option( "mux", "asfh" );
438             m.option( "dst", qfu( sout.psz_mms ), sout.i_mms );
439             m.end();
440
441             ADD( m );
442             more = true;
443         }
444
445         if ( sout.b_rtp )
446         {
447             SoutMrl m;
448             if ( sout.b_udp )
449             {
450                 m.begin( "std" );
451                 m.option(  "access", "udp" );
452                 if( sout.psz_mux )
453                     m.option( "mux", qfu( sout.psz_mux ) );
454                 m.option( "dst", qfu( sout.psz_udp ), sout.i_udp );
455             }
456             else
457             {
458                 m.begin( "rtp" );
459
460                 if( sout.psz_rtp && *sout.psz_rtp )
461                     m.option( "dst", qfu( sout.psz_rtp ) );
462                 if( sout.psz_mux )
463                     m.option( "mux", qfu( sout.psz_mux ) );
464
465                 m.option( "port", sout.i_rtp );
466                 if( !sout.psz_mux || strncmp( sout.psz_mux, "ts", 2 ) )
467                 {
468                     m.option( "port-audio", sout.i_rtp_audio );
469                     m.option( "port-video", sout.i_rtp_video );
470                 }
471             }
472
473             /* SAP */
474             if ( sout.b_sap )
475             {
476                 m.option( "sap" );
477                 m.option( "group", qfu( sout.psz_group ) );
478                 m.option( "name", qfu( sout.psz_name ) );
479             }
480
481             m.end();
482             ADD( m );
483             more = true;
484         }
485
486         if( sout.b_icecast )
487         {
488             SoutMrl m;
489             QString url;
490
491             url = qfu(sout.sa_icecast.psz_username) + "@"
492                 + qfu( sout.psz_icecast )
493                 + ":" + QString::number( sout.i_icecast, 10 )
494                 + "/" + qfu( sout.psz_icecast_mountpoint );
495
496             m.begin( "std" );
497             m.option( "access", "shout" );
498             m.option( "mux", "ogg" );
499             m.option( "dst", url );
500             m.end();
501
502             ADD( m );
503             more = true;
504         }
505
506         if ( counter )
507             smrl.end();
508
509         mrl = smrl.getMrl();
510     }
511     }
512
513     if ( sout.b_all_es )
514         mrl.append( " :sout-all" );
515
516     if ( sout.b_sout_keep )
517         mrl.append( " :sout-keep" );
518
519     ui.mrlEdit->setText( mrl );
520     free( sout.psz_file );free( sout.psz_http ); free( sout.psz_mms );
521     free( sout.psz_rtp ); free( sout.psz_udp ); free( sout.psz_mux );
522     free( sout.psz_name ); free( sout.psz_group );
523     free( sout.psz_icecast ); free( sout.psz_icecast_mountpoint );
524     free( sout.sa_icecast.psz_password ); free( sout.sa_icecast.psz_username );
525 }
526
527