File: dispatch.h

package info (click to toggle)
bino 1.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,636 kB
  • sloc: cpp: 21,288; sh: 4,849; makefile: 314; sed: 16
file content (397 lines) | stat: -rw-r--r-- 13,287 bytes parent folder | download | duplicates (2)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * This file is part of bino, a 3D video player.
 *
 * Copyright (C) 2010, 2011, 2012, 2013, 2015
 * Martin Lambers <marlam@marlam.de>
 * Joe <cuchac@email.cz>
 * Binocle <http://binocle.com> (author: Olivier Letz <oletz@binocle.com>)
 * Frédéric Bour <frederic.bour@lakaban.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef DISPATCH_H
#define DISPATCH_H

#include <string>
#include <sstream>
#include <stdint.h>

#include "base/ser.h"
#include "base/pth.h"

#include "media_data.h"

class gui;
class audio_output;
class video_output;
class media_input;
class player;


/* The open_init_data class contains everything that is needed to open
 * a media input. */

class open_input_data : public serializable
{
public:
    device_request dev_request;    // Request for input device settings
    std::vector<std::string> urls; // Input media objects
    parameters params;             // Initial per-video output parameters
                                   // (other parameter fields are ignored)
public:
    open_input_data();

    // Serialization
    void save(std::ostream &os) const;
    void load(std::istream &is);
};


/* A controller can send commands to the dispatch (e.g. "pause", "seek",
 * "adjust colors", ...). The dispatch then reacts on this command, and sends
 * a notification to all controllers afterwards. The controllers may react
 * on the notification or ignore it.
 *
 * For example, the video output controller may notice that the user pressed the
 * 'p' key to pause the video. So it sends the "pause" command to the dispatch.
 * The dipatch updates its state accordingly, and notifies all controllers that
 * the video is now paused. The video output could use this notification to display
 * a pause symbol on screen, and the audio output controller may play a pause jingle
 * (however, in the case of pause, both currently simply ignore the notification).
 */

// A command that can be sent to the dispatch by a controller.

class command
{
public:
    enum type
    {
        noop,                           // no parameters
        quit,                           // no parameters
        // Play state
        open,                           // open_input_data
        close,                          // no parameters
        toggle_play,                    // no parameters
        toggle_pause,                   // no parameters
        step,                           // no parameters
        seek,                           // float (relative adjustment)
        set_pos,                        // float (absolute position)
        // Per-Session parameters
        set_audio_device,               // int
        set_quality,                    // int
        set_stereo_mode,                // parameters::stereo_mode
        set_stereo_mode_swap,           // bool
        toggle_stereo_mode_swap,        // no parameters
        set_crosstalk,                  // 3 floats (absolute values)
        set_fullscreen_screens,         // int
        set_fullscreen_flip_left,       // bool
        set_fullscreen_flop_left,       // bool
        set_fullscreen_flip_right,      // bool
        set_fullscreen_flop_right,      // bool
        set_fullscreen_inhibit_screensaver,     // bool
        set_fullscreen_3d_ready_sync,   // bool
        set_contrast,                   // float (absolute value)
        adjust_contrast,                // float (relative adjustment)
        set_brightness,                 // float (absolute value)
        adjust_brightness,              // float (relative adjustment)
        set_hue,                        // float (absolute value)
        adjust_hue,                     // float (relative adjustment)
        set_saturation,                 // float (absolute value)
        adjust_saturation,              // float (relative adjustment)
        set_zoom,                       // float (absolute value)
        adjust_zoom,                    // float (relative adjustment)
        set_loop_mode,                  // parameters::loop_mode_t
        set_audio_delay,                // int64_t (absolute value, in microseconds)
        set_subtitle_encoding,          // string (encoding name)
        set_subtitle_font,              // string (font name)
        set_subtitle_size,              // int
        set_subtitle_scale,             // float
        set_subtitle_color,             // uint64_t
        set_subtitle_shadow,            // int
#if HAVE_LIBXNVCTRL
        set_sdi_output_format,          // int
        set_sdi_output_left_stereo_mode,  // parameters::stereo_mode_t
        set_sdi_output_right_stereo_mode, // parameters::stereo_mode_t
#endif // HAVE_LIBXNVCTRL
        // Per-Video parameters
        set_video_stream,               // int
        cycle_video_stream,             // no parameters
        set_audio_stream,               // int
        cycle_audio_stream,             // no parameters
        set_subtitle_stream,            // int
        cycle_subtitle_stream,          // no parameters
        set_stereo_layout,              // video_frame::stereo_layout
        set_stereo_layout_swap,         // bool
        set_crop_aspect_ratio,          // float
        set_source_aspect_ratio,        // float
        set_parallax,                   // float (absolute value)
        adjust_parallax,                // float (relative adjustment)
        set_ghostbust,                  // float (absolute value)
        adjust_ghostbust,               // float (relative adjustment)
        set_subtitle_parallax,          // float (absolute value)
        adjust_subtitle_parallax,       // float (relative adjustment)
        // Volatile parameters
        toggle_fullscreen,              // no parameters
        center,                         // no parameters
        set_audio_volume,               // float (absolute value)
        adjust_audio_volume,            // float (relative adjustment)
        toggle_audio_mute,              // no parameters
        update_display_pos,             // no parameters
    };
    
    type type;
    std::string param;

    command() :
        type(noop)
    {
    }

    command(enum type t) :
        type(t)
    {
    }

    command(enum type t, int p) :
        type(t)
    {
        std::ostringstream oss;
        s11n::save(oss, p);
        param = oss.str();
    }

