]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/streamout.cpp
885aa6eff77adb4f6e8d32921e05055c44b4786b
[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.2 2003/03/22 11:21:58 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         mrl = "udp" + encapsulation + ":"
233               + net_addr->GetLineText(0)
234               + wxString::Format( ":%d", net_port->GetValue() );
235         break;
236
237     case RTP_ACCESS_OUT:
238         mrl = "rtp" + encapsulation + ":"
239               + net_addr->GetLineText(0)
240               + wxString::Format( ":%d", net_port->GetValue() );
241         break;
242     }
243
244     mrl_combo->SetValue( mrl );
245 }
246
247 wxPanel *SoutDialog::AccessPanel( wxWindow* parent )
248 {
249     int i;
250     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
251                                   wxSize(200, 200) );
252
253     wxFlexGridSizer *sizer = new wxFlexGridSizer( 2, 4, 20 );
254     wxStaticBox *panel_box = new wxStaticBox( panel, -1, _("Output Method") );
255     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
256                                                           wxHORIZONTAL );
257
258     static const wxString access_output_array[] =
259     {
260         _("File"),
261         _("HTTP"),
262         _("UDP"),
263         _("RTP")
264     };
265
266     for( i=0; i<4; i++ )
267     {
268         access_radios[i] = new wxRadioButton( panel, AccessRadio1_Event + i,
269                                               access_output_array[i],
270                                               wxDefaultPosition, wxDefaultSize,
271                                               wxRB_SINGLE );
272
273         access_subpanels[i] = new wxPanel( panel, -1,
274                                            wxDefaultPosition, wxDefaultSize );
275     }
276
277     /* File row */
278     wxFlexGridSizer *subpanel_sizer;
279     wxStaticText *label;
280     subpanel_sizer = new wxFlexGridSizer( 3, 1, 20 );
281     label = new wxStaticText( access_subpanels[0], -1, _("Filename") );
282     file_combo = new wxComboBox( access_subpanels[0], FileName_Event, "",
283                                  wxPoint(20,25), wxSize(200, -1), 0, NULL );
284     wxButton *browse_button = new wxButton( access_subpanels[0],
285                                             FileBrowse_Event, _("Browse...") );
286     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
287     subpanel_sizer->Add( file_combo, 1,
288                          wxEXPAND | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
289     subpanel_sizer->Add( browse_button, 0,
290                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
291
292     access_subpanels[0]->SetSizerAndFit( subpanel_sizer );
293
294     /* Net row */
295     subpanel_sizer = new wxFlexGridSizer( 4, 1, 20 );
296     label = new wxStaticText( access_subpanels[2], -1, _("Address") );
297     net_addr = new wxTextCtrl( access_subpanels[2], NetAddr_Event, "",
298                                wxDefaultPosition, wxSize( 200, -1 ),
299                                wxTE_PROCESS_ENTER);
300     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
301     subpanel_sizer->Add( net_addr, 1,
302                          wxEXPAND | wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
303
304     label = new wxStaticText( access_subpanels[2], -1, _("Port") );
305     net_port = new wxSpinCtrl( access_subpanels[2], NetPort_Event,
306                                wxString::Format(_("%d"), 0/*val*/),
307                                wxDefaultPosition, wxDefaultSize,
308                                wxSP_ARROW_KEYS,
309                                0, 16000, 0/*val*/);
310
311     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
312     subpanel_sizer->Add( net_port, 0,
313                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
314
315     access_subpanels[2]->SetSizerAndFit( subpanel_sizer );
316
317
318     /* Stuff everything into the main panel */
319     for( i=0; i<4; i++ )
320     {
321         sizer->Add( access_radios[i], 0,
322                     wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
323         sizer->Add( access_subpanels[i], 1, wxEXPAND | wxALIGN_CENTER_VERTICAL
324                     | wxALIGN_LEFT );
325     }
326
327     panel_sizer->Add( sizer, 1, wxEXPAND, 0 );
328
329     panel->SetSizerAndFit( panel_sizer );
330
331     return panel;
332 }
333
334 wxPanel *SoutDialog::EncapsulationPanel( wxWindow* parent )
335 {
336     int i;
337     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
338                                   wxSize(200, 200) );
339
340     wxStaticBox *panel_box = new wxStaticBox( panel, -1,
341                                               _("Encapsulation Method") );
342     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
343                                                           wxHORIZONTAL );
344
345     static const wxString encapsulation_array[] =
346     {
347         _("MPEG TS"),
348         _("MPEG PS"),
349         _("AVI"),
350         _("Ogg")
351     };
352
353     /* Stuff everything into the main panel */
354     for( i=0; i<4; i++ )
355     {
356         encapsulation_radios[i] =
357             new wxRadioButton( panel, EncapsulationRadio1_Event + i,
358                                encapsulation_array[i] );
359         panel_sizer->Add( encapsulation_radios[i], 0,
360                           wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
361     }
362
363     panel->SetSizerAndFit( panel_sizer );
364
365     /* Update encapsulation panel */
366     encapsulation_radios[0]->Enable();
367     i_encapsulation_type = TS_ENCAPSULATION;
368
369     return panel;
370 }
371
372 void SoutDialog::ParseMRL()
373 {
374     /* Initialise MRL value */
375     char *psz_sout = config_GetPsz( p_intf, "sout" );
376     if( psz_sout )
377     {
378         mrl = psz_sout;
379         free( psz_sout );
380     }
381
382     /* Parse the MRL */
383     wxString access = mrl.BeforeFirst( '/' );
384     wxString encapsulation = mrl.BeforeFirst( ':' ).AfterFirst('/');
385
386     if( !access.Cmp( "http" ) )
387     {
388         i_access_type = HTTP_ACCESS_OUT;
389     }
390     else if( !access.Cmp( "udp" ) )
391     {
392         i_access_type = UDP_ACCESS_OUT;
393     }
394     else if( !access.Cmp( "rtp" ) )
395     {
396         i_access_type = RTP_ACCESS_OUT;
397     }
398     else
399     {
400         i_access_type = FILE_ACCESS_OUT;
401     }
402
403     if( !encapsulation.Cmp( "ps" ) )
404     {
405         i_encapsulation_type = PS_ENCAPSULATION;
406     }
407     else if( !encapsulation.Cmp( "avi" ) )
408     {
409         i_encapsulation_type = AVI_ENCAPSULATION;
410     }
411     else if( !encapsulation.Cmp( "ogg" ) )
412     {
413         i_encapsulation_type = OGG_ENCAPSULATION;
414     }
415     else
416     {
417         i_encapsulation_type = TS_ENCAPSULATION;
418     }
419
420     wxString second_part = mrl.AfterFirst( ':' );
421
422     if( i_access_type == FILE_ACCESS_OUT )
423     {
424         /* The whole second part of the MRL is the filename */
425         file_combo->SetValue( second_part );
426     }
427     else
428     {
429         /* we've got address:port */
430         wxString address = second_part.BeforeLast( ':' );
431         net_addr->SetValue( address );
432
433         long int i_port;
434         wxString port = second_part.AfterLast( ':' );
435         if( port.ToLong( &i_port ) )
436         {
437             net_port->SetValue( i_port );
438         }
439         else
440         {
441             net_port->SetValue( config_GetInt( p_intf, "server-port" ) );
442         }
443     }
444
445     /* Update access output panel */
446     wxCommandEvent dummy_event;
447     dummy_event.SetId( AccessRadio1_Event + i_access_type );
448     OnAccessTypeChange( dummy_event );
449
450     /* Update encapsulation output panel */
451     dummy_event.SetId( EncapsulationRadio1_Event + i_encapsulation_type );
452     OnEncapsulationChange( dummy_event );
453 }
454
455 /*****************************************************************************
456  * Events methods.
457  *****************************************************************************/
458 void SoutDialog::OnOk( wxCommandEvent& WXUNUSED(event) )
459 {
460     EndModal( wxID_OK );
461 }
462
463 void SoutDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
464 {
465     EndModal( wxID_CANCEL );
466 }
467
468 void SoutDialog::OnMRLChange( wxCommandEvent& event )
469 {
470     mrl = event.GetString();
471 }
472
473 /*****************************************************************************
474  * Access output panel event methods.
475  *****************************************************************************/
476 void SoutDialog::OnAccessTypeChange( wxCommandEvent& event )
477 {
478     int i;
479     i_access_type = event.GetId() - AccessRadio1_Event;
480
481     switch( i_access_type )
482     {
483     case FILE_ACCESS_OUT:
484         access_subpanels[0]->Enable();
485         access_subpanels[2]->Disable();
486         access_subpanels[3]->Disable();
487         for( i = 1; i < 4; i++ )
488         {
489             encapsulation_radios[i]->Enable();
490         }
491         break;
492
493     case HTTP_ACCESS_OUT:
494         access_subpanels[0]->Disable();
495         access_subpanels[2]->Enable();
496         access_subpanels[3]->Disable();
497         for( i = 1; i < 4; i++ )
498         {
499             encapsulation_radios[i]->Enable();
500         }
501         break;
502
503     case UDP_ACCESS_OUT:
504     case RTP_ACCESS_OUT:
505         access_subpanels[0]->Disable();
506         access_subpanels[2]->Enable();
507         access_subpanels[3]->Enable();
508         for( i = 1; i < 4; i++ )
509         {
510             encapsulation_radios[i]->Disable();
511         }
512         encapsulation_radios[TS_ENCAPSULATION]->SetValue(true);
513         i_encapsulation_type = TS_ENCAPSULATION;
514         break;
515     }
516
517     for( i = 0; i < 4; i++ )
518     {
519         access_radios[i]->SetValue( event.GetId() == (AccessRadio1_Event+i) );
520     }
521
522     UpdateMRL();
523 }
524
525 /*****************************************************************************
526  * File access output event methods.
527  *****************************************************************************/
528 void SoutDialog::OnFileChange( wxCommandEvent& WXUNUSED(event) )
529 {
530     UpdateMRL();
531 }
532
533 void SoutDialog::OnFileBrowse( wxCommandEvent& WXUNUSED(event) )
534 {
535     wxFileDialog dialog( this, _("Save file"), "", "", "*.*", wxSAVE );
536
537     if( dialog.ShowModal() == wxID_OK )
538     {
539         file_combo->SetValue( dialog.GetPath() );
540         UpdateMRL();
541     }
542 }
543
544 /*****************************************************************************
545  * Net access output event methods.
546  *****************************************************************************/
547 void SoutDialog::OnNetChange( wxCommandEvent& WXUNUSED(event) )
548 {
549     UpdateMRL();
550 }
551
552 /*****************************************************************************
553  * Encapsulation panel event methods.
554  *****************************************************************************/
555 void SoutDialog::OnEncapsulationChange( wxCommandEvent& event )
556 {
557     i_encapsulation_type = event.GetId() - EncapsulationRadio1_Event;
558     UpdateMRL();
559 }