2022-02-03 03:51:07 +08:00
|
|
|
#include "demuxer.h"
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2019-03-02 23:43:43 +08:00
|
|
|
#include <libavutil/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "decoder.h"
|
|
|
|
#include "events.h"
|
|
|
|
#include "recorder.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/buffer_util.h"
|
|
|
|
#include "util/log.h"
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2022-02-12 04:34:58 +08:00
|
|
|
#define SC_PACKET_HEADER_SIZE 12
|
2022-02-12 02:31:01 +08:00
|
|
|
|
|
|
|
#define SC_PACKET_FLAG_CONFIG (UINT64_C(1) << 63)
|
2022-02-12 02:33:35 +08:00
|
|
|
#define SC_PACKET_FLAG_KEY_FRAME (UINT64_C(1) << 62)
|
2022-02-12 02:31:01 +08:00
|
|
|
|
2022-02-12 02:33:35 +08:00
|
|
|
#define SC_PACKET_PTS_MASK (SC_PACKET_FLAG_KEY_FRAME - 1)
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_recv_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
|
2019-03-02 23:43:43 +08:00
|
|
|
// The video stream contains raw packets, without time information. When we
|
|
|
|
// record, we retrieve the timestamps separately, from a "meta" header
|
|
|
|
// added by the server before each raw packet.
|
|
|
|
//
|
|
|
|
// The "meta" header length is 12 bytes:
|
|
|
|
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
|
|
|
|
// <-------------> <-----> <-----------------------------...
|
|
|
|
// PTS packet raw packet
|
|
|
|
// size
|
|
|
|
//
|
|
|
|
// It is followed by <packet_size> bytes containing the packet/frame.
|
2022-02-12 02:31:01 +08:00
|
|
|
//
|
|
|
|
// The most significant bits of the PTS are used for packet flags:
|
|
|
|
//
|
|
|
|
// byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
|
2022-02-12 02:33:35 +08:00
|
|
|
// CK...... ........ ........ ........ ........ ........ ........ ........
|
|
|
|
// ^^<------------------------------------------------------------------->
|
|
|
|
// || PTS
|
|
|
|
// | `- config packet
|
|
|
|
// `-- key frame
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2022-02-12 04:34:58 +08:00
|
|
|
uint8_t header[SC_PACKET_HEADER_SIZE];
|
|
|
|
ssize_t r = net_recv_all(demuxer->socket, header, SC_PACKET_HEADER_SIZE);
|
|
|
|
if (r < SC_PACKET_HEADER_SIZE) {
|
2019-07-31 07:55:32 +08:00
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 02:31:01 +08:00
|
|
|
uint64_t pts_flags = buffer_read64be(header);
|
2019-07-31 07:55:32 +08:00
|
|
|
uint32_t len = buffer_read32be(&header[8]);
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(len);
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
if (av_new_packet(packet, len)) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2019-07-31 07:55:32 +08:00
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
r = net_recv_all(demuxer->socket, packet->data, len);
|
2019-11-27 20:39:42 +08:00
|
|
|
if (r < 0 || ((uint32_t) r) < len) {
|
2019-07-31 07:55:32 +08:00
|
|
|
av_packet_unref(packet);
|
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 02:31:01 +08:00
|
|
|
if (pts_flags & SC_PACKET_FLAG_CONFIG) {
|
|
|
|
packet->pts = AV_NOPTS_VALUE;
|
|
|
|
} else {
|
|
|
|
packet->pts = pts_flags & SC_PACKET_PTS_MASK;
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2022-02-12 02:33:35 +08:00
|
|
|
if (pts_flags & SC_PACKET_FLAG_KEY_FRAME) {
|
|
|
|
packet->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
}
|
|
|
|
|
2022-02-12 02:35:47 +08:00
|
|
|
packet->dts = packet->pts;
|
2019-07-31 07:55:32 +08:00
|
|
|
return true;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
static bool
|
2022-02-03 03:51:07 +08:00
|
|
|
push_packet_to_sinks(struct sc_demuxer *demuxer, const AVPacket *packet) {
|
|
|
|
for (unsigned i = 0; i < demuxer->sink_count; ++i) {
|
|
|
|
struct sc_packet_sink *sink = demuxer->sinks[i];
|
2021-04-11 21:01:05 +08:00
|
|
|
if (!sink->ops->push(sink, packet)) {
|
|
|
|
LOGE("Could not send config packet to sink %d", i);
|
2019-07-31 07:55:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_push_packet(struct sc_demuxer *demuxer, AVPacket *packet) {
|
2019-07-31 07:55:32 +08:00
|
|
|
bool is_config = packet->pts == AV_NOPTS_VALUE;
|
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
// A config packet must not be decoded immediately (it contains no
|
2019-07-31 07:55:32 +08:00
|
|
|
// frame); instead, it must be concatenated with the future data packet.
|
2022-02-03 03:51:07 +08:00
|
|
|
if (demuxer->pending || is_config) {
|
2019-07-31 07:55:32 +08:00
|
|
|
size_t offset;
|
2022-02-03 03:51:07 +08:00
|
|
|
if (demuxer->pending) {
|
|
|
|
offset = demuxer->pending->size;
|
|
|
|
if (av_grow_packet(demuxer->pending, packet->size)) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2019-07-31 07:55:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
offset = 0;
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->pending = av_packet_alloc();
|
|
|
|
if (!demuxer->pending) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-06-14 15:07:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
2022-02-03 03:51:07 +08:00
|
|
|
if (av_new_packet(demuxer->pending, packet->size)) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2022-02-03 03:51:07 +08:00
|
|
|
av_packet_free(&demuxer->pending);
|
2019-07-31 07:55:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
memcpy(demuxer->pending->data + offset, packet->data, packet->size);
|
2019-07-31 07:55:32 +08:00
|
|
|
|
|
|
|
if (!is_config) {
|
|
|
|
// prepare the concat packet to send to the decoder
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->pending->pts = packet->pts;
|
|
|
|
demuxer->pending->dts = packet->dts;
|
|
|
|
demuxer->pending->flags = packet->flags;
|
|
|
|
packet = demuxer->pending;
|
2019-07-31 07:55:32 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 04:03:24 +08:00
|
|
|
bool ok = push_packet_to_sinks(demuxer, packet);
|
2019-07-31 07:55:32 +08:00
|
|
|
|
2022-02-03 04:03:24 +08:00
|
|
|
if (!is_config && demuxer->pending) {
|
|
|
|
// the pending packet must be discarded (consumed or error)
|
|
|
|
av_packet_free(&demuxer->pending);
|
2019-07-31 07:55:32 +08:00
|
|
|
}
|
2022-02-03 04:03:24 +08:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not process packet");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
static void
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_close_first_sinks(struct sc_demuxer *demuxer, unsigned count) {
|
2021-04-11 21:01:05 +08:00
|
|
|
while (count) {
|
2022-02-03 03:51:07 +08:00
|
|
|
struct sc_packet_sink *sink = demuxer->sinks[--count];
|
2021-04-11 21:01:05 +08:00
|
|
|
sink->ops->close(sink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_close_sinks(struct sc_demuxer *demuxer) {
|
|
|
|
sc_demuxer_close_first_sinks(demuxer, demuxer->sink_count);
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_open_sinks(struct sc_demuxer *demuxer, const AVCodec *codec) {
|
|
|
|
for (unsigned i = 0; i < demuxer->sink_count; ++i) {
|
|
|
|
struct sc_packet_sink *sink = demuxer->sinks[i];
|
2021-04-11 21:01:05 +08:00
|
|
|
if (!sink->ops->open(sink, codec)) {
|
|
|
|
LOGE("Could not open packet sink %d", i);
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_close_first_sinks(demuxer, i);
|
2021-04-11 21:01:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
static int
|
2022-02-03 03:51:07 +08:00
|
|
|
run_demuxer(void *data) {
|
|
|
|
struct sc_demuxer *demuxer = data;
|
2019-07-31 07:55:32 +08:00
|
|
|
|
2022-01-16 08:45:36 +08:00
|
|
|
const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
|
2019-03-02 23:43:43 +08:00
|
|
|
if (!codec) {
|
|
|
|
LOGE("H.264 decoder not found");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->codec_ctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!demuxer->codec_ctx) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2019-07-31 07:55:32 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
if (!sc_demuxer_open_sinks(demuxer, codec)) {
|
|
|
|
LOGE("Could not open demuxer sinks");
|
2019-07-31 07:55:32 +08:00
|
|
|
goto finally_free_codec_ctx;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->parser = av_parser_init(AV_CODEC_ID_H264);
|
|
|
|
if (!demuxer->parser) {
|
2019-07-31 07:55:32 +08:00
|
|
|
LOGE("Could not initialize parser");
|
2021-04-11 21:01:05 +08:00
|
|
|
goto finally_close_sinks;
|
2019-07-31 07:55:32 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
// We must only pass complete frames to av_parser_parse2()!
|
|
|
|
// It's more complicated, but this allows to reduce the latency by 1 frame!
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
|
2019-07-31 07:55:32 +08:00
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
AVPacket *packet = av_packet_alloc();
|
|
|
|
if (!packet) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-06-14 15:07:49 +08:00
|
|
|
goto finally_close_parser;
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
for (;;) {
|
2022-02-03 03:51:07 +08:00
|
|
|
bool ok = sc_demuxer_recv_packet(demuxer, packet);
|
2019-07-31 07:55:32 +08:00
|
|
|
if (!ok) {
|
|
|
|
// end of stream
|
|
|
|
break;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
ok = sc_demuxer_push_packet(demuxer, packet);
|
2021-06-14 15:07:49 +08:00
|
|
|
av_packet_unref(packet);
|
2019-07-31 07:55:32 +08:00
|
|
|
if (!ok) {
|
|
|
|
// cannot process packet (error already logged)
|
2019-03-02 23:43:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("End of frames");
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
if (demuxer->pending) {
|
|
|
|
av_packet_free(&demuxer->pending);
|
2019-07-31 07:55:32 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
av_packet_free(&packet);
|
|
|
|
finally_close_parser:
|
2022-02-03 03:51:07 +08:00
|
|
|
av_parser_close(demuxer->parser);
|
2021-04-11 21:01:05 +08:00
|
|
|
finally_close_sinks:
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_close_sinks(demuxer);
|
2019-07-31 07:55:32 +08:00
|
|
|
finally_free_codec_ctx:
|
2022-02-03 03:51:07 +08:00
|
|
|
avcodec_free_context(&demuxer->codec_ctx);
|
2019-03-02 23:43:43 +08:00
|
|
|
end:
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->cbs->on_eos(demuxer, demuxer->cbs_userdata);
|
2021-05-16 21:32:31 +08:00
|
|
|
|
2019-03-02 23:43:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_init(struct sc_demuxer *demuxer, sc_socket socket,
|
|
|
|
const struct sc_demuxer_callbacks *cbs, void *cbs_userdata) {
|
|
|
|
demuxer->socket = socket;
|
|
|
|
demuxer->pending = NULL;
|
|
|
|
demuxer->sink_count = 0;
|
2021-05-16 21:32:31 +08:00
|
|
|
|
|
|
|
assert(cbs && cbs->on_eos);
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->cbs = cbs;
|
|
|
|
demuxer->cbs_userdata = cbs_userdata;
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_add_sink(struct sc_demuxer *demuxer, struct sc_packet_sink *sink) {
|
|
|
|
assert(demuxer->sink_count < SC_DEMUXER_MAX_SINKS);
|
2021-04-11 21:01:05 +08:00
|
|
|
assert(sink);
|
|
|
|
assert(sink->ops);
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->sinks[demuxer->sink_count++] = sink;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_start(struct sc_demuxer *demuxer) {
|
|
|
|
LOGD("Starting demuxer thread");
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
bool ok = sc_thread_create(&demuxer->thread, run_demuxer, "scrcpy-demuxer",
|
|
|
|
demuxer);
|
2021-02-01 01:24:35 +08:00
|
|
|
if (!ok) {
|
2022-02-05 21:06:03 +08:00
|
|
|
LOGE("Could not start demuxer thread");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2022-02-03 03:51:07 +08:00
|
|
|
sc_demuxer_join(struct sc_demuxer *demuxer) {
|
|
|
|
sc_thread_join(&demuxer->thread, NULL);
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|