Created
May 20, 2022 13:51
-
-
Save YeldhamDev/654a4ed43161ba4e6f17fa7647f08270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FFMPEG_STREAMER_H | |
#define FFMPEG_STREAMER_H | |
#include "player.h" | |
#include "core/reference.h" | |
#include "scene/audio/audio_stream_player.h" | |
#include "scene/main/node.h" | |
#include "scene/resources/texture.h" | |
#include "servers/audio/audio_stream.h" | |
class AudioStreamFFmpeg; | |
class FFmpegStreamer : public Node { | |
GDCLASS(FFmpegStreamer, Node); | |
private: | |
enum State { | |
LOADING, | |
UNINITIALIZED, | |
INITIALIZED, | |
DECODING, | |
SEEK, | |
BUFFERING, | |
END_OF_FILE, | |
}; | |
Ref<ImageTexture> texture; | |
Ref<Image> image; | |
MediaPlayerContext *video_player = nullptr; | |
AudioStreamPlayer *audio_player = nullptr; | |
Ref<AudioStreamFFmpeg> audio_stream = nullptr; | |
double last_video_frame_time = -1.0; | |
double first_audio_frame_time = -1.0; | |
double audio_progress_time = -1.0; | |
bool was_loading = false; | |
bool first_frame = true; | |
bool paused = false; | |
bool looping = false; | |
// bool video_playback = false; | |
int width = 0; | |
int height = 0; | |
// float video_length = 0.0f; | |
// double video_current_time = 0.0f; | |
int data_size = 0; | |
// bool audio_playback = false; | |
// int channels = 0; | |
// int frequency = 0; | |
// float audio_length = 0.0f; | |
// double audio_current_time = 0.0f; | |
protected: | |
void _notification(int p_what); | |
static void _bind_methods(); | |
public: | |
void load_path(String p_path, bool p_convert_to_rgb = true); | |
void play(); | |
void pause(); | |
bool is_playing() const; | |
void set_loop(bool p_enable); | |
bool has_loop() const; | |
Ref<ImageTexture> get_video_texture(); | |
// float get_length() const; | |
// | |
// float get_playback_position() const; | |
// void seek(float p_time); | |
FFmpegStreamer(); | |
~FFmpegStreamer(); | |
}; | |
/*** AudioStreamFFmpeg ***/ | |
#include "core/reference.h" | |
#include "core/resource.h" | |
#include "servers/audio/audio_stream.h" | |
class AudioStreamFFmpeg : public AudioStream { | |
GDCLASS(AudioStreamFFmpeg, AudioStream) | |
private: | |
friend class AudioStreamPlaybackFFmpeg; | |
uint64_t pos; | |
int mix_rate = 44100; | |
bool stereo = false; | |
int hz = 639; | |
public: | |
void reset(); | |
void set_position(uint64_t pos); | |
virtual Ref<AudioStreamPlayback> instance_playback(); | |
virtual String get_stream_name() const; | |
void gen_tone(int16_t *pcm_buf, int size); | |
virtual float get_length() const { return 0; } // if supported, otherwise return 0 | |
protected: | |
static void _bind_methods(); | |
}; | |
/*** AudioStreamPlaybackFFmpeg ***/ | |
class AudioStreamPlaybackFFmpeg : public AudioStreamPlaybackResampled { | |
GDCLASS(AudioStreamPlaybackFFmpeg, AudioStreamPlaybackResampled) | |
friend class AudioStreamFFmpeg; | |
private: | |
const int BUFFER_SIZE = 1024; | |
MediaPlayerContext *player = nullptr; | |
void *audio_buffer; | |
Ref<AudioStreamFFmpeg> base; | |
bool active = false; | |
public: | |
void set_player(MediaPlayerContext *p_player); | |
virtual void start(float p_from_pos = 0.0); | |
virtual void stop(); | |
virtual bool is_playing() const; | |
virtual int get_loop_count() const; // times it looped | |
virtual float get_playback_position() const; | |
virtual void seek(float p_time); | |
virtual void _mix_internal(AudioFrame *p_buffer, int p_frames); | |
virtual float get_length() const; // if supported, otherwise return 0 | |
virtual float get_stream_sampling_rate(); | |
AudioStreamPlaybackFFmpeg(); | |
~AudioStreamPlaybackFFmpeg(); | |
}; | |
#endif // FFMPEG_STREAMER_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment