Author:
Cero (posted
2012-08-17 20:55:28 , viewed 748 times)
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
203
204
205
206
207
208
209
package main ;
import static org .lwjgl .opengl .GL11 .GL_COLOR_BUFFER_BIT ;
import static org .lwjgl .opengl .GL11 .GL_LINEAR ;
import static org .lwjgl .opengl .GL11 .GL_QUADS ;
import static org .lwjgl .opengl .GL11 .GL_RGBA ;
import static org .lwjgl .opengl .GL11 .GL_TEXTURE ;
import static org .lwjgl .opengl .GL11 .GL_TEXTURE_2D ;
import static org .lwjgl .opengl .GL11 .GL_TEXTURE_MAG_FILTER ;
import static org .lwjgl .opengl .GL11 .GL_TEXTURE_MIN_FILTER ;
import static org .lwjgl .opengl .GL11 .GL_TEXTURE_WRAP_S ;
import static org .lwjgl .opengl .GL11 .GL_TEXTURE_WRAP_T ;
import static org .lwjgl .opengl .GL11 .GL_UNSIGNED_BYTE ;
import static org .lwjgl .opengl .GL11 .glBegin ;
import static org .lwjgl .opengl .GL11 .glBindTexture ;
import static org .lwjgl .opengl .GL11 .glClear ;
import static org .lwjgl .opengl .GL11 .glEnable ;
import static org .lwjgl .opengl .GL11 .glEnd ;
import static org .lwjgl .opengl .GL11 .glGenTextures ;
import static org .lwjgl .opengl .GL11 .glTexCoord2f ;
import static org .lwjgl .opengl .GL11 .glTexImage2D ;
import static org .lwjgl .opengl .GL11 .glTexParameteri ;
import static org .lwjgl .opengl .GL11 .glVertex2f ;
import static org .lwjgl .opengl .GL12 .GL_CLAMP_TO_EDGE ;
import java .io .File ;
import java .nio .ByteBuffer ;
import java .nio .IntBuffer ;
import org .gstreamer .Bus ;
import org .gstreamer .Gst ;
import org .gstreamer .GstObject ;
import org .gstreamer .elements .PlayBin2 ;
import org .gstreamer .elements .RGBDataSink ;
import org .lwjgl .BufferUtils ;
import org .lwjgl .opengl .Display ;
import org .lwjgl .opengl .GL11 ;
import org .lwjgl .opengl .GL12 ;
import org .newdawn .slick .opengl .Texture ;
import org .newdawn .slick .opengl .TextureImpl ;
public class GStreamerPlayer
{
private int videoWidth , videoHeight ;
private int texture ;
private ByteBuffer buffer ;
private boolean done = false ;
private PlayBin2 playbin ;
private float scaleFactor = 1.0f ;
public GStreamerPlayer (String strToPlay , double invol )
{
try
{
GStreamerLibraryLoaderImpl .getInstance ().load ();
}
catch (Exception e )
{
e .printStackTrace ();
}
Gst .init ("VideoPlayer" , new String [] {});
playbin = new PlayBin2 ("VideoPlayer" );
RGBListener listener = new RGBListener ();
playbin .setInputFile (new File (strToPlay ));
RGBDataSink sink = new RGBDataSink (("sink" ), listener );
sink .setPassDirectBuffer (true );
playbin .setVideoSink (sink );
playbin .getBus ().connect (new Bus .ERROR ()
{
@Override
public void errorMessage (GstObject arg0 , int arg1 , String arg2 )
{
done = true ;
System .out .println ("Error: " +arg2 +" " +arg1 );
}
});
playbin .getBus ().connect (new Bus .EOS ()
{
@Override
public void endOfStream (GstObject arg0 )
{
done = true ;
}
});
Gst .getExecutor ().execute (new Runnable ()
{
@Override
public void run ()
{
playbin .play ();
}
});
playbin .setVolume (invol );
buffer = null ;
}
public boolean isDone ()
{
return done ;
}
public void updateAndRender ()
{
if (buffer == null )
{
return ;
}
Texture lastUsed = TextureImpl .getLastBind ();
TextureImpl .unbind ();
if (texture == 0 )
{
texture = glGenTextures ();
glEnable (GL_TEXTURE );
glBindTexture (GL_TEXTURE_2D , texture );
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR );
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR );
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE );
glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE );
glTexImage2D (GL_TEXTURE_2D , 0 , GL_RGBA , videoWidth , videoHeight , 0 , GL_RGBA , GL_UNSIGNED_BYTE , buffer );
}
glClear (GL_COLOR_BUFFER_BIT );
glBindTexture (GL_TEXTURE_2D , texture );
synchronized (buffer )
{
buffer .rewind ();
GL11 .glTexSubImage2D (GL11 .GL_TEXTURE_2D , 0 , 0 , 0 , videoWidth , videoHeight , GL12 .GL_BGRA , GL12 .GL_UNSIGNED_INT_8_8_8_8_REV , buffer );
}
float x1 , x2 , y1 , y2 ;
y1 = (Display .getHeight () - (videoHeight * scaleFactor )) / 2.0f ;
y2 = y1 + videoHeight * scaleFactor ;
x1 = (Display .getWidth () - (videoWidth * scaleFactor )) / 2.0f ;
x2 = x1 + videoWidth * scaleFactor ;
glBegin (GL_QUADS );
{
glTexCoord2f (0 , 0 );
glVertex2f (x1 , y1 );
glTexCoord2f (1 , 0 );
glVertex2f (x2 , y1 );
glTexCoord2f (1 , 1 );
glVertex2f (x2 , y2 );
glTexCoord2f (0 , 1 );
glVertex2f (x1 , y2 );
}
glEnd ();
if (lastUsed != null )
lastUsed .bind ();
}
public void destroy ()
{
playbin .setState (org .gstreamer .State .NULL );
playbin .dispose ();
}
public int getVideoWidth () { return videoWidth ; }
public int getVideoHeight () { return videoHeight ; }
private class RGBListener implements RGBDataSink .Listener
{
@Override
public void rgbFrame (boolean preroll , int width , int height , IntBuffer rgb )
{
if (buffer == null )
{
buffer = BufferUtils .createByteBuffer (width * height * 4 );
videoWidth = width ;
videoHeight = height ;
float scaleWidth = (float )( (float )Display .getWidth () / (float )videoWidth );
float scaleHeight = (float )( (float )Display .getHeight () / (float )videoHeight );
scaleFactor = Math .min (scaleWidth , scaleHeight );
}
synchronized (buffer )
{
buffer .rewind ();
buffer .asIntBuffer ().put (rgb );
}
}
}
}
Special syntax:
To highlight a line (yellow background), prefix it with '@@'
To indicate that a line should be removed (red background), prefix it with '-'
To indicate that a line should be added (green background), prefix it with '+'
To post multiple snippets, seperate them by '~~~~'
java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org