channels.py 8.35 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
from src.base.utils import SimpleUtil as SU
David Trattnig's avatar
David Trattnig committed
23
from src.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/source IDs.
36
    """
37
38
    QUEUE_A = "in_filesystem_0"
    QUEUE_B = "in_filesystem_1"
David Trattnig's avatar
David Trattnig committed
39
40
    HTTP_A  = "in_http_0"
    HTTP_B  = "in_http_1"
David Trattnig's avatar
David Trattnig committed
41
42
    HTTPS_A  = "in_https_0"
    HTTPS_B  = "in_https_1"
43
44
45
46
47
    LIVE_0 = "linein_0"
    LIVE_1 = "linein_1"
    LIVE_2 = "linein_2"
    LIVE_3 = "linein_3"
    LIVE_4 = "linein_4"
48
    FALLBACK_QUEUE_A = "in_fallback_scheduled_0"
49
    FALLBACK_QUEUE_B = "in_fallback_scheduled_1"
50
51
    FALLBACK_STATION_FOLDER = "station_folder"
    FALLBACK_STATION_PLAYLIST = "station_playlist"
52

53
54
55
    def __str__(self):
        return str(self.value)

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
class ChannelResolver():
    """
    Helpers for resolving channel enumerations.
    """

    @staticmethod
    def live_channel_for_resource(channel: str):
        """
        Returns the channel enum for a given channel string.
        """
        if not channel: return None
        channel = "linein_" + channel.split("line://")[1]

        if channel == Channel.LIVE_0.value:
            return Channel.LIVE_0
        elif channel == Channel.LIVE_1.value:
            return Channel.LIVE_2
        elif channel == Channel.LIVE_3.value:
            return Channel.LIVE_3
        elif channel == Channel.LIVE_4.value:
            return Channel.LIVE_4
        elif channel == Channel.LIVE_5.value:
            return Channel.LIVE_5
        else:
            return None


84
85
86
87
class ChannelType(Enum):
    """
    Engine channel types mapped to `Entry` source types.
    """
88
    QUEUE = {
89
        "id": "fs",
90
        "numeric": 0,
91
92
        "channels": [Channel.QUEUE_A, Channel.QUEUE_B]
    }
David Trattnig's avatar
David Trattnig committed
93
    HTTP = {
94
        "id": "http",
95
        "numeric": 1,
David Trattnig's avatar
Fix.    
David Trattnig committed
96
        "channels": [Channel.HTTP_A, Channel.HTTP_B]
97
    }
David Trattnig's avatar
David Trattnig committed
98
99
    HTTPS = {
        "id": "https",
100
        "numeric": 2,
David Trattnig's avatar
Fix.    
David Trattnig committed
101
        "channels": [Channel.HTTPS_A, Channel.HTTPS_B]
David Trattnig's avatar
David Trattnig committed
102
    }
103
104
    LIVE = {
        "id": "live",
105
        "numeric": 3,
106
        "channels": [
107
            Channel.LIVE_0,
108
109
110
111
112
113
            Channel.LIVE_1,
            Channel.LIVE_2,
            Channel.LIVE_3,
            Channel.LIVE_4
        ]
    }
114
115
116
117
    FALLBACK_QUEUE = {
        "id": "fallback_queue",
        "numeric": 4,
        "channels": [Channel.FALLBACK_QUEUE_A, Channel.FALLBACK_QUEUE_B]
118
    }
119
120
121
122
123
    FALLBACK_POOL = {
        "id": "fallback_pool",
        "numeric": 5,
        "channels": [Channel.FALLBACK_STATION_FOLDER, Channel.FALLBACK_STATION_PLAYLIST]
    }
124

125
126
127
128
    @property
    def channels(self):
        return self.value["channels"]

129
130
131
132
    @property
    def numeric(self):
        return self.value["numeric"]

133
    def __str__(self):
134
135
        return str(self.value["id"])

136

David Trattnig's avatar
David Trattnig committed
137
138
139
140
141
142
143
144
145
146
147
148
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"
149

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167


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
168
            logger (Logger):        The logger
169
170
171
        """
        self.config = config
        self.logger = logger
172

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        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
        }
189
190


191
192
193
194
195
196
197
198
199
200
201
202
203
204
    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]


205
206
    def type_of_channel(self, channel):
        """
207
        Retrieves a `ChannelType` for the given `Channel`.
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
        """
        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


223
224
225
226
227
    def type_for_resource(self, resource_type):
        """
        Retrieves a `ChannelType` for the given `ResourceType`.

        Only default mappings can be evaluatated. Custom variations
228
        like fallback channels are not respected.
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
        """
        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"
284

285
286
287
288
289
290
291
        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)