-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoTextureFLV.as
More file actions
136 lines (115 loc) · 4.96 KB
/
VideoTextureFLV.as
File metadata and controls
136 lines (115 loc) · 4.96 KB
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
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.display3D.Context3DProfile;
import flash.events.NetStatusEvent;
import flash.display3D.Context3DProgramType;
import flash.display3D.Context3D;
import flash.display3D.IndexBuffer3D;
import flash.geom.Matrix3D;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.display3D.textures.VideoTexture;
import flash.events.SecurityErrorEvent;
import flash.display3D.VertexBuffer3D;
import flash.display3D.Context3DTextureFormat;
import com.adobe.utils.AGALMiniAssembler;
import flash.display3D.Program3D;
import flash.display3D.Context3DVertexBufferFormat;
import flash.display3D.Context3DBlendFactor;
public class VideoTextureFLV extends Sprite {
private var context3D:Context3D;
private var indexbuffer:IndexBuffer3D;
private var m:Matrix3D = new Matrix3D();
private var nc:NetConnection;
private var ns:NetStream;
private var videoTexture:VideoTexture;
public function VideoTextureFLV() {
this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
private function init(e:Event):void {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
if (stage.stage3Ds.length > 0){
stage.stage3Ds[0].addEventListener(Event.CONTEXT3D_CREATE, contextCreated, false, 0, true);
stage.stage3Ds[0].requestContext3DMatchingProfiles(Vector.<String>([Context3DProfile.BASELINE, Context3DProfile.BASELINE_CONSTRAINED, Context3DProfile.BASELINE_EXTENDED, Context3DProfile.STANDARD, Context3DProfile.STANDARD_CONSTRAINED, Context3DProfile.STANDARD_EXTENDED]));
} else {
trace("Error: there is no Stage3D available")
}
}
private function contextCreated(event:Event):void {
context3D = stage.stage3Ds[0].context3D;
context3D.configureBackBuffer(1280, 800, 4, false, false, true);
trace(context3D.driverInfo);
var vertices:Vector.<Number> = Vector.<Number>([
0.5, -1, 0, 1, 0,
0.5, 0.5, 0, 1, 1,
-1, 0.5, 0, 0, 1,
-1,-1, 0, 0, 0
]);
// create the buffer to upload the vertices
var vertexbuffer:VertexBuffer3D = context3D.createVertexBuffer(4, 5);
// upload the vertices
vertexbuffer.uploadFromVector(vertices, 0, 4);
// create the buffer to upload the indices
indexbuffer = context3D.createIndexBuffer(6);
// upload the indices
indexbuffer.uploadFromVector(Vector.<uint>([0, 1, 2, 2, 3, 0]), 0, 6);
// create the mini assembler
var vertexShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
// assemble the vertex shader
vertexShaderAssembler.assemble(Context3DProgramType.VERTEX, "m44 op, va0, vc0\n" + "mov v0, va1");
var fragmentShaderAssembler:AGALMiniAssembler = new AGALMiniAssembler();
// assemble the fragment shader
fragmentShaderAssembler.assemble(Context3DProgramType.FRAGMENT, "tex ft1, v0, fs0 <2d,linear, nomip>\n" + "mov oc, ft1");
// create the shader program
var program:Program3D = context3D.createProgram();
// upload the vertex and fragment shaders
program.upload(vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
// set the vertex buffer
context3D.setVertexBufferAt(0, vertexbuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
context3D.setVertexBufferAt(1, vertexbuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
context3D.setBlendFactors(Context3DBlendFactor.ONE, Context3DBlendFactor.ONE);//This line adds alpha channel support
videoTexture = context3D.createVideoTexture();
context3D.setTextureAt(0, videoTexture);
context3D.setProgram(program);
m.appendScale(1, -1, 1);
context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true);
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
nc.connect(null);
}
private function netStatusHandler(event:NetStatusEvent):void {
//trace(event.info.code);
switch (event.info.code){
case "NetConnection.Connect.Success":
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true);
ns.client = {onMetaData:getMeta, onPlayStatus:onPlayStatus};
videoTexture.attachNetStream(ns);
ns.play("video.flv");
this.addEventListener(Event.ENTER_FRAME, render, false, 0, true);
break;
case "NetStream.Play.StreamNotFound":
trace("Stream not found");
break;
}
}
//Metadata handler
private function getMeta(mdata:Object):void {
//trace("metadata");
}
//Seek video to begin after complete
private function onPlayStatus(infoObject:Object):void {
ns.seek(0);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler:", event.text);
}
private function render(event:Event):void {
context3D.clear(0.2, 0.5, 0.3, 1);//Context3D background color
context3D.drawTriangles(indexbuffer);
context3D.present();
}
}
}