2024-02-27 20:44:35 +01:00
|
|
|
import json
|
2020-05-01 12:38:57 +02:00
|
|
|
import uuid
|
|
|
|
|
2024-02-27 20:44:35 +01:00
|
|
|
from django.conf import settings
|
2020-04-15 20:29:59 +02:00
|
|
|
from django.db import models
|
2020-04-29 18:48:23 +02:00
|
|
|
from django.urls import reverse
|
2020-04-23 20:19:49 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-05-01 12:38:57 +02:00
|
|
|
from django.db.models.signals import pre_delete
|
|
|
|
from portier.common import handlers
|
2024-02-27 20:44:35 +01:00
|
|
|
from . import signals_shared
|
2020-04-15 20:29:59 +02:00
|
|
|
|
|
|
|
|
2020-04-23 21:08:39 +02:00
|
|
|
class Stream(models.Model):
|
2020-04-29 21:09:04 +02:00
|
|
|
stream = models.UUIDField(unique=True, default=uuid.uuid4, help_text=_('stream_stream_help'))
|
|
|
|
name = models.CharField(max_length=100, help_text=_('stream_name_help'))
|
2020-04-15 20:29:59 +02:00
|
|
|
|
2020-04-26 19:46:58 +02:00
|
|
|
# the same stream uuid can be published multiple times to different origin
|
|
|
|
# servers. this is a valid scheme to achieve a failover on the origin layer.
|
|
|
|
# thus we need to keep track if a stream is published at least once,
|
|
|
|
# and only send signals when we are going to / coming from 0 published streams.
|
|
|
|
publish_counter = models.PositiveIntegerField(default=0)
|
|
|
|
|
2020-04-23 21:08:39 +02:00
|
|
|
def on_publish(self, param):
|
2020-04-26 19:46:58 +02:00
|
|
|
# if so far there were less than one incoming streams, this stream
|
|
|
|
# is now being considered active
|
|
|
|
if self.publish_counter < 1:
|
2024-02-27 20:44:35 +01:00
|
|
|
signals_shared.stream_active.send(sender=self.__class__,
|
2020-04-26 22:25:58 +02:00
|
|
|
stream=str(self.stream),
|
2020-04-26 19:46:58 +02:00
|
|
|
param=param
|
|
|
|
)
|
|
|
|
|
|
|
|
# keep track of this incoming stream
|
|
|
|
self.publish_counter += 1
|
|
|
|
self.save()
|
2020-04-15 20:29:59 +02:00
|
|
|
|
2020-04-23 21:08:39 +02:00
|
|
|
def on_unpublish(self, param):
|
2020-04-26 19:46:58 +02:00
|
|
|
# note that we now have on less incoming stream
|
|
|
|
if self.publish_counter > 0:
|
|
|
|
self.publish_counter -= 1
|
|
|
|
|
|
|
|
# if we now have less than one incoming stream, this stream is being
|
|
|
|
# considered inactive
|
|
|
|
if self.publish_counter < 1:
|
2024-02-27 20:44:35 +01:00
|
|
|
signals_shared.stream_inactive.send(sender=self.__class__,
|
2020-04-26 22:25:58 +02:00
|
|
|
stream=str(self.stream),
|
2020-04-26 19:46:58 +02:00
|
|
|
param=param
|
|
|
|
)
|
|
|
|
self.save()
|
2020-04-15 20:29:59 +02:00
|
|
|
|
2020-04-29 18:48:23 +02:00
|
|
|
def get_absolute_url(self):
|
2024-02-27 20:44:35 +01:00
|
|
|
return reverse('config:stream_detail', kwargs={'pk': self.pk})
|
2020-04-29 18:48:23 +02:00
|
|
|
|
|
|
|
def class_name(self):
|
2020-04-29 21:09:04 +02:00
|
|
|
return _('stream_class_name')
|
2020-04-29 18:48:23 +02:00
|
|
|
|
2020-04-15 20:29:59 +02:00
|
|
|
def __str__(self):
|
2020-04-23 21:08:39 +02:00
|
|
|
return self.name
|
2020-05-01 12:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
pre_delete.connect(handlers.remove_obj_perms_connected_with_user, sender=Stream)
|
2024-02-27 20:44:35 +01:00
|
|
|
|
|
|
|
class Restream(models.Model):
|
|
|
|
FORMATS = (
|
|
|
|
('flv', 'flv (RTMP)'),
|
|
|
|
('mpegts', 'mpegts (SRT)'),
|
|
|
|
)
|
|
|
|
stream = models.ForeignKey(Stream, on_delete=models.CASCADE, help_text=_('restream_stream_help'))
|
|
|
|
target = models.CharField(max_length=500, help_text=_('restream_target_help'))
|
|
|
|
name = models.CharField(max_length=100, help_text=_('restream_name_help'))
|
|
|
|
active = models.BooleanField(help_text=_('restream_activate_help'))
|
|
|
|
format = models.CharField(max_length=6, choices=FORMATS, default='flv', help_text=_('restream_format_help'))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('restream_verbose_name')
|
|
|
|
verbose_name_plural = _('restream_verbose_name_plural')
|
|
|
|
|
|
|
|
def class_name(self):
|
|
|
|
return _('restream_class_name')
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('config:restream_detail', kwargs={'pk': self.pk})
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{} to {}'.format(self.stream, self.name)
|
|
|
|
|
|
|
|
def get_json_config(self):
|
|
|
|
config = {
|
|
|
|
'name': self.name,
|
|
|
|
'app': settings.GLOBAL_STREAM_NAMESPACE,
|
|
|
|
'stream': str(self.stream.stream),
|
|
|
|
'target': self.target,
|
|
|
|
'format': self.format
|
|
|
|
}
|
|
|
|
return json.dumps(config)
|
|
|
|
|
|
|
|
|
|
|
|
pre_delete.connect(handlers.remove_obj_perms_connected_with_user, sender=Restream)
|