    command(enum type t, float p) :
        type(t)
    {
        std::ostringstream oss;
        s11n::save(oss, p);
        param = oss.str();
    }

    command(enum type t, int64_t p) :
        type(t)
    {
        std::ostringstream oss;
        s11n::save(oss, p);
        param = oss.str();
    }

    command(enum type t, uint64_t p) :
        type(t)
    {
        std::ostringstream oss;
        s11n::save(oss, p);
        param = oss.str();
    }

    command(enum type t, const std::string &p) :
        type(t), param(p)
    {
    }
};

// A notification that can be sent to controllers by the dispatch.
// It signals that the corresponding value has changed.

class notification
{
public:
    enum type
    {
        noop,
        quit,
        // Play state
        open,
        play,
        pause,
        pos,
        // Per-Session parameters
        audio_device,
        quality,
        stereo_mode,
        stereo_mode_swap,
        crosstalk,
        fullscreen_screens,
        fullscreen_flip_left,
        fullscreen_flop_left,
        fullscreen_flip_right,
        fullscreen_flop_right,
        fullscreen_inhibit_screensaver,
        fullscreen_3d_ready_sync,
        contrast,
        brightness,
        hue,
        saturation,
        zoom,
        loop_mode,
        audio_delay,
        subtitle_encoding,
        subtitle_font,
        subtitle_size,
        subtitle_scale,
        subtitle_color,
        subtitle_shadow,
#if HAVE_LIBXNVCTRL
        sdi_output_format,
        sdi_output_left_stereo_mode,
        sdi_output_right_stereo_mode,
#endif // HAVE_LIBXNVCTRL
        // Per-Video parameters
        video_stream,
        audio_stream,
        subtitle_stream,
        stereo_layout,
        stereo_layout_swap,
        crop_aspect_ratio,
        source_aspect_ratio,
        parallax,
        ghostbust,
        subtitle_parallax,
        // Volatile parameters
        fullscreen,
        center,
        audio_volume,
        audio_mute,
        display_pos,
    };

    const enum type type;

public:
    notification(enum type t) : type(t)
    {
    }
};

// The controller interface.

class controller
{
public:
    controller() throw ();
    virtual ~controller();

    /* The controller uses this function to send a command to the player. */
    static void send_cmd(const command& cmd);
    // Convenience wrappers:
    static void send_cmd(enum command::type t) { send_cmd(command(t)); }
    static void send_cmd(enum command::type t, int p) { send_cmd(command(t, p)); }
    static void send_cmd(enum command::type t, float p) { send_cmd(command(t, p)); }
    static void send_cmd(enum command::type t, int64_t p) { send_cmd(command(t, p)); }
    static void send_cmd(enum command::type t, uint64_t p) { send_cmd(command(t, p)); }
    static void send_cmd(enum command::type t, const std::string& p) { send_cmd(command(t, p)); }

    /* The controller receives notifications via this function. The default
     * implementation simply ignores the notification. */
    virtual void receive_notification(const notification& note);

    /* The controller is asked to process events via this function. The default
     * implementation simply does nothing. */
    virtual void process_events();

    /* The controller can prevent Bino from exiting when there is no video
     * to play. In this case, the following function should return 'false'.
     * This is intended to be used for controllers that might send another
     * 'open' command in the future. */
    virtual bool allow_early_quit();
};

// The dispatch (singleton).

class dispatch
{
private:
    int* _argc;
    char** _argv;
    const bool _eq;
    const bool _eq_3d;
    const bool _eq_slave_node;
    const bool _gui_mode;
    const bool _have_display;
    // Objects
    class gui* _gui;
    class audio_output* _audio_output;
    class video_output* _video_output;
    class media_input* _media_input;
    class player* _player;
    std::vector<controller*> _controllers;
    unsigned int _controllers_version;
    mutex _controllers_mutex;
    // Parameters
    open_input_data _input_data;
    class parameters _parameters;
    // State
    bool _playing;
    bool _pausing;
    float _position;

    void stop_player();
    void force_stop(bool reopen_media_input = true);

    bool early_quit_is_allowed() const;
    void visit_all_controllers(int action, const notification& note) const;
    void notify_all(const notification& note);

public:
    dispatch(int* argc, char** argv,
            bool equalizer, bool equalizer_3d, bool equalizer_slave_node,
            bool gui, bool have_display, msg::level_t log_level,
            bool benchmark, int swap_interval) throw ();
    virtual ~dispatch();

    void register_controller(controller* c);
    void deregister_controller(controller* c);

    void init(const open_input_data& input_data);
    void deinit();

    static void step();

    /* Process events for all controllers */
    static void process_all_events();

    /* Access parameters and state (read-only) */
    static const class parameters& parameters();
    static const class audio_output* audio_output();    // NULL if not available
    static const class video_output* video_output();    // NULL if not available
    static const class media_input* media_input();      // NULL if no input is opened
    static bool playing();
    static bool pausing();
    static float position();

    /* Receive a command from a controller. */
    void receive_cmd(const command& cmd);

    /* Interface for the player. TODO: remove this! */
    class audio_output* get_audio_output(); // NULL if not available
    class video_output* get_video_output(); // NULL if not available
    class media_input* get_media_input();   // NULL if not available
    void set_playing(bool p);
    void set_pausing(bool p);
    void set_position(float pos);

    /* Interface for Equalizer. */
    class open_input_data* get_input_data();
    std::string save_state() const;
    void load_state(const std::string& s);
    void stop_eq_player() { stop_player(); }

    /* Helper function for text-based controllers: parse a command from a string.
     * Return false if this fails, otherwise store the command in c. */
    static bool parse_command(const std::string& s, command* c);
};

#endif