channels.py 7.38 KB
Newer Older
1
#
David Trattnig's avatar
David Trattnig committed
2
# Aura Engine (https://gitlab.servus.at/aura/engine)
3
#
David Trattnig's avatar
David Trattnig committed
4
# Copyright (C) 2017-2020 - The Aura Engine Team.
5
#
David Trattnig's avatar
David Trattnig committed
6
7
8
9
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
10
#
David Trattnig's avatar
David Trattnig committed
11
12
13
14
# 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 Affero General Public License for more details.
15
#
David Trattnig's avatar
David Trattnig committed
16
17
18
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

19
20
21

from enum import Enum

22
23
from modules.base.utils import SimpleUtil as SU
from modules.core.resources import ResourceType
24
25


David Trattnig's avatar
David Trattnig committed
26
27
28
29
30
31
class TransitionType(Enum):
    """
    Types of fade-in and fade-out transition.
    """
    INSTANT = "instant"
    FADE = "fade"
32

33
34
class Channel(Enum):
    """
35
    Channel name mappings to the Liqidsoap channel IDs.
36
    """
37
38
39
40
    QUEUE_A = "in_filesystem_0"
    QUEUE_B = "in_filesystem_1"
    FALLBACK_QUEUE_A = "in_fallback_scheduled_0"
    FALLBACK_QUEUE_B = "in_fallback_scheduled_1"
David Trattnig's avatar
David Trattnig committed
41
42
    HTTP_A  = "in_http_0"
    HTTP_B  = "in_http_1"
David Trattnig's avatar
David Trattnig committed
43
44
    HTTPS_A  = "in_https_0"
    HTTPS_B  = "in_https_1"
45
46
47
48
49
    LIVE_0 = "linein_0"
    LIVE_1 = "linein_1"
    LIVE_2 = "linein_2"
    LIVE_3 = "linein_3"
    LIVE_4 = "linein_4"
50

51
52
53
    def __str__(self):
        return str(self.value)

54

55
56
57
58
class ChannelType(Enum):
    """
    Engine channel types mapped to `Entry` source types.
    """
59
    QUEUE = {
60
        "id": "fs",
61
        "numeric": 0,
62
63
64
65
66
67
        "channels": [Channel.QUEUE_A, Channel.QUEUE_B]
    }
    FALLBACK_QUEUE = {
        "id": "fallback_queue",
        "numeric": 0,
        "channels": [Channel.FALLBACK_QUEUE_A, Channel.FALLBACK_QUEUE_B]
68
    }
David Trattnig's avatar
David Trattnig committed
69
    HTTP = {
70
        "id": "http",
71
        "numeric": 1,
David Trattnig's avatar
Fix.    
David Trattnig committed
72
        "channels": [Channel.HTTP_A, Channel.HTTP_B]
73
    }
David Trattnig's avatar
David Trattnig committed
74
75
    HTTPS = {
        "id": "https",
76
        "numeric": 2,
David Trattnig's avatar
Fix.    
David Trattnig committed
77
        "channels": [Channel.HTTPS_A, Channel.HTTPS_B]
David Trattnig's avatar
David Trattnig committed
78
    }
79
80
    LIVE = {
        "id": "live",
81
        "numeric": 3,
82
83
84
85
86
87
88
89
90
        "channels": [
            Channel.LIVE_0, 
            Channel.LIVE_1,
            Channel.LIVE_2,
            Channel.LIVE_3,
            Channel.LIVE_4
        ]
    }

91
92
93
94
    @property
    def channels(self):
        return self.value["channels"]

95
96
97
98
    @property
    def numeric(self):
        return self.value["numeric"]

99
    def __str__(self):
100
101
        return str(self.value["id"])

102

David Trattnig's avatar
David Trattnig committed
103
104
105
106
107
108
109
110
111
112
113
114
class EntryPlayState(Enum):
    UNKNOWN = "unknown"
    LOADING = "loading"
    READY = "ready_to_play"
    PLAYING = "playing"
    FINISHED = "finished"

class LiquidsoapResponse(Enum):
    SUCCESS = "Done"
    STREAM_STATUS_POLLING = "polling"
    STREAM_STATUS_STOPPED = "stopped"
    STREAM_STATUS_CONNECTED = "connected"
David Trattnig's avatar
David Trattnig committed
115
    
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133


