Add a stream lock and an idle state for stream to provide continuous frame flow

This commit is contained in:
Mehmet Mert Yildiran 2018-11-01 11:23:34 +03:00
parent e071ac62a1
commit a229bdc9c9
3 changed files with 34 additions and 28 deletions

View file

@ -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)

13
stream_starter.py Normal file
View file

@ -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!")

View file

@ -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