def video_feed(request): resp = StreamingHttpResponse() ip_address = request.GET['ip_address'] camera_user = request.GET['camera_user'] if request.GET['camera_user'] is not None else '' camera_password = request.GET['camera_password'] if request.GET['camera_password'] is not None else '' camera_brand = request.GET['camera_brand'] streamming_type = request.GET['streamming_type'] # URLPathByBranddata = URLPathByBrand() url_path = URLPathByBrand.objects.get(camera_brand=camera_brand, streamming_type=streamming_type).URLPath if camera_brand == 'MAC': streamming_url = 0 else: if streamming_type == "IMAGE": streamming_url = 'http://' + camera_user + ':' + camera_password + '@' + ip_address + url_path else: streamming_url = 'rtsp://' + camera_user + ':' + camera_password + '@' + ip_address + url_path try: generator = generate(streamming_url) if generator != 'error': resp = StreamingHttpResponse(generator, content_type="multipart/x-mixed-replace; boundary=frame") return resp else: print('video veeding is error of streaming.') except: resp.close() print('except on view.py')
def get_stream(request): """ Stream the video coming from connected camera """ Capturing.objects.create() response = StreamingHttpResponse( video_stream(), content_type="multipart/x-mixed-replace;boundary=frame") if request.method == "GET": return response elif request.method == "POST": capturing = Capturing.objects.last() capturing.stop = True capturing.save() response.close() return Response({"detail": "Stopped video stream"}, status=status.HTTP_202_ACCEPTED)
def test_streaming_response(self): filename = os.path.join(os.path.dirname(__file__), 'abc.txt') # file isn't closed until we close the response. file1 = open(filename) r = StreamingHttpResponse(file1) self.assertFalse(file1.closed) r.close() self.assertTrue(file1.closed) # when multiple file are assigned as content, make sure they are all # closed with the response. file1 = open(filename) file2 = open(filename) r = StreamingHttpResponse(file1) r.streaming_content = file2 self.assertFalse(file1.closed) self.assertFalse(file2.closed) r.close() self.assertTrue(file1.closed) self.assertTrue(file2.closed)
def test_streaming_response(self): filename = os.path.join(os.path.dirname(upath(__file__)), 'abc.txt') # file isn't closed until we close the response. file1 = open(filename) r = StreamingHttpResponse(file1) self.assertFalse(file1.closed) r.close() self.assertTrue(file1.closed) # when multiple file are assigned as content, make sure they are all # closed with the response. file1 = open(filename) file2 = open(filename) r = StreamingHttpResponse(file1) r.streaming_content = file2 self.assertFalse(file1.closed) self.assertFalse(file2.closed) r.close() self.assertTrue(file1.closed) self.assertTrue(file2.closed)