Compare commits
20 Commits
server_thr
...
traits.4
Author | SHA1 | Date | |
---|---|---|---|
7fa922d8e6 | |||
ac7dca42b0 | |||
492162a24f | |||
2ab148bd79 | |||
6795564044 | |||
b7cdbce8e5 | |||
5cd847a856 | |||
3d36915d9b | |||
c64f5ff71e | |||
db418d0825 | |||
b481ab0aaf | |||
cb7017e2c1 | |||
5c6b04cf01 | |||
153d5e4892 | |||
620b117a15 | |||
14e83175a6 | |||
0d46d0b376 | |||
4632ef224c | |||
bc07943624 | |||
003fcb0442 |
12
README.md
12
README.md
@ -491,18 +491,6 @@ scrcpy -Sw
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Render expired frames
|
|
||||||
|
|
||||||
By default, to minimize latency, _scrcpy_ always renders the last decoded frame
|
|
||||||
available, and drops any previous one.
|
|
||||||
|
|
||||||
To force the rendering of all frames (at a cost of a possible increased
|
|
||||||
latency), use:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
scrcpy --render-expired-frames
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Show touches
|
#### Show touches
|
||||||
|
|
||||||
For presentations, it may be useful to show physical touches (on the physical
|
For presentations, it may be useful to show physical touches (on the physical
|
||||||
|
@ -155,10 +155,6 @@ Supported names are currently "direct3d", "opengl", "opengles2", "opengles", "me
|
|||||||
.UR https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER
|
.UR https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER
|
||||||
.UE
|
.UE
|
||||||
|
|
||||||
.TP
|
|
||||||
.B \-\-render\-expired\-frames
|
|
||||||
By default, to minimize latency, scrcpy always renders the last available decoded frame, and drops any previous ones. This flag forces to render all frames, at a cost of a possible increased latency.
|
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BI "\-\-rotation " value
|
.BI "\-\-rotation " value
|
||||||
Set the initial display rotation. Possibles values are 0, 1, 2 and 3. Each increment adds a 90 degrees rotation counterclockwise.
|
Set the initial display rotation. Possibles values are 0, 1, 2 and 3. Each increment adds a 90 degrees rotation counterclockwise.
|
||||||
|
@ -143,12 +143,6 @@ scrcpy_print_usage(const char *arg0) {
|
|||||||
" \"opengles2\", \"opengles\", \"metal\" and \"software\".\n"
|
" \"opengles2\", \"opengles\", \"metal\" and \"software\".\n"
|
||||||
" <https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER>\n"
|
" <https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER>\n"
|
||||||
"\n"
|
"\n"
|
||||||
" --render-expired-frames\n"
|
|
||||||
" By default, to minimize latency, scrcpy always renders the\n"
|
|
||||||
" last available decoded frame, and drops any previous ones.\n"
|
|
||||||
" This flag forces to render all frames, at a cost of a\n"
|
|
||||||
" possible increased latency.\n"
|
|
||||||
"\n"
|
|
||||||
" --rotation value\n"
|
" --rotation value\n"
|
||||||
" Set the initial display rotation.\n"
|
" Set the initial display rotation.\n"
|
||||||
" Possibles values are 0, 1, 2 and 3. Each increment adds a 90\n"
|
" Possibles values are 0, 1, 2 and 3. Each increment adds a 90\n"
|
||||||
@ -816,7 +810,8 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
|
|||||||
opts->stay_awake = true;
|
opts->stay_awake = true;
|
||||||
break;
|
break;
|
||||||
case OPT_RENDER_EXPIRED_FRAMES:
|
case OPT_RENDER_EXPIRED_FRAMES:
|
||||||
opts->render_expired_frames = true;
|
LOGW("Option --render-expired-frames has been removed. This "
|
||||||
|
"flag has been ignored.");
|
||||||
break;
|
break;
|
||||||
case OPT_WINDOW_TITLE:
|
case OPT_WINDOW_TITLE:
|
||||||
opts->window_title = optarg;
|
opts->window_title = optarg;
|
||||||
|
@ -8,4 +8,7 @@
|
|||||||
#define MIN(X,Y) (X) < (Y) ? (X) : (Y)
|
#define MIN(X,Y) (X) < (Y) ? (X) : (Y)
|
||||||
#define MAX(X,Y) (X) > (Y) ? (X) : (Y)
|
#define MAX(X,Y) (X) > (Y) ? (X) : (Y)
|
||||||
|
|
||||||
|
#define container_of(ptr, type, member) \
|
||||||
|
((type *) (((char *) (ptr)) - offsetof(type, member)))
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,20 +8,9 @@
|
|||||||
# define _DARWIN_C_SOURCE
|
# define _DARWIN_C_SOURCE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <libavcodec/version.h>
|
|
||||||
#include <libavformat/version.h>
|
#include <libavformat/version.h>
|
||||||
#include <SDL2/SDL_version.h>
|
#include <SDL2/SDL_version.h>
|
||||||
|
|
||||||
// In ffmpeg/doc/APIchanges:
|
|
||||||
// 2016-04-11 - 6f69f7a / 9200514 - lavf 57.33.100 / 57.5.0 - avformat.h
|
|
||||||
// Add AVStream.codecpar, deprecate AVStream.codec.
|
|
||||||
#if (LIBAVFORMAT_VERSION_MICRO >= 100 /* FFmpeg */ && \
|
|
||||||
LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)) \
|
|
||||||
|| (LIBAVFORMAT_VERSION_MICRO < 100 && /* Libav */ \
|
|
||||||
LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 5, 0))
|
|
||||||
# define SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// In ffmpeg/doc/APIchanges:
|
// In ffmpeg/doc/APIchanges:
|
||||||
// 2018-02-06 - 0694d87024 - lavf 58.9.100 - avformat.h
|
// 2018-02-06 - 0694d87024 - lavf 58.9.100 - avformat.h
|
||||||
// Deprecate use of av_register_input_format(), av_register_output_format(),
|
// Deprecate use of av_register_input_format(), av_register_output_format(),
|
||||||
@ -33,15 +22,6 @@
|
|||||||
# define SCRCPY_LAVF_REQUIRES_REGISTER_ALL
|
# define SCRCPY_LAVF_REQUIRES_REGISTER_ALL
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// In ffmpeg/doc/APIchanges:
|
|
||||||
// 2016-04-21 - 7fc329e - lavc 57.37.100 - avcodec.h
|
|
||||||
// Add a new audio/video encoding and decoding API with decoupled input
|
|
||||||
// and output -- avcodec_send_packet(), avcodec_receive_frame(),
|
|
||||||
// avcodec_send_frame() and avcodec_receive_packet().
|
|
||||||
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
|
|
||||||
# define SCRCPY_LAVF_HAS_NEW_ENCODING_DECODING_API
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if SDL_VERSION_ATLEAST(2, 0, 5)
|
#if SDL_VERSION_ATLEAST(2, 0, 5)
|
||||||
// <https://wiki.libsdl.org/SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH>
|
// <https://wiki.libsdl.org/SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH>
|
||||||
# define SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
# define SCRCPY_SDL_HAS_HINT_MOUSE_FOCUS_CLICKTHROUGH
|
||||||
|
@ -4,14 +4,40 @@
|
|||||||
|
|
||||||
#include "events.h"
|
#include "events.h"
|
||||||
#include "video_buffer.h"
|
#include "video_buffer.h"
|
||||||
|
#include "trait/frame_sink.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
void
|
/** Downcast packet_sink to decoder */
|
||||||
decoder_init(struct decoder *decoder, struct video_buffer *vb) {
|
#define DOWNCAST(SINK) container_of(SINK, struct decoder, packet_sink)
|
||||||
decoder->video_buffer = vb;
|
|
||||||
|
static void
|
||||||
|
decoder_close_first_sinks(struct decoder *decoder, unsigned count) {
|
||||||
|
while (count) {
|
||||||
|
struct sc_frame_sink *sink = decoder->sinks[--count];
|
||||||
|
sink->ops->close(sink);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
static inline void
|
||||||
|
decoder_close_sinks(struct decoder *decoder) {
|
||||||
|
decoder_close_first_sinks(decoder, decoder->sink_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
decoder_open_sinks(struct decoder *decoder) {
|
||||||
|
for (unsigned i = 0; i < decoder->sink_count; ++i) {
|
||||||
|
struct sc_frame_sink *sink = decoder->sinks[i];
|
||||||
|
if (!sink->ops->open(sink)) {
|
||||||
|
LOGE("Could not open frame sink %d", i);
|
||||||
|
decoder_close_first_sinks(decoder, i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
||||||
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
decoder->codec_ctx = avcodec_alloc_context3(codec);
|
||||||
if (!decoder->codec_ctx) {
|
if (!decoder->codec_ctx) {
|
||||||
@ -25,52 +51,105 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
decoder->frame = av_frame_alloc();
|
||||||
|
if (!decoder->frame) {
|
||||||
|
LOGE("Could not create decoder frame");
|
||||||
|
avcodec_close(decoder->codec_ctx);
|
||||||
|
avcodec_free_context(&decoder->codec_ctx);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
if (!decoder_open_sinks(decoder)) {
|
||||||
decoder_close(struct decoder *decoder) {
|
LOGE("Could not open decoder sinks");
|
||||||
|
av_frame_free(&decoder->frame);
|
||||||
avcodec_close(decoder->codec_ctx);
|
avcodec_close(decoder->codec_ctx);
|
||||||
avcodec_free_context(&decoder->codec_ctx);
|
avcodec_free_context(&decoder->codec_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
decoder_close(struct decoder *decoder) {
|
||||||
|
decoder_close_sinks(decoder);
|
||||||
|
av_frame_free(&decoder->frame);
|
||||||
|
avcodec_close(decoder->codec_ctx);
|
||||||
|
avcodec_free_context(&decoder->codec_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
push_frame_to_sinks(struct decoder *decoder, const AVFrame *frame) {
|
||||||
|
for (unsigned i = 0; i < decoder->sink_count; ++i) {
|
||||||
|
struct sc_frame_sink *sink = decoder->sinks[i];
|
||||||
|
if (!sink->ops->push(sink, frame)) {
|
||||||
|
LOGE("Could not send frame to sink %d", i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
decoder_push(struct decoder *decoder, const AVPacket *packet) {
|
decoder_push(struct decoder *decoder, const AVPacket *packet) {
|
||||||
// the new decoding/encoding API has been introduced by:
|
bool is_config = packet->pts == AV_NOPTS_VALUE;
|
||||||
// <http://git.videolan.org/?p=ffmpeg.git;a=commitdiff;h=7fc329e2dd6226dfecaa4a1d7adf353bf2773726>
|
if (is_config) {
|
||||||
#ifdef SCRCPY_LAVF_HAS_NEW_ENCODING_DECODING_API
|
// nothing to do
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
if ((ret = avcodec_send_packet(decoder->codec_ctx, packet)) < 0) {
|
if ((ret = avcodec_send_packet(decoder->codec_ctx, packet)) < 0) {
|
||||||
LOGE("Could not send video packet: %d", ret);
|
LOGE("Could not send video packet: %d", ret);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ret = avcodec_receive_frame(decoder->codec_ctx,
|
ret = avcodec_receive_frame(decoder->codec_ctx, decoder->frame);
|
||||||
decoder->video_buffer->producer_frame);
|
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
// a frame was received
|
// a frame was received
|
||||||
video_buffer_producer_offer_frame(decoder->video_buffer);
|
bool ok = push_frame_to_sinks(decoder, decoder->frame);
|
||||||
|
// A frame lost should not make the whole pipeline fail. The error, if
|
||||||
|
// any, is already logged.
|
||||||
|
(void) ok;
|
||||||
} else if (ret != AVERROR(EAGAIN)) {
|
} else if (ret != AVERROR(EAGAIN)) {
|
||||||
LOGE("Could not receive video frame: %d", ret);
|
LOGE("Could not receive video frame: %d", ret);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
int got_picture;
|
|
||||||
int len = avcodec_decode_video2(decoder->codec_ctx,
|
|
||||||
decoder->video_buffer->producer_frame,
|
|
||||||
&got_picture,
|
|
||||||
packet);
|
|
||||||
if (len < 0) {
|
|
||||||
LOGE("Could not decode video packet: %d", len);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (got_picture) {
|
|
||||||
video_buffer_producer_offer_frame(decoder->video_buffer);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static bool
|
||||||
decoder_interrupt(struct decoder *decoder) {
|
decoder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
|
||||||
video_buffer_interrupt(decoder->video_buffer);
|
struct decoder *decoder = DOWNCAST(sink);
|
||||||
|
return decoder_open(decoder, codec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
decoder_packet_sink_close(struct sc_packet_sink *sink) {
|
||||||
|
struct decoder *decoder = DOWNCAST(sink);
|
||||||
|
decoder_close(decoder);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
decoder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
|
||||||
|
struct decoder *decoder = DOWNCAST(sink);
|
||||||
|
return decoder_push(decoder, packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
decoder_init(struct decoder *decoder) {
|
||||||
|
static const struct sc_packet_sink_ops ops = {
|
||||||
|
.open = decoder_packet_sink_open,
|
||||||
|
.close = decoder_packet_sink_close,
|
||||||
|
.push = decoder_packet_sink_push,
|
||||||
|
};
|
||||||
|
|
||||||
|
decoder->packet_sink.ops = &ops;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink) {
|
||||||
|
assert(decoder->sink_count < DECODER_MAX_SINKS);
|
||||||
|
assert(sink);
|
||||||
|
assert(sink->ops);
|
||||||
|
decoder->sinks[decoder->sink_count++] = sink;
|
||||||
}
|
}
|
||||||
|
@ -3,30 +3,27 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
#include "trait/packet_sink.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <libavformat/avformat.h>
|
#include <libavformat/avformat.h>
|
||||||
|
|
||||||
struct video_buffer;
|
#define DECODER_MAX_SINKS 1
|
||||||
|
|
||||||
struct decoder {
|
struct decoder {
|
||||||
struct video_buffer *video_buffer;
|
struct sc_packet_sink packet_sink; // packet sink trait
|
||||||
|
|
||||||
|
struct sc_frame_sink *sinks[DECODER_MAX_SINKS];
|
||||||
|
unsigned sink_count;
|
||||||
|
|
||||||
AVCodecContext *codec_ctx;
|
AVCodecContext *codec_ctx;
|
||||||
|
AVFrame *frame;
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
decoder_init(struct decoder *decoder, struct video_buffer *vb);
|
decoder_init(struct decoder *decoder);
|
||||||
|
|
||||||
bool
|
|
||||||
decoder_open(struct decoder *decoder, const AVCodec *codec);
|
|
||||||
|
|
||||||
void
|
void
|
||||||
decoder_close(struct decoder *decoder);
|
decoder_add_sink(struct decoder *decoder, struct sc_frame_sink *sink);
|
||||||
|
|
||||||
bool
|
|
||||||
decoder_push(struct decoder *decoder, const AVPacket *packet);
|
|
||||||
|
|
||||||
void
|
|
||||||
decoder_interrupt(struct decoder *decoder);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,6 +5,9 @@
|
|||||||
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
|
/** Downcast packet_sink to recorder */
|
||||||
|
#define DOWNCAST(SINK) container_of(SINK, struct recorder, packet_sink)
|
||||||
|
|
||||||
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
|
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
|
||||||
|
|
||||||
static const AVOutputFormat *
|
static const AVOutputFormat *
|
||||||
@ -57,50 +60,6 @@ recorder_queue_clear(struct recorder_queue *queue) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
recorder_init(struct recorder *recorder,
|
|
||||||
const char *filename,
|
|
||||||
enum sc_record_format format,
|
|
||||||
struct size declared_frame_size) {
|
|
||||||
recorder->filename = strdup(filename);
|
|
||||||
if (!recorder->filename) {
|
|
||||||
LOGE("Could not strdup filename");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ok = sc_mutex_init(&recorder->mutex);
|
|
||||||
if (!ok) {
|
|
||||||
LOGC("Could not create mutex");
|
|
||||||
free(recorder->filename);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ok = sc_cond_init(&recorder->queue_cond);
|
|
||||||
if (!ok) {
|
|
||||||
LOGC("Could not create cond");
|
|
||||||
sc_mutex_destroy(&recorder->mutex);
|
|
||||||
free(recorder->filename);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
queue_init(&recorder->queue);
|
|
||||||
recorder->stopped = false;
|
|
||||||
recorder->failed = false;
|
|
||||||
recorder->format = format;
|
|
||||||
recorder->declared_frame_size = declared_frame_size;
|
|
||||||
recorder->header_written = false;
|
|
||||||
recorder->previous = NULL;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
recorder_destroy(struct recorder *recorder) {
|
|
||||||
sc_cond_destroy(&recorder->queue_cond);
|
|
||||||
sc_mutex_destroy(&recorder->mutex);
|
|
||||||
free(recorder->filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
recorder_get_format_name(enum sc_record_format format) {
|
recorder_get_format_name(enum sc_record_format format) {
|
||||||
switch (format) {
|
switch (format) {
|
||||||
@ -110,88 +69,6 @@ recorder_get_format_name(enum sc_record_format format) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
|
||||||
const char *format_name = recorder_get_format_name(recorder->format);
|
|
||||||
assert(format_name);
|
|
||||||
const AVOutputFormat *format = find_muxer(format_name);
|
|
||||||
if (!format) {
|
|
||||||
LOGE("Could not find muxer");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
recorder->ctx = avformat_alloc_context();
|
|
||||||
if (!recorder->ctx) {
|
|
||||||
LOGE("Could not allocate output context");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// contrary to the deprecated API (av_oformat_next()), av_muxer_iterate()
|
|
||||||
// returns (on purpose) a pointer-to-const, but AVFormatContext.oformat
|
|
||||||
// still expects a pointer-to-non-const (it has not be updated accordingly)
|
|
||||||
// <https://github.com/FFmpeg/FFmpeg/commit/0694d8702421e7aff1340038559c438b61bb30dd>
|
|
||||||
recorder->ctx->oformat = (AVOutputFormat *) format;
|
|
||||||
|
|
||||||
av_dict_set(&recorder->ctx->metadata, "comment",
|
|
||||||
"Recorded by scrcpy " SCRCPY_VERSION, 0);
|
|
||||||
|
|
||||||
AVStream *ostream = avformat_new_stream(recorder->ctx, input_codec);
|
|
||||||
if (!ostream) {
|
|
||||||
avformat_free_context(recorder->ctx);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API
|
|
||||||
ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
||||||
ostream->codecpar->codec_id = input_codec->id;
|
|
||||||
ostream->codecpar->format = AV_PIX_FMT_YUV420P;
|
|
||||||
ostream->codecpar->width = recorder->declared_frame_size.width;
|
|
||||||
ostream->codecpar->height = recorder->declared_frame_size.height;
|
|
||||||
#else
|
|
||||||
ostream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
||||||
ostream->codec->codec_id = input_codec->id;
|
|
||||||
ostream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
||||||
ostream->codec->width = recorder->declared_frame_size.width;
|
|
||||||
ostream->codec->height = recorder->declared_frame_size.height;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int ret = avio_open(&recorder->ctx->pb, recorder->filename,
|
|
||||||
AVIO_FLAG_WRITE);
|
|
||||||
if (ret < 0) {
|
|
||||||
LOGE("Failed to open output file: %s", recorder->filename);
|
|
||||||
// ostream will be cleaned up during context cleaning
|
|
||||||
avformat_free_context(recorder->ctx);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOGI("Recording started to %s file: %s", format_name, recorder->filename);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
recorder_close(struct recorder *recorder) {
|
|
||||||
if (recorder->header_written) {
|
|
||||||
int ret = av_write_trailer(recorder->ctx);
|
|
||||||
if (ret < 0) {
|
|
||||||
LOGE("Failed to write trailer to %s", recorder->filename);
|
|
||||||
recorder->failed = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// the recorded file is empty
|
|
||||||
recorder->failed = true;
|
|
||||||
}
|
|
||||||
avio_close(recorder->ctx->pb);
|
|
||||||
avformat_free_context(recorder->ctx);
|
|
||||||
|
|
||||||
if (recorder->failed) {
|
|
||||||
LOGE("Recording failed to %s", recorder->filename);
|
|
||||||
} else {
|
|
||||||
const char *format_name = recorder_get_format_name(recorder->format);
|
|
||||||
LOGI("Recording complete to %s file: %s", format_name, recorder->filename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
|
recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
|
||||||
AVStream *ostream = recorder->ctx->streams[0];
|
AVStream *ostream = recorder->ctx->streams[0];
|
||||||
@ -205,13 +82,8 @@ recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
|
|||||||
// copy the first packet to the extra data
|
// copy the first packet to the extra data
|
||||||
memcpy(extradata, packet->data, packet->size);
|
memcpy(extradata, packet->data, packet->size);
|
||||||
|
|
||||||
#ifdef SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API
|
|
||||||
ostream->codecpar->extradata = extradata;
|
ostream->codecpar->extradata = extradata;
|
||||||
ostream->codecpar->extradata_size = packet->size;
|
ostream->codecpar->extradata_size = packet->size;
|
||||||
#else
|
|
||||||
ostream->codec->extradata = extradata;
|
|
||||||
ostream->codec->extradata_size = packet->size;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int ret = avformat_write_header(recorder->ctx, NULL);
|
int ret = avformat_write_header(recorder->ctx, NULL);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
@ -317,7 +189,26 @@ run_recorder(void *data) {
|
|||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!recorder->failed) {
|
||||||
|
if (recorder->header_written) {
|
||||||
|
int ret = av_write_trailer(recorder->ctx);
|
||||||
|
if (ret < 0) {
|
||||||
|
LOGE("Failed to write trailer to %s", recorder->filename);
|
||||||
|
recorder->failed = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// the recorded file is empty
|
||||||
|
recorder->failed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recorder->failed) {
|
||||||
|
LOGE("Recording failed to %s", recorder->filename);
|
||||||
|
} else {
|
||||||
|
const char *format_name = recorder_get_format_name(recorder->format);
|
||||||
|
LOGI("Recording complete to %s file: %s", format_name, recorder->filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGD("Recorder thread ended");
|
LOGD("Recorder thread ended");
|
||||||
@ -325,34 +216,80 @@ run_recorder(void *data) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
static bool
|
||||||
recorder_start(struct recorder *recorder) {
|
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
||||||
LOGD("Starting recorder thread");
|
const char *format_name = recorder_get_format_name(recorder->format);
|
||||||
|
assert(format_name);
|
||||||
|
const AVOutputFormat *format = find_muxer(format_name);
|
||||||
|
if (!format) {
|
||||||
|
LOGE("Could not find muxer");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
recorder->ctx = avformat_alloc_context();
|
||||||
|
if (!recorder->ctx) {
|
||||||
|
LOGE("Could not allocate output context");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// contrary to the deprecated API (av_oformat_next()), av_muxer_iterate()
|
||||||
|
// returns (on purpose) a pointer-to-const, but AVFormatContext.oformat
|
||||||
|
// still expects a pointer-to-non-const (it has not be updated accordingly)
|
||||||
|
// <https://github.com/FFmpeg/FFmpeg/commit/0694d8702421e7aff1340038559c438b61bb30dd>
|
||||||
|
recorder->ctx->oformat = (AVOutputFormat *) format;
|
||||||
|
|
||||||
|
av_dict_set(&recorder->ctx->metadata, "comment",
|
||||||
|
"Recorded by scrcpy " SCRCPY_VERSION, 0);
|
||||||
|
|
||||||
|
AVStream *ostream = avformat_new_stream(recorder->ctx, input_codec);
|
||||||
|
if (!ostream) {
|
||||||
|
avformat_free_context(recorder->ctx);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
||||||
|
ostream->codecpar->codec_id = input_codec->id;
|
||||||
|
ostream->codecpar->format = AV_PIX_FMT_YUV420P;
|
||||||
|
ostream->codecpar->width = recorder->declared_frame_size.width;
|
||||||
|
ostream->codecpar->height = recorder->declared_frame_size.height;
|
||||||
|
|
||||||
|
int ret = avio_open(&recorder->ctx->pb, recorder->filename,
|
||||||
|
AVIO_FLAG_WRITE);
|
||||||
|
if (ret < 0) {
|
||||||
|
LOGE("Failed to open output file: %s", recorder->filename);
|
||||||
|
// ostream will be cleaned up during context cleaning
|
||||||
|
avformat_free_context(recorder->ctx);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGD("Starting recorder thread");
|
||||||
bool ok = sc_thread_create(&recorder->thread, run_recorder, "recorder",
|
bool ok = sc_thread_create(&recorder->thread, run_recorder, "recorder",
|
||||||
recorder);
|
recorder);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGC("Could not start recorder thread");
|
LOGC("Could not start recorder thread");
|
||||||
|
avformat_free_context(recorder->ctx);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOGI("Recording started to %s file: %s", format_name, recorder->filename);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
recorder_stop(struct recorder *recorder) {
|
recorder_close(struct recorder *recorder) {
|
||||||
sc_mutex_lock(&recorder->mutex);
|
sc_mutex_lock(&recorder->mutex);
|
||||||
recorder->stopped = true;
|
recorder->stopped = true;
|
||||||
sc_cond_signal(&recorder->queue_cond);
|
sc_cond_signal(&recorder->queue_cond);
|
||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
recorder_join(struct recorder *recorder) {
|
|
||||||
sc_thread_join(&recorder->thread, NULL);
|
sc_thread_join(&recorder->thread, NULL);
|
||||||
|
|
||||||
|
avio_close(recorder->ctx->pb);
|
||||||
|
avformat_free_context(recorder->ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
static bool
|
||||||
recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
||||||
sc_mutex_lock(&recorder->mutex);
|
sc_mutex_lock(&recorder->mutex);
|
||||||
assert(!recorder->stopped);
|
assert(!recorder->stopped);
|
||||||
@ -376,3 +313,73 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
|||||||
sc_mutex_unlock(&recorder->mutex);
|
sc_mutex_unlock(&recorder->mutex);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
recorder_packet_sink_open(struct sc_packet_sink *sink, const AVCodec *codec) {
|
||||||
|
struct recorder *recorder = DOWNCAST(sink);
|
||||||
|
return recorder_open(recorder, codec);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
recorder_packet_sink_close(struct sc_packet_sink *sink) {
|
||||||
|
struct recorder *recorder = DOWNCAST(sink);
|
||||||
|
recorder_close(recorder);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
recorder_packet_sink_push(struct sc_packet_sink *sink, const AVPacket *packet) {
|
||||||
|
struct recorder *recorder = DOWNCAST(sink);
|
||||||
|
return recorder_push(recorder, packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
recorder_init(struct recorder *recorder,
|
||||||
|
const char *filename,
|
||||||
|
enum sc_record_format format,
|
||||||
|
struct size declared_frame_size) {
|
||||||
|
recorder->filename = strdup(filename);
|
||||||
|
if (!recorder->filename) {
|
||||||
|
LOGE("Could not strdup filename");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ok = sc_mutex_init(&recorder->mutex);
|
||||||
|
if (!ok) {
|
||||||
|
LOGC("Could not create mutex");
|
||||||
|
free(recorder->filename);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ok = sc_cond_init(&recorder->queue_cond);
|
||||||
|
if (!ok) {
|
||||||
|
LOGC("Could not create cond");
|
||||||
|
sc_mutex_destroy(&recorder->mutex);
|
||||||
|
free(recorder->filename);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_init(&recorder->queue);
|
||||||
|
recorder->stopped = false;
|
||||||
|
recorder->failed = false;
|
||||||
|
recorder->format = format;
|
||||||
|
recorder->declared_frame_size = declared_frame_size;
|
||||||
|
recorder->header_written = false;
|
||||||
|
recorder->previous = NULL;
|
||||||
|
|
||||||
|
static const struct sc_packet_sink_ops ops = {
|
||||||
|
.open = recorder_packet_sink_open,
|
||||||
|
.close = recorder_packet_sink_close,
|
||||||
|
.push = recorder_packet_sink_push,
|
||||||
|
};
|
||||||
|
|
||||||
|
recorder->packet_sink.ops = &ops;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
recorder_destroy(struct recorder *recorder) {
|
||||||
|
sc_cond_destroy(&recorder->queue_cond);
|
||||||
|
sc_mutex_destroy(&recorder->mutex);
|
||||||
|
free(recorder->filename);
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "coords.h"
|
#include "coords.h"
|
||||||
#include "scrcpy.h"
|
#include "scrcpy.h"
|
||||||
|
#include "trait/packet_sink.h"
|
||||||
#include "util/queue.h"
|
#include "util/queue.h"
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
|
|
||||||
@ -19,6 +20,8 @@ struct record_packet {
|
|||||||
struct recorder_queue QUEUE(struct record_packet);
|
struct recorder_queue QUEUE(struct record_packet);
|
||||||
|
|
||||||
struct recorder {
|
struct recorder {
|
||||||
|
struct sc_packet_sink packet_sink; // packet sink trait
|
||||||
|
|
||||||
char *filename;
|
char *filename;
|
||||||
enum sc_record_format format;
|
enum sc_record_format format;
|
||||||
AVFormatContext *ctx;
|
AVFormatContext *ctx;
|
||||||
@ -28,7 +31,7 @@ struct recorder {
|
|||||||
sc_thread thread;
|
sc_thread thread;
|
||||||
sc_mutex mutex;
|
sc_mutex mutex;
|
||||||
sc_cond queue_cond;
|
sc_cond queue_cond;
|
||||||
bool stopped; // set on recorder_stop() by the stream reader
|
bool stopped; // set on recorder_close()
|
||||||
bool failed; // set on packet write failure
|
bool failed; // set on packet write failure
|
||||||
struct recorder_queue queue;
|
struct recorder_queue queue;
|
||||||
|
|
||||||
@ -46,22 +49,4 @@ recorder_init(struct recorder *recorder, const char *filename,
|
|||||||
void
|
void
|
||||||
recorder_destroy(struct recorder *recorder);
|
recorder_destroy(struct recorder *recorder);
|
||||||
|
|
||||||
bool
|
|
||||||
recorder_open(struct recorder *recorder, const AVCodec *input_codec);
|
|
||||||
|
|
||||||
void
|
|
||||||
recorder_close(struct recorder *recorder);
|
|
||||||
|
|
||||||
bool
|
|
||||||
recorder_start(struct recorder *recorder);
|
|
||||||
|
|
||||||
void
|
|
||||||
recorder_stop(struct recorder *recorder);
|
|
||||||
|
|
||||||
void
|
|
||||||
recorder_join(struct recorder *recorder);
|
|
||||||
|
|
||||||
bool
|
|
||||||
recorder_push(struct recorder *recorder, const AVPacket *packet);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -25,14 +25,12 @@
|
|||||||
#include "server.h"
|
#include "server.h"
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
#include "tiny_xpm.h"
|
#include "tiny_xpm.h"
|
||||||
#include "video_buffer.h"
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/net.h"
|
#include "util/net.h"
|
||||||
|
|
||||||
static struct server server;
|
static struct server server;
|
||||||
static struct screen screen;
|
static struct screen screen;
|
||||||
static struct fps_counter fps_counter;
|
static struct fps_counter fps_counter;
|
||||||
static struct video_buffer video_buffer;
|
|
||||||
static struct stream stream;
|
static struct stream stream;
|
||||||
static struct decoder decoder;
|
static struct decoder decoder;
|
||||||
static struct recorder recorder;
|
static struct recorder recorder;
|
||||||
@ -247,7 +245,6 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
|
|
||||||
bool server_started = false;
|
bool server_started = false;
|
||||||
bool fps_counter_initialized = false;
|
bool fps_counter_initialized = false;
|
||||||
bool video_buffer_initialized = false;
|
|
||||||
bool file_handler_initialized = false;
|
bool file_handler_initialized = false;
|
||||||
bool recorder_initialized = false;
|
bool recorder_initialized = false;
|
||||||
bool stream_started = false;
|
bool stream_started = false;
|
||||||
@ -305,11 +302,6 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
}
|
}
|
||||||
fps_counter_initialized = true;
|
fps_counter_initialized = true;
|
||||||
|
|
||||||
if (!video_buffer_init(&video_buffer, options->render_expired_frames)) {
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
video_buffer_initialized = true;
|
|
||||||
|
|
||||||
if (options->control) {
|
if (options->control) {
|
||||||
if (!file_handler_init(&file_handler, server.serial,
|
if (!file_handler_init(&file_handler, server.serial,
|
||||||
options->push_target)) {
|
options->push_target)) {
|
||||||
@ -318,7 +310,7 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
file_handler_initialized = true;
|
file_handler_initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder_init(&decoder, &video_buffer);
|
decoder_init(&decoder);
|
||||||
dec = &decoder;
|
dec = &decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,7 +328,15 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
|
|
||||||
av_log_set_callback(av_log_callback);
|
av_log_set_callback(av_log_callback);
|
||||||
|
|
||||||
stream_init(&stream, server.video_socket, dec, rec);
|
stream_init(&stream, server.video_socket);
|
||||||
|
|
||||||
|
if (dec) {
|
||||||
|
stream_add_sink(&stream, &dec->packet_sink);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rec) {
|
||||||
|
stream_add_sink(&stream, &rec->packet_sink);
|
||||||
|
}
|
||||||
|
|
||||||
if (options->display) {
|
if (options->display) {
|
||||||
if (options->control) {
|
if (options->control) {
|
||||||
@ -368,12 +368,13 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
.fullscreen = options->fullscreen,
|
.fullscreen = options->fullscreen,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!screen_init(&screen, &video_buffer, &fps_counter,
|
if (!screen_init(&screen, &fps_counter, &screen_params)) {
|
||||||
&screen_params)) {
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
screen_initialized = true;
|
screen_initialized = true;
|
||||||
|
|
||||||
|
decoder_add_sink(&decoder, &screen.frame_sink);
|
||||||
|
|
||||||
if (options->turn_screen_off) {
|
if (options->turn_screen_off) {
|
||||||
struct control_msg msg;
|
struct control_msg msg;
|
||||||
msg.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
|
msg.type = CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE;
|
||||||
@ -397,12 +398,13 @@ scrcpy(const struct scrcpy_options *options) {
|
|||||||
ret = event_loop(options);
|
ret = event_loop(options);
|
||||||
LOGD("quit...");
|
LOGD("quit...");
|
||||||
|
|
||||||
|
// Close the window immediately on closing, because screen_destroy() may
|
||||||
|
// only be called once the stream thread is joined (it may take time)
|
||||||
|
screen_hide_window(&screen);
|
||||||
|
|
||||||
end:
|
end:
|
||||||
// stop stream and controller so that they don't continue once their socket
|
// The stream is not stopped explicitly, because it will stop by itself on
|
||||||
// is shutdown
|
// end-of-stream
|
||||||
if (stream_started) {
|
|
||||||
stream_stop(&stream);
|
|
||||||
}
|
|
||||||
if (controller_started) {
|
if (controller_started) {
|
||||||
controller_stop(&controller);
|
controller_stop(&controller);
|
||||||
}
|
}
|
||||||
@ -446,10 +448,6 @@ end:
|
|||||||
file_handler_destroy(&file_handler);
|
file_handler_destroy(&file_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (video_buffer_initialized) {
|
|
||||||
video_buffer_destroy(&video_buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fps_counter_initialized) {
|
if (fps_counter_initialized) {
|
||||||
fps_counter_join(&fps_counter);
|
fps_counter_join(&fps_counter);
|
||||||
fps_counter_destroy(&fps_counter);
|
fps_counter_destroy(&fps_counter);
|
||||||
|
@ -72,7 +72,6 @@ struct scrcpy_options {
|
|||||||
bool control;
|
bool control;
|
||||||
bool display;
|
bool display;
|
||||||
bool turn_screen_off;
|
bool turn_screen_off;
|
||||||
bool render_expired_frames;
|
|
||||||
bool prefer_text;
|
bool prefer_text;
|
||||||
bool window_borderless;
|
bool window_borderless;
|
||||||
bool mipmaps;
|
bool mipmaps;
|
||||||
@ -120,7 +119,6 @@ struct scrcpy_options {
|
|||||||
.control = true, \
|
.control = true, \
|
||||||
.display = true, \
|
.display = true, \
|
||||||
.turn_screen_off = false, \
|
.turn_screen_off = false, \
|
||||||
.render_expired_frames = false, \
|
|
||||||
.prefer_text = false, \
|
.prefer_text = false, \
|
||||||
.window_borderless = false, \
|
.window_borderless = false, \
|
||||||
.mipmaps = true, \
|
.mipmaps = true, \
|
||||||
|
124
app/src/screen.c
124
app/src/screen.c
@ -13,6 +13,8 @@
|
|||||||
|
|
||||||
#define DISPLAY_MARGINS 96
|
#define DISPLAY_MARGINS 96
|
||||||
|
|
||||||
|
#define DOWNCAST(SINK) container_of(SINK, struct screen, frame_sink)
|
||||||
|
|
||||||
static inline struct size
|
static inline struct size
|
||||||
get_rotated_size(struct size size, int rotation) {
|
get_rotated_size(struct size size, int rotation) {
|
||||||
struct size rotated_size;
|
struct size rotated_size;
|
||||||
@ -191,27 +193,6 @@ screen_update_content_rect(struct screen *screen) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
on_frame_available(struct video_buffer *vb, void *userdata) {
|
|
||||||
(void) vb;
|
|
||||||
(void) userdata;
|
|
||||||
|
|
||||||
static SDL_Event new_frame_event = {
|
|
||||||
.type = EVENT_NEW_FRAME,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Post the event on the UI thread
|
|
||||||
SDL_PushEvent(&new_frame_event);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
on_frame_skipped(struct video_buffer *vb, void *userdata) {
|
|
||||||
(void) vb;
|
|
||||||
|
|
||||||
struct screen *screen = userdata;
|
|
||||||
fps_counter_add_skipped_frame(screen->fps_counter);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline SDL_Texture *
|
static inline SDL_Texture *
|
||||||
create_texture(struct screen *screen) {
|
create_texture(struct screen *screen) {
|
||||||
SDL_Renderer *renderer = screen->renderer;
|
SDL_Renderer *renderer = screen->renderer;
|
||||||
@ -262,11 +243,58 @@ event_watcher(void *data, SDL_Event *event) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static bool
|
||||||
|
screen_frame_sink_open(struct sc_frame_sink *sink) {
|
||||||
|
struct screen *screen = DOWNCAST(sink);
|
||||||
|
(void) screen;
|
||||||
|
#ifndef NDEBUG
|
||||||
|
screen->open = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// nothing to do, the screen is already open on the main thread
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
screen_frame_sink_close(struct sc_frame_sink *sink) {
|
||||||
|
struct screen *screen = DOWNCAST(sink);
|
||||||
|
(void) screen;
|
||||||
|
#ifndef NDEBUG
|
||||||
|
screen->open = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// nothing to do, the screen lifecycle is not managed by the frame producer
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
screen_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
|
||||||
|
struct screen *screen = DOWNCAST(sink);
|
||||||
|
|
||||||
|
bool previous_frame_skipped;
|
||||||
|
bool ok = video_buffer_push(&screen->vb, frame, &previous_frame_skipped);
|
||||||
|
if (!ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (previous_frame_skipped) {
|
||||||
|
fps_counter_add_skipped_frame(screen->fps_counter);
|
||||||
|
// The EVENT_NEW_FRAME triggered for the previous frame will consume
|
||||||
|
// this new frame instead
|
||||||
|
} else {
|
||||||
|
static SDL_Event new_frame_event = {
|
||||||
|
.type = EVENT_NEW_FRAME,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Post the event on the UI thread
|
||||||
|
SDL_PushEvent(&new_frame_event);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
screen_init(struct screen *screen, struct video_buffer *vb,
|
screen_init(struct screen *screen, struct fps_counter *fps_counter,
|
||||||
struct fps_counter *fps_counter,
|
|
||||||
const struct screen_params *params) {
|
const struct screen_params *params) {
|
||||||
screen->vb = vb;
|
|
||||||
screen->fps_counter = fps_counter;
|
screen->fps_counter = fps_counter;
|
||||||
|
|
||||||
screen->resize_pending = false;
|
screen->resize_pending = false;
|
||||||
@ -274,11 +302,11 @@ screen_init(struct screen *screen, struct video_buffer *vb,
|
|||||||
screen->fullscreen = false;
|
screen->fullscreen = false;
|
||||||
screen->maximized = false;
|
screen->maximized = false;
|
||||||
|
|
||||||
static const struct video_buffer_callbacks cbs = {
|
bool ok = video_buffer_init(&screen->vb);
|
||||||
.on_frame_available = on_frame_available,
|
if (!ok) {
|
||||||
.on_frame_skipped = on_frame_skipped,
|
LOGE("Could not initialize video buffer");
|
||||||
};
|
return false;
|
||||||
video_buffer_set_consumer_callbacks(vb, &cbs, screen);
|
}
|
||||||
|
|
||||||
screen->frame_size = params->frame_size;
|
screen->frame_size = params->frame_size;
|
||||||
screen->rotation = params->rotation;
|
screen->rotation = params->rotation;
|
||||||
@ -324,6 +352,7 @@ screen_init(struct screen *screen, struct video_buffer *vb,
|
|||||||
if (!screen->renderer) {
|
if (!screen->renderer) {
|
||||||
LOGC("Could not create renderer: %s", SDL_GetError());
|
LOGC("Could not create renderer: %s", SDL_GetError());
|
||||||
SDL_DestroyWindow(screen->window);
|
SDL_DestroyWindow(screen->window);
|
||||||
|
video_buffer_destroy(&screen->vb);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -375,6 +404,17 @@ screen_init(struct screen *screen, struct video_buffer *vb,
|
|||||||
LOGC("Could not create texture: %s", SDL_GetError());
|
LOGC("Could not create texture: %s", SDL_GetError());
|
||||||
SDL_DestroyRenderer(screen->renderer);
|
SDL_DestroyRenderer(screen->renderer);
|
||||||
SDL_DestroyWindow(screen->window);
|
SDL_DestroyWindow(screen->window);
|
||||||
|
video_buffer_destroy(&screen->vb);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
screen->frame = av_frame_alloc();
|
||||||
|
if (!screen->frame) {
|
||||||
|
LOGC("Could not create screen frame");
|
||||||
|
SDL_DestroyTexture(screen->texture);
|
||||||
|
SDL_DestroyRenderer(screen->renderer);
|
||||||
|
SDL_DestroyWindow(screen->window);
|
||||||
|
video_buffer_destroy(&screen->vb);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -393,6 +433,18 @@ screen_init(struct screen *screen, struct video_buffer *vb,
|
|||||||
SDL_AddEventWatch(event_watcher, screen);
|
SDL_AddEventWatch(event_watcher, screen);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static const struct sc_frame_sink_ops ops = {
|
||||||
|
.open = screen_frame_sink_open,
|
||||||
|
.close = screen_frame_sink_close,
|
||||||
|
.push = screen_frame_sink_push,
|
||||||
|
};
|
||||||
|
|
||||||
|
screen->frame_sink.ops = &ops;
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
screen->open = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,11 +453,21 @@ screen_show_window(struct screen *screen) {
|
|||||||
SDL_ShowWindow(screen->window);
|
SDL_ShowWindow(screen->window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
screen_hide_window(struct screen *screen) {
|
||||||
|
SDL_HideWindow(screen->window);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
screen_destroy(struct screen *screen) {
|
screen_destroy(struct screen *screen) {
|
||||||
|
#ifndef NDEBUG
|
||||||
|
assert(!screen->open);
|
||||||
|
#endif
|
||||||
|
av_frame_free(&screen->frame);
|
||||||
SDL_DestroyTexture(screen->texture);
|
SDL_DestroyTexture(screen->texture);
|
||||||
SDL_DestroyRenderer(screen->renderer);
|
SDL_DestroyRenderer(screen->renderer);
|
||||||
SDL_DestroyWindow(screen->window);
|
SDL_DestroyWindow(screen->window);
|
||||||
|
video_buffer_destroy(&screen->vb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -510,7 +572,9 @@ update_texture(struct screen *screen, const AVFrame *frame) {
|
|||||||
|
|
||||||
static bool
|
static bool
|
||||||
screen_update_frame(struct screen *screen) {
|
screen_update_frame(struct screen *screen) {
|
||||||
const AVFrame *frame = video_buffer_consumer_take_frame(screen->vb);
|
av_frame_unref(screen->frame);
|
||||||
|
video_buffer_consume(&screen->vb, screen->frame);
|
||||||
|
AVFrame *frame = screen->frame;
|
||||||
|
|
||||||
fps_counter_add_rendered_frame(screen->fps_counter);
|
fps_counter_add_rendered_frame(screen->fps_counter);
|
||||||
|
|
||||||
|
@ -9,11 +9,19 @@
|
|||||||
|
|
||||||
#include "coords.h"
|
#include "coords.h"
|
||||||
#include "opengl.h"
|
#include "opengl.h"
|
||||||
|
#include "trait/frame_sink.h"
|
||||||
|
#include "video_buffer.h"
|
||||||
|
|
||||||
struct video_buffer;
|
struct video_buffer;
|
||||||
|
|
||||||
struct screen {
|
struct screen {
|
||||||
struct video_buffer *vb;
|
struct sc_frame_sink frame_sink; // frame sink trait
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
bool open; // track the open/close state to assert correct behavior
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct video_buffer vb;
|
||||||
struct fps_counter *fps_counter;
|
struct fps_counter *fps_counter;
|
||||||
|
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
@ -36,6 +44,8 @@ struct screen {
|
|||||||
bool fullscreen;
|
bool fullscreen;
|
||||||
bool maximized;
|
bool maximized;
|
||||||
bool mipmaps;
|
bool mipmaps;
|
||||||
|
|
||||||
|
AVFrame *frame;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct screen_params {
|
struct screen_params {
|
||||||
@ -58,14 +68,20 @@ struct screen_params {
|
|||||||
|
|
||||||
// initialize screen, create window, renderer and texture (window is hidden)
|
// initialize screen, create window, renderer and texture (window is hidden)
|
||||||
bool
|
bool
|
||||||
screen_init(struct screen *screen, struct video_buffer *vb,
|
screen_init(struct screen *screen, struct fps_counter *fps_counter,
|
||||||
struct fps_counter *fps_counter,
|
|
||||||
const struct screen_params *params);
|
const struct screen_params *params);
|
||||||
|
|
||||||
// destroy window, renderer and texture (if any)
|
// destroy window, renderer and texture (if any)
|
||||||
void
|
void
|
||||||
screen_destroy(struct screen *screen);
|
screen_destroy(struct screen *screen);
|
||||||
|
|
||||||
|
// hide the window
|
||||||
|
//
|
||||||
|
// It is used to hide the window immediately on closing without waiting for
|
||||||
|
// screen_destroy()
|
||||||
|
void
|
||||||
|
screen_hide_window(struct screen *screen);
|
||||||
|
|
||||||
// render the texture to the renderer
|
// render the texture to the renderer
|
||||||
//
|
//
|
||||||
// Set the update_content_rect flag if the window or content size may have
|
// Set the update_content_rect flag if the window or content size may have
|
||||||
|
114
app/src/stream.c
114
app/src/stream.c
@ -66,25 +66,11 @@ notify_stopped(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
process_config_packet(struct stream *stream, AVPacket *packet) {
|
push_packet_to_sinks(struct stream *stream, const AVPacket *packet) {
|
||||||
if (stream->recorder && !recorder_push(stream->recorder, packet)) {
|
for (unsigned i = 0; i < stream->sink_count; ++i) {
|
||||||
LOGE("Could not send config packet to recorder");
|
struct sc_packet_sink *sink = stream->sinks[i];
|
||||||
return false;
|
if (!sink->ops->push(sink, packet)) {
|
||||||
}
|
LOGE("Could not send config packet to sink %d", i);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
process_frame(struct stream *stream, AVPacket *packet) {
|
|
||||||
if (stream->decoder && !decoder_push(stream->decoder, packet)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream->recorder) {
|
|
||||||
packet->dts = packet->pts;
|
|
||||||
|
|
||||||
if (!recorder_push(stream->recorder, packet)) {
|
|
||||||
LOGE("Could not send packet to recorder");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,9 +97,11 @@ stream_parse(struct stream *stream, AVPacket *packet) {
|
|||||||
packet->flags |= AV_PKT_FLAG_KEY;
|
packet->flags |= AV_PKT_FLAG_KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok = process_frame(stream, packet);
|
packet->dts = packet->pts;
|
||||||
|
|
||||||
|
bool ok = push_packet_to_sinks(stream, packet);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
LOGE("Could not process frame");
|
LOGE("Could not process packet");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +144,7 @@ stream_push_packet(struct stream *stream, AVPacket *packet) {
|
|||||||
|
|
||||||
if (is_config) {
|
if (is_config) {
|
||||||
// config packet
|
// config packet
|
||||||
bool ok = process_config_packet(stream, packet);
|
bool ok = push_packet_to_sinks(stream, packet);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -177,6 +165,33 @@ stream_push_packet(struct stream *stream, AVPacket *packet) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
stream_close_first_sinks(struct stream *stream, unsigned count) {
|
||||||
|
while (count) {
|
||||||
|
struct sc_packet_sink *sink = stream->sinks[--count];
|
||||||
|
sink->ops->close(sink);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
stream_close_sinks(struct stream *stream) {
|
||||||
|
stream_close_first_sinks(stream, stream->sink_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
stream_open_sinks(struct stream *stream, const AVCodec *codec) {
|
||||||
|
for (unsigned i = 0; i < stream->sink_count; ++i) {
|
||||||
|
struct sc_packet_sink *sink = stream->sinks[i];
|
||||||
|
if (!sink->ops->open(sink, codec)) {
|
||||||
|
LOGE("Could not open packet sink %d", i);
|
||||||
|
stream_close_first_sinks(stream, i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
run_stream(void *data) {
|
run_stream(void *data) {
|
||||||
struct stream *stream = data;
|
struct stream *stream = data;
|
||||||
@ -193,27 +208,15 @@ run_stream(void *data) {
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stream->decoder && !decoder_open(stream->decoder, codec)) {
|
if (!stream_open_sinks(stream, codec)) {
|
||||||
LOGE("Could not open decoder");
|
LOGE("Could not open stream sinks");
|
||||||
goto finally_free_codec_ctx;
|
goto finally_free_codec_ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stream->recorder) {
|
|
||||||
if (!recorder_open(stream->recorder, codec)) {
|
|
||||||
LOGE("Could not open recorder");
|
|
||||||
goto finally_close_decoder;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!recorder_start(stream->recorder)) {
|
|
||||||
LOGE("Could not start recorder");
|
|
||||||
goto finally_close_recorder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stream->parser = av_parser_init(AV_CODEC_ID_H264);
|
stream->parser = av_parser_init(AV_CODEC_ID_H264);
|
||||||
if (!stream->parser) {
|
if (!stream->parser) {
|
||||||
LOGE("Could not initialize parser");
|
LOGE("Could not initialize parser");
|
||||||
goto finally_stop_and_join_recorder;
|
goto finally_close_sinks;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We must only pass complete frames to av_parser_parse2()!
|
// We must only pass complete frames to av_parser_parse2()!
|
||||||
@ -243,20 +246,8 @@ run_stream(void *data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
av_parser_close(stream->parser);
|
av_parser_close(stream->parser);
|
||||||
finally_stop_and_join_recorder:
|
finally_close_sinks:
|
||||||
if (stream->recorder) {
|
stream_close_sinks(stream);
|
||||||
recorder_stop(stream->recorder);
|
|
||||||
LOGI("Finishing recording...");
|
|
||||||
recorder_join(stream->recorder);
|
|
||||||
}
|
|
||||||
finally_close_recorder:
|
|
||||||
if (stream->recorder) {
|
|
||||||
recorder_close(stream->recorder);
|
|
||||||
}
|
|
||||||
finally_close_decoder:
|
|
||||||
if (stream->decoder) {
|
|
||||||
decoder_close(stream->decoder);
|
|
||||||
}
|
|
||||||
finally_free_codec_ctx:
|
finally_free_codec_ctx:
|
||||||
avcodec_free_context(&stream->codec_ctx);
|
avcodec_free_context(&stream->codec_ctx);
|
||||||
end:
|
end:
|
||||||
@ -265,12 +256,18 @@ end:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
stream_init(struct stream *stream, socket_t socket,
|
stream_init(struct stream *stream, socket_t socket) {
|
||||||
struct decoder *decoder, struct recorder *recorder) {
|
|
||||||
stream->socket = socket;
|
stream->socket = socket;
|
||||||
stream->decoder = decoder,
|
|
||||||
stream->recorder = recorder;
|
|
||||||
stream->has_pending = false;
|
stream->has_pending = false;
|
||||||
|
stream->sink_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
stream_add_sink(struct stream *stream, struct sc_packet_sink *sink) {
|
||||||
|
assert(stream->sink_count < STREAM_MAX_SINKS);
|
||||||
|
assert(sink);
|
||||||
|
assert(sink->ops);
|
||||||
|
stream->sinks[stream->sink_count++] = sink;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -285,13 +282,6 @@ stream_start(struct stream *stream) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
stream_stop(struct stream *stream) {
|
|
||||||
if (stream->decoder) {
|
|
||||||
decoder_interrupt(stream->decoder);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
stream_join(struct stream *stream) {
|
stream_join(struct stream *stream) {
|
||||||
sc_thread_join(&stream->thread, NULL);
|
sc_thread_join(&stream->thread, NULL);
|
||||||
|
@ -8,14 +8,19 @@
|
|||||||
#include <libavformat/avformat.h>
|
#include <libavformat/avformat.h>
|
||||||
#include <SDL2/SDL_atomic.h>
|
#include <SDL2/SDL_atomic.h>
|
||||||
|
|
||||||
|
#include "trait/packet_sink.h"
|
||||||
#include "util/net.h"
|
#include "util/net.h"
|
||||||
#include "util/thread.h"
|
#include "util/thread.h"
|
||||||
|
|
||||||
|
#define STREAM_MAX_SINKS 2
|
||||||
|
|
||||||
struct stream {
|
struct stream {
|
||||||
socket_t socket;
|
socket_t socket;
|
||||||
sc_thread thread;
|
sc_thread thread;
|
||||||
struct decoder *decoder;
|
|
||||||
struct recorder *recorder;
|
struct sc_packet_sink *sinks[STREAM_MAX_SINKS];
|
||||||
|
unsigned sink_count;
|
||||||
|
|
||||||
AVCodecContext *codec_ctx;
|
AVCodecContext *codec_ctx;
|
||||||
AVCodecParserContext *parser;
|
AVCodecParserContext *parser;
|
||||||
// successive packets may need to be concatenated, until a non-config
|
// successive packets may need to be concatenated, until a non-config
|
||||||
@ -25,15 +30,14 @@ struct stream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
stream_init(struct stream *stream, socket_t socket,
|
stream_init(struct stream *stream, socket_t socket);
|
||||||
struct decoder *decoder, struct recorder *recorder);
|
|
||||||
|
void
|
||||||
|
stream_add_sink(struct stream *stream, struct sc_packet_sink *sink);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
stream_start(struct stream *stream);
|
stream_start(struct stream *stream);
|
||||||
|
|
||||||
void
|
|
||||||
stream_stop(struct stream *stream);
|
|
||||||
|
|
||||||
void
|
void
|
||||||
stream_join(struct stream *stream);
|
stream_join(struct stream *stream);
|
||||||
|
|
||||||
|
24
app/src/trait/frame_sink.h
Normal file
24
app/src/trait/frame_sink.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#ifndef SC_FRAME_SINK
|
||||||
|
#define SC_FRAME_SINK
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frame sink trait.
|
||||||
|
*
|
||||||
|
* Component able to receive AVFrames should implement this trait.
|
||||||
|
*/
|
||||||
|
struct sc_frame_sink {
|
||||||
|
const struct sc_frame_sink_ops *ops;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sc_frame_sink_ops {
|
||||||
|
bool (*open)(struct sc_frame_sink *sink);
|
||||||
|
void (*close)(struct sc_frame_sink *sink);
|
||||||
|
bool (*push)(struct sc_frame_sink *sink, const AVFrame *frame);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
25
app/src/trait/packet_sink.h
Normal file
25
app/src/trait/packet_sink.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#ifndef SC_PACKET_SINK
|
||||||
|
#define SC_PACKET_SINK
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <libavcodec/avcodec.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Packet sink trait.
|
||||||
|
*
|
||||||
|
* Component able to receive AVPackets should implement this trait.
|
||||||
|
*/
|
||||||
|
struct sc_packet_sink {
|
||||||
|
const struct sc_packet_sink_ops *ops;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sc_packet_sink_ops {
|
||||||
|
bool (*open)(struct sc_packet_sink *sink, const AVCodec *codec);
|
||||||
|
void (*close)(struct sc_packet_sink *sink);
|
||||||
|
bool (*push)(struct sc_packet_sink *sink, const AVPacket *packet);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -7,67 +7,36 @@
|
|||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
bool
|
bool
|
||||||
video_buffer_init(struct video_buffer *vb, bool wait_consumer) {
|
video_buffer_init(struct video_buffer *vb) {
|
||||||
vb->producer_frame = av_frame_alloc();
|
|
||||||
if (!vb->producer_frame) {
|
|
||||||
goto error_0;
|
|
||||||
}
|
|
||||||
|
|
||||||
vb->pending_frame = av_frame_alloc();
|
vb->pending_frame = av_frame_alloc();
|
||||||
if (!vb->pending_frame) {
|
if (!vb->pending_frame) {
|
||||||
goto error_1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
vb->consumer_frame = av_frame_alloc();
|
vb->tmp_frame = av_frame_alloc();
|
||||||
if (!vb->consumer_frame) {
|
if (!vb->tmp_frame) {
|
||||||
goto error_2;
|
av_frame_free(&vb->pending_frame);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok = sc_mutex_init(&vb->mutex);
|
bool ok = sc_mutex_init(&vb->mutex);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
goto error_3;
|
av_frame_free(&vb->pending_frame);
|
||||||
}
|
av_frame_free(&vb->tmp_frame);
|
||||||
|
return false;
|
||||||
vb->wait_consumer = wait_consumer;
|
|
||||||
if (wait_consumer) {
|
|
||||||
ok = sc_cond_init(&vb->pending_frame_consumed_cond);
|
|
||||||
if (!ok) {
|
|
||||||
sc_mutex_destroy(&vb->mutex);
|
|
||||||
goto error_2;
|
|
||||||
}
|
|
||||||
// interrupted is not used if wait_consumer is disabled since offering
|
|
||||||
// a frame will never block
|
|
||||||
vb->interrupted = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// there is initially no frame, so consider it has already been consumed
|
// there is initially no frame, so consider it has already been consumed
|
||||||
vb->pending_frame_consumed = true;
|
vb->pending_frame_consumed = true;
|
||||||
|
|
||||||
// The callbacks must be set by the consumer via
|
|
||||||
// video_buffer_set_consumer_callbacks()
|
|
||||||
vb->cbs = NULL;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
error_3:
|
|
||||||
av_frame_free(&vb->consumer_frame);
|
|
||||||
error_2:
|
|
||||||
av_frame_free(&vb->pending_frame);
|
|
||||||
error_1:
|
|
||||||
av_frame_free(&vb->producer_frame);
|
|
||||||
error_0:
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
video_buffer_destroy(struct video_buffer *vb) {
|
video_buffer_destroy(struct video_buffer *vb) {
|
||||||
if (vb->wait_consumer) {
|
|
||||||
sc_cond_destroy(&vb->pending_frame_consumed_cond);
|
|
||||||
}
|
|
||||||
sc_mutex_destroy(&vb->mutex);
|
sc_mutex_destroy(&vb->mutex);
|
||||||
av_frame_free(&vb->consumer_frame);
|
|
||||||
av_frame_free(&vb->pending_frame);
|
av_frame_free(&vb->pending_frame);
|
||||||
av_frame_free(&vb->producer_frame);
|
av_frame_free(&vb->tmp_frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
@ -77,71 +46,43 @@ swap_frames(AVFrame **lhs, AVFrame **rhs) {
|
|||||||
*rhs = tmp;
|
*rhs = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
bool
|
||||||
video_buffer_set_consumer_callbacks(struct video_buffer *vb,
|
video_buffer_push(struct video_buffer *vb, const AVFrame *frame,
|
||||||
const struct video_buffer_callbacks *cbs,
|
bool *previous_frame_skipped) {
|
||||||
void *cbs_userdata) {
|
|
||||||
assert(!vb->cbs); // must be set only once
|
|
||||||
assert(cbs);
|
|
||||||
assert(cbs->on_frame_available);
|
|
||||||
vb->cbs = cbs;
|
|
||||||
vb->cbs_userdata = cbs_userdata;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
video_buffer_producer_offer_frame(struct video_buffer *vb) {
|
|
||||||
assert(vb->cbs);
|
|
||||||
|
|
||||||
sc_mutex_lock(&vb->mutex);
|
sc_mutex_lock(&vb->mutex);
|
||||||
if (vb->wait_consumer) {
|
|
||||||
// wait for the current (expired) frame to be consumed
|
// Use a temporary frame to preserve pending_frame in case of error.
|
||||||
while (!vb->pending_frame_consumed && !vb->interrupted) {
|
// tmp_frame is an empty frame, no need to call av_frame_unref() beforehand.
|
||||||
sc_cond_wait(&vb->pending_frame_consumed_cond, &vb->mutex);
|
int r = av_frame_ref(vb->tmp_frame, frame);
|
||||||
}
|
if (r) {
|
||||||
|
LOGE("Could not ref frame: %d", r);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
av_frame_unref(vb->pending_frame);
|
// Now that av_frame_ref() succeeded, we can replace the previous
|
||||||
swap_frames(&vb->producer_frame, &vb->pending_frame);
|
// pending_frame
|
||||||
|
swap_frames(&vb->pending_frame, &vb->tmp_frame);
|
||||||
|
av_frame_unref(vb->tmp_frame);
|
||||||
|
|
||||||
bool skipped = !vb->pending_frame_consumed;
|
if (previous_frame_skipped) {
|
||||||
|
*previous_frame_skipped = !vb->pending_frame_consumed;
|
||||||
|
}
|
||||||
vb->pending_frame_consumed = false;
|
vb->pending_frame_consumed = false;
|
||||||
|
|
||||||
sc_mutex_unlock(&vb->mutex);
|
sc_mutex_unlock(&vb->mutex);
|
||||||
|
|
||||||
if (skipped) {
|
return true;
|
||||||
if (vb->cbs->on_frame_skipped)
|
|
||||||
vb->cbs->on_frame_skipped(vb, vb->cbs_userdata);
|
|
||||||
} else {
|
|
||||||
vb->cbs->on_frame_available(vb, vb->cbs_userdata);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const AVFrame *
|
void
|
||||||
video_buffer_consumer_take_frame(struct video_buffer *vb) {
|
video_buffer_consume(struct video_buffer *vb, AVFrame *dst) {
|
||||||
sc_mutex_lock(&vb->mutex);
|
sc_mutex_lock(&vb->mutex);
|
||||||
assert(!vb->pending_frame_consumed);
|
assert(!vb->pending_frame_consumed);
|
||||||
vb->pending_frame_consumed = true;
|
vb->pending_frame_consumed = true;
|
||||||
|
|
||||||
swap_frames(&vb->consumer_frame, &vb->pending_frame);
|
av_frame_move_ref(dst, vb->pending_frame);
|
||||||
av_frame_unref(vb->pending_frame);
|
// av_frame_move_ref() resets its source frame, so no need to call
|
||||||
|
// av_frame_unref()
|
||||||
|
|
||||||
if (vb->wait_consumer) {
|
|
||||||
// unblock video_buffer_offer_decoded_frame()
|
|
||||||
sc_cond_signal(&vb->pending_frame_consumed_cond);
|
|
||||||
}
|
|
||||||
sc_mutex_unlock(&vb->mutex);
|
sc_mutex_unlock(&vb->mutex);
|
||||||
|
|
||||||
// consumer_frame is only written from this thread, no need to lock
|
|
||||||
return vb->consumer_frame;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
video_buffer_interrupt(struct video_buffer *vb) {
|
|
||||||
if (vb->wait_consumer) {
|
|
||||||
sc_mutex_lock(&vb->mutex);
|
|
||||||
vb->interrupted = true;
|
|
||||||
sc_mutex_unlock(&vb->mutex);
|
|
||||||
// wake up blocking wait
|
|
||||||
sc_cond_signal(&vb->pending_frame_consumed_cond);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -12,71 +12,39 @@
|
|||||||
typedef struct AVFrame AVFrame;
|
typedef struct AVFrame AVFrame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* There are 3 frames in memory:
|
* A video buffer holds 1 pending frame, which is the last frame received from
|
||||||
* - one frame is held by the producer (producer_frame)
|
* the producer (typically, the decoder).
|
||||||
* - one frame is held by the consumer (consumer_frame)
|
|
||||||
* - one frame is shared between the producer and the consumer (pending_frame)
|
|
||||||
*
|
*
|
||||||
* The producer generates a frame into the producer_frame (it may takes time).
|
* If a pending frame has not been consumed when the producer pushes a new
|
||||||
|
* frame, then it is lost. The intent is to always provide access to the very
|
||||||
|
* last frame to minimize latency.
|
||||||
*
|
*
|
||||||
* Once the frame is produced, it calls video_buffer_producer_offer_frame(),
|
* The producer and the consumer typically do not live in the same thread.
|
||||||
* which swaps the producer and pending frames.
|
* That's the reason why the callback on_frame_available() does not provide the
|
||||||
*
|
* frame as parameter: the consumer might post an event to its own thread to
|
||||||
* When the consumer is notified that a new frame is available, it calls
|
* retrieve the pending frame from there, and that frame may have changed since
|
||||||
* video_buffer_consumer_take_frame() to retrieve it, which swaps the pending
|
* the callback if producer pushed a new one in between.
|
||||||
* and consumer frames. The frame is valid until the next call, without
|
|
||||||
* blocking the producer.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct video_buffer {
|
struct video_buffer {
|
||||||
AVFrame *producer_frame;
|
|
||||||
AVFrame *pending_frame;
|
AVFrame *pending_frame;
|
||||||
AVFrame *consumer_frame;
|
AVFrame *tmp_frame; // To preserve the pending frame on error
|
||||||
|
|
||||||
sc_mutex mutex;
|
sc_mutex mutex;
|
||||||
bool wait_consumer; // never overwrite a pending frame if it is not consumed
|
|
||||||
bool interrupted;
|
|
||||||
|
|
||||||
sc_cond pending_frame_consumed_cond;
|
|
||||||
bool pending_frame_consumed;
|
bool pending_frame_consumed;
|
||||||
|
|
||||||
const struct video_buffer_callbacks *cbs;
|
|
||||||
void *cbs_userdata;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct video_buffer_callbacks {
|
|
||||||
// Called when a new frame can be consumed by
|
|
||||||
// video_buffer_consumer_take_frame(vb)
|
|
||||||
// This callback is mandatory (it must not be NULL).
|
|
||||||
void (*on_frame_available)(struct video_buffer *vb, void *userdata);
|
|
||||||
|
|
||||||
// Called when a pending frame has been overwritten by the producer
|
|
||||||
// This callback is optional (it may be NULL).
|
|
||||||
void (*on_frame_skipped)(struct video_buffer *vb, void *userdata);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
video_buffer_init(struct video_buffer *vb, bool wait_consumer);
|
video_buffer_init(struct video_buffer *vb);
|
||||||
|
|
||||||
void
|
void
|
||||||
video_buffer_destroy(struct video_buffer *vb);
|
video_buffer_destroy(struct video_buffer *vb);
|
||||||
|
|
||||||
void
|
bool
|
||||||
video_buffer_set_consumer_callbacks(struct video_buffer *vb,
|
video_buffer_push(struct video_buffer *vb, const AVFrame *frame, bool *skipped);
|
||||||
const struct video_buffer_callbacks *cbs,
|
|
||||||
void *cbs_userdata);
|
|
||||||
|
|
||||||
// set the producer frame as ready for consuming
|
|
||||||
void
|
void
|
||||||
video_buffer_producer_offer_frame(struct video_buffer *vb);
|
video_buffer_consume(struct video_buffer *vb, AVFrame *dst);
|
||||||
|
|
||||||
// mark the consumer frame as consumed and return it
|
|
||||||
// the frame is valid until the next call to this function
|
|
||||||
const AVFrame *
|
|
||||||
video_buffer_consumer_take_frame(struct video_buffer *vb);
|
|
||||||
|
|
||||||
// wake up and avoid any blocking call
|
|
||||||
void
|
|
||||||
video_buffer_interrupt(struct video_buffer *vb);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -58,7 +58,6 @@ static void test_options(void) {
|
|||||||
"--push-target", "/sdcard/Movies",
|
"--push-target", "/sdcard/Movies",
|
||||||
"--record", "file",
|
"--record", "file",
|
||||||
"--record-format", "mkv",
|
"--record-format", "mkv",
|
||||||
"--render-expired-frames",
|
|
||||||
"--serial", "0123456789abcdef",
|
"--serial", "0123456789abcdef",
|
||||||
"--show-touches",
|
"--show-touches",
|
||||||
"--turn-screen-off",
|
"--turn-screen-off",
|
||||||
@ -87,7 +86,6 @@ static void test_options(void) {
|
|||||||
assert(!strcmp(opts->push_target, "/sdcard/Movies"));
|
assert(!strcmp(opts->push_target, "/sdcard/Movies"));
|
||||||
assert(!strcmp(opts->record_filename, "file"));
|
assert(!strcmp(opts->record_filename, "file"));
|
||||||
assert(opts->record_format == SC_RECORD_FORMAT_MKV);
|
assert(opts->record_format == SC_RECORD_FORMAT_MKV);
|
||||||
assert(opts->render_expired_frames);
|
|
||||||
assert(!strcmp(opts->serial, "0123456789abcdef"));
|
assert(!strcmp(opts->serial, "0123456789abcdef"));
|
||||||
assert(opts->show_touches);
|
assert(opts->show_touches);
|
||||||
assert(opts->turn_screen_off);
|
assert(opts->turn_screen_off);
|
||||||
|
Reference in New Issue
Block a user