class ChannelRouter():
    """
    Wires source types with channels and channel-types.
    """
    config = None
    logger = None
    resource_mapping = None
    active_channels = None


    def __init__(self, config, logger):
        """
        Constructor

        Args:
            config (AuraConfig):    The configuration
134
            logger (Logger):        The logger
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
        """
        self.config = config
        self.logger = logger
        
        self.resource_mapping = {
            ResourceType.FILE: ChannelType.QUEUE,
            ResourceType.STREAM_HTTP: ChannelType.HTTP,
            ResourceType.STREAM_HTTPS: ChannelType.HTTPS,
            ResourceType.LINE: ChannelType.LIVE,
            ResourceType.PLAYLIST: ChannelType.QUEUE,
            ResourceType.POOL: ChannelType.QUEUE
        }

        self.active_channels = {
            ChannelType.QUEUE: Channel.QUEUE_A,
            ChannelType.FALLBACK_QUEUE: Channel.FALLBACK_QUEUE_A,
            ChannelType.HTTP: Channel.HTTP_A,
            ChannelType.HTTPS: Channel.HTTPS_A,
            ChannelType.LIVE: Channel.LIVE_0
        }
    
    
    def set_active(self, channel_type, channel):
        """
        Set the channel for the given resource type active
        """
        self.active_channels[channel_type] = channel


    def get_active(self, channel_type):
        """
        Retrieves the active channel for the given resource type
        """
        return self.active_channels[channel_type]


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    def type_of_channel(self, channel):
        """
        Retrieves a `ChannelType` for the given `Channel`.          
        """
        if channel in ChannelType.QUEUE.channels:
            return ChannelType.QUEUE
        elif channel in ChannelType.FALLBACK_QUEUE.channels:
            return ChannelType.FALLBACK_QUEUE
        elif channel in ChannelType.HTTP.channels:
            return ChannelType.HTTP
        elif channel in ChannelType.HTTPS.channels:
            return ChannelType.HTTPS
        elif channel in ChannelType.LIVE.channels:
            return ChannelType.LIVE
        else:
            return None


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
    def type_for_resource(self, resource_type):
        """
        Retrieves a `ChannelType` for the given `ResourceType`.

        Only default mappings can be evaluatated. Custom variations
        like fallback channels are not respected.    
        """
        return self.resource_mapping.get(resource_type)


    def channel_swap(self, channel_type):
        """
        Returns the currently inactive channel for a given type. For example if the currently some
        file on channel QUEUE A is playing, the channel QUEUE B is returned for being used
        to queue new entries.

        Args:
            entry_type (ResourceType): The resource type such es file, stream or live source

        Returns:
            (Channel, Channel): The previous and new channel
        """
        previous_channel = self.active_channels[channel_type]
        new_channel = None
        msg = None

        if channel_type == ChannelType.QUEUE:

            if previous_channel == Channel.QUEUE_A:
                new_channel = Channel.QUEUE_B
                msg = "Swapped queue channel from A > B"
            else:
                new_channel = Channel.QUEUE_A
                msg = "Swapped queue channel from B > A"

        elif channel_type == ChannelType.FALLBACK_QUEUE:

            if previous_channel == Channel.FALLBACK_QUEUE_A:
                new_channel = Channel.FALLBACK_QUEUE_B
                msg = "Swapped fallback queue channel from A > B"
            else:
                new_channel = Channel.FALLBACK_QUEUE_A
                msg = "Swapped fallback channel from B > A"

        elif channel_type == ChannelType.HTTP:

            if previous_channel == Channel.HTTP_A:
                new_channel = Channel.HTTP_B
                msg = "Swapped HTTP Stream channel from A > B"
            else:
                new_channel = Channel.HTTP_A
                msg = "Swapped HTTP Stream channel from B > A"

        elif channel_type == ChannelType.HTTPS:

            if previous_channel == Channel.HTTPS_A:
                new_channel = Channel.HTTPS_B
                msg = "Swapped HTTPS Stream channel from A > B"
            else:
                new_channel = Channel.HTTPS_A
                msg = "Swapped HTTPS Stream channel from B > A"
        
        else:
            self.logger.warning(SU.red(f"No channel to swap - invalid entry_type '{channel_type}'"))

        if msg: self.logger.info(SU.pink(msg))
        return (previous_channel, new_channel)