]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/streamout.cpp
* modules/gui/wxwindows/*: small fixes to the open and streamout dialogs.
[vlc] / modules / gui / wxwindows / streamout.cpp
1 /*****************************************************************************
2  * streamout.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: streamout.cpp,v 1.3 2003/03/29 11:15:14 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/notebook.h>
45 #include <wx/textctrl.h>
46 #include <wx/combobox.h>
47 #include <wx/spinctrl.h>
48 #include <wx/statline.h>
49
50 #include <vlc/intf.h>
51
52 #include "wxwindows.h"
53
54 #ifndef wxRB_SINGLE
55 #   define wxRB_SINGLE 0
56 #endif
57
58 enum
59 {
60     FILE_ACCESS_OUT = 0,
61     HTTP_ACCESS_OUT,
62     UDP_ACCESS_OUT,
63     RTP_ACCESS_OUT
64 };
65
66 enum
67 {
68     TS_ENCAPSULATION = 0,
69     PS_ENCAPSULATION,
70     AVI_ENCAPSULATION,
71     OGG_ENCAPSULATION
72 };
73
74 /*****************************************************************************
75  * Event Table.
76  *****************************************************************************/
77
78 /* IDs for the controls and the menu commands */
79 enum
80 {
81     Notebook_Event = wxID_HIGHEST,
82     MRL_Event,
83
84     FileBrowse_Event,
85     FileName_Event,
86
87     AccessType_Event,
88     AccessRadio1_Event, AccessRadio2_Event,
89     AccessRadio3_Event, AccessRadio4_Event,
90     NetPort_Event,
91     NetAddr_Event,
92
93     EncapsulationRadio1_Event, EncapsulationRadio2_Event,
94     EncapsulationRadio3_Event, EncapsulationRadio4_Event,
95 };
96
97 BEGIN_EVENT_TABLE(SoutDialog, wxDialog)
98     /* Button events */
99     EVT_BUTTON(wxID_OK, SoutDialog::OnOk)
100     EVT_BUTTON(wxID_CANCEL, SoutDialog::OnCancel)
101
102     /* Events generated by the access output panel */
103     EVT_RADIOBUTTON(AccessRadio1_Event, SoutDialog::OnAccessTypeChange)
104     EVT_RADIOBUTTON(AccessRadio2_Event, SoutDialog::OnAccessTypeChange)
105     EVT_RADIOBUTTON(AccessRadio3_Event, SoutDialog::OnAccessTypeChange)
106     EVT_RADIOBUTTON(AccessRadio4_Event, SoutDialog::OnAccessTypeChange)
107     EVT_TEXT(FileName_Event, SoutDialog::OnFileChange)
108     EVT_BUTTON(FileBrowse_Event, SoutDialog::OnFileBrowse)
109     EVT_TEXT(NetPort_Event, SoutDialog::OnNetChange)
110     EVT_SPINCTRL(NetPort_Event, SoutDialog::OnNetChange)
111     EVT_TEXT(NetAddr_Event, SoutDialog::OnNetChange)
112  
113     /* Events generated by the encapsulation panel */
114     EVT_RADIOBUTTON(EncapsulationRadio1_Event,
115                     SoutDialog::OnEncapsulationChange)
116     EVT_RADIOBUTTON(EncapsulationRadio2_Event,
117                     SoutDialog::OnEncapsulationChange)
118     EVT_RADIOBUTTON(EncapsulationRadio3_Event,
119                     SoutDialog::OnEncapsulationChange)
120     EVT_RADIOBUTTON(EncapsulationRadio4_Event,
121                     SoutDialog::OnEncapsulationChange)
122
123 END_EVENT_TABLE()
124
125 /*****************************************************************************
126  * Constructor.
127  *****************************************************************************/
128 SoutDialog::SoutDialog( intf_thread_t *_p_intf, Interface *_p_main_interface ):
129     wxDialog( _p_main_interface, -1, _("Stream Output"), wxDefaultPosition,
130              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
131 {
132     /* Initializations */
133     p_intf = _p_intf;
134     p_main_interface = _p_main_interface;
135
136     /* Create a panel to put everything in */
137     wxPanel *panel = new wxPanel( this, -1 );
138     panel->SetAutoLayout( TRUE );
139
140     /* Create MRL combobox */
141     wxBoxSizer *mrl_sizer_sizer = new wxBoxSizer( wxHORIZONTAL );
142     wxStaticBox *mrl_box = new wxStaticBox( panel, -1,
143                                _("Stream Output MRL") );
144     wxStaticBoxSizer *mrl_sizer = new wxStaticBoxSizer( mrl_box,
145                                                         wxHORIZONTAL );
146     wxStaticText *mrl_label = new wxStaticText( panel, -1,
147                                                 _("Destination Target:") );
148     mrl_combo = new wxComboBox( panel, MRL_Event, mrl,
149                                 wxPoint(20,25), wxSize(120, -1),
150                                 0, NULL );
151     mrl_sizer->Add( mrl_label, 0, wxEXPAND | wxALL, 5 );
152     mrl_sizer->Add( mrl_combo, 1, wxEXPAND | wxALL, 5 );
153     mrl_sizer_sizer->Add( mrl_sizer, 1, wxEXPAND | wxALL, 5 );
154
155     /* Create the output encapsulation panel */
156     encapsulation_panel = EncapsulationPanel( panel );
157
158     /* Create the access output panel */
159     access_panel = AccessPanel( panel );
160
161     /* Separation */
162     wxStaticLine *static_line = new wxStaticLine( panel, wxID_OK );
163
164     /* Create the buttons */
165     wxButton *ok_button = new wxButton( panel, wxID_OK, _("OK") );
166     ok_button->SetDefault();
167     wxButton *cancel_button = new wxButton( panel, wxID_CANCEL, _("Cancel") );
168
169     /* Place everything in sizers */
170     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
171     button_sizer->Add( ok_button, 0, wxALL, 5 );
172     button_sizer->Add( cancel_button, 0, wxALL, 5 );
173     button_sizer->Layout();
174     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
175     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
176     panel_sizer->Add( mrl_sizer_sizer, 0, wxEXPAND, 5 );
177     panel_sizer->Add( access_panel, 1, wxEXPAND | wxALL, 5 );
178     panel_sizer->Add( encapsulation_panel, 0, wxEXPAND | wxALL, 5 );
179     panel_sizer->Add( static_line, 0, wxEXPAND | wxALL, 5 );
180     panel_sizer->Add( button_sizer, 0, wxALIGN_LEFT | wxALIGN_BOTTOM |
181                       wxALL, 5 );
182     panel_sizer->Layout();
183     panel->SetSizerAndFit( panel_sizer );
184     main_sizer->Add( panel, 1, wxGROW, 0 );
185     main_sizer->Layout();
186     SetSizerAndFit( main_sizer );
187
188     /* Update all the values */
189     ParseMRL();
190 }
191
192 SoutDialog::~SoutDialog()
193 {
194 }
195
196 /*****************************************************************************
197  * Private methods.
198  *****************************************************************************/
199 void SoutDialog::UpdateMRL()
200 {
201     wxString encapsulation;
202
203     switch( i_encapsulation_type )
204     {
205     case PS_ENCAPSULATION:
206         encapsulation = "/ps";
207         break;
208     case AVI_ENCAPSULATION:
209         encapsulation = "/avi";
210         break;
211     case OGG_ENCAPSULATION:
212         encapsulation = "/ogg";
213         break;
214     case TS_ENCAPSULATION:
215     default:
216         encapsulation = "/ts";
217         break;
218     }
219
220     switch( i_access_type )
221     {
222     case FILE_ACCESS_OUT:
223         mrl = "file" + encapsulation + ":" + file_combo->GetValue();
224         break;
225
226     case HTTP_ACCESS_OUT:
227         mrl = "http" + encapsulation + ":" + net_addr->GetLineText(0)
228               + wxString::Format( ":%d", net_port->GetValue() );
229         break;
230
231     case UDP_ACCESS_OUT:
232     case RTP_ACCESS_OUT:
233         mrl = ( i_access_type == UDP_ACCESS_OUT ) ? "udp" : "rtp";
234         mrl += encapsulation + ":" + net_addr->GetLineText(0);
235         if( net_port->GetValue() != config_GetInt( p_intf, "server-port" ) )
236         {
237             mrl += wxString::Format( ":%d", net_port->GetValue() );
238         }
239         break;
240     }
241
242     mrl_combo->SetValue( mrl );
243 }
244
245 wxPanel *SoutDialog::AccessPanel( wxWindow* parent )
246 {
247     int i;
248     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
249                                   wxSize(200, 200) );
250
251     wxFlexGridSizer *sizer = new wxFlexGridSizer( 2, 4, 20 );
252     wxStaticBox *panel_box = new wxStaticBox( panel, -1, _("Output Method") );
253     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
254                                                           wxHORIZONTAL );
255
256     static const wxString access_output_array[] =
257     {
258         _("File"),
259         _("HTTP"),
260         _("UDP"),
261         _("RTP")
262     };
263
264     for( i=0; i<4; i++ )
265     {
266         access_radios[i] = new wxRadioButton( panel, AccessRadio1_Event + i,
267                                               access_output_array[i],
268                                               wxDefaultPosition, wxDefaultSize,
269                                               wxRB_SINGLE );
270
271         access_subpanels[i] = new wxPanel( panel, -1,
272                                            wxDefaultPosition, wxDefaultSize );
273     }
274
275     /* File row */
276     wxFlexGridSizer *subpanel_sizer;
277     wxStaticText *label;
278     subpanel_sizer = new wxFlexGridSizer( 3, 1, 20 );
279     label = new wxStaticText( access_subpanels[0], -1, _("Filename") );
280     file_combo = new wxComboBox( access_subpanels[0], FileName_Event, "",
281                                  wxPoint(20,25), wxSize(200, -1), 0, NULL );
282     wxButton *browse_button = new wxButton( access_subpanels[0],
283                                             FileBrowse_Event, _("Browse...") );
284     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
285     subpanel_sizer->Add( file_combo, 1,
286                          wxEXPAND | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
287     subpanel_sizer->Add( browse_button, 0,
288                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
289
290     access_subpanels[0]->SetSizerAndFit( subpanel_sizer );
291
292     /* Net row */
293     subpanel_sizer = new wxFlexGridSizer( 4, 1, 20 );
294     label = new wxStaticText( access_subpanels[2], -1, _("Address") );
295     net_addr = new wxTextCtrl( access_subpanels[2], NetAddr_Event, "",
296                                wxDefaultPosition, wxSize( 200, -1 ),
297                                wxTE_PROCESS_ENTER);
298     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
299     subpanel_sizer->Add( net_addr, 1,
300                          wxEXPAND | wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
301
302     int val = config_GetInt( p_intf, "server-port" );
303     label = new wxStaticText( access_subpanels[2], -1, _("Port") );
304     net_port = new wxSpinCtrl( access_subpanels[2], NetPort_Event,
305                                wxString::Format(_("%d"), val),
306                                wxDefaultPosition, wxDefaultSize,
307                                wxSP_ARROW_KEYS,
308                                0, 16000, val );
309
310     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
311     subpanel_sizer->Add( net_port, 0,
312                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
313
314     access_subpanels[2]->SetSizerAndFit( subpanel_sizer );
315
316
317     /* Stuff everything into the main panel */
318     for( i=0; i<4; i++ )
319     {
320         sizer->Add( access_radios[i], 0,
321                     wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
322         sizer->Add( access_subpanels[i], 1, wxEXPAND | wxALIGN_CENTER_VERTICAL
323                     | wxALIGN_LEFT );
324     }
325
326     panel_sizer->Add( sizer, 1, wxEXPAND, 0 );
327
328     panel->SetSizerAndFit( panel_sizer );
329
330     return panel;
331 }
332
333 wxPanel *SoutDialog::EncapsulationPanel( wxWindow* parent )
334 {
335     int i;
336     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
337                                   wxSize(200, 200) );
338
339     wxStaticBox *panel_box = new wxStaticBox( panel, -1,
340                                               _("Encapsulation Method") );
341     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
342                                                           wxHORIZONTAL );
343
344     static const wxString encapsulation_array[] =
345     {
346         _("MPEG TS"),
347         _("MPEG PS"),
348         _("AVI"),
349         _("Ogg")
350     };
351
352     /* Stuff everything into the main panel */
353     for( i=0; i<4; i++ )
354     {
355         encapsulation_radios[i] =
356             new wxRadioButton( panel, EncapsulationRadio1_Event + i,
357                                encapsulation_array[i] );
358         panel_sizer->Add( encapsulation_radios[i], 0,
359                           wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL, 5 );
360     }
361
362     panel->SetSizerAndFit( panel_sizer );
363
364     /* Update encapsulation panel */
365     encapsulation_radios[0]->Enable();
366     i_encapsulation_type = TS_ENCAPSULATION;
367
368     return panel;
369 }
370
371 void SoutDialog::ParseMRL()
372 {
373     /* Initialise MRL value */
374     char *psz_sout = config_GetPsz( p_intf, "sout" );
375     if( psz_sout )
376     {
377         mrl = psz_sout;
378         free( psz_sout );
379     }
380
381     /* Parse the MRL */
382     wxString access = mrl.BeforeFirst( '/' );
383     wxString encapsulation = mrl.BeforeFirst( ':' ).AfterFirst('/');
384
385     if( !access.Cmp( "http" ) )
386     {
387         i_access_type = HTTP_ACCESS_OUT;
388     }
389     else if( !access.Cmp( "udp" ) )
390     {
391         i_access_type = UDP_ACCESS_OUT;
392     }
393     else if( !access.Cmp( "rtp" ) )
394     {
395         i_access_type = RTP_ACCESS_OUT;
396     }
397     else
398     {
399         i_access_type = FILE_ACCESS_OUT;
400     }
401
402     if( !encapsulation.Cmp( "ps" ) )
403     {
404         i_encapsulation_type = PS_ENCAPSULATION;
405     }
406     else if( !encapsulation.Cmp( "avi" ) )
407     {
408         i_encapsulation_type = AVI_ENCAPSULATION;
409     }
410     else if( !encapsulation.Cmp( "ogg" ) )
411     {
412         i_encapsulation_type = OGG_ENCAPSULATION;
413     }
414     else
415     {
416         i_encapsulation_type = TS_ENCAPSULATION;
417     }
418
419     wxString second_part = mrl.AfterFirst( ':' );
420
421     if( i_access_type == FILE_ACCESS_OUT )
422     {
423         /* The whole second part of the MRL is the filename */
424         file_combo->SetValue( second_part );
425     }
426     else
427     {
428         /* we've got address:port */
429         wxString address = second_part.BeforeLast( ':' );
430         net_addr->SetValue( address );
431
432         long int i_port;
433         wxString port = second_part.AfterLast( ':' );
434         if( port.ToLong( &i_port ) )
435         {
436             net_port->SetValue( i_port );
437         }
438         else
439         {
440             net_port->SetValue( config_GetInt( p_intf, "server-port" ) );
441         }
442     }
443
444     /* Update access output panel */
445     wxCommandEvent dummy_event;
446     dummy_event.SetId( AccessRadio1_Event + i_access_type );
447     OnAccessTypeChange( dummy_event );
448
449     /* Update encapsulation output panel */
450     dummy_event.SetId( EncapsulationRadio1_Event + i_encapsulation_type );
451     OnEncapsulationChange( dummy_event );
452 }
453
454 /*****************************************************************************
455  * Events methods.
456  *****************************************************************************/
457 void SoutDialog::OnOk( wxCommandEvent& WXUNUSED(event) )
458 {
459     EndModal( wxID_OK );
460 }
461
462 void SoutDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
463 {
464     EndModal( wxID_CANCEL );
465 }
466
467 void SoutDialog::OnMRLChange( wxCommandEvent& event )
468 {
469     mrl = event.GetString();
470 }
471
472 /*****************************************************************************
473  * Access output panel event methods.
474  *****************************************************************************/
475 void SoutDialog::OnAccessTypeChange( wxCommandEvent& event )
476 {
477     int i;
478     i_access_type = event.GetId() - AccessRadio1_Event;
479
480     switch( i_access_type )
481     {
482     case FILE_ACCESS_OUT:
483         access_subpanels[0]->Enable();
484         access_subpanels[2]->Disable();
485         access_subpanels[3]->Disable();
486         for( i = 1; i < 4; i++ )
487         {
488             encapsulation_radios[i]->Enable();
489         }
490         break;
491
492     case HTTP_ACCESS_OUT:
493         access_subpanels[0]->Disable();
494         access_subpanels[2]->Enable();
495         access_subpanels[3]->Disable();
496         for( i = 1; i < 4; i++ )
497         {
498             encapsulation_radios[i]->Enable();
499         }
500         break;
501
502     case UDP_ACCESS_OUT:
503     case RTP_ACCESS_OUT:
504         access_subpanels[0]->Disable();
505         access_subpanels[2]->Enable();
506         access_subpanels[3]->Enable();
507         for( i = 1; i < 4; i++ )
508         {
509             encapsulation_radios[i]->Disable();
510         }
511         encapsulation_radios[TS_ENCAPSULATION]->SetValue(true);
512         i_encapsulation_type = TS_ENCAPSULATION;
513         break;
514     }
515
516     for( i = 0; i < 4; i++ )
517     {
518         access_radios[i]->SetValue( event.GetId() == (AccessRadio1_Event+i) );
519     }
520
521     UpdateMRL();
522 }
523
524 /*****************************************************************************
525  * File access output event methods.
526  *****************************************************************************/
527 void SoutDialog::OnFileChange( wxCommandEvent& WXUNUSED(event) )
528 {
529     UpdateMRL();
530 }
531
532 void SoutDialog::OnFileBrowse( wxCommandEvent& WXUNUSED(event) )
533 {
534     wxFileDialog dialog( this, _("Save file"), "", "", "*.*", wxSAVE );
535
536     if( dialog.ShowModal() == wxID_OK )
537     {
538         file_combo->SetValue( dialog.GetPath() );
539         UpdateMRL();
540     }
541 }
542
543 /*****************************************************************************
544  * Net access output event methods.
545  *****************************************************************************/
546 void SoutDialog::OnNetChange( wxCommandEvent& WXUNUSED(event) )
547 {
548     UpdateMRL();
549 }
550
551 /*****************************************************************************
552  * Encapsulation panel event methods.
553  *****************************************************************************/
554 void SoutDialog::OnEncapsulationChange( wxCommandEvent& event )
555 {
556     i_encapsulation_type = event.GetId() - EncapsulationRadio1_Event;
557     UpdateMRL();
558 }