File: wx_player.cpp

package info (click to toggle)
vlc 3.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208,020 kB
  • sloc: ansic: 443,448; cpp: 111,223; objc: 36,399; sh: 6,737; makefile: 6,627; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (266 lines) | stat: -rw-r--r-- 8,795 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// g++ wx_player.cpp `wx-config --libs` `wx-config --cxxflags` `pkg-config --cflags gtk+-2.0 libvlc` `pkg-config --libs gtk+-2.0 libvlc` -o wx_player

/* License WTFPL http://sam.zoy.org/wtfpl/ */
/* Written by Vincent Schüßler */

#include <wx/wx.h>
#include <wx/filename.h>
#include <vlc/vlc.h>
#include <climits>

#ifdef __WXGTK__
    #include <gdk/gdkx.h>
    #include <gtk/gtk.h>
    #include <wx/gtk/win_gtk.h>
    #define GET_XID(window) GDK_WINDOW_XWINDOW(GTK_PIZZA(window->m_wxwindow)->bin_window)
#endif

#define myID_PLAYPAUSE wxID_HIGHEST+1
#define myID_STOP wxID_HIGHEST+2
#define myID_TIMELINE wxID_HIGHEST+3
#define myID_VOLUME wxID_HIGHEST+4

#define TIMELINE_MAX (INT_MAX-9)
#define VOLUME_MAX 100

DECLARE_EVENT_TYPE(vlcEVT_END, -1)
DECLARE_EVENT_TYPE(vlcEVT_POS, -1)
DEFINE_EVENT_TYPE(vlcEVT_END)
DEFINE_EVENT_TYPE(vlcEVT_POS)

void OnPositionChanged_VLC(const libvlc_event_t *event, void *data);
void OnEndReached_VLC(const libvlc_event_t *event, void *data);

class MainWindow : public wxFrame {
    public:
        MainWindow(const wxString& title);
        ~MainWindow();

    private:
        void initVLC();

        void OnOpen(wxCommandEvent& event);
        void OnPlayPause(wxCommandEvent& event);
        void OnStop(wxCommandEvent& event);
        void OnPositionChanged_USR(wxCommandEvent& event);
        void OnPositionChanged_VLC(wxCommandEvent& event);
        void OnEndReached_VLC(wxCommandEvent& event);
        void OnVolumeChanged(wxCommandEvent& event);
        void OnVolumeClicked(wxMouseEvent& event);
        void OnTimelineClicked(wxMouseEvent& event);

        void play();
        void pause();
        void stop();
        void setTimeline(float value);
        void connectTimeline();

        wxButton *playpause_button;
        wxButton *stop_button;
        wxSlider *timeline;
        wxSlider *volume_slider;
        wxWindow *player_widget;

        libvlc_media_player_t *media_player;
        libvlc_instance_t *vlc_inst;
        libvlc_event_manager_t *vlc_evt_man;
};

MainWindow *mainWindow;

MainWindow::MainWindow(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition) {
    // setup menubar
    wxMenuBar *menubar;
    wxMenu *file;
    menubar = new wxMenuBar;
    file = new wxMenu;
    file->Append(wxID_OPEN, wxT("&Open"));
    menubar->Append(file, wxT("&File"));
    SetMenuBar(menubar);
    Connect(wxID_OPEN, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnOpen));

    // setup vbox
    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    this->SetSizer(vbox);

    //setup player widget
    player_widget = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
    player_widget->SetBackgroundColour(wxColour(wxT("black")));
    vbox->Add(player_widget, 1, wxEXPAND | wxALIGN_TOP);

    //setup timeline slider
    timeline = new wxSlider(this, myID_TIMELINE, 0, 0, TIMELINE_MAX);
    timeline->Enable(false);
    vbox->Add(timeline, 0, wxEXPAND);
    connectTimeline();
    timeline->Connect(myID_TIMELINE, wxEVT_LEFT_UP, wxMouseEventHandler(MainWindow::OnTimelineClicked));

    //setup control panel
    wxPanel *controlPanel = new wxPanel(this, wxID_ANY);

    //setup hbox
    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
    controlPanel->SetSizer(hbox);
    vbox->Add(controlPanel, 0, wxEXPAND);

    //setup controls
    playpause_button = new wxButton(controlPanel, myID_PLAYPAUSE, wxT("Play"));
    stop_button = new wxButton(controlPanel, myID_STOP, wxT("Stop"));
    volume_slider = new wxSlider(controlPanel, myID_VOLUME, VOLUME_MAX, 0, VOLUME_MAX, wxDefaultPosition, wxSize(100, -1));
    playpause_button->Enable(false);
    stop_button->Enable(false);
    hbox->Add(playpause_button);
    hbox->Add(stop_button);
    hbox->AddStretchSpacer();
    hbox->Add(volume_slider);
    Connect(myID_PLAYPAUSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::OnPlayPause));
    Connect(myID_STOP, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::OnStop));
    Connect(myID_VOLUME, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler(MainWindow::OnVolumeChanged));
    volume_slider->Connect(myID_VOLUME, wxEVT_LEFT_UP, wxMouseEventHandler(MainWindow::OnVolumeClicked));

    //setup vlc
    vlc_inst = libvlc_new(0, NULL);
    media_player = libvlc_media_player_new(vlc_inst);
    vlc_evt_man = libvlc_media_player_event_manager(media_player);
    libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEndReached, ::OnEndReached_VLC, NULL);
    libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPositionChanged, ::OnPositionChanged_VLC, NULL);
    Connect(wxID_ANY, vlcEVT_END, wxCommandEventHandler(MainWindow::OnEndReached_VLC));
    Connect(wxID_ANY, vlcEVT_POS, wxCommandEventHandler(MainWindow::OnPositionChanged_VLC));

    Show(true);
    initVLC();
}

