Notify end-of-stream via callback

Do not send a SDL event directly, to make stream independent of SDL.
This commit is contained in:
Romain Vimont 2021-05-16 15:32:31 +02:00
parent 1e64f0f931
commit 83116fc199
3 changed files with 32 additions and 13 deletions

View file

@ -241,6 +241,16 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
free(local_fmt);
}
static void
stream_on_eos(struct stream *stream, void *userdata) {
(void) stream;
(void) userdata;
SDL_Event stop_event;
stop_event.type = EVENT_STREAM_STOPPED;
SDL_PushEvent(&stop_event);
}
bool
scrcpy(const struct scrcpy_options *options) {
if (!server_init(&server)) {
@ -343,7 +353,10 @@ scrcpy(const struct scrcpy_options *options) {
av_log_set_callback(av_log_callback);
stream_init(&stream, server.video_socket);
const struct stream_callbacks stream_cbs = {
.on_eos = stream_on_eos,
};
stream_init(&stream, server.video_socket, &stream_cbs, NULL);
if (dec) {
stream_add_sink(&stream, &dec->packet_sink);

View file

@ -3,7 +3,6 @@
#include <assert.h>
#include <libavformat/avformat.h>
#include <libavutil/time.h>
#include <SDL2/SDL_events.h>
#include <unistd.h>
#include "decoder.h"
@ -58,13 +57,6 @@ stream_recv_packet(struct stream *stream, AVPacket *packet) {
return true;
}
static void
notify_stopped(void) {
SDL_Event stop_event;
stop_event.type = EVENT_STREAM_STOPPED;
SDL_PushEvent(&stop_event);
}
static bool
push_packet_to_sinks(struct stream *stream, const AVPacket *packet) {
for (unsigned i = 0; i < stream->sink_count; ++i) {
@ -251,15 +243,22 @@ finally_close_sinks:
finally_free_codec_ctx:
avcodec_free_context(&stream->codec_ctx);
end:
notify_stopped();
stream->cbs->on_eos(stream, stream->cbs_userdata);
return 0;
}
void
stream_init(struct stream *stream, socket_t socket) {
stream_init(struct stream *stream, socket_t socket,
const struct stream_callbacks *cbs, void *cbs_userdata) {
stream->socket = socket;
stream->has_pending = false;
stream->sink_count = 0;
assert(cbs && cbs->on_eos);
stream->cbs = cbs;
stream->cbs_userdata = cbs_userdata;
}
void

View file

@ -6,7 +6,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <libavformat/avformat.h>
#include <SDL2/SDL_atomic.h>
#include "trait/packet_sink.h"
#include "util/net.h"
@ -27,10 +26,18 @@ struct stream {
// packet is available
bool has_pending;
AVPacket pending;
const struct stream_callbacks *cbs;
void *cbs_userdata;
};
struct stream_callbacks {
void (*on_eos)(struct stream *stream, void *userdata);
};
void
stream_init(struct stream *stream, socket_t socket);
stream_init(struct stream *stream, socket_t socket,
const struct stream_callbacks *cbs, void *cbs_userdata);
void
stream_add_sink(struct stream *stream, struct sc_packet_sink *sink);