2018-07-28 22:01:30 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* video_stream_gdnative.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-07-28 22:01:30 +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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "video_stream_gdnative.h"
|
2018-12-12 15:21:44 +01:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2018-12-12 15:21:44 +01:00
|
|
|
#include "servers/audio_server.h"
|
2018-07-28 22:01:30 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
VideoDecoderServer *VideoDecoderServer::instance = nullptr;
|
2018-07-28 22:01:30 +02:00
|
|
|
|
|
|
|
static VideoDecoderServer decoder_server;
|
|
|
|
|
2018-12-12 15:21:44 +01:00
|
|
|
const int AUX_BUFFER_SIZE = 1024; // Buffer 1024 samples.
|
|
|
|
|
2018-07-28 22:01:30 +02:00
|
|
|
// NOTE: Callbacks for the GDNative libraries.
|
|
|
|
extern "C" {
|
|
|
|
godot_int GDAPI godot_videodecoder_file_read(void *ptr, uint8_t *buf, int buf_size) {
|
|
|
|
// ptr is a FileAccess
|
|
|
|
FileAccess *file = reinterpret_cast<FileAccess *>(ptr);
|
|
|
|
|
|
|
|
// if file exists
|
|
|
|
if (file) {
|
|
|
|
long bytes_read = file->get_buffer(buf, buf_size);
|
|
|
|
// No bytes to read => EOF
|
|
|
|
if (bytes_read == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return bytes_read;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GDAPI godot_videodecoder_file_seek(void *ptr, int64_t pos, int whence) {
|
|
|
|
// file
|
|
|
|
FileAccess *file = reinterpret_cast<FileAccess *>(ptr);
|
|
|
|
|
|
|
|
if (file) {
|
2019-04-08 11:03:37 +02:00
|
|
|
size_t len = file->get_len();
|
2018-07-28 22:01:30 +02:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET: {
|
|
|
|
// Just for explicitness
|
|
|
|
size_t new_pos = static_cast<size_t>(pos);
|
|
|
|
if (new_pos > len) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
file->seek(new_pos);
|
|
|
|
pos = static_cast<int64_t>(file->get_position());
|
|
|
|
return pos;
|
|
|
|
} break;
|
|
|
|
case SEEK_CUR: {
|
|
|
|
// Just in case it doesn't exist
|
2019-02-21 20:57:39 +01:00
|
|
|
if (pos < 0 && (size_t)-pos > file->get_position()) {
|
2018-07-28 22:01:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pos = pos + static_cast<int>(file->get_position());
|
|
|
|
file->seek(pos);
|
|
|
|
pos = static_cast<int64_t>(file->get_position());
|
|
|
|
return pos;
|
|
|
|
} break;
|
|
|
|
case SEEK_END: {
|
|
|
|
// Just in case something goes wrong
|
2019-02-21 20:57:39 +01:00
|
|
|
if ((size_t)-pos > len) {
|
2018-07-28 22:01:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
file->seek_end(pos);
|
|
|
|
pos = static_cast<int64_t>(file->get_position());
|
|
|
|
return pos;
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
// Only 4 possible options, hence default = AVSEEK_SIZE
|
|
|
|
// Asks to return the length of file
|
|
|
|
return static_cast<int64_t>(len);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// In case nothing works out.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDAPI godot_videodecoder_register_decoder(const godot_videodecoder_interface_gdnative *p_interface) {
|
|
|
|
decoder_server.register_decoder_interface(p_interface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VideoStreamPlaybackGDNative starts here.
|
|
|
|
|
|
|
|
bool VideoStreamPlaybackGDNative::open_file(const String &p_file) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(interface == nullptr, false);
|
2018-07-28 22:01:30 +02:00
|
|
|
file = FileAccess::open(p_file, FileAccess::READ);
|
|
|
|
bool file_opened = interface->open_file(data_struct, file);
|
|
|
|
|
2019-03-11 12:07:22 +01:00
|
|
|
if (file_opened) {
|
|
|
|
num_channels = interface->get_channels(data_struct);
|
|
|
|
mix_rate = interface->get_mix_rate(data_struct);
|
2018-07-28 22:01:30 +02:00
|
|
|
|
2019-03-11 12:07:22 +01:00
|
|
|
godot_vector2 vec = interface->get_texture_size(data_struct);
|
|
|
|
texture_size = *(Vector2 *)&vec;
|
2019-10-11 19:03:49 +02:00
|
|
|
// Only do memset if num_channels > 0 otherwise it will crash.
|
|
|
|
if (num_channels > 0) {
|
|
|
|
pcm = (float *)memalloc(num_channels * AUX_BUFFER_SIZE * sizeof(float));
|
|
|
|
memset(pcm, 0, num_channels * AUX_BUFFER_SIZE * sizeof(float));
|
|
|
|
}
|
2018-07-28 22:01:30 +02:00
|
|
|
|
2019-03-11 12:07:22 +01:00
|
|
|
pcm_write_idx = -1;
|
|
|
|
samples_decoded = 0;
|
2018-07-28 22:01:30 +02:00
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Image> img;
|
|
|
|
img.instance();
|
|
|
|
img->create((int)texture_size.width, false, (int)texture_size.height, Image::FORMAT_RGBA8);
|
|
|
|
|
|
|
|
texture->create_from_image(img);
|
2019-03-11 12:07:22 +01:00
|
|
|
}
|
2018-07-28 22:01:30 +02:00
|
|
|
|
|
|
|
return file_opened;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::update(float p_delta) {
|
|
|
|
if (!playing || paused) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
time += p_delta;
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND(interface == nullptr);
|
2018-07-28 22:01:30 +02:00
|
|
|
interface->update(data_struct, p_delta);
|
|
|
|
|
2019-10-11 19:03:49 +02:00
|
|
|
// Don't mix if there's no audio (num_channels == 0).
|
|
|
|
if (mix_callback && num_channels > 0) {
|
2019-05-03 21:04:58 +02:00
|
|
|
if (pcm_write_idx >= 0) {
|
|
|
|
// Previous remains
|
2019-09-19 07:28:33 +02:00
|
|
|
int mixed = mix_callback(mix_udata, pcm + pcm_write_idx * num_channels, samples_decoded);
|
2019-05-03 21:04:58 +02:00
|
|
|
if (mixed == samples_decoded) {
|
|
|
|
pcm_write_idx = -1;
|
|
|
|
} else {
|
|
|
|
samples_decoded -= mixed;
|
|
|
|
pcm_write_idx += mixed;
|
|
|
|
}
|
2018-07-28 22:01:30 +02:00
|
|
|
}
|
2019-05-03 21:04:58 +02:00
|
|
|
if (pcm_write_idx < 0) {
|
|
|
|
samples_decoded = interface->get_audioframe(data_struct, pcm, AUX_BUFFER_SIZE);
|
|
|
|
pcm_write_idx = mix_callback(mix_udata, pcm, samples_decoded);
|
|
|
|
if (pcm_write_idx == samples_decoded) {
|
|
|
|
pcm_write_idx = -1;
|
|
|
|
} else {
|
|
|
|
samples_decoded -= pcm_write_idx;
|
|
|
|
}
|
2018-07-28 22:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 07:27:31 +02:00
|
|
|
if (seek_backward) {
|
|
|
|
update_texture();
|
|
|
|
seek_backward = false;
|
|
|
|
}
|
2018-07-28 22:01:30 +02:00
|
|
|
|
2019-09-19 07:27:31 +02:00
|
|
|
while (interface->get_playback_position(data_struct) < time && playing) {
|
2018-07-28 22:01:30 +02:00
|
|
|
update_texture();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::update_texture() {
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedByteArray *pba = (PackedByteArray *)interface->get_videoframe(data_struct);
|
2018-07-28 22:01:30 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
if (pba == nullptr) {
|
2018-07-28 22:01:30 +02:00
|
|
|
playing = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> img = memnew(Image(texture_size.width, texture_size.height, 0, Image::FORMAT_RGBA8, *pba));
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
texture->update(img, true);
|
2018-07-28 22:01:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ctor and dtor
|
|
|
|
|
|
|
|
VideoStreamPlaybackGDNative::VideoStreamPlaybackGDNative() :
|
2020-05-12 17:01:17 +02:00
|
|
|
texture(Ref<ImageTexture>(memnew(ImageTexture))) {}
|
2018-07-28 22:01:30 +02:00
|
|
|
|
|
|
|
VideoStreamPlaybackGDNative::~VideoStreamPlaybackGDNative() {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::cleanup() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (data_struct) {
|
2018-07-28 22:01:30 +02:00
|
|
|
interface->destructor(data_struct);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (pcm) {
|
2018-12-12 15:21:44 +01:00
|
|
|
memfree(pcm);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-06-28 02:20:07 +02:00
|
|
|
if (file) {
|
|
|
|
file->close();
|
|
|
|
memdelete(file);
|
|
|
|
file = nullptr;
|
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
pcm = nullptr;
|
2018-07-28 22:01:30 +02:00
|
|
|
time = 0;
|
|
|
|
num_channels = -1;
|
2020-04-02 01:20:12 +02:00
|
|
|
interface = nullptr;
|
|
|
|
data_struct = nullptr;
|
2018-07-28 22:01:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::set_interface(const godot_videodecoder_interface_gdnative *p_interface) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND(p_interface == nullptr);
|
|
|
|
if (interface != nullptr) {
|
2018-07-28 22:01:30 +02:00
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
interface = p_interface;
|
|
|
|
data_struct = interface->constructor((godot_object *)this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// controls
|
|
|
|
|
|
|
|
bool VideoStreamPlaybackGDNative::is_playing() const {
|
|
|
|
return playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoStreamPlaybackGDNative::is_paused() const {
|
|
|
|
return paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::play() {
|
|
|
|
stop();
|
|
|
|
|
|
|
|
playing = true;
|
|
|
|
|
|
|
|
delay_compensation = ProjectSettings::get_singleton()->get("audio/video_delay_compensation_ms");
|
|
|
|
delay_compensation /= 1000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::stop() {
|
|
|
|
if (playing) {
|
|
|
|
seek(0);
|
|
|
|
}
|
|
|
|
playing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::seek(float p_time) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND(interface == nullptr);
|
2018-07-28 22:01:30 +02:00
|
|
|
interface->seek(data_struct, p_time);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_time < time) {
|
2019-09-19 07:27:31 +02:00
|
|
|
seek_backward = true;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-09-19 07:27:31 +02:00
|
|
|
time = p_time;
|
|
|
|
// reset audio buffers
|
|
|
|
memset(pcm, 0, num_channels * AUX_BUFFER_SIZE * sizeof(float));
|
|
|
|
pcm_write_idx = -1;
|
|
|
|
samples_decoded = 0;
|
2018-07-28 22:01:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::set_paused(bool p_paused) {
|
|
|
|
paused = p_paused;
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:43:37 +02:00
|
|
|
Ref<Texture2D> VideoStreamPlaybackGDNative::get_texture() const {
|
2018-07-28 22:01:30 +02:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
float VideoStreamPlaybackGDNative::get_length() const {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(interface == nullptr, 0);
|
2018-07-28 22:01:30 +02:00
|
|
|
return interface->get_length(data_struct);
|
|
|
|
}
|
|
|
|
|
|
|
|
float VideoStreamPlaybackGDNative::get_playback_position() const {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(interface == nullptr, 0);
|
2018-07-28 22:01:30 +02:00
|
|
|
return interface->get_playback_position(data_struct);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoStreamPlaybackGDNative::has_loop() const {
|
|
|
|
// TODO: Implement looping?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::set_loop(bool p_enable) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::set_audio_track(int p_idx) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND(interface == nullptr);
|
2018-07-28 22:01:30 +02:00
|
|
|
interface->set_audio_track(data_struct, p_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamPlaybackGDNative::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) {
|
|
|
|
mix_udata = p_userdata;
|
|
|
|
mix_callback = p_callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VideoStreamPlaybackGDNative::get_channels() const {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(interface == nullptr, 0);
|
2018-07-28 22:01:30 +02:00
|
|
|
|
|
|
|
return (num_channels > 0) ? num_channels : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VideoStreamPlaybackGDNative::get_mix_rate() const {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(interface == nullptr, 0);
|
2018-07-28 22:01:30 +02:00
|
|
|
|
|
|
|
return mix_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --- NOTE VideoStreamGDNative starts here. ----- */
|
|
|
|
|
|
|
|
Ref<VideoStreamPlayback> VideoStreamGDNative::instance_playback() {
|
|
|
|
Ref<VideoStreamPlaybackGDNative> pb = memnew(VideoStreamPlaybackGDNative);
|
|
|
|
VideoDecoderGDNative *decoder = decoder_server.get_decoder(file.get_extension().to_lower());
|
2020-05-14 16:41:43 +02:00
|
|
|
if (decoder == nullptr) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-07-28 22:01:30 +02:00
|
|
|
pb->set_interface(decoder->interface);
|
|
|
|
pb->set_audio_track(audio_track);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pb->open_file(file)) {
|
2018-07-28 22:01:30 +02:00
|
|
|
return pb;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2018-07-28 22:01:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamGDNative::set_file(const String &p_file) {
|
|
|
|
file = p_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
String VideoStreamGDNative::get_file() {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamGDNative::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_file", "file"), &VideoStreamGDNative::set_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_file"), &VideoStreamGDNative::get_file);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "file", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_file", "get_file");
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoStreamGDNative::set_audio_track(int p_track) {
|
|
|
|
audio_track = p_track;
|
|
|
|
}
|
2018-12-12 15:47:57 +01:00
|
|
|
|
|
|
|
/* --- NOTE ResourceFormatLoaderVideoStreamGDNative starts here. ----- */
|
|
|
|
|
2020-04-20 04:19:21 +02:00
|
|
|
RES ResourceFormatLoaderVideoStreamGDNative::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) {
|
2018-12-12 15:47:57 +01:00
|
|
|
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
if (!f) {
|
|
|
|
if (r_error) {
|
|
|
|
*r_error = ERR_CANT_OPEN;
|
|
|
|
}
|
|
|
|
return RES();
|
|
|
|
}
|
2019-03-06 19:45:57 +01:00
|
|
|
memdelete(f);
|
2018-12-12 15:47:57 +01:00
|
|
|
VideoStreamGDNative *stream = memnew(VideoStreamGDNative);
|
|
|
|
stream->set_file(p_path);
|
|
|
|
Ref<VideoStreamGDNative> ogv_stream = Ref<VideoStreamGDNative>(stream);
|
|
|
|
if (r_error) {
|
|
|
|
*r_error = OK;
|
|
|
|
}
|
|
|
|
return ogv_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatLoaderVideoStreamGDNative::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
Map<String, int>::Element *el = VideoDecoderServer::get_instance()->get_extensions().front();
|
|
|
|
while (el) {
|
|
|
|
p_extensions->push_back(el->key());
|
|
|
|
el = el->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceFormatLoaderVideoStreamGDNative::handles_type(const String &p_type) const {
|
|
|
|
return ClassDB::is_parent_class(p_type, "VideoStream");
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatLoaderVideoStreamGDNative::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 (VideoDecoderServer::get_instance()->get_extensions().has(el)) {
|
2018-12-12 15:47:57 +01:00
|
|
|
return "VideoStreamGDNative";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-12-12 15:47:57 +01:00
|
|
|
return "";
|
|
|
|
}
|