From 5c6ca82c134bb0cc0340ed10b6646e46d5c47f96 Mon Sep 17 00:00:00 2001 From: Daniel Perelman Date: Mon, 11 May 2020 02:57:30 -0700 Subject: [PATCH] Initial working version. --- .gitignore | 61 ++++++++++++ camera/__init__.py | 0 camera/admin.py | 3 + camera/apps.py | 5 + camera/consumers.py | 42 +++++++++ camera/migrations/__init__.py | 0 camera/models.py | 3 + camera/routing.py | 8 ++ camera/templates/index.html | 173 ++++++++++++++++++++++++++++++++++ camera/tests.py | 3 + camera/urls.py | 8 ++ camera/views.py | 19 ++++ camera_site/__init__.py | 0 camera_site/asgi.py | 12 +++ camera_site/routing.py | 9 ++ camera_site/settings.py | 94 ++++++++++++++++++ camera_site/urls.py | 21 +++++ manage.py | 21 +++++ run_daphne.sh | 5 + run_viewer.sh | 1 + 20 files changed, 488 insertions(+) create mode 100644 .gitignore create mode 100644 camera/__init__.py create mode 100644 camera/admin.py create mode 100644 camera/apps.py create mode 100644 camera/consumers.py create mode 100644 camera/migrations/__init__.py create mode 100644 camera/models.py create mode 100644 camera/routing.py create mode 100644 camera/templates/index.html create mode 100644 camera/tests.py create mode 100644 camera/urls.py create mode 100644 camera/views.py create mode 100644 camera_site/__init__.py create mode 100644 camera_site/asgi.py create mode 100644 camera_site/routing.py create mode 100644 camera_site/settings.py create mode 100644 camera_site/urls.py create mode 100755 manage.py create mode 100755 run_daphne.sh create mode 100755 run_viewer.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..473a980 --- /dev/null +++ b/.gitignore @@ -0,0 +1,61 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# sqlite database +*.sqlite3 diff --git a/camera/__init__.py b/camera/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/camera/admin.py b/camera/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/camera/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/camera/apps.py b/camera/apps.py new file mode 100644 index 0000000..5892952 --- /dev/null +++ b/camera/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class CameraConfig(AppConfig): + name = 'camera' diff --git a/camera/consumers.py b/camera/consumers.py new file mode 100644 index 0000000..288cc61 --- /dev/null +++ b/camera/consumers.py @@ -0,0 +1,42 @@ +from channels.generic.websocket import AsyncWebsocketConsumer + + +class VideoConsumer(AsyncWebsocketConsumer): + async def connect(self): + self.room_name = self.scope['url_route']['kwargs']['room_name'] + self.kind = self.scope['url_route']['kwargs']['kind'] + self.listen_group_name = '%s_%s' % (self.kind, self.room_name) + other_kind = 'client' if self.kind == 'host' else 'host' + self.send_group_name = '%s_%s' % (other_kind, self.room_name) + + # Join room group + await self.channel_layer.group_add( + self.listen_group_name, + self.channel_name + ) + + await self.accept() + + async def disconnect(self, close_code): + # Leave room group + await self.channel_layer.group_discard( + self.listen_group_name, + self.channel_name + ) + + # Receive message from WebSocket + async def receive(self, text_data): + await self.channel_layer.group_send( + self.send_group_name, + { + 'type': 'chat_message', + 'message': text_data + } + ) + + # Receive message from room group + async def chat_message(self, event): + message = event['message'] + + # Send message to WebSocket + await self.send(text_data=message) diff --git a/camera/migrations/__init__.py b/camera/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/camera/models.py b/camera/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/camera/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/camera/routing.py b/camera/routing.py new file mode 100644 index 0000000..ee4b939 --- /dev/null +++ b/camera/routing.py @@ -0,0 +1,8 @@ +from django.urls import re_path + +from . import consumers + +websocket_urlpatterns = [ + re_path(r'camera/ws/(?Phost|client)/(?P\w+)/$', + consumers.VideoConsumer), +] diff --git a/camera/templates/index.html b/camera/templates/index.html new file mode 100644 index 0000000..e05956f --- /dev/null +++ b/camera/templates/index.html @@ -0,0 +1,173 @@ + + + + + Simple WebRTC + + + + + + + + + diff --git a/camera/tests.py b/camera/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/camera/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/camera/urls.py b/camera/urls.py new file mode 100644 index 0000000..332f444 --- /dev/null +++ b/camera/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('camera/', views.camera, name='camera'), + path('camera//qr', views.camera_qr, name='camera_qr'), +] diff --git a/camera/views.py b/camera/views.py new file mode 100644 index 0000000..d31cd74 --- /dev/null +++ b/camera/views.py @@ -0,0 +1,19 @@ +from io import BytesIO +import qrcode + +from django.http import HttpResponse +from django.shortcuts import render +from django.urls import reverse + + +def camera(request): + return render(request, 'index.html') + + +def camera_qr(request, room_name): + camera_url = reverse('camera') + camera_url = request.build_absolute_uri(camera_url) + '#' + room_name + img = qrcode.make(camera_url) + output = BytesIO() + img.save(output, "PNG") + return HttpResponse(output.getvalue(), content_type='image/png') diff --git a/camera_site/__init__.py b/camera_site/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/camera_site/asgi.py b/camera_site/asgi.py new file mode 100644 index 0000000..ee20932 --- /dev/null +++ b/camera_site/asgi.py @@ -0,0 +1,12 @@ +""" +ASGI entrypoint. Configures Django and then runs the application +defined in the ASGI_APPLICATION setting. +""" + +import os +import django +from channels.routing import get_default_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "camera_site.settings") +django.setup() +application = get_default_application() diff --git a/camera_site/routing.py b/camera_site/routing.py new file mode 100644 index 0000000..cfccc34 --- /dev/null +++ b/camera_site/routing.py @@ -0,0 +1,9 @@ +from channels.routing import ProtocolTypeRouter, URLRouter +import camera.routing + +application = ProtocolTypeRouter({ + # (http->django views is added by default) + 'websocket': URLRouter( + camera.routing.websocket_urlpatterns + ), +}) diff --git a/camera_site/settings.py b/camera_site/settings.py new file mode 100644 index 0000000..b18e430 --- /dev/null +++ b/camera_site/settings.py @@ -0,0 +1,94 @@ +""" +Django settings for camera_site project. + +Generated by 'django-admin startproject' using Django 3.0.4. + +For more information on this file, see +https://docs.djangoproject.com/en/3.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'xh9dx24&9r8yx!@@qyjn@b7z%b-_mc&@itrv&52z#8@=08sdlr' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = False + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'channels', + 'camera', + 'django.contrib.contenttypes', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'camera_site.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + ], + }, + }, +] + +WSGI_APPLICATION = 'camera_site.wsgi.application' + +# Internationalization +# https://docs.djangoproject.com/en/3.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.0/howto/static-files/ + +STATIC_URL = '/static/' + +ASGI_APPLICATION = 'camera_site.routing.application' +CHANNEL_LAYERS = { + 'default': { + 'BACKEND': 'channels_redis.core.RedisChannelLayer', + 'CONFIG': { + "hosts": [('127.0.0.1', 6379)], + }, + }, +} + +SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') diff --git a/camera_site/urls.py b/camera_site/urls.py new file mode 100644 index 0000000..d381ca6 --- /dev/null +++ b/camera_site/urls.py @@ -0,0 +1,21 @@ +"""camera_site URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.conf.urls import include +from django.urls import path + +urlpatterns = [ + path('', include('camera.urls')), +] diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..711e53a --- /dev/null +++ b/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'camera_site.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/run_daphne.sh b/run_daphne.sh new file mode 100755 index 0000000..1c6c190 --- /dev/null +++ b/run_daphne.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +export DJANGO_SETTINGS_MODULE=camera_site.local_settings +daphne -u site.sock camera_site.asgi:application + diff --git a/run_viewer.sh b/run_viewer.sh new file mode 100755 index 0000000..ba99866 --- /dev/null +++ b/run_viewer.sh @@ -0,0 +1 @@ +chromium --incognito --app=https://localhost/viewer/lobby/