ADTF  3.18.2
Demo Script Control Playback
Description
This scripting example demonstrates how to control a playback session using ADTF Control using python or a simple shell script.
Location
./src/examples/scripts/adtf_control/
This example shows:
  • how to start a session
  • how to change a property value
  • how to start playback
  • how to wait until playback finishes
  • how to shutdown adtf
Remarks
  • To execute the python script a Python installation is required !
Script (Python)
#!/usr/bin/python
# This example demonstrates how to control adtf via adtf_control in order to
# - start a session
# - change a property value
# - start playback
# - wait until playback finishes
# - shutdown adtf
from subprocess import Popen, PIPE
import os
import time
# this is a helper class to execute adtf_control with a given command
class AdtfControl:
def __init__(self, url = "http://localhost:8000", executable = ""):
self.url = url
self.executable = executable
if not self.executable:
adtf_dir = os.environ["ADTF_DIR"]
if adtf_dir:
self.executable = adtf_dir + "/bin/adtf_control"
else:
self.executable = "adtf_control"
def exec_control(self, args):
call = [self.executable] + list(map(str, args))
if Popen(call).wait() != 0:
raise Exception("adtf_control exited with an error")
def exec_command(self, args):
call = [self.executable, "--url", self.url, "-e"] + list(map(str, args))
process = Popen(call, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = process.communicate()
exit_code = process.wait()
return [stdout, stderr, exit_code]
def check_if_adtf_is_alive(self):
try:
if self.isalive() == "yes":
return True
return False
except:
return False
def wait_for_runlevel(self, runlevel = 1):
while True:
try:
if self.runlevel().startswith(str(runlevel)):
return
time.sleep(1)
except:
pass
def wait_for_startup_complete(self, process = None):
while True:
if process != None and process.poll() != None:
raise Exception("Launcher process has exited.")
try:
if self.startupcompleted().startswith("yes"):
return
except:
pass
time.sleep(1)
def __getattr__(self, name):
def forward_to_control(*args, **kwargs):
(stdout, stderr, exit_code) = self.exec_command([name] + list(map(str, args)))
if exit_code != 0:
raise Exception("adtf_control returned an error: " + str(stderr))
return str(stdout.decode("utf-8"))
return forward_to_control
class AdtfLauncher:
def __init__(self, args, executable = ""):
if not executable:
adtf_dir = os.environ["ADTF_DIR"]
if adtf_dir:
executable = adtf_dir + "/bin/adtf_launcher"
else:
executable = "adtf_launcher"
call = [executable] + list(map(str, args))
self.process = Popen(call)
url = "http://localhost:8000"
adtf_dir = os.environ["ADTF_DIR"]
ac = AdtfControl(url)
# shutdown existing instances
if ac.check_if_adtf_is_alive():
print("shutting down currently active instance")
ac.shutdown()
time.sleep(3)
# launch the session
launcher = AdtfLauncher(["--session", adtf_dir + "/src/examples/projects/adtf_example_project/adtfsessions/demo_playback_session/demo_playback.adtfsession", "--control-url", url])
ac.wait_for_startup_complete(launcher.process)
ac.runlevel("filtergraph")
# set a property of a system service, in this case tell it to play as fast as it can
ac.setprop("[playback.service.adtf]", "playback_speed", 0)
# set a property of a graph element
ac.setprop("default/playback", "start_on_startup", "false")
# create an event buffer where we store events from the playback service
event_buffer = ac.createeventbuffer("[playback.service.adtf]", "player")
ac.runlevel("running")
# because we disabled automatic playback we need to explicitly start it
ac.play()
# Wait for playback event with event id 1 (EndOfStreams)
ac.waitevent(event_buffer, 1)
# for demostration of a 'playlist' like functionality we just reopen the current files
playback_files = ac.playbackfiles()
ac.open(playback_files)
ac.play()
ac.waitevent(event_buffer, 1)
ac.shutdown()
launcher.process.wait()
Script (Shell)
#!/bin/bash -x
# This example demonstrates how to control adtf via adtf_control in order to
# - start a session
# - change a property value
# - start playback
# - wait until playback finishes
# - shutdown adtf
URL="http://localhost:8000"
function ac_unchecked()
{
$ADTF_DIR/bin/adtf_control --url $URL -e $@
}
function ac()
{
$ADTF_DIR/bin/adtf_control --url $URL -e $@
if [ "$?" -ne "0" ]; then
exit 1
fi
}
# shutdown existing instances
if ac_unchecked isalive;
then
echo "shutting down currently active instance"
ac shutdown
sleep 3
fi
# launch the session
$ADTF_DIR/bin/adtf_launcher --session $ADTF_DIR/src/examples/projects/adtf_example_project/adtfsessions/demo_playback_session/demo_playback.adtfsession --control-url $URL&
# make sure we kill the launcher when the script exits in any way
trap "kill $!" EXIT
trap "exit" INT TERM
#make sure its up and running
echo "waiting for adtf to get ready"
while [ "`ac_unchecked startupcompleted`" != "yes" ];
do
if ! kill -0 $! > /dev/null 2>&1;
then
echo "ADTF process has exited";
exit 1
fi
sleep 1
done
ac runlevel filtergraph
# set a property of a system service, in this case tell it to play as fast as it can
ac setprop "[playback.service.adtf]" playback_speed 0
# set a property of a graph element
ac setprop "default/playback" start_on_startup false
# create an event buffer where we store events from the playback service
EVENTBUFFER=`ac createeventbuffer "[playback.service.adtf]" player`
ac runlevel running
# because we disabled automatic playback we need to explicitly start it
ac play
# Wait for playback event with event id 1 (EndOfStreams)
ac waitevent $EVENTBUFFER 1
# for demostration of a 'playlist' like functionality we just reopen the current files
PLAYBACK_FILES=`ac playbackfiles`
ac open $PLAYBACK_FILES
ac play
ac waitevent $EVENTBUFFER 1
ac shutdown