Skip to content

Commit

Permalink
fix video file loading from wasm packaged files (#7630)
Browse files Browse the repository at this point in the history
#changelog #emscripten
  • Loading branch information
ofTheo authored Sep 2, 2023
1 parent b818ea0 commit 56d8198
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions addons/ofxEmscripten/libs/html5video/include/html5video.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern "C"{
extern int html5video_player_create();
extern void html5video_player_delete(int id);
extern void html5video_player_load(int id,const char* src);
extern void html5video_player_load_url(int id,const char* src);
extern const char* html5video_player_pixel_format(int it);
extern void html5video_player_set_pixel_format(int it, const char* format);
extern int html5video_player_update(int id, int update_pixels, unsigned char* pixels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var LibraryHTML5Video = {
},

html5video_player_load__deps: ['$GL'],
html5video_player_load: function(player_id, src) {
html5video_player_load_url: function(player_id, src) {
VIDEO.player[player_id].src = UTF8ToString(src);
var texId = GL.getNewId(GL.textures);
var texture = GLctx.createTexture();
Expand All @@ -125,6 +125,40 @@ var LibraryHTML5Video = {
VIDEO.player[player_id].textureId = texId;
},

html5video_player_load: function(player_id, src) {

try {
var filePath = UTF8ToString(src); // The path to your file in MEMFS
var data = FS.readFile(filePath, { encoding: 'binary' });
var ext = filePath.split('.').pop();

var stats = FS.stat(filePath)
var fileSizeInBytes = stats.size;

const blob = new Blob([data], { type: 'video/' + ext });
const videoSrc = URL.createObjectURL(blob);

VIDEO.player[player_id].src = videoSrc;
var texId = GL.getNewId(GL.textures);
var texture = GLctx.createTexture();
texture.name = texId;
GL.textures[texId] = texture;
GLctx.bindTexture(GLctx.TEXTURE_2D, texture);
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MAG_FILTER, GLctx.LINEAR);
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_MIN_FILTER, GLctx.LINEAR);
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_S, GLctx.CLAMP_TO_EDGE);
GLctx.texParameteri(GLctx.TEXTURE_2D, GLctx.TEXTURE_WRAP_T, GLctx.CLAMP_TO_EDGE);
VIDEO.player[player_id].textureId = texId;

// Check the file size
//console.log('File size:' + fileSizeInBytes);
//console.log('data size:' + data.length);
} catch (error) {
console.error('Error reading file:' + filePath + " " + error);
}
},


html5video_player_pixel_format: function(player_id) {
var string = VIDEO.player[player_id].pixelFormat;
var size = lengthBytesUTF8(string) + 1;
Expand Down
15 changes: 12 additions & 3 deletions addons/ofxEmscripten/src/ofxEmscriptenVideoPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ ofxEmscriptenVideoPlayer::~ofxEmscriptenVideoPlayer() {
}

bool ofxEmscriptenVideoPlayer::load(string name){
if (name.substr(0, 12) == "blob:http://" || name.substr(0, 13) == "blob:https://"){
html5video_player_load(player_id, name.c_str());
if (name.substr(0, 7) == "http://" || name.substr(0, 8) == "https://"){
html5video_player_load_url(player_id, name.c_str());
} else{
html5video_player_load(player_id, ofToDataPath(name).c_str());
}
Expand Down Expand Up @@ -86,7 +86,16 @@ void ofxEmscriptenVideoPlayer::update(){
texture.texData.bAllocated = true;
texture.setUseExternalTextureID(html5video_player_texture_id(player_id));
}
}
}else{
if( !bHadValidFrame && !bWarnBlocked ){
if( ofGetElapsedTimef() - timePlayRequested > 3.0 ){
string errorMsg = "ofxEmscriptenVideoPlayer::update video is not playing - check your browser preferences 'Auto Play' and click allow for this site ";
ofLogError() << errorMsg << endl;
std::cout << errorMsg << endl;
bWarnBlocked = true;
}
}
}
}

void ofxEmscriptenVideoPlayer::play(){
Expand Down
3 changes: 3 additions & 0 deletions addons/ofxEmscripten/src/ofxEmscriptenVideoPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,7 @@ class ofxEmscriptenVideoPlayer: public ofBaseVideoPlayer {
ofTexture texture;
ofPixels pixels;
bool usePixels;
float timePlayRequested = 0;
bool bHadValidFrame = false;
bool bWarnBlocked = false;
};

0 comments on commit 56d8198

Please sign in to comment.