From a229bdc9c939f7046a9ddd43efc99245c07bf9f4 Mon Sep 17 00:00:00 2001 From: Mehmet Mert Yildiran Date: Thu, 1 Nov 2018 11:23:34 +0300 Subject: [PATCH] Add a stream lock and an idle state for stream to provide continuous frame flow --- scene/scene.py | 21 +++++++++++++++++++++ stream_starter.py | 13 +++++++++++++ test1.py | 28 ---------------------------- 3 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 stream_starter.py delete mode 100644 test1.py diff --git a/scene/scene.py b/scene/scene.py index 95a863ea..f427d1b9 100644 --- a/scene/scene.py +++ b/scene/scene.py @@ -7,6 +7,11 @@ import random import shutil import subprocess as sp import warnings +from time import sleep +try: + import thread # Low-level threading API (Python 2.7) +except ImportError: + import _thread as thread # Low-level threading API (Python 3.x) from tqdm import tqdm as ProgressDisplay @@ -58,6 +63,7 @@ class Scene(Container): self.frame_num = 0 self.current_scene_time = 0 self.original_skipping_status = self.skip_animations + self.stream_lock = False if self.name is None: self.name = self.__class__.__name__ if self.random_seed is not None: @@ -455,6 +461,8 @@ class Scene(Container): raise EndSceneEarlyException() def play(self, *args, **kwargs): + if IS_LIVE_STREAMING: + self.stream_lock = False if len(args) == 0: warnings.warn("Called Scene.play with no animations") return @@ -491,8 +499,21 @@ class Scene(Container): else: self.continual_update(0) self.num_plays += 1 + + if IS_LIVE_STREAMING: + self.stream_lock = True + thread.start_new_thread(self.idle_stream, ()) + return self + def idle_stream(self): + while(self.stream_lock): + self.update_frame() + n_frames = 1 + frame = self.get_frame() + self.add_frames(*[frame] * n_frames) + sleep(self.frame_duration * 999/1000) + def clean_up_animations(self, *animations): for animation in animations: animation.clean_up(self) diff --git a/stream_starter.py b/stream_starter.py new file mode 100644 index 00000000..17794c4c --- /dev/null +++ b/stream_starter.py @@ -0,0 +1,13 @@ +from big_ol_pile_of_manim_imports import * +import subprocess +from time import sleep +from manim import Manim + +if not IS_STREAMING_TO_TWITCH: + FNULL = open(os.devnull, 'w') + subprocess.Popen([STREAMING_CLIENT, STREAMING_PROTOCOL + '://' + STREAMING_IP + ':' + STREAMING_PORT + '?listen'], stdout=FNULL, stderr=FNULL) + sleep(3) + +manim = Manim() + +print("YOUR STREAM IS READY!") diff --git a/test1.py b/test1.py deleted file mode 100644 index 70d65e88..00000000 --- a/test1.py +++ /dev/null @@ -1,28 +0,0 @@ -from big_ol_pile_of_manim_imports import * -from manim import Manim - - -manim = Manim() - -circle = Circle() -square = Square() -line = Line(np.array([3,0,0]),np.array([5,0,0])) -triangle = Polygon(np.array([0,0,0]),np.array([1,1,0]),np.array([1,-1,0])) - -manim.wait(3) -manim.add(line) -manim.wait(3) -manim.play(ShowCreation(circle)) -manim.wait(3) -manim.play(FadeOut(circle)) -manim.wait(3) -manim.play(GrowFromCenter(square)) -manim.wait(3) -manim.play(Transform(square,triangle)) -manim.wait(10) - -manim.close_movie_pipe() - -# ffplay tcp://127.0.0.1:2000?listen -# for listening the stream -# start mplayer before running this script