From f26a636c6e5b80b573a185064f48e2086be914c4 Mon Sep 17 00:00:00 2001 From: Halsey Burgund Date: Mon, 13 Nov 2017 21:30:01 -0500 Subject: [PATCH] add streams/:id/kill/ endpoint to allow for easy killing of streams (#364) --- roundware/api2/views.py | 16 +++++++++++++++- roundware/lib/api.py | 4 ++++ roundwared/icecast2.py | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/roundware/api2/views.py b/roundware/api2/views.py index f8e991d1..3bd36f10 100644 --- a/roundware/api2/views.py +++ b/roundware/api2/views.py @@ -18,7 +18,7 @@ from roundware.lib.api import (get_project_tags_new as get_project_tags, modify_stream, move_listener, heartbeat, skip_ahead, pause, resume, add_asset_to_envelope, get_currently_streaming_asset, save_asset_from_request, vote_asset, check_is_active, - vote_count_by_asset, log_event, play) + vote_count_by_asset, log_event, play, kill) from roundware.api2.permissions import AuthenticatedReadAdminWrite from rest_framework import viewsets, status from rest_framework.permissions import IsAuthenticated, DjangoObjectPermissions @@ -828,6 +828,7 @@ class StreamViewSet(viewsets.ViewSet): api/2/streams/:id/pause/ api/2/streams/:id/resume/ api/2/streams/:id/isactive/ + api/2/streams/:id/kill/ """ permission_classes = (IsAuthenticated,) @@ -923,6 +924,19 @@ def isactive(self, request, pk=None): return Response({"detail": str(e)}, status.HTTP_400_BAD_REQUEST) + @detail_route(methods=['post']) + def kill(self, request, pk=None): + try: + result = kill(pk, "mp3") + stream_id = int(pk) + return Response({ + 'stream_id': stream_id, + 'success': result + }) + except Exception as e: + return Response({"detail": str(e)}, + status.HTTP_400_BAD_REQUEST) + class TagViewSet(viewsets.ViewSet): """ diff --git a/roundware/lib/api.py b/roundware/lib/api.py index 1b55c7ab..e3b1020a 100644 --- a/roundware/lib/api.py +++ b/roundware/lib/api.py @@ -476,6 +476,10 @@ def check_is_active(session_id): return stream_exists(session_id, audio_format) +def kill(session_id, audio_format): + admin = icecast2.Admin() + result = admin.kill_source(icecast2.mount_point(session_id, audio_format)) + return result # create_envelope # args: (operation, session_id, [tags]) diff --git a/roundwared/icecast2.py b/roundwared/icecast2.py index ec66a2f4..9cb5a5c0 100644 --- a/roundwared/icecast2.py +++ b/roundwared/icecast2.py @@ -42,6 +42,14 @@ def stream_exists(self, mount): return True return False + def kill_source(self, mount): + result = self.process_xml( + "/admin/killsource?mount=" + mount, "//icestats/source/@mount") + if not self.stream_exists(mount): + return True + else: + return False + def process_xml(self, url, xpath): # logger.debug("Request: %s, auth=%s", self.base_uri + url, self.auth) response = requests.get(self.base_uri + url, auth=self.auth)