3533e018f259216329ccbb35569d32bbdad66a26
[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 nepi.execution.ec import ExperimentController
25 from nepi.execution.resource import ResourceState, ResourceAction 
26
27 from optparse import OptionParser, SUPPRESS_HELP
28 import os
29
30 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
31
32 parser = OptionParser(usage = usage)
33 parser.add_option("-a", "--hostname1", dest="hostname1", 
34         help="Remote host 1", type="str")
35 parser.add_option("-b", "--hostname2", dest="hostname2", 
36         help="Remote host 2", type="str")
37 parser.add_option("-u", "--username", dest="username", 
38         help="Username to SSH to remote host", type="str")
39 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
40         help="Path to private SSH key to be used for connection", 
41         type="str")
42 (options, args) = parser.parse_args()
43
44 hostname1 = options.hostname1
45 hostname2 = options.hostname2
46 username = options.username
47 ssh_key = options.ssh_key
48
49 ## Create the experiment controller
50 ec = ExperimentController(exp_id = "vlc_streamming")
51
52 ## Register node 1
53 node1 = ec.register_resource("linux::Node")
54 # Set the hostname of the first node to use for the experiment
55 ec.set(node1, "hostname", hostname1)
56 # username should be your SSH user 
57 ec.set(node1, "username", username)
58 # Absolute path to the SSH private key
59 ec.set(node1, "identity", ssh_key)
60 # Clean all files, results, etc, from previous experiments wit the same exp_id
61 ec.set(node1, "cleanExperiment", True)
62 # Kill all running processes in the node before running the experiment
63 ec.set(node1, "cleanProcesses", True)
64
65 ## Register node 2 
66 node2 = ec.register_resource("linux::Node")
67 # Set the hostname of the first node to use for the experiment
68 ec.set(node2, "hostname", hostname2)
69 # username should be your SSH user 
70 ec.set(node2, "username", username)
71 # Absolute path to the SSH private key
72 ec.set(node2, "identity", ssh_key)
73 # Clean all files, results, etc, from previous experiments wit the same exp_id
74 ec.set(node2, "cleanExperiment", True)
75 # Kill all running processes in the node before running the experiment
76 ec.set(node2, "cleanProcesses", True)
77
78 # Register VLC server
79 video = "big_buck_bunny_240p_mpeg4_lq.ts"
80 local_path_to_video = os.path.join(
81         os.path.dirname(os.path.realpath(__file__)),
82             "..", video)
83
84 command = ("sudo -S dbus-uuidgen --ensure; sleep 3;"
85           "vlc -I dummy -vvv ${SHARE}/%s " 
86           "--sout '#rtp{dst=%s,port=5004,mux=ts}' vlc://quit") % \
87                   (video, hostname2)
88
89 server = ec.register_resource("linux::Application")
90 ec.set(server, "depends", "vlc")
91 ec.set(server, "files", local_path_to_video)
92 ec.set(server, "command", command)
93 ec.register_connection(server, node1)
94
95 # Register VLC client
96 command = ("sudo -S dbus-uuidgen --ensure; sleep 3; "
97         "vlc -I dummy rtp://%s:5004/%s "
98         "--sout '#std{access=file,mux=ts,dst=VIDEO}'") % \
99                 (hostname2, video)
100
101 client = ec.register_resource("linux::Application")
102 ec.set(client, "depends", "vlc")
103 ec.set(client, "command", command)
104 ec.register_connection(client, node2)
105
106 # The stream can only be retrieved after ccnd is running
107 ec.register_condition(server, ResourceAction.START, 
108         client, ResourceState.STARTED)
109
110 ## Deploy all resources
111 ec.deploy()
112
113 # Wait until the ccncat is finished
114 ec.wait_finished([server])
115
116 video = ec.trace(client, "VIDEO")
117 f = open("video.ts", "w")
118 f.write(video)
119 f.close()
120
121 ec.shutdown()
122
123 print "Streamed VIDEO stored localy at video.ts"
124