-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_tvshow.pl
executable file
·202 lines (184 loc) · 6.64 KB
/
start_tvshow.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/perl
use Net::UPnP::ControlPoint;
use Net::UPnP::AV::MediaRenderer;
use Net::UPnP::ActionResponse;
use Net::UPnP::AV::MediaServer;
my $DLNAserver = "DLNA"; #regexp name of you local DLNA server
my $filename = "city"; #regexp for searching filename
my $TVname = 'VIERA'; #DLNA device (TV) must contain this string in name, otherwise it will be skipped
my $stopbeforeplay = 1; #will stop/start TV one time
## END OF CONFIG
## DO NOT CHANGE BELOW THIS LINE (only if you know what to do :)
## =============================================================
## simple-DLNA-remote-controller
#$Net::UPnP::DEBUG = 1;
sub now {
$now = `date +"%x %T"`;
chomp($now);
$now = $now . " == ";
return $now;
}
$SIG{'INT'} = 'exit_handler';
sub exit_handler () {
print "Caught CTRL-C!! Cleaning up, terminating. \n";
### Do cleanup . kill the children
if ($c) {
$c->release();
$c->remove();
}
$SIG{'INT'} = 'DEFAULT';
exit;
}
print now()."Started. Running search.\n";
my $obj = Net::UPnP::ControlPoint->new();
@dev_list = $obj->search(st =>'upnp:rootdevice', mx => 5);
print now()."Found $#dev_list devices\n";
print now()."Searching for \"$filename\" at all DLNA servers..\n";
foreach $dev (@dev_list) {
$device_type = $dev->getdevicetype();
if ($device_type ne 'urn:schemas-upnp-org:device:MediaServer:1') {
next;
}
unless ($dev->getservicebyname('urn:schemas-upnp-org:service:ContentDirectory:1')) {
next;
}
print now(). "Found ".$dev->getfriendlyname() . "\n";
if ($dev->getfriendlyname() !~ /$DLNAserver/) {
next;
}
$mediaServer = Net::UPnP::AV::MediaServer->new();
$mediaServer->setdevice($dev);
@content_list = $mediaServer->getcontentlist(ObjectID => 0);
foreach $content (@content_list) {
find_content($mediaServer, $content, 1);
}
my $parentid;
sub find_content {
my ($mediaServer, $content, $indent) = @_;
my $id = $content->getid();
my $title = $content->gettitle();
for ($n=0; $n<$indent; $n++) {
}
if ($content->isitem()) {
if (length($content->getdate())) {
}
}
if ($title =~ /$filename/) {
$file = $content->geturl();
$filecontenttype = $content->getcontenttype();
$fileid = $content->getid();
$filetitle = $content->gettitle();
}
unless ($content->iscontainer()) {
return;
}
@child_content_list = $mediaServer->getcontentlist(ObjectID => $id );
if (@child_content_list <= 0) {
return;
}
$indent++;
foreach my $child_content (@child_content_list) {
$parentid = $child_content->getid();
find_content($mediaServer, $child_content, $indent);
}
}
}
if ($file) {
print now()."found $filename at $file\n";
}
else {
print now()."file $filename not found! Terminating.\n";
exit(1);
}
use IPC::Semaphore::Concurrency;
while (1) {
our $c = IPC::Semaphore::Concurrency->new('/tmp/sem_file');
if ( $c->acquire(0, 0, -1, 0) ) {
print now()."Semaphore is free, OK\n";
} else {
print now()."Semaphore is busy, probably another instance is running! Will not start, removing semaphore.\n";
$c->release();
exit(1);
}
print now() . "[!] Starting loop \n";
my $obj = Net::UPnP::ControlPoint->new();
@dev_list = ();
my $retry_cnt = 0;
while (@dev_list <= 0 ) {
print now() . "Searching for renderers.. @dev_list\n";
@dev_list = $obj->search(st =>'urn:schemas-upnp-org:device:MediaRenderer:1', mx => 5);
$retry_cnt++;
if ($retry_cnt >= 5) {
print now() . "[!] No renderers found. Releasing semaphore, exiting.\n";
$c->release();
exit(1);
}
}
print now() . "Found $#dev_list renderers\n";
$devNum= 0;
foreach $dev (@dev_list) {
my $device_type = $dev->getdevicetype();
if ($device_type ne 'urn:schemas-upnp-org:device:MediaRenderer:1') {
next;
}
$devNum++;
my $friendlyname = $dev->getfriendlyname();
print now() . "found [$devNum] : device name: [" . $friendlyname . "] " ;
if ($friendlyname !~ /$TVname/) { print "skipping this device.\n";next;}
my $renderer = Net::UPnP::AV::MediaRenderer->new();
$renderer->setdevice($dev);
$condir_service = $dev->getservicebyname('urn:schemas-upnp-org:service:AVTransport:1');
%action_in_arg = (
'ObjectID' => 0,
'InstanceID' => '0'
);
$action_res = $condir_service->postcontrol('GetTransportInfo', \%action_in_arg);
$actrion_out_arg = $action_res->getargumentlist();
$x = $actrion_out_arg->{'CurrentTransportState'};
print "Device current state is <<$x>>. ";
if ( ($x !~ /PLAY/) || ($stopbeforeplay == 1) ) {
if ($stopbeforeplay == 1) {
print "First run, force stop-start \n";
} else { print "Device is in bad state, starting up! \n"; }
} else {
print "This is ok, skipping.\n";
next;
}
$meta = <<EOF;
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"><item id="2\$8\$1B" parentID="2\$15" restricted="true"> <dc:title>final_movie</dc:title> <upnp:class>object.item.videoItem</upnp:class> <res protocolInfo="http-get:*:video/x-msvideo:*" size="138332664" duration="2:35:27.079" resolution="1366x768" bitrate="6002933" sampleFrequency="44100" nrAudioChannels="1">$file</res></item></DIDL-Lite>
EOF
$id = 0;
$renderer->setAVTransportURI(InstanceID => $id, CurrentURI => $file, CurrentURIMetaData => $meta);
my $child=fork();
if ($child == 0) { #child process
#waiting for semaphore
if ($c->acquire(0,TRUE,-1,0)) {
#all good
} else {
#something wrong
}
if ($stopbeforeplay == 1) {
print now() . "device [$devNum] pid $$ command: [stop] \n";
$renderer->stop();
$stopbeforeplay = 0;
}
print now() . "device [$devNum] pid $$ command: [play] $fileid $file \n";sleep 1;
$renderer->play();
$devNum++;
exit;
} else {
push (@childs,$child);
#PARENT nothing else to do here
}
} #for each devices
print now(). "removing semaphore, mass play all device now.\n";
$c->release();
$c->remove();
$stopbeforeplay = 0;
sleep 1;
print now()."waiting for childs to finish.\n";
for my $pid (@childs) {
waitpid $pid, 0;
}
print now(). "finished loop\n";
}#global while ends