diff --git a/console.py b/console.py new file mode 100644 index 0000000..72d33cb --- /dev/null +++ b/console.py @@ -0,0 +1,103 @@ +""" +Usage - + + `python console.py` + (Cmd)help + + Autocompletions should work... + +""" +import sys +import os +sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/python-api') + +from gstswitch.server import Server +from gstswitch.helpers import * +from gstswitch.controller import Controller +import cmd +import inspect + + + +class Console(cmd.Cmd): + + COMPOSITE_MAPPING = { + 'none': Controller.COMPOSITE_NONE, + 'pip': Controller.COMPOSITE_PIP, + 'dual_preview': Controller.COMPOSITE_DUAL_PREVIEW, + 'preview': Controller.COMPOSITE_DUAL_PREVIEW, + 'equal': Controller.COMPOSITE_DUAL_EQUAL, + 'dual_equal': Controller.COMPOSITE_DUAL_EQUAL + } + + SWITCH_MAPPING = { + 'video1': Controller.SWITCH_VIDEO_1, + 'video2': Controller.SWITCH_VIDEO_2, + 'audio': Controller.SWITCH_AUDIO + } + + def do_get_compose_port(self, line): + c = Controller() + c.establish_connection() + print c.get_compose_port() + + def help_get_compose_port(self): + print "Get the Compose Port" + + def do_get_encode_port(self, line): + c = Controller() + c.establish_connection() + print c.get_encode_port() + + def do_get_audio_port(self, line): + c = Controller() + c.establish_connection() + print c.get_audio_port() + + def do_get_preview_ports(self, line): + c = Controller() + c.establish_connection() + print c.get_preview_ports() + + def do_set_composite_mode(self, line): + try: + val = self.COMPOSITE_MAPPING[line.lower()] + except KeyError: + print "Invalid" + c = Controller() + c.establish_connection() + print c.set_composite_mode(val) + + def help_set_composite_mode(self): + print "Valid modes - {0}".format(", ".join([i for i in self.COMPOSITE_MAPPING])) + + def complete_set_composite_mode(self, text, line, begidx, endidx): + return [i for i in self.COMPOSITE_MAPPING if i.startswith(text)] + + def do_adjust_pip(self, line): + c = Controller() + c.establish_connection() + if len(line.split()) != 4: + print + print c.adjust_pip(*map(int, line.split())) + + def do_switch(self, line): + try: + val = self.SWITCH_MAPPING[line.lower()] + except KeyError: + print "Invalid" + c = Controller() + c.establish_connection() + print c.switch(self.SWITCH_MAPPING[line.split()[0]], int(line.split()[1])) + + def complete_switch(self, text, line, begidx, endidx): + return [i for i in self.SWITCH_MAPPING if i.startswith(text)] + + + + + + +if __name__ == '__main__': + con = Console() + con.cmdloop() diff --git a/python-api/Makefile b/python-api/Makefile index 328cf4e..79337f8 100644 --- a/python-api/Makefile +++ b/python-api/Makefile @@ -17,7 +17,6 @@ unittests: @rm -rf reports @mkdir -p reports/coverage/unittests py.test --cov gstswitch tests/unittests/ --pep8 -v -s - @make clean pep8: pep8 gstswitch diff --git a/python-api/gstswitch/controller.py b/python-api/gstswitch/controller.py index 8e2a6d2..b9e5454 100644 --- a/python-api/gstswitch/controller.py +++ b/python-api/gstswitch/controller.py @@ -18,6 +18,13 @@ class Controller(object): :param: None """ + COMPOSITE_NONE = 0 + COMPOSITE_PIP = 1 + COMPOSITE_DUAL_PREVIEW = 2 + COMPOSITE_DUAL_EQUAL = 3 + SWITCH_VIDEO_1 = ord('A') + SWITCH_VIDEO_2 = ord('B') + SWITCH_AUDIO = ord('a') def __init__( self, @@ -206,14 +213,20 @@ def get_preview_ports(self): 'Should return a GVariant tuple') def set_composite_mode(self, mode): - """Set the current composite mode. Modes between 0 and 3 are allowed. + """Set the current composite mode. + Modes allowed are: + - COMPOSITE_NONE + - COMPOSITE_PIP + - COMPOSITE_DUAL_PREVIEW + - COMPOSITE_DUAL_EQUAL :param mode: new composite mode :returns: True when requested """ self.establish_connection() # only modes from 0 to 3 are supported - if mode >= 0 and mode <= 3: + res = None + if mode in range(0, 4): try: conn = self.connection.set_composite_mode(mode) print conn @@ -293,7 +306,10 @@ def adjust_pip(self, xpos, ypos, width, height): def switch(self, channel, port): """Switch the channel to the target port - :param channel: The channel to be switched, 'A', 'B', 'a' + :param channel: The channel to be switched: + SWITCH_VIDEO_1 + SWITCH_VIDEO_2 + SWITCH_AUDIO :param port: The target port number :returns: True when requested """ diff --git a/python-api/tests/integrationtests/test_controller.py b/python-api/tests/integrationtests/test_controller.py index 0936748..08484f7 100644 --- a/python-api/tests/integrationtests/test_controller.py +++ b/python-api/tests/integrationtests/test_controller.py @@ -306,7 +306,8 @@ def set_composite_mode(self, mode, generate_frames=False): sources.terminate_video() serv.terminate(1) if not generate_frames: - if mode == 3: + controller = Controller() + if mode == Controller.COMPOSITE_DUAL_EQUAL: assert res is False else: assert res is True @@ -343,10 +344,21 @@ def verify_output(self, mode, video): return True return False - def test_set_composite_mode(self): + def test_set_composite_mode_none(self): """Test set_composite_mode""" - for i in range(4): - self.set_composite_mode(i) + self.set_composite_mode(Controller.COMPOSITE_NONE) + + def test_set_composite_mode_pip(self): + """Test set_composite_mode""" + self.set_composite_mode(Controller.COMPOSITE_PIP) + + def test_set_composite_mode_preview(self): + """Test set_composite_mode""" + self.set_composite_mode(Controller.COMPOSITE_DUAL_PREVIEW) + + def test_set_composite_mode_equal(self): + """Test set_composite_mode""" + self.set_composite_mode(Controller.COMPOSITE_DUAL_EQUAL) class TestNewRecord(object): @@ -421,7 +433,7 @@ def adjust_pip(self, sources.new_test_video(pattern=4) sources.new_test_video(pattern=5) controller = Controller() - controller.set_composite_mode(1) + controller.set_composite_mode(Controller.COMPOSITE_PIP) time.sleep(3) res = controller.adjust_pip(xpos, ypos, width, heigth) time.sleep(3) @@ -513,7 +525,7 @@ def switch(self, channel, port, index): def test_switch(self): """Test switch""" dic = [ - [65, 3004] + [Controller.SWITCH_VIDEO_1, 3004] ] start = 5 for i in range(start, 6): diff --git a/python-api/tests/unittests/test_controller_unit.py b/python-api/tests/unittests/test_controller_unit.py index 199beaa..aa96d7e 100644 --- a/python-api/tests/unittests/test_controller_unit.py +++ b/python-api/tests/unittests/test_controller_unit.py @@ -284,14 +284,14 @@ def test_unpack(self): controller.establish_connection = Mock(return_value=None) controller.connection = MockConnection(True) with pytest.raises(ConnectionReturnError): - controller.set_composite_mode(1) + controller.set_composite_mode(Controller.COMPOSITE_NONE) def test_normal_unpack(self): """Test if valid""" controller = Controller(address='unix:abstract=abcdef') controller.establish_connection = Mock(return_value=None) controller.connection = MockConnection(False) - assert controller.set_composite_mode(1) is True + assert controller.set_composite_mode(Controller.COMPOSITE_NONE) is True class TestSetEncodeMode(object): @@ -364,14 +364,14 @@ def test_unpack(self): controller.establish_connection = Mock(return_value=None) controller.connection = MockConnection(True) with pytest.raises(ConnectionReturnError): - controller.switch(1, 2) + controller.switch(Controller.SWITCH_VIDEO_1, 2) def test_normal_unpack(self): """Test if valid""" controller = Controller(address='unix:abstract=abcdef') controller.establish_connection = Mock(return_value=None) controller.connection = MockConnection(False) - assert controller.switch(1, 2) is True + assert controller.switch(Controller.SWITCH_VIDEO_1, 2) is True class TestClickVideo(object): diff --git a/tools/gstswitchcontroller.c b/tools/gstswitchcontroller.c index 1ef6d1d..c6083a8 100644 --- a/tools/gstswitchcontroller.c +++ b/tools/gstswitchcontroller.c @@ -548,6 +548,7 @@ gst_switch_controller_call_client (GstSwitchController * controller, GDBusConnection * connection, gint role, const gchar * method_name, GVariant * parameters, const GVariantType * reply_type) { + GVariant *value = NULL; GError *error = NULL; @@ -580,7 +581,7 @@ gst_switch_controller_call_client (GstSwitchController * controller, g_variant_unref (value); return NULL; } - g_assert (value != NULL); + // g_assert (value != NULL); return value; } @@ -597,7 +598,6 @@ gst_switch_controller_call_clients (GstSwitchController * controller, GDBusConnection *connection; GVariant *value; GList *ui, *clients = NULL; - g_variant_ref_sink (parameters); switch (role) {