2021-04-04 06:10:44 +08:00
|
|
|
#include "v4l2_sink.h"
|
|
|
|
|
2021-11-25 04:44:29 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
#include "util/log.h"
|
2021-11-13 06:12:51 +08:00
|
|
|
#include "util/str.h"
|
2021-04-04 06:10:44 +08:00
|
|
|
|
|
|
|
/** Downcast frame_sink to sc_v4l2_sink */
|
|
|
|
#define DOWNCAST(SINK) container_of(SINK, struct sc_v4l2_sink, frame_sink)
|
|
|
|
|
|
|
|
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
|
|
|
|
|
|
|
|
static const AVOutputFormat *
|
|
|
|
find_muxer(const char *name) {
|
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API
|
|
|
|
void *opaque = NULL;
|
|
|
|
#endif
|
|
|
|
const AVOutputFormat *oformat = NULL;
|
|
|
|
do {
|
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API
|
|
|
|
oformat = av_muxer_iterate(&opaque);
|
|
|
|
#else
|
|
|
|
oformat = av_oformat_next(oformat);
|
|
|
|
#endif
|
|
|
|
// until null or containing the requested name
|
2021-11-13 06:08:19 +08:00
|
|
|
} while (oformat && !sc_str_list_contains(oformat->name, ',', name));
|
2021-04-04 06:10:44 +08:00
|
|
|
return oformat;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
write_header(struct sc_v4l2_sink *vs, const AVPacket *packet) {
|
|
|
|
AVStream *ostream = vs->format_ctx->streams[0];
|
|
|
|
|
|
|
|
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
|
|
|
if (!extradata) {
|
|
|
|
LOGC("Could not allocate extradata");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the first packet to the extra data
|
|
|
|
memcpy(extradata, packet->data, packet->size);
|
|
|
|
|
|
|
|
ostream->codecpar->extradata = extradata;
|
|
|
|
ostream->codecpar->extradata_size = packet->size;
|
|
|
|
|
|
|
|
int ret = avformat_write_header(vs->format_ctx, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGE("Failed to write header to %s", vs->device_name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rescale_packet(struct sc_v4l2_sink *vs, AVPacket *packet) {
|
|
|
|
AVStream *ostream = vs->format_ctx->streams[0];
|
|
|
|
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
write_packet(struct sc_v4l2_sink *vs, AVPacket *packet) {
|
|
|
|
if (!vs->header_written) {
|
|
|
|
bool ok = write_header(vs, packet);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
vs->header_written = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
rescale_packet(vs, packet);
|
|
|
|
|
|
|
|
bool ok = av_write_frame(vs->format_ctx, packet) >= 0;
|
|
|
|
|
|
|
|
// Failing to write the last frame is not very serious, no future frame may
|
|
|
|
// depend on it, so the resulting file will still be valid
|
|
|
|
(void) ok;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
encode_and_write_frame(struct sc_v4l2_sink *vs, const AVFrame *frame) {
|
|
|
|
int ret = avcodec_send_frame(vs->encoder_ctx, frame);
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN)) {
|
|
|
|
LOGE("Could not send v4l2 video frame: %d", ret);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
AVPacket *packet = vs->packet;
|
2021-04-04 06:10:44 +08:00
|
|
|
ret = avcodec_receive_packet(vs->encoder_ctx, packet);
|
|
|
|
if (ret == 0) {
|
|
|
|
// A packet was received
|
|
|
|
|
|
|
|
bool ok = write_packet(vs, packet);
|
2021-04-27 00:05:11 +08:00
|
|
|
av_packet_unref(packet);
|
2021-04-04 06:10:44 +08:00
|
|
|
if (!ok) {
|
|
|
|
LOGW("Could not send packet to v4l2 sink");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (ret != AVERROR(EAGAIN)) {
|
|
|
|
LOGE("Could not receive v4l2 video packet: %d", ret);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
run_v4l2_sink(void *data) {
|
|
|
|
struct sc_v4l2_sink *vs = data;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
sc_mutex_lock(&vs->mutex);
|
|
|
|
|
2021-06-26 21:53:05 +08:00
|
|
|
while (!vs->stopped && !vs->has_frame) {
|
2021-04-04 06:10:44 +08:00
|
|
|
sc_cond_wait(&vs->cond, &vs->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vs->stopped) {
|
|
|
|
sc_mutex_unlock(&vs->mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-26 21:53:05 +08:00
|
|
|
vs->has_frame = false;
|
2021-04-04 06:10:44 +08:00
|
|
|
sc_mutex_unlock(&vs->mutex);
|
|
|
|
|
2021-06-26 21:02:18 +08:00
|
|
|
sc_video_buffer_consume(&vs->vb, vs->frame);
|
2021-07-04 18:30:49 +08:00
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
bool ok = encode_and_write_frame(vs, vs->frame);
|
2021-04-27 00:00:47 +08:00
|
|
|
av_frame_unref(vs->frame);
|
2021-04-04 06:10:44 +08:00
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not send frame to v4l2 sink");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("V4l2 thread ended");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-04 18:42:22 +08:00
|
|
|
static void
|
|
|
|
sc_video_buffer_on_new_frame(struct sc_video_buffer *vb, bool previous_skipped,
|
|
|
|
void *userdata) {
|
|
|
|
(void) vb;
|
|
|
|
struct sc_v4l2_sink *vs = userdata;
|
|
|
|
|
|
|
|
if (!previous_skipped) {
|
|
|
|
sc_mutex_lock(&vs->mutex);
|
|
|
|
vs->has_frame = true;
|
|
|
|
sc_cond_signal(&vs->cond);
|
|
|
|
sc_mutex_unlock(&vs->mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
static bool
|
|
|
|
sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
|
2021-07-04 18:42:22 +08:00
|
|
|
static const struct sc_video_buffer_callbacks cbs = {
|
|
|
|
.on_new_frame = sc_video_buffer_on_new_frame,
|
|
|
|
};
|
|
|
|
|
2021-07-07 01:04:05 +08:00
|
|
|
bool ok = sc_video_buffer_init(&vs->vb, vs->buffering_time, &cbs, vs);
|
2021-04-04 06:10:44 +08:00
|
|
|
if (!ok) {
|
2021-07-04 21:47:39 +08:00
|
|
|
LOGE("Could not initialize video buffer");
|
2021-04-04 06:10:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-04 21:24:51 +08:00
|
|
|
ok = sc_video_buffer_start(&vs->vb);
|
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not start video buffer");
|
|
|
|
goto error_video_buffer_destroy;
|
|
|
|
}
|
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
ok = sc_mutex_init(&vs->mutex);
|
|
|
|
if (!ok) {
|
|
|
|
LOGC("Could not create mutex");
|
2021-07-04 21:24:51 +08:00
|
|
|
goto error_video_buffer_stop_and_join;
|
2021-04-04 06:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ok = sc_cond_init(&vs->cond);
|
|
|
|
if (!ok) {
|
|
|
|
LOGC("Could not create cond");
|
|
|
|
goto error_mutex_destroy;
|
|
|
|
}
|
|
|
|
|
2021-10-20 19:35:58 +08:00
|
|
|
const AVOutputFormat *format = find_muxer("v4l2");
|
|
|
|
if (!format) {
|
|
|
|
// Alternative name
|
|
|
|
format = find_muxer("video4linux2");
|
|
|
|
}
|
2021-04-04 06:10:44 +08:00
|
|
|
if (!format) {
|
|
|
|
LOGE("Could not find v4l2 muxer");
|
|
|
|
goto error_cond_destroy;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_RAWVIDEO);
|
|
|
|
if (!encoder) {
|
|
|
|
LOGE("Raw video encoder not found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vs->format_ctx = avformat_alloc_context();
|
|
|
|
if (!vs->format_ctx) {
|
|
|
|
LOGE("Could not allocate v4l2 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>
|
|
|
|
vs->format_ctx->oformat = (AVOutputFormat *) format;
|
2021-06-14 00:31:48 +08:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
|
2021-04-04 06:10:44 +08:00
|
|
|
vs->format_ctx->url = strdup(vs->device_name);
|
|
|
|
if (!vs->format_ctx->url) {
|
|
|
|
LOGE("Could not strdup v4l2 device name");
|
|
|
|
goto error_avformat_free_context;
|
|
|
|
}
|
2021-06-14 00:31:48 +08:00
|
|
|
#else
|
|
|
|
strncpy(vs->format_ctx->filename, vs->device_name,
|
|
|
|
sizeof(vs->format_ctx->filename));
|
|
|
|
#endif
|
2021-04-04 06:10:44 +08:00
|
|
|
|
|
|
|
AVStream *ostream = avformat_new_stream(vs->format_ctx, encoder);
|
|
|
|
if (!ostream) {
|
|
|
|
LOGE("Could not allocate new v4l2 stream");
|
|
|
|
goto error_avformat_free_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
ostream->codecpar->codec_id = encoder->id;
|
|
|
|
ostream->codecpar->format = AV_PIX_FMT_YUV420P;
|
|
|
|
ostream->codecpar->width = vs->frame_size.width;
|
|
|
|
ostream->codecpar->height = vs->frame_size.height;
|
|
|
|
|
|
|
|
int ret = avio_open(&vs->format_ctx->pb, vs->device_name, AVIO_FLAG_WRITE);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGE("Failed to open output device: %s", vs->device_name);
|
|
|
|
// ostream will be cleaned up during context cleaning
|
|
|
|
goto error_avformat_free_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
vs->encoder_ctx = avcodec_alloc_context3(encoder);
|
|
|
|
if (!vs->encoder_ctx) {
|
|
|
|
LOGC("Could not allocate codec context for v4l2");
|
|
|
|
goto error_avio_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
vs->encoder_ctx->width = vs->frame_size.width;
|
|
|
|
vs->encoder_ctx->height = vs->frame_size.height;
|
|
|
|
vs->encoder_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
|
|
|
vs->encoder_ctx->time_base.num = 1;
|
|
|
|
vs->encoder_ctx->time_base.den = 1;
|
|
|
|
|
|
|
|
if (avcodec_open2(vs->encoder_ctx, encoder, NULL) < 0) {
|
|
|
|
LOGE("Could not open codec for v4l2");
|
|
|
|
goto error_avcodec_free_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
vs->frame = av_frame_alloc();
|
|
|
|
if (!vs->frame) {
|
|
|
|
LOGE("Could not create v4l2 frame");
|
|
|
|
goto error_avcodec_close;
|
|
|
|
}
|
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
vs->packet = av_packet_alloc();
|
|
|
|
if (!vs->packet) {
|
|
|
|
LOGE("Could not allocate packet");
|
|
|
|
goto error_av_frame_free;
|
|
|
|
}
|
|
|
|
|
2021-06-26 21:53:05 +08:00
|
|
|
vs->has_frame = false;
|
2021-06-26 21:54:27 +08:00
|
|
|
vs->header_written = false;
|
|
|
|
vs->stopped = false;
|
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
LOGD("Starting v4l2 thread");
|
|
|
|
ok = sc_thread_create(&vs->thread, run_v4l2_sink, "v4l2", vs);
|
|
|
|
if (!ok) {
|
|
|
|
LOGC("Could not start v4l2 thread");
|
2021-06-14 15:07:49 +08:00
|
|
|
goto error_av_packet_free;
|
2021-04-04 06:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGI("v4l2 sink started to device: %s", vs->device_name);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
error_av_packet_free:
|
|
|
|
av_packet_free(&vs->packet);
|
2021-04-04 06:10:44 +08:00
|
|
|
error_av_frame_free:
|
|
|
|
av_frame_free(&vs->frame);
|
|
|
|
error_avcodec_close:
|
|
|
|
avcodec_close(vs->encoder_ctx);
|
|
|
|
error_avcodec_free_context:
|
|
|
|
avcodec_free_context(&vs->encoder_ctx);
|
|
|
|
error_avio_close:
|
|
|
|
avio_close(vs->format_ctx->pb);
|
|
|
|
error_avformat_free_context:
|
|
|
|
avformat_free_context(vs->format_ctx);
|
|
|
|
error_cond_destroy:
|
|
|
|
sc_cond_destroy(&vs->cond);
|
|
|
|
error_mutex_destroy:
|
|
|
|
sc_mutex_destroy(&vs->mutex);
|
2021-07-04 21:24:51 +08:00
|
|
|
error_video_buffer_stop_and_join:
|
|
|
|
sc_video_buffer_stop(&vs->vb);
|
|
|
|
sc_video_buffer_join(&vs->vb);
|
2021-04-04 06:10:44 +08:00
|
|
|
error_video_buffer_destroy:
|
2021-06-26 21:02:18 +08:00
|
|
|
sc_video_buffer_destroy(&vs->vb);
|
2021-04-04 06:10:44 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sc_v4l2_sink_close(struct sc_v4l2_sink *vs) {
|
|
|
|
sc_mutex_lock(&vs->mutex);
|
|
|
|
vs->stopped = true;
|
|
|
|
sc_cond_signal(&vs->cond);
|
|
|
|
sc_mutex_unlock(&vs->mutex);
|
|
|
|
|
2021-07-04 21:24:51 +08:00
|
|
|
sc_video_buffer_stop(&vs->vb);
|
|
|
|
|
2021-04-04 06:10:44 +08:00
|
|
|
sc_thread_join(&vs->thread, NULL);
|
2021-07-04 21:24:51 +08:00
|
|
|
sc_video_buffer_join(&vs->vb);
|
2021-04-04 06:10:44 +08:00
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
av_packet_free(&vs->packet);
|
2021-04-04 06:10:44 +08:00
|
|
|
av_frame_free(&vs->frame);
|
|
|
|
avcodec_close(vs->encoder_ctx);
|
|
|
|
avcodec_free_context(&vs->encoder_ctx);
|
|
|
|
avio_close(vs->format_ctx->pb);
|
|
|
|
avformat_free_context(vs->format_ctx);
|
|
|
|
sc_cond_destroy(&vs->cond);
|
|
|
|
sc_mutex_destroy(&vs->mutex);
|
2021-06-26 21:02:18 +08:00
|
|
|
sc_video_buffer_destroy(&vs->vb);
|
2021-04-04 06:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_v4l2_sink_push(struct sc_v4l2_sink *vs, const AVFrame *frame) {
|
2021-07-04 18:42:22 +08:00
|
|
|
return sc_video_buffer_push(&vs->vb, frame);
|
2021-04-04 06:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_v4l2_frame_sink_open(struct sc_frame_sink *sink) {
|
|
|
|
struct sc_v4l2_sink *vs = DOWNCAST(sink);
|
|
|
|
return sc_v4l2_sink_open(vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sc_v4l2_frame_sink_close(struct sc_frame_sink *sink) {
|
|
|
|
struct sc_v4l2_sink *vs = DOWNCAST(sink);
|
|
|
|
sc_v4l2_sink_close(vs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_v4l2_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
|
|
|
|
struct sc_v4l2_sink *vs = DOWNCAST(sink);
|
|
|
|
return sc_v4l2_sink_push(vs, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sc_v4l2_sink_init(struct sc_v4l2_sink *vs, const char *device_name,
|
2021-10-30 21:20:39 +08:00
|
|
|
struct sc_size frame_size, sc_tick buffering_time) {
|
2021-04-04 06:10:44 +08:00
|
|
|
vs->device_name = strdup(device_name);
|
|
|
|
if (!vs->device_name) {
|
|
|
|
LOGE("Could not strdup v4l2 device name");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vs->frame_size = frame_size;
|
2021-07-07 01:04:05 +08:00
|
|
|
vs->buffering_time = buffering_time;
|
2021-04-04 06:10:44 +08:00
|
|
|
|
|
|
|
static const struct sc_frame_sink_ops ops = {
|
|
|
|
.open = sc_v4l2_frame_sink_open,
|
|
|
|
.close = sc_v4l2_frame_sink_close,
|
|
|
|
.push = sc_v4l2_frame_sink_push,
|
|
|
|
};
|
|
|
|
|
|
|
|
vs->frame_sink.ops = &ops;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_v4l2_sink_destroy(struct sc_v4l2_sink *vs) {
|
|
|
|
free(vs->device_name);
|
|
|
|
}
|