2016-10-17 17:14:07 +02:00
|
|
|
/*************************************************************************/
|
2017-09-01 16:07:55 +02:00
|
|
|
/* video_stream_webm.cpp */
|
2016-10-17 17:14:07 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-10-17 17:14:07 +02:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-10-17 17:14:07 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
#include "video_stream_webm.h"
|
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2020-01-26 11:24:14 +01:00
|
|
|
#include "servers/audio_server.h"
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2017-04-28 19:28:21 +02:00
|
|
|
#include "thirdparty/misc/yuv2rgb.h"
|
|
|
|
|
2020-01-26 11:24:14 +01:00
|
|
|
// libsimplewebm
|
|
|
|
#include <OpusVorbisDecoder.hpp>
|
|
|
|
#include <VPXDecoder.hpp>
|
|
|
|
|
|
|
|
// libvpx
|
|
|
|
#include <vpx/vpx_image.h>
|
2017-09-14 22:45:02 +02:00
|
|
|
|
2020-01-26 11:24:14 +01:00
|
|
|
// libwebm
|
|
|
|
#include <mkvparser/mkvparser.h>
|
2016-10-17 17:14:07 +02:00
|
|
|
|
|
|
|
class MkvReader : public mkvparser::IMkvReader {
|
|
|
|
public:
|
|
|
|
MkvReader(const String &p_file) {
|
|
|
|
file = FileAccess::open(p_file, FileAccess::READ);
|
2017-09-14 22:45:02 +02:00
|
|
|
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_COND_MSG(!file, "Failed loading resource: '" + p_file + "'.");
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
~MkvReader() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (file) {
|
2016-10-17 17:14:07 +02:00
|
|
|
memdelete(file);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int Read(long long pos, long len, unsigned char *buf) {
|
|
|
|
if (file) {
|
2019-03-26 18:51:13 +01:00
|
|
|
if (file->get_position() != (uint64_t)pos) {
|
2016-10-17 17:14:07 +02:00
|
|
|
file->seek(pos);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-03-26 18:51:13 +01:00
|
|
|
if (file->get_buffer(buf, len) == (uint64_t)len) {
|
2016-10-17 17:14:07 +02:00
|
|
|
return 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int Length(long long *total, long long *available) {
|
|
|
|
if (file) {
|
2021-05-25 08:58:49 +02:00
|
|
|
const uint64_t len = file->get_length();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (total) {
|
2016-10-17 17:14:07 +02:00
|
|
|
*total = len;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (available) {
|
2016-10-17 17:14:07 +02:00
|
|
|
*available = len;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileAccess *file;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
VideoStreamPlaybackWebm::VideoStreamPlaybackWebm() :
|
2020-05-12 17:01:17 +02:00
|
|
|
|
|
|
|
texture(memnew(ImageTexture)) {}
|
2016-10-17 17:14:07 +02:00
|
|
|
VideoStreamPlaybackWebm::~VideoStreamPlaybackWebm() {
|
|
|
|
delete_pointers();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoStreamPlaybackWebm::open_file(const String &p_file) {
|
|
|
|
file_name = p_file;
|
|
|
|
webm = memnew(WebMDemuxer(new MkvReader(file_name), 0, audio_track));
|
|
|
|
if (webm->isOpen()) {
|
2017-09-14 22:45:02 +02:00
|
|
|
video = memnew(VPXDecoder(*webm, OS::get_singleton()->get_processor_count()));
|
2016-10-17 17:14:07 +02:00
|
|
|
if (video->isOpen()) {
|
|
|
|
audio = memnew(OpusVorbisDecoder(*webm));
|
|
|
|
if (audio->isOpen()) {
|
|
|
|
audio_frame = memnew(WebMFrame);
|
2017-09-14 22:45:02 +02:00
|
|
|
pcm = (float *)memalloc(sizeof(float) * audio->getBufferSamples() * webm->getChannels());
|
2016-10-17 17:14:07 +02:00
|
|
|
} else {
|
|
|
|
memdelete(audio);
|
2020-04-02 01:20:12 +02:00
|
|
|
audio = nullptr;
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
frame_data.resize((webm->getWidth() * webm->getHeight()) << 2);
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Image> img;
|
2021-06-18 00:03:09 +02:00
|
|
|
img.instantiate();
|
2019-06-11 20:43:37 +02:00
|
|
|
img->create(webm->getWidth(), webm->getHeight(), false, Image::FORMAT_RGBA8);
|
|
|
|
texture->create_from_image(img);
|
2016-10-17 17:14:07 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
memdelete(video);
|
2020-04-02 01:20:12 +02:00
|
|
|
video = nullptr;
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
memdelete(webm);
|
2020-04-02 01:20:12 +02:00
|
|
|
webm = nullptr;
|
2016-10-17 17:14:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackWebm::stop() {
|
|
|
|
if (playing) {
|
|
|
|
delete_pointers();
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
pcm = nullptr;
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
audio_frame = nullptr;
|
|
|
|
video_frames = nullptr;
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
video = nullptr;
|
|
|
|
audio = nullptr;
|
2016-10-17 17:14:07 +02:00
|
|
|
|
|
|
|
open_file(file_name); //Should not fail here...
|
|
|
|
|
|
|
|
video_frames_capacity = video_frames_pos = 0;
|
|
|
|
num_decoded_samples = 0;
|
|
|
|
samples_offset = -1;
|
|
|
|
video_frame_delay = video_pos = 0.0;
|
|
|
|
}
|
|
|
|
time = 0.0;
|
|
|
|
playing = false;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
void VideoStreamPlaybackWebm::play() {
|
|
|
|
stop();
|
|
|
|
|
2021-02-17 17:44:49 +01:00
|
|
|
delay_compensation = ProjectSettings::get_singleton()->get("audio/video/video_delay_compensation_ms");
|
2016-10-17 17:14:07 +02:00
|
|
|
delay_compensation /= 1000.0;
|
|
|
|
|
|
|
|
playing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoStreamPlaybackWebm::is_playing() const {
|
|
|
|
return playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackWebm::set_paused(bool p_paused) {
|
|
|
|
paused = p_paused;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
bool VideoStreamPlaybackWebm::is_paused() const {
|
2016-10-17 17:14:07 +02:00
|
|
|
return paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackWebm::set_loop(bool p_enable) {
|
|
|
|
//Empty
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
bool VideoStreamPlaybackWebm::has_loop() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float VideoStreamPlaybackWebm::get_length() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (webm) {
|
2016-10-17 17:14:07 +02:00
|
|
|
return webm->getLength();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2017-09-21 05:31:36 +02:00
|
|
|
float VideoStreamPlaybackWebm::get_playback_position() const {
|
2016-10-17 17:14:07 +02:00
|
|
|
return video_pos;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-09-21 05:31:36 +02:00
|
|
|
void VideoStreamPlaybackWebm::seek(float p_time) {
|
2021-04-14 20:39:13 +02:00
|
|
|
WARN_PRINT_ONCE("Seeking in Theora and WebM videos is not implemented yet (it's only supported for GDNative-provided video streams).");
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackWebm::set_audio_track(int p_idx) {
|
|
|
|
audio_track = p_idx;
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Texture2D> VideoStreamPlaybackWebm::get_texture() const {
|
2016-10-17 17:14:07 +02:00
|
|
|
return texture;
|
|
|
|
}
|
2017-09-14 22:45:02 +02:00
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
void VideoStreamPlaybackWebm::update(float p_delta) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if ((!playing || paused) || !video) {
|
2016-10-17 17:14:07 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
time += p_delta;
|
|
|
|
|
|
|
|
if (time < video_pos) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
bool audio_buffer_full = false;
|
|
|
|
|
|
|
|
if (samples_offset > -1) {
|
|
|
|
//Mix remaining samples
|
|
|
|
const int to_read = num_decoded_samples - samples_offset;
|
|
|
|
const int mixed = mix_callback(mix_udata, pcm + samples_offset * webm->getChannels(), to_read);
|
|
|
|
if (mixed != to_read) {
|
|
|
|
samples_offset += mixed;
|
|
|
|
audio_buffer_full = true;
|
|
|
|
} else {
|
|
|
|
samples_offset = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool hasAudio = (audio && mix_callback);
|
2017-09-14 22:45:02 +02:00
|
|
|
while ((hasAudio && !audio_buffer_full && !has_enough_video_frames()) ||
|
|
|
|
(!hasAudio && video_frames_pos == 0)) {
|
|
|
|
if (hasAudio && !audio_buffer_full && audio_frame->isValid() &&
|
|
|
|
audio->getPCMF(*audio_frame, pcm, num_decoded_samples) && num_decoded_samples > 0) {
|
2016-10-17 17:14:07 +02:00
|
|
|
const int mixed = mix_callback(mix_udata, pcm, num_decoded_samples);
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
if (mixed != num_decoded_samples) {
|
2016-10-17 17:14:07 +02:00
|
|
|
samples_offset = mixed;
|
|
|
|
audio_buffer_full = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebMFrame *video_frame;
|
|
|
|
if (video_frames_pos >= video_frames_capacity) {
|
|
|
|
WebMFrame **video_frames_new = (WebMFrame **)memrealloc(video_frames, ++video_frames_capacity * sizeof(void *));
|
|
|
|
ERR_FAIL_COND(!video_frames_new); //Out of memory
|
|
|
|
(video_frames = video_frames_new)[video_frames_capacity - 1] = memnew(WebMFrame);
|
|
|
|
}
|
|
|
|
video_frame = video_frames[video_frames_pos];
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!webm->readFrame(video_frame, audio_frame)) { //This will invalidate frames
|
2016-10-17 17:14:07 +02:00
|
|
|
break; //Can't demux, EOS?
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (video_frame->isValid()) {
|
2016-10-17 17:14:07 +02:00
|
|
|
++video_frames_pos;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
};
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
bool video_frame_done = false;
|
|
|
|
while (video_frames_pos > 0 && !video_frame_done) {
|
2016-10-17 17:14:07 +02:00
|
|
|
WebMFrame *video_frame = video_frames[0];
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
// It seems VPXDecoder::decode has to be executed even though we might skip this frame
|
|
|
|
if (video->decode(*video_frame)) {
|
|
|
|
VPXDecoder::IMAGE_ERROR err;
|
|
|
|
VPXDecoder::Image image;
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
if (should_process(*video_frame)) {
|
|
|
|
if ((err = video->getImage(image)) != VPXDecoder::NO_FRAME) {
|
|
|
|
if (err == VPXDecoder::NO_ERROR && image.w == webm->getWidth() && image.h == webm->getHeight()) {
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = frame_data.ptrw();
|
2017-09-14 22:45:02 +02:00
|
|
|
bool converted = false;
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2019-02-18 22:19:33 +01:00
|
|
|
if (image.chromaShiftW == 0 && image.chromaShiftH == 0 && image.cs == VPX_CS_SRGB) {
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *wp = w;
|
2019-02-18 22:19:33 +01:00
|
|
|
unsigned char *rRow = image.planes[2];
|
|
|
|
unsigned char *gRow = image.planes[0];
|
|
|
|
unsigned char *bRow = image.planes[1];
|
2019-04-30 14:12:46 +02:00
|
|
|
for (int i = 0; i < image.h; i++) {
|
|
|
|
for (int j = 0; j < image.w; j++) {
|
2019-02-18 22:19:33 +01:00
|
|
|
*wp++ = rRow[j];
|
|
|
|
*wp++ = gRow[j];
|
|
|
|
*wp++ = bRow[j];
|
|
|
|
*wp++ = 255;
|
|
|
|
}
|
|
|
|
rRow += image.linesize[2];
|
|
|
|
gRow += image.linesize[0];
|
|
|
|
bRow += image.linesize[1];
|
|
|
|
}
|
|
|
|
converted = true;
|
|
|
|
} else if (image.chromaShiftW == 1 && image.chromaShiftH == 1) {
|
2020-02-17 22:06:54 +01:00
|
|
|
yuv420_2_rgb8888(w, image.planes[0], image.planes[1], image.planes[2], image.w, image.h, image.linesize[0], image.linesize[1], image.w << 2);
|
2020-01-26 11:24:14 +01:00
|
|
|
//libyuv::I420ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2], image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
|
2017-09-14 22:45:02 +02:00
|
|
|
converted = true;
|
|
|
|
} else if (image.chromaShiftW == 1 && image.chromaShiftH == 0) {
|
2020-02-17 22:06:54 +01:00
|
|
|
yuv422_2_rgb8888(w, image.planes[0], image.planes[1], image.planes[2], image.w, image.h, image.linesize[0], image.linesize[1], image.w << 2);
|
2020-01-26 11:24:14 +01:00
|
|
|
//libyuv::I422ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2], image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
|
2017-09-14 22:45:02 +02:00
|
|
|
converted = true;
|
|
|
|
} else if (image.chromaShiftW == 0 && image.chromaShiftH == 0) {
|
2020-02-17 22:06:54 +01:00
|
|
|
yuv444_2_rgb8888(w, image.planes[0], image.planes[1], image.planes[2], image.w, image.h, image.linesize[0], image.linesize[1], image.w << 2);
|
2020-01-26 11:24:14 +01:00
|
|
|
//libyuv::I444ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2], image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
|
2017-09-14 22:45:02 +02:00
|
|
|
converted = true;
|
|
|
|
} else if (image.chromaShiftW == 2 && image.chromaShiftH == 0) {
|
2020-01-26 11:24:14 +01:00
|
|
|
//libyuv::I411ToARGB(image.planes[0], image.linesize[0], image.planes[2], image.linesize[2] image.planes[1], image.linesize[1], w.ptr(), image.w << 2, image.w, image.h);
|
|
|
|
//converted = true;
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
if (converted) {
|
|
|
|
Ref<Image> img = memnew(Image(image.w, image.h, 0, Image::FORMAT_RGBA8, frame_data));
|
2019-06-11 20:43:37 +02:00
|
|
|
texture->update(img); //Zero copy send to visual server
|
2017-09-14 22:45:02 +02:00
|
|
|
video_frame_done = true;
|
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
video_pos = video_frame->time;
|
|
|
|
memmove(video_frames, video_frames + 1, (--video_frames_pos) * sizeof(void *));
|
|
|
|
video_frames[video_frames_pos] = video_frame;
|
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (video_frames_pos == 0 && webm->isEOS()) {
|
2016-10-17 17:14:07 +02:00
|
|
|
stop();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackWebm::set_mix_callback(VideoStreamPlayback::AudioMixCallback p_callback, void *p_userdata) {
|
|
|
|
mix_callback = p_callback;
|
|
|
|
mix_udata = p_userdata;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
int VideoStreamPlaybackWebm::get_channels() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (audio) {
|
2016-10-17 17:14:07 +02:00
|
|
|
return webm->getChannels();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
int VideoStreamPlaybackWebm::get_mix_rate() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (audio) {
|
2016-10-17 17:14:07 +02:00
|
|
|
return webm->getSampleRate();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
inline bool VideoStreamPlaybackWebm::has_enough_video_frames() const {
|
2016-10-17 17:14:07 +02:00
|
|
|
if (video_frames_pos > 0) {
|
2020-02-07 21:01:03 +01:00
|
|
|
// FIXME: AudioServer output latency was fixed in af9bb0e, previously it used to
|
|
|
|
// systematically return 0. Now that it gives a proper latency, it broke this
|
|
|
|
// code where the delay compensation likely never really worked.
|
|
|
|
//const double audio_delay = AudioServer::get_singleton()->get_output_latency();
|
2016-10-17 17:14:07 +02:00
|
|
|
const double video_time = video_frames[video_frames_pos - 1]->time;
|
2020-02-07 21:01:03 +01:00
|
|
|
return video_time >= time + /* audio_delay + */ delay_compensation;
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
bool VideoStreamPlaybackWebm::should_process(WebMFrame &video_frame) {
|
2020-02-07 21:01:03 +01:00
|
|
|
// FIXME: AudioServer output latency was fixed in af9bb0e, previously it used to
|
|
|
|
// systematically return 0. Now that it gives a proper latency, it broke this
|
|
|
|
// code where the delay compensation likely never really worked.
|
|
|
|
//const double audio_delay = AudioServer::get_singleton()->get_output_latency();
|
|
|
|
return video_frame.time >= time + /* audio_delay + */ delay_compensation;
|
2017-09-14 22:45:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
void VideoStreamPlaybackWebm::delete_pointers() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pcm) {
|
2016-10-17 17:14:07 +02:00
|
|
|
memfree(pcm);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (audio_frame) {
|
2016-10-17 17:14:07 +02:00
|
|
|
memdelete(audio_frame);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-03 21:52:50 +02:00
|
|
|
if (video_frames) {
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int i = 0; i < video_frames_capacity; ++i) {
|
2019-06-03 21:52:50 +02:00
|
|
|
memdelete(video_frames[i]);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
memfree(video_frames);
|
2019-06-03 21:52:50 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (video) {
|
2016-10-17 17:14:07 +02:00
|
|
|
memdelete(video);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (audio) {
|
2016-10-17 17:14:07 +02:00
|
|
|
memdelete(audio);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (webm) {
|
2016-10-17 17:14:07 +02:00
|
|
|
memdelete(webm);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
VideoStreamWebm::VideoStreamWebm() {}
|
2016-10-17 17:14:07 +02:00
|
|
|
|
|
|
|
Ref<VideoStreamPlayback> VideoStreamWebm::instance_playback() {
|
|
|
|
Ref<VideoStreamPlaybackWebm> pb = memnew(VideoStreamPlaybackWebm);
|
|
|
|
pb->set_audio_track(audio_track);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pb->open_file(file)) {
|
2016-10-17 17:14:07 +02:00
|
|
|
return pb;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2016-10-17 17:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamWebm::set_file(const String &p_file) {
|
|
|
|
file = p_file;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-09-14 22:45:02 +02:00
|
|
|
String VideoStreamWebm::get_file() {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamWebm::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStreamWebm::set_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_file"), &VideoStreamWebm::get_file);
|
|
|
|
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "file", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_file", "get_file");
|
2017-09-14 22:45:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 17:14:07 +02:00
|
|
|
void VideoStreamWebm::set_audio_track(int p_track) {
|
|
|
|
audio_track = p_track;
|
|
|
|
}
|
2018-07-03 19:22:35 +02:00
|
|
|
|
|
|
|
////////////
|
|
|
|
|
2021-02-11 18:18:45 +01:00
|
|
|
RES ResourceFormatLoaderWebm::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
|
2018-07-03 19:22:35 +02:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
if (!f) {
|
|
|
|
if (r_error) {
|
|
|
|
*r_error = ERR_CANT_OPEN;
|
|
|
|
}
|
|
|
|
return RES();
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoStreamWebm *stream = memnew(VideoStreamWebm);
|
|
|
|
stream->set_file(p_path);
|
|
|
|
|
|
|
|
Ref<VideoStreamWebm> webm_stream = Ref<VideoStreamWebm>(stream);
|
|
|
|
|
|
|
|
if (r_error) {
|
|
|
|
*r_error = OK;
|
|
|
|
}
|
|
|
|
|
2019-11-10 09:49:13 +01:00
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
2018-07-03 19:22:35 +02:00
|
|
|
return webm_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatLoaderWebm::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
p_extensions->push_back("webm");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceFormatLoaderWebm::handles_type(const String &p_type) const {
|
|
|
|
return ClassDB::is_parent_class(p_type, "VideoStream");
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatLoaderWebm::get_resource_type(const String &p_path) const {
|
|
|
|
String el = p_path.get_extension().to_lower();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (el == "webm") {
|
2018-07-03 19:22:35 +02:00
|
|
|
return "VideoStreamWebm";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-07-03 19:22:35 +02:00
|
|
|
return "";
|
|
|
|
}
|