forked from ps3mediaserver/ps3mediaserver
/
VirtualVideoAction.java
164 lines (149 loc) · 5.54 KB
/
VirtualVideoAction.java
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
/*
* PS3 Media Server, for streaming any medias to your PS3.
* Copyright (C) 2008 A.Brochard
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package net.pms.dlna.virtual;
import java.io.IOException;
import java.io.InputStream;
import net.pms.PMS;
import net.pms.dlna.DLNAResource;
import net.pms.network.HTTPResource;
/**
* Implements a container that when browsed, an action will be performed.
* The class assumes that the action to be performed is to toggle a boolean value.
* Because of this, the thumbnail is either a green tick mark or a red cross. Equivalent
* videos are shown after the value is toggled.<p>
* However this is just cosmetic. Any action can be performed.
*/
public abstract class VirtualVideoAction extends DLNAResource {
private boolean enabled;
protected String name;
private String thumbnailIconOK;
private String thumbnailIconKO;
private String thumbnailContentType;
private String videoOk;
private String videoKo;
private long timer1;
/**Constructor for this class. Recommended instantation includes overriding the {@link #enable()} function (example shown in the link).
* @param name Name that is shown via the UPNP ContentBrowser service. This field cannot be changed after the instantiation.
* @param enabled If true, a green tick mark is shown as thumbnail. If false, a red cross is shown. This initial value
* is usually changed via the {@link #enable()} function.
*/
public VirtualVideoAction(String name, boolean enabled) {
this.name = name;
thumbnailContentType = HTTPResource.PNG_TYPEMIME;
thumbnailIconOK = "images/apply-256.png";
thumbnailIconKO = "images/button_cancel-256.png";
this.videoOk = "videos/action_success-512.mpg";
this.videoKo = "videos/button_cancel-512.mpg";
timer1 = -1;
this.enabled = enabled;
}
@Override
public boolean isTranscodeFolderAvailable() {
return false;
}
/**This function is called as an action from the UPNP client when
* the user tries to play this item. This function calls instead the enable()
* function in order to execute an action.
* As the client expects to play an item, a really short video (less than 1s) is shown with
* the results of the action.
* @see #enable()
* @see net.pms.dlna.DLNAResource#getInputStream()
*/
@Override
public InputStream getInputStream() throws IOException {
if (timer1 == -1) {
timer1 = System.currentTimeMillis();
} else if (System.currentTimeMillis() - timer1 < 2000) {
timer1 = -1;
}
if (timer1 != -1) {
enabled = enable();
}
return getResourceInputStream(enabled ? videoOk : videoKo);
}
/**Prototype. This function is called by {@link #getInputStream()} and is the core of this class.
* The main purpose of this function is toggle a boolean variable somewhere.
* The value of that boolean variable is shown then as either a green tick mark or a red cross.
* However, this is just a cosmetic thing. Any Java code can be executed in this function, not only toggling a boolean variable.
* Recommended way to instantiate this class is as follows:
* <pre> VirtualFolder vf;
* [...]
* vf.addChild(new VirtualVideoAction(Messages.getString("PMS.3"), configuration.isMencoderNoOutOfSync()) {
* public boolean enable() {
* configuration.setMencoderNoOutOfSync(!configuration.isMencoderNoOutOfSync());
* return configuration.isMencoderNoOutOfSync();
* }
* }); </pre>
* @return If true, a green tick mark is shown as thumbnail. If false, a red cross is shown.
*/
public abstract boolean enable();
@Override
public String getName() {
return name;
}
/**As this item is not a container, returns false.
* @return false
* @see net.pms.dlna.DLNAResource#isFolder()
*/
@Override
public boolean isFolder() {
return false;
}
/**Returns an invalid length as this item is not
* TODO: (botijo) VirtualFolder returns 0 instead of -1.
* @return -1, an invalid length for an item.
* @see net.pms.dlna.DLNAResource#length()
*/
@Override
public long length() {
return -1; //DLNAMediaInfo.TRANS_SIZE;
}
public long lastModified() {
return 0;
}
@Override
public String getSystemName() {
return getName();
}
/**Returns either a green tick mark or a red cross that represents the actual
* value of this item
* @see net.pms.dlna.DLNAResource#getThumbnailInputStream()
*/
@Override
public InputStream getThumbnailInputStream() {
return getResourceInputStream(enabled ? thumbnailIconOK : thumbnailIconKO);
}
/**@return PNG type, as the thumbnail can only be either a green tick mark or a red cross.
* @see #getThumbnailInputStream()
* @see net.pms.dlna.DLNAResource#getThumbnailContentType()
*/
@Override
public String getThumbnailContentType() {
return thumbnailContentType;
}
/**TODO: (botijo) Why is ext being set here?
* @return True, as this kind of item is always valid.
* @see net.pms.dlna.DLNAResource#isValid()
*/
@Override
public boolean isValid() {
setExt(PMS.get().getAssociatedExtension("toto.mpg"));
return true;
}
}