Skip to content

Commit 56d8198

Browse files
authored
fix video file loading from wasm packaged files (#7630)
#changelog #emscripten
1 parent b818ea0 commit 56d8198

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

addons/ofxEmscripten/libs/html5video/include/html5video.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern "C"{
44
extern int html5video_player_create();
55
extern void html5video_player_delete(int id);
66
extern void html5video_player_load(int id,const char* src);
7+
extern void html5video_player_load_url(int id,const char* src);
78
extern const char* html5video_player_pixel_format(int it);
89
extern void html5video_player_set_pixel_format(int it, const char* format);
910
extern int html5video_player_update(int id, int update_pixels, unsigned char* pixels);

addons/ofxEmscripten/libs/html5video/lib/emscripten/library_html5video.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var LibraryHTML5Video = {
111111
},
112112

113113
html5video_player_load__deps: ['$GL'],
114-
html5video_player_load: function(player_id, src) {
114+
html5video_player_load_url: function(player_id, src) {
115115
VIDEO.player[player_id].src = UTF8ToString(src);
116116
var texId = GL.getNewId(GL.textures);
117117
var texture = GLctx.createTexture();
@@ -125,6 +125,40 @@ var LibraryHTML5Video = {
125125
VIDEO.player[player_id].textureId = texId;
126126
},
127127

128+
html5video_player_load: function(player_id, src) {
129+
130+
try {
131+
var filePath = UTF8ToString(src); // The path to your file in MEMFS
132+
var data = FS.readFile(filePath, { encoding: 'binary' });
133+
var ext = filePath.split('.').pop();
134+
135+
var stats = FS.stat(filePath)
136+
var fileSizeInBytes = stats.size;
137+
138+
const blob = new Blob([data], { type: 'video/' + ext });
139+
const videoSrc = URL.createObjectURL(blob);
140+
141+
VIDEO.player[player_id].src = videoSrc;
142+
var texId = GL.getNewId(GL.textures);
143+
var texture = GLctx.createTexture();
144+
texture.name = texId;
145+
GL.textures[texId] = texture;
146+
GLctx.bindTexture(GLctx.TEXTURE_2D, texture);
147+
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MAG_FILTER, GLctx.LINEAR);
148+
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MIN_FILTER, GLctx.LINEAR);
149+
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_S, GLctx.CLAMP_TO_EDGE);
150+
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_T, GLctx.CLAMP_TO_EDGE);
151+
VIDEO.player[player_id].textureId = texId;
152+
153+
// Check the file size
154+
//console.log('File size:' + fileSizeInBytes);
155+
//console.log('data size:' + data.length);
156+
} catch (error) {
157+
console.error('Error reading file:' + filePath + " " + error);
158+
}
159+
},
160+
161+
128162
html5video_player_pixel_format: function(player_id) {
129163
var string = VIDEO.player[player_id].pixelFormat;
130164
var size = lengthBytesUTF8(string) + 1;

addons/ofxEmscripten/src/ofxEmscriptenVideoPlayer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ ofxEmscriptenVideoPlayer::~ofxEmscriptenVideoPlayer() {
2929
}
3030

3131
bool ofxEmscriptenVideoPlayer::load(string name){
32-
if (name.substr(0, 12) == "blob:http://" || name.substr(0, 13) == "blob:https://"){
33-
html5video_player_load(player_id, name.c_str());
32+
if (name.substr(0, 7) == "http://" || name.substr(0, 8) == "https://"){
33+
html5video_player_load_url(player_id, name.c_str());
3434
} else{
3535
html5video_player_load(player_id, ofToDataPath(name).c_str());
3636
}
@@ -86,7 +86,16 @@ void ofxEmscriptenVideoPlayer::update(){
8686
texture.texData.bAllocated = true;
8787
texture.setUseExternalTextureID(html5video_player_texture_id(player_id));
8888
}
89-
}
89+
}else{
90+
if( !bHadValidFrame && !bWarnBlocked ){
91+
if( ofGetElapsedTimef() - timePlayRequested > 3.0 ){
92+
string errorMsg = "ofxEmscriptenVideoPlayer::update video is not playing - check your browser preferences 'Auto Play' and click allow for this site ";
93+
ofLogError() << errorMsg << endl;
94+
std::cout << errorMsg << endl;
95+
bWarnBlocked = true;
96+
}
97+
}
98+
}
9099
}
91100

92101
void ofxEmscriptenVideoPlayer::play(){

addons/ofxEmscripten/src/ofxEmscriptenVideoPlayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ class ofxEmscriptenVideoPlayer: public ofBaseVideoPlayer {
6767
ofTexture texture;
6868
ofPixels pixels;
6969
bool usePixels;
70+
float timePlayRequested = 0;
71+
bool bHadValidFrame = false;
72+
bool bWarnBlocked = false;
7073
};

0 commit comments

Comments
 (0)