use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / linux / vlc_streaming.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 #
19 # Example of how to run this experiment (replace with your information):
20 #
21 # $ cd <path-to-nepi>
22 # python examples/linux/vlc_streaming.py -a <hostname1> -b <hostname2> -u <username> -i <ssh-key>
23
24 from __future__ import print_function
25
26 from nepi.execution.ec import ExperimentController
27 from nepi.execution.resource import ResourceState, ResourceAction 
28
29 from optparse import OptionParser, SUPPRESS_HELP
30 import os
31
32 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
33
34 parser = OptionParser(usage = usage)
35 parser.add_option("-a", "--hostname1", dest="hostname1", 
36         help="Remote host 1", type="str")
37 parser.add_option("-b", "--hostname2", dest="hostname2", 
38         help="Remote host 2", type="str")
39 parser.add_option("-u", "--username", dest="username", 
40         help="Username to SSH to remote host", type="str")
41 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
42         help="Path to private SSH key to be used for connection", 
43         type="str")
44 (options, args) = parser.parse_args()
45
46 hostname1 = options.hostname1
47 hostname2 = options.hostname2
48 username = options.username
49 ssh_key = options.ssh_key
50
51 ## Create the experiment controller
52 ec = ExperimentController(exp_id = "vlc_streamming")
53
54 ## Register node 1
55 node1 = ec.register_resource("linux::Node")
56 # Set the hostname of the first node to use for the experiment
57 ec.set(node1, "hostname", hostname1)
58 # username should be your SSH user 
59 ec.set(node1, "username", username)
60 # Absolute path to the SSH private key
61 ec.set(node1, "identity", ssh_key)
62 # Clean all files, results, etc, from previous experiments wit the same exp_id
63 ec.set(node1, "cleanExperiment", True)
64 # Kill all running processes in the node before running the experiment
65 ec.set(node1, "cleanProcesses", True)
66
67 ## Register node 2 
68 node2 = ec.register_resource("linux::Node")
69 # Set the hostname of the first node to use for the experiment
70 ec.set(node2, "hostname", hostname2)
71 # username should be your SSH user 
72 ec.set(node2, "username", username)
73 # Absolute path to the SSH private key
74 ec.set(node2, "identity", ssh_key)
75 # Clean all files, results, etc, from previous experiments wit the same exp_id
76 ec.set(node2, "cleanExperiment", True)
77 # Kill all running processes in the node before running the experiment
78 ec.set(node2, "cleanProcesses", True)
79
80 # Register VLC server
81 video = "big_buck_bunny_240p_mpeg4_lq.ts"
82 local_path_to_video = os.path.join(
83         os.path.dirname(os.path.realpath(__file__)),
84             "..", video)
85
86 command = ("sudo -S dbus-uuidgen --ensure; sleep 3;"
87           "vlc -I dummy -vvv ${SHARE}/%s " 
88           "--sout '#rtp{dst=%s,port=5004,mux=ts}' vlc://quit") % \
89                   (video, hostname2)
90
91 server = ec.register_resource("linux::Application")
92 ec.set(server, "depends", "vlc")
93 ec.set(server, "files", local_path_to_video)
94 ec.set(server, "command", command)
95 ec.register_connection(server, node1)
96
97 # Register VLC client
98 command = ("sudo -S dbus-uuidgen --ensure; sleep 3; "
99         "vlc -I dummy rtp://%s:5004/%s "
100         "--sout '#std{access=file,mux=ts,dst=VIDEO}'") % \
101                 (hostname2, video)
102
103 client = ec.register_resource("linux::Application")
104 ec.set(client, "depends", "vlc")
105 ec.set(client, "command", command)
106 ec.register_connection(client, node2)
107
108 # The stream can only be retrieved after ccnd is running
109 ec.register_condition(server, ResourceAction.START, 
110         client, ResourceState.STARTED)
111
112 ## Deploy all resources
113 ec.deploy()
114
115 # Wait until the ccncat is finished
116 ec.wait_finished([server])
117
118 video = ec.trace(client, "VIDEO")
119 f = open("video.ts", "w")
120 f.write(video)
121 f.close()
122
123 ec.shutdown()
124
125 print("Streamed VIDEO stored localy at video.ts")
126