diff --git a/portier/settings.py b/portier/settings.py index 7357278..aeb75ba 100644 --- a/portier/settings.py +++ b/portier/settings.py @@ -36,7 +36,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'srs.apps.SrsConfig', + 'rtmp.apps.RtmpConfig', 'restream.apps.RestreamConfig', ] diff --git a/portier/urls.py b/portier/urls.py index bb2d48f..d1ecf72 100644 --- a/portier/urls.py +++ b/portier/urls.py @@ -18,5 +18,5 @@ from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), - path('srs/', include('srs.urls')) + path('rtmp/', include('rtmp.urls')) ] diff --git a/restream/admin.py b/restream/admin.py index 2669f3c..1fe1551 100644 --- a/restream/admin.py +++ b/restream/admin.py @@ -3,7 +3,7 @@ from .models import RestreamConfig class RestreamConfigAdmin(admin.ModelAdmin): - fields = ['name', 'active', 'streamkey', 'target'] + fields = ['name', 'active', 'stream', 'target'] admin.site.register(RestreamConfig, RestreamConfigAdmin) diff --git a/restream/migrations/0001_initial.py b/restream/migrations/0001_initial.py index 465dc00..01dfa22 100644 --- a/restream/migrations/0001_initial.py +++ b/restream/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.5 on 2020-04-13 17:45 +# Generated by Django 3.0.5 on 2020-04-23 19:04 from django.db import migrations, models import django.db.models.deletion @@ -9,7 +9,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('srs', '0005_auto_20200413_1745'), + ('rtmp', '0001_initial'), ] operations = [ @@ -20,7 +20,7 @@ class Migration(migrations.Migration): ('target', models.CharField(max_length=500)), ('name', models.CharField(max_length=100)), ('active', models.BooleanField()), - ('streamkey', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='srs.Streamkey')), + ('stream', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rtmp.Stream')), ], ), ] diff --git a/restream/models.py b/restream/models.py index d3d2828..fa2f286 100644 --- a/restream/models.py +++ b/restream/models.py @@ -2,14 +2,14 @@ from django.db import models # Create your models here. -from srs.models import Streamkey +from rtmp.models import Stream class RestreamConfig(models.Model): - streamkey = models.ForeignKey(Streamkey, on_delete=models.CASCADE) + stream = models.ForeignKey(Stream, on_delete=models.CASCADE) target = models.CharField(max_length=500) name = models.CharField(max_length=100) active = models.BooleanField() def __str__(self): - return '{} to {}'.format(self.streamkey, self.name) + return '{} to {}'.format(self.stream, self.name) diff --git a/restream/signals.py b/restream/signals.py index 8bb481a..4c66258 100644 --- a/restream/signals.py +++ b/restream/signals.py @@ -1,12 +1,12 @@ import logging from django.dispatch import receiver -from srs.signals import on_publish, on_unpublish +from rtmp.signals import on_publish, on_unpublish from portier.celery import app as celery from .models import RestreamConfig -from srs.models import Streamkey +from rtmp.models import Stream logger = logging.getLogger(__name__) @@ -20,13 +20,12 @@ def callback_on_unpublish(sender, **kwargs): @receiver(on_publish) def callback_on_publish(sender, **kwargs): logger.info("start publish - {}".format(kwargs['name'])) - streamkey = Streamkey.objects.get(key=kwargs['streamkey']) - configs = RestreamConfig.objects.filter(streamkey=streamkey) + stream = Stream.objects.get(key=kwargs['stream']) + configs = RestreamConfig.objects.filter(stream=stream) for config in configs: - pass celery.send_task('main.start_restream', kwargs={ 'app': kwargs['app'], - 'streamkey': kwargs['streamkey'], + 'stream': kwargs['stream'], 'target': config.target, 'id': config.id }) diff --git a/srs/__init__.py b/rtmp/__init__.py similarity index 100% rename from srs/__init__.py rename to rtmp/__init__.py diff --git a/rtmp/admin.py b/rtmp/admin.py new file mode 100644 index 0000000..45af363 --- /dev/null +++ b/rtmp/admin.py @@ -0,0 +1,14 @@ +from django.contrib import admin +from .models import Application, Stream + + +class ApplicationAdmin(admin.ModelAdmin): + fields = ['name'] + + +class StreamAdmin(admin.ModelAdmin): + fields = ['application', 'stream', 'name'] + + +admin.site.register(Application, ApplicationAdmin) +admin.site.register(Stream, StreamAdmin) diff --git a/rtmp/apps.py b/rtmp/apps.py new file mode 100644 index 0000000..42c20dc --- /dev/null +++ b/rtmp/apps.py @@ -0,0 +1,8 @@ +from django.apps import AppConfig + + +class RtmpConfig(AppConfig): + name = 'rtmp' + + def ready(self): + import rtmp.signals # noqa diff --git a/srs/migrations/0001_initial.py b/rtmp/migrations/0001_initial.py similarity index 58% rename from srs/migrations/0001_initial.py rename to rtmp/migrations/0001_initial.py index 017069c..288c5ea 100644 --- a/srs/migrations/0001_initial.py +++ b/rtmp/migrations/0001_initial.py @@ -1,8 +1,8 @@ -# Generated by Django 3.0.5 on 2020-04-13 15:30 +# Generated by Django 3.0.5 on 2020-04-23 19:04 -from django.conf import settings from django.db import migrations, models import django.db.models.deletion +import uuid class Migration(migrations.Migration): @@ -10,7 +10,6 @@ class Migration(migrations.Migration): initial = True dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -18,16 +17,16 @@ class Migration(migrations.Migration): name='Application', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=16)), + ('name', models.CharField(help_text='rtmp_application_name', max_length=100, unique=True)), ], ), migrations.CreateModel( - name='Streamkey', + name='Stream', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('key', models.CharField(max_length=64)), - ('application', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='srs.Application')), - ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('stream', models.CharField(default=uuid.uuid4, max_length=64, unique=True)), + ('name', models.CharField(max_length=100)), + ('application', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rtmp.Application')), ], ), ] diff --git a/srs/migrations/__init__.py b/rtmp/migrations/__init__.py similarity index 100% rename from srs/migrations/__init__.py rename to rtmp/migrations/__init__.py diff --git a/srs/models.py b/rtmp/models.py similarity index 55% rename from srs/models.py rename to rtmp/models.py index 218ce13..9dc331f 100644 --- a/srs/models.py +++ b/rtmp/models.py @@ -6,38 +6,32 @@ from . import signals class Application(models.Model): - name = models.CharField(max_length=100, unique=True, help_text=_("srs_application_name")) + name = models.CharField(max_length=100, unique=True, help_text=_("rtmp_application_name")) def __str__(self): return self.name -class Streamkey(models.Model): +class Stream(models.Model): application = models.ForeignKey(Application, on_delete=models.CASCADE) - key = models.CharField(max_length=64, unique=True, default=uuid.uuid4()) + stream = models.CharField(max_length=64, unique=True, default=uuid.uuid4) name = models.CharField(max_length=100) - def on_publish(self, client_ip, client_id, vhost, param): + def on_publish(self, param): signals.on_publish.send(sender=self.__class__, name=self.name, - streamkey=self.key, + stream=self.stream, app=str(self.application), - client_ip=client_ip, - client_id=client_id, - vhost=vhost, param=param ) - def on_unpublish(self, client_ip, client_id, vhost, param): + def on_unpublish(self, param): signals.on_unpublish.send(sender=self.__class__, name=self.name, - streamkey=self.key, + stream=self.stream, app=str(self.application), - client_ip=client_ip, - client_id=client_id, - vhost=vhost, param=param ) def __str__(self): - return '{}'.format(self.name) + return self.name diff --git a/rtmp/signals.py b/rtmp/signals.py new file mode 100644 index 0000000..42054c2 --- /dev/null +++ b/rtmp/signals.py @@ -0,0 +1,4 @@ +from django.dispatch import Signal + +on_publish = Signal(providing_args=['application', 'stream', 'params']) +on_unpublish = Signal(providing_args=['application', 'stream', 'params']) diff --git a/srs/tests.py b/rtmp/tests.py similarity index 100% rename from srs/tests.py rename to rtmp/tests.py diff --git a/srs/urls.py b/rtmp/urls.py similarity index 50% rename from srs/urls.py rename to rtmp/urls.py index 24f5924..a80bfb0 100644 --- a/srs/urls.py +++ b/rtmp/urls.py @@ -2,5 +2,5 @@ from django.urls import path from . import views urlpatterns = [ - path('callback', views.callback, name='callback'), + path('callback/srs', views.callback_srs, name='callback_srs'), ] diff --git a/srs/views.py b/rtmp/views.py similarity index 53% rename from srs/views.py rename to rtmp/views.py index 39232fb..2bf13bc 100644 --- a/srs/views.py +++ b/rtmp/views.py @@ -11,40 +11,32 @@ logger = logging.getLogger(__name__) @csrf_exempt -def callback(request): +def callback_srs(request): if request.method != 'POST': return HttpResponse('1', status=405) - json_data = json.loads(request.body) + try: + json_data = json.loads(request.body) + except json.decoder.JSONDecodeError: + return HttpResponse('1', status=400) try: - client_ip = json_data['ip'] - client_id = json_data['client_id'] - vhost = json_data['vhost'] - param = json_data['param'] app_name = json_data['app'] stream_name = json_data['stream'] + param = json_data['param'] except KeyError: return HttpResponse('1', status=401) try: application = models.Application.objects.get(name=app_name) - streamkey = models.Streamkey.objects.get(key=stream_name, application=application) + stream = models.Stream.objects.get(stream=stream_name, application=application) except ObjectDoesNotExist: return HttpResponse('1', status=401) if json_data.get('action') == 'on_publish': - streamkey.on_publish(client_ip=client_ip, - client_id=client_id, - vhost=vhost, - param=param - ) + stream.on_publish(param=param) if json_data.get('action') == 'on_unpublish': - streamkey.on_unpublish(client_ip=client_ip, - client_id=client_id, - vhost=vhost, - param=param - ) + stream.on_unpublish(param=param) return HttpResponse('0') diff --git a/srs/admin.py b/srs/admin.py deleted file mode 100644 index 3ef5f7f..0000000 --- a/srs/admin.py +++ /dev/null @@ -1,14 +0,0 @@ -from django.contrib import admin -from .models import Application, Streamkey - - -class ApplicationAdmin(admin.ModelAdmin): - fields = ['name'] - - -class StreamkeyAdmin(admin.ModelAdmin): - fields = ['application', 'key', 'name'] - - -admin.site.register(Application, ApplicationAdmin) -admin.site.register(Streamkey, StreamkeyAdmin) diff --git a/srs/apps.py b/srs/apps.py deleted file mode 100644 index 8dd0da0..0000000 --- a/srs/apps.py +++ /dev/null @@ -1,8 +0,0 @@ -from django.apps import AppConfig - - -class SrsConfig(AppConfig): - name = 'srs' - - def ready(self): - import srs.signals # noqa diff --git a/srs/migrations/0002_streamkey_name.py b/srs/migrations/0002_streamkey_name.py deleted file mode 100644 index 179ed6b..0000000 --- a/srs/migrations/0002_streamkey_name.py +++ /dev/null @@ -1,19 +0,0 @@ -# Generated by Django 3.0.5 on 2020-04-13 15:40 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('srs', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='streamkey', - name='name', - field=models.CharField(default='', max_length=16), - preserve_default=False, - ), - ] diff --git a/srs/migrations/0003_remove_streamkey_owner.py b/srs/migrations/0003_remove_streamkey_owner.py deleted file mode 100644 index 2ff211e..0000000 --- a/srs/migrations/0003_remove_streamkey_owner.py +++ /dev/null @@ -1,17 +0,0 @@ -# Generated by Django 3.0.5 on 2020-04-13 17:21 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('srs', '0002_streamkey_name'), - ] - - operations = [ - migrations.RemoveField( - model_name='streamkey', - name='owner', - ), - ] diff --git a/srs/migrations/0004_auto_20200413_1736.py b/srs/migrations/0004_auto_20200413_1736.py deleted file mode 100644 index 8e74277..0000000 --- a/srs/migrations/0004_auto_20200413_1736.py +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by Django 3.0.5 on 2020-04-13 17:36 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('srs', '0003_remove_streamkey_owner'), - ] - - operations = [ - migrations.AlterField( - model_name='application', - name='name', - field=models.CharField(max_length=100, unique=True), - ), - migrations.AlterField( - model_name='streamkey', - name='key', - field=models.CharField(max_length=64, unique=True), - ), - migrations.AlterField( - model_name='streamkey', - name='name', - field=models.CharField(max_length=100), - ), - ] diff --git a/srs/migrations/0005_auto_20200413_1745.py b/srs/migrations/0005_auto_20200413_1745.py deleted file mode 100644 index 08c808b..0000000 --- a/srs/migrations/0005_auto_20200413_1745.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 3.0.5 on 2020-04-13 17:45 - -from django.db import migrations, models -import uuid - - -class Migration(migrations.Migration): - - dependencies = [ - ('srs', '0004_auto_20200413_1736'), - ] - - operations = [ - migrations.AlterField( - model_name='streamkey', - name='key', - field=models.CharField(default=uuid.UUID('b5777854-4533-49dc-b38d-69738d8844d6'), - max_length=64, unique=True), - ), - ] diff --git a/srs/signals.py b/srs/signals.py deleted file mode 100644 index 2618e9f..0000000 --- a/srs/signals.py +++ /dev/null @@ -1,4 +0,0 @@ -from django.dispatch import Signal - -on_publish = Signal(providing_args=['application', 'streamkey', 'node']) -on_unpublish = Signal(providing_args=['application', 'streamkey', 'node'])