~martijnbraam/pyatem

pyatem/gtk_switcher/atemwindow.py -rw-r--r-- 22.1 KiB
ed164087Martijn Braam i18n: update translation files from source 5 days ago
                                                                                
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import ctypes
import json
import sys
import threading
import time
import traceback
from datetime import datetime

import gi

from gtk_switcher.debugger import DebuggerWindow
from gtk_switcher.videohubconnection import VideoHubConnection
from pyatem.hexdump import hexdump

from gtk_switcher.audio import AudioPage
from gtk_switcher.camera import CameraPage
from gtk_switcher.decorators import field, call_fields
from gtk_switcher.macroeditor import MacroEditorWindow
from gtk_switcher.media import MediaPage
from gtk_switcher.connectionwindow import ConnectionWindow
from gtk_switcher.switcher import SwitcherPage
from pyatem.command import ProgramInputCommand, PreviewInputCommand, AutoCommand, TransitionPositionCommand, \
    InputPropertiesCommand
from pyatem.protocol import AtemProtocol

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, GObject, Gio, Gdk

gi.require_version('Handy', '1')
from gi.repository import Handy


class AtemConnection(threading.Thread):
    def __init__(self, callback, disconnected, transfer_progress, download_done, connected):
        threading.Thread.__init__(self)
        self.callback = callback
        self.disconnected = disconnected
        self._connected = connected
        self.transfer_progress = transfer_progress
        self.download_done = download_done
        self.atem = None
        self.ip = None
        self.stop = False
        self.connected = False

    def run(self):
        # Don't run if the ip isn't set yet
        if self.ip is None or self.ip == '0.0.0.0':
            if AtemProtocol.usb_exists():
                self.mixer = AtemProtocol(usb="auto")
            else:
                return
        else:
            self.mixer = AtemProtocol(self.ip)
        self.mixer.on('change', self.do_callback)
        self.mixer.on('connected', self.do_connected)
        self.mixer.on('disconnected', self.do_disconnected)
        self.mixer.on('transfer-progress', self.do_transfer_progress)
        self.mixer.on('download-done', self.do_download_done)
        try:
            self.mixer.connect()
        except ConnectionError as e:
            sys.stderr.write(f"Could not connect to {self.ip}: {e}")
            return
        self.connected = True
        while not self.stop:
            try:
                self.mixer.loop()
            except Exception as e:
                traceback.print_exc()
                print("EXCEPTION", repr(e))

    def do_callback(self, *args, **kwargs):
        GLib.idle_add(self.callback, *args, **kwargs)

    def do_disconnected(self):
        self.connected = False
        GLib.idle_add(self.disconnected)

    def do_connected(self):
        self.connected = True
        GLib.idle_add(self._connected)

    def do_transfer_progress(self, store, slot, progress):
        GLib.idle_add(self.transfer_progress, store, slot, progress)

    def do_download_done(self, store, slot, data):
        GLib.idle_add(self.download_done, store, slot, data)

    def get_id(self):

        # returns id of the respective thread
        if hasattr(self, '_thread_id'):
            return self._thread_id
        for id, thread in threading._active.items():
            if thread is self:
                return id

    def die(self):
        thread_id = self.get_id()
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id,
                                                         ctypes.py_object(SystemExit))
        if res > 1:
            ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
            print('Exception raise failure')