MainWindow::~MainWindow() {
    libvlc_media_player_release(media_player);
    libvlc_release(vlc_inst);
}

void MainWindow::initVLC() {
    #ifdef __WXGTK__
        libvlc_media_player_set_xwindow(media_player, GET_XID(this->player_widget));
    #else
        LONG style = GetWindowLong( player_widget->GetHandle(), GWL_STYLE );
        if( !(style & WS_CLIPCHILDREN) )
            SetWindowLong( player_widget->GetHandle(), GWL_STYLE, style | WS_CLIPCHILDREN );
        libvlc_media_player_set_hwnd(media_player, this->player_widget->GetHandle());
    #endif
}

void MainWindow::OnOpen(wxCommandEvent& event) {
    wxFileDialog openFileDialog(this, wxT("Choose File"));

    if (openFileDialog.ShowModal() == wxID_CANCEL) {
        return;
    }
    else {
        libvlc_media_t *media;
        wxFileName filename = wxFileName::FileName(openFileDialog.GetPath());
        filename.MakeRelativeTo();
        media = libvlc_media_new_path(vlc_inst, filename.GetFullPath().mb_str());
        libvlc_media_player_set_media(media_player, media);
        play();
        libvlc_media_release(media);
    }
}

void MainWindow::OnPlayPause(wxCommandEvent& event) {
    if(libvlc_media_player_is_playing(media_player) == 1) {
        pause();
    }
    else {
        play();
    }
}

void MainWindow::OnStop(wxCommandEvent& event) {
    stop();
}

void MainWindow::OnPositionChanged_USR(wxCommandEvent& event) {
    libvlc_media_player_set_position(media_player, (float) event.GetInt() / (float) TIMELINE_MAX);
}

void MainWindow::OnPositionChanged_VLC(wxCommandEvent& event) {
    float factor = libvlc_media_player_get_position(media_player);
    setTimeline(factor);
}

void MainWindow::OnEndReached_VLC(wxCommandEvent& event) {
    stop();
}

void MainWindow::OnVolumeChanged(wxCommandEvent& event) {
    libvlc_audio_set_volume(media_player, volume_slider->GetValue());
}

void MainWindow::OnVolumeClicked(wxMouseEvent& event) {
    wxSize size = mainWindow->volume_slider->GetSize();
    float position = (float) event.GetX() / (float) size.GetWidth();
    mainWindow->volume_slider->SetValue(position*VOLUME_MAX);
    libvlc_audio_set_volume(mainWindow->media_player, position*VOLUME_MAX);
    event.Skip();
}

void MainWindow::OnTimelineClicked(wxMouseEvent& event) {
    wxSize size = mainWindow->timeline->GetSize();
    float position = (float) event.GetX() / (float) size.GetWidth();
    libvlc_media_player_set_position(mainWindow->media_player, position);
    mainWindow->setTimeline(position);
    event.Skip();
}

void MainWindow::play() {
    libvlc_media_player_play(media_player);
    playpause_button->SetLabel(wxT("Pause"));
    playpause_button->Enable(true);
    stop_button->Enable(true);
    timeline->Enable(true);
}

void MainWindow::pause() {
    libvlc_media_player_pause(media_player);
    playpause_button->SetLabel(wxT("Play"));
}

void MainWindow::stop() {
    pause();
    libvlc_media_player_stop(media_player);
    stop_button->Enable(false);
    setTimeline(0.0);
    timeline->Enable(false);
}

void MainWindow::setTimeline(float value) {
    if(value < 0.0) value = 0.0;
    if(value > 1.0) value = 1.0;
    Disconnect(myID_TIMELINE);
    timeline->SetValue((int) (value * TIMELINE_MAX));
    connectTimeline();
}

void MainWindow::connectTimeline() {
    Connect(myID_TIMELINE, wxEVT_COMMAND_SLIDER_UPDATED, wxCommandEventHandler(MainWindow::OnPositionChanged_USR));
}

class MyApp : public wxApp {
    public:
        virtual bool OnInit();
};

void OnPositionChanged_VLC(const libvlc_event_t *event, void *data) {
    wxCommandEvent evt(vlcEVT_POS, wxID_ANY);
    mainWindow->GetEventHandler()->AddPendingEvent(evt);
}

void OnEndReached_VLC(const libvlc_event_t *event, void *data) {
    wxCommandEvent evt(vlcEVT_END, wxID_ANY);
    mainWindow->GetEventHandler()->AddPendingEvent(evt);
}

bool MyApp::OnInit() {
    mainWindow = new MainWindow(wxT("wxWidgets libVLC demo"));
    return true;
}

IMPLEMENT_APP(MyApp)