class AtemWindow(SwitcherPage, MediaPage, AudioPage, CameraPage):
    def __init__(self, application, args):
        self.application = application
        self.args = args

        self.debug = args.debug

        Handy.init()

        self.settings = Gio.Settings.new('nl.brixit.Switcher')
        self.settings.connect('changed::switcher-ip', self.on_switcher_ip_changed)
        self.settings.connect('changed::connections', self.on_connection_settings_changed)

        builder = Gtk.Builder()
        builder.set_translation_domain("openswitcher")
        builder.add_from_resource('/nl/brixit/switcher/ui/mixer.glade')
        builder.connect_signals(self)
        css = Gio.resources_lookup_data("/nl/brixit/switcher/ui/style.css", 0)

        self.provider = Gtk.CssProvider()
        self.provider.load_from_data(css.get_data())

        self.window = builder.get_object("main_window")
        self.window.set_application(self.application)
        self.headerbar = builder.get_object("headerbar")

        # Load requested view
        self.mainstack = builder.get_object("mainstack")
        self.connectionstack = builder.get_object("connectionstack")
        self.mainstack.set_visible_child_name(args.view)
        self.connectionstack.set_visible_child_name("disconnected")

        self.mainstack.connect('notify::visible-child', self.on_page_changed)

        SwitcherPage.__init__(self, builder)
        MediaPage.__init__(self, builder)
        AudioPage.__init__(self, builder)
        CameraPage.__init__(self, builder)

        self.status_model = builder.get_object('status_model')
        self.status_mode = builder.get_object('status_mode')
        self.focus_dummy = builder.get_object('focus_dummy')
        self.disable_shortcuts = False
        self.macro_edit = False

        self.timecode_mode = 0
        self.timecode_offset = 0

        self.aux_follow_audio = set()

        self.hardware_threads = {}
        self.connection_settings = {}

        self.apply_css(self.window, self.provider)

        self.window.show_all()

        self.firmware_version = None
        self.mode = None

        self.connection = AtemConnection(self.on_change, self.on_disconnect, self.on_transfer_progress,
                                         self.on_download_done, self.on_connect)

        if args.ip:
            self.connection.ip = args.ip
            if args.persist:
                self.settings.set_string('switcher-ip', args.ip)
        else:
            if self.connection.ip == "0.0.0.0":
                ConnectionWindow(self.window, self.connection, self.application)

            self.connection.ip = self.settings.get_string('switcher-ip')
        self.connection.daemon = True
        self.connection.start()

        accel = Gtk.AccelGroup()
        accel.connect(Gdk.keyval_from_name('space'), 0, 0, self.on_cut_shortcut)
        accel.connect(Gdk.keyval_from_name('ISO_Enter'), 0, 0, self.on_auto_shortcut)
        accel.connect(Gdk.keyval_from_name('Return'), 0, 0, self.on_auto_shortcut)
        accel.connect(Gdk.keyval_from_name('KP_Enter'), 0, 0, self.on_auto_shortcut)
        accel.connect(Gdk.keyval_from_name('F12'), 0, 0, self.on_debugger_shortcut)


        for i in range(0, 9):
            accel.connect(Gdk.keyval_from_name(str(i)), 0, 0, self.on_preview_keyboard_change)
            accel.connect(Gdk.keyval_from_name(str(i)), Gdk.ModifierType.CONTROL_MASK, 0,
                          self.on_program_keyboard_change)
            accel.connect(Gdk.keyval_from_name(str(i)), Gdk.ModifierType.MOD1_MASK, 0,
                          self.on_cutbus_keyboard_change)

        self.window.add_accel_group(accel)

        GLib.timeout_add_seconds(1, self.on_clock)

        Gtk.main()

    def on_preview_keyboard_change(self, widget, window, key, modifier):
        if self.disable_shortcuts:
            return

        index = key - 49
        cmd = PreviewInputCommand(index=0, source=index + 1)
        self.connection.mixer.send_commands([cmd])

    def on_program_keyboard_change(self, widget, window, key, modifier):
        if self.disable_shortcuts:
            return

        index = key - 49
        cmd = ProgramInputCommand(index=0, source=index + 1)
        self.connection.mixer.send_commands([cmd])

    def on_cutbus_keyboard_change(self, widget, window, key, modifier):
        if self.disable_shortcuts:
            return

        index = key - 49
        cmd = PreviewInputCommand(index=0, source=index + 1)
        auto = AutoCommand(index=0)
        self.connection.mixer.send_commands([cmd, auto])

    def on_entry_focus(self, *args):
        self.disable_shortcuts = True

    def on_entry_unfocus(self, *args):
        self.disable_shortcuts = False
        self.focus_dummy.grab_focus()

    def on_context_menu(self, *args):
        pass

    def on_entry_activate(self, *args):
        self.focus_dummy.grab_focus()

    def hook_up_focus(self, widget):
        widget.connect("focus-in-event", self.on_entry_focus)
        widget.connect("focus-out-event", self.on_entry_unfocus)
        widget.connect("activate", self.on_entry_activate)

    def apply_css(self, widget, provider):
        Gtk.StyleContext.add_provider(widget.get_style_context(),
                                      provider,
                                      Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

        if isinstance(widget, Gtk.Container):
            widget.forall(self.apply_css, provider)

    def on_switcher_ip_changed(self, *args):
        print("Connection settings changed")
        print("Closing old connection...")
        self.connection.die()
        self.connection.join(timeout=1)
        for tid in self.hardware_threads:
            self.hardware_threads[tid].die()
        for me in self.me:
            self.me.remove(me)
        for widget in self.main_blocks:
            self.main_blocks.remove(widget)
        time.sleep(0.1)
        self.clear_audio_state()
        print("Starting new connection to {}".format(self.settings.get_string('switcher-ip')))
        self.connection = AtemConnection(self.on_change, self.on_disconnect, self.on_transfer_progress,
                                         self.on_download_done, self.on_connect)
        self.connection.daemon = True
        self.connection.ip = self.settings.get_string('switcher-ip')
        self.connection.start()

    def on_connection_settings_changed(self, *args):
        print("Connection stored config changed")

    def set_class(self, widget, classname, state):
        if state:
            widget.get_style_context().add_class(classname)
        else:
            widget.get_style_context().remove_class(classname)

    def on_main_window_destroy(self, widget):
        Gtk.main_quit()

    def on_preferences_button_clicked(self, widget):
        ConnectionWindow(self.window, self.connection, self.application)

    def on_disconnect(self):
        self.connectionstack.set_visible_child_name("disconnected")
        for tid in self.hardware_threads:
            self.hardware_threads[tid].die()
        print("Disconnected from mixer")

    def on_connect(self):
        # Load stored connection-specific settings if available
        settings = self.settings.get_string('connections')
        settings = json.loads(settings)
        if self.connection.ip in settings:
            self.connection_settings = settings[self.connection.ip]
        else:
            self.connection_settings = {
                "videohubs": []
            }

        # Connect to stored videohubs
        if 'videohubs' in self.connection_settings:
            hubs = self.connection_settings['videohubs']
            for ip in hubs:
                hub = hubs[ip]
                thread = VideoHubConnection(ip, self.on_videohub_connect, self.on_videohub_disconnect,
                                            self.on_videohub_input_change, self.on_videohub_output_change,
                                            self.on_videohub_route_change)
                thread.daemon = True
                thread.id = f'hub-{ip}'
                thread.start()
                self.hardware_threads[thread.id] = thread

    def on_debugger_shortcut(self, *args):
        if self.disable_shortcuts:
            return

        DebuggerWindow(self.window, self.connection, self.application)

    def on_videohub_connect(self, hub_id):
        print(f"Connected to {hub_id}")
        from gtk_switcher.videohubbus import VideoHubBus
        for hubid in self.connection_settings['videohubs']:
            ip = self.connection_settings['videohubs'][hubid]['ip']
            for outputid in self.connection_settings['videohubs'][hubid]['outputs']:
                output = self.connection_settings['videohubs'][hubid]['outputs'][outputid]
                if output['bus']:
                    bus = VideoHubBus(self.provider, self.hardware_threads[f'hub-{ip}'], outputid)
                    self.main_blocks.add(bus)

            for me in self.me:
                if hasattr(me, 'add_router'):
                    me.add_router(self.hardware_threads[f'hub-{ip}'], self.connection_settings['videohubs'][hubid])

        self.main_blocks.show_all()

    def on_videohub_disconnect(self, hub_id):
        print(f"Disconnected from {hub_id}")

    def on_videohub_input_change(self, hub_id, index, inputs):
        pass

    def on_videohub_output_change(self, hub_id, index, outputs):
        pass

    def on_videohub_route_change(self, hub_id, index, source):
        hub = self.hardware_threads[hub_id]
        ip = hub.ip
        if 'videohubs' not in self.connection_settings:
            return

        if ip not in self.connection_settings['videohubs']:
            return

        if self.connection_settings['videohubs'][ip]['outputs'][str(index)]['rename']:
            atem_input = self.connection_settings['videohubs'][ip]['outputs'][str(index)]['source']
            new_name = hub.inputs[source]['label']
            if ':' in new_name:
                button, new_name = new_name.split(':', maxsplit=1)
                new_name = new_name.strip()
                button = button.strip()
            else:
                button = None

            cmd = InputPropertiesCommand(source_index=atem_input, label=new_name, short_label=button)
            self.connection.mixer.send_commands([cmd])

    def on_bypass_firmware_clicked(self, widget, *args):
        self.connectionstack.set_visible_child_name("connected")

    def on_change(self, field, data):
        global _callbacks
        if self.args.dump is not None and field in self.args.dump:
            if isinstance(data, bytes):
                print('== {} ({} bytes)=='.format(field, len(data)))
                hexdump(data)
            else:
                print('== {} ({} bytes)=='.format(field, len(data.raw)))
                hexdump(data.raw)
                print(data)

        # Call all the registered decorators
        call_fields(field, self, data)

        try:
            if field == 'firmware-version':
                self.firmware_version = data
                if data.major < 2:
                    self.connectionstack.set_visible_child_name("firmware")
                elif data.minor < 28:
                    self.connectionstack.set_visible_child_name("firmware")
                else:
                    self.connectionstack.set_visible_child_name("connected")
                print("Firmware: {}".format(data.version))
            elif field == 'time':
                self.on_time_sync(data)
            elif field == 'time-config':
                self.on_timecode_config_change(data)
            elif field == 'input-properties':
                self.on_input_layout_change(data)
                self.on_camera_layout_change(data)
            elif field == 'program-bus-input':
                self.on_program_input_change(data)
            elif field == 'preview-bus-input':
                self.on_preview_input_change(data)
            elif field == 'transition-position':
                self.on_transition_position_change(data)
            elif field == 'transition-settings':
                self.on_transition_settings_change(data)
            elif field == 'transition-preview':
                self.on_transition_preview_change(data)
            elif field == 'key-on-air':
                self.on_key_on_air_change(data)
            elif field == 'color-generator':
                self.on_color_change(data)
            elif field == 'fade-to-black':
                self.on_ftb_change(data)
            elif field == 'fade-to-black-state':
                self.on_ftb_state_change(data)
            elif field == 'mixer-effect-config':
                self.on_mixer_effect_config_change(data)
            elif field == 'topology':
                self.on_topology_change(data)
            elif field == 'product-name':
                self.status_model.set_text(data.name)
                print("Mixer model: {}".format(data.name))
            elif field == 'video-mode':
                self.mode = data
                for me in self.me:
                    me.set_mode(data)
                self.status_mode.set_text(data.get_label())
            elif field == 'dkey-properties':
                self.on_dsk_change(data)
            elif field == 'dkey-state':
                self.on_dsk_state_change(data)
            elif field == 'mediaplayer-slots':
                self.on_mediaplayer_slots_change(data)
            elif field == 'mediaplayer-file-info':
                self.on_mediaplayer_file_info_change(data)
            elif field == 'transition-mix':
                self.on_transition_mix_change(data)
            elif field == 'transition-dip':
                self.on_transition_dip_change(data)
            elif field == 'transition-wipe':
                self.on_transition_wipe_change(data)
            elif field == 'transition-dve':
                self.on_transition_dve_change(data)
            elif field == 'fairlight-master-properties':
                self.on_fairlight_master_properties_change(data)
            elif field == 'fairlight-audio-input':
                self.on_fairlight_audio_input_change(data)
            elif field == 'atem-eq-band-properties':
                self.on_fairlight_eq_band_change(data)
            elif field == 'audio-input':
                self.on_audio_input_change(data)
            elif field == 'fairlight-strip-properties':
                self.on_fairlight_strip_properties_change(data)
            elif field == 'fairlight-tally':
                self.on_fairlight_tally_change(data)
            elif field == 'audio-mixer-tally':
                self.on_audio_mixer_tally_change(data)
            elif field == 'audio-mixer-master-properties':
                self.on_audio_mixer_master_properties_change(data)
            elif field == 'audio-mixer-monitor-properties':
                self.on_audio_monitor_properties_change(data)
            elif field == 'key-properties-base':
                self.on_key_properties_base_change(data)
            elif field == 'key-properties-luma':
                self.on_key_properties_luma_change(data)
            elif field == 'key-properties-dve':
                self.on_key_properties_dve_change(data)
            elif field == 'recording-settings':
                self.on_stream_recording_setting_change(data)
            elif field == 'recording-disk':
                self.on_stream_recording_disks_change(data)
            elif field == 'recording-status':
                self.on_stream_recording_status_change(data)
            elif field == 'recording-duration':
                self.on_stream_recording_duration_change(data)
            elif field == 'aux-output-source':
                self.on_aux_output_source_change(data)
                if data.index in self.aux_follow_audio:
                    self.on_aux_monitor_source_change(data)
            elif field == 'dkey-properties-base':
                self.on_dkey_properties_base_change(data)
            elif field == 'macro-properties':
                self.on_macro_properties_change(data)
            elif field == 'audio-meter-levels':
                self.on_audio_meter_levels_change(data)
            elif field == 'fairlight-meter-levels':
                self.on_fairlight_meter_levels_change(data)
            elif field == 'fairlight-master-levels':
                self.on_fairlight_master_levels_change(data)
            elif field == 'streaming-service':
                self.on_streaming_service_change(data)
            elif field == 'streaming-audio-bitrate':
                self.on_streaming_audio_bitrate_change(data)
            elif field == 'streaming-stats':
                self.on_streaming_stats_change(data)
            elif field == 'streaming-status':
                self.on_streaming_status_change(data)

            else:
                if field == 'time':
                    return
                if not self.debug and self.args.dump is not None and len(self.args.dump) > 0:
                    return
                if isinstance(data, bytes):
                    print(field)
                else:
                    print(data)
        except Exception as e:
            # When the connection breaks on initial sync the UI events are queued but no mixer state is present
            # catch the exceptions and bring the software into the disconnected state cleanly
            if self.connection.connected:
                raise
            print(f"Exception while disconnected: {e}")

    def on_time_sync(self, data):
        seconds = data.total_seconds()
        if self.timecode_mode == 0:
            self.timecode_offset = seconds - datetime.now().timestamp()
        else:
            t = time.localtime()
            tod = 3600 * t.tm_hour + 60 * t.tm_min + t.tm_sec
            self.timecode_offset = tod - seconds

    def on_timecode_config_change(self, data):
        self.timecode_mode = data.mode

    def on_clock(self):
        self.on_clock_stream_recorder()
        self.on_clock_stream_live()
        return True

    def on_transfer_progress(self, store, slot, progress):
        if store == 0:
            # Media transfer
            self.on_media_transfer_progress(slot, progress)

    def on_download_done(self, store, slot, data):
        if store == 0:
            # Media transfer
            self.on_media_download_done(slot, data)
        if store == 0xffff:
            # Macro fetch
            if self.macro_edit:
                self.macro_edit = False

                MacroEditorWindow(self.window, self.application, self.connection, slot, data)

    def on_page_changed(self, widget, *args):
        page = widget.get_visible_child_name()
        if page == 'media':
            self.on_page_media_open()
        if page == 'audio':
            self.enable_levels()
        else:
            self.disable_levels()

#    @field('input-properties')
#    def on_input_properties_changed(self, data):
#        print(data)