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"
|
2023-02-11 05:52:40 +08:00
|
|
|
#include "packet_merger.h"
|
2019-03-02 23:43:43 +08:00
|
|
|
#include "recorder.h"
|
2022-08-03 21:13:16 +08:00
|
|
|
#include "util/binary.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#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
|
|
|
|
2023-02-03 19:35:37 +08:00
|
|
|
static enum AVCodecID
|
2023-02-11 01:48:20 +08:00
|
|
|
sc_demuxer_to_avcodec_id(uint32_t codec_id) {
|
2023-02-03 19:35:37 +08:00
|
|
|
#define SC_CODEC_ID_H264 UINT32_C(0x68323634) // "h264" in ASCII
|
2023-02-03 19:40:55 +08:00
|
|
|
#define SC_CODEC_ID_H265 UINT32_C(0x68323635) // "h265" in ASCII
|
2023-02-03 19:42:22 +08:00
|
|
|
#define SC_CODEC_ID_AV1 UINT32_C(0x00617631) // "av1" in ASCII
|
2023-02-03 19:35:37 +08:00
|
|
|
switch (codec_id) {
|
|
|
|
case SC_CODEC_ID_H264:
|
|
|
|
return AV_CODEC_ID_H264;
|
2023-02-03 19:40:55 +08:00
|
|
|
case SC_CODEC_ID_H265:
|
|
|
|
return AV_CODEC_ID_HEVC;
|
2023-02-03 19:42:22 +08:00
|
|
|
case SC_CODEC_ID_AV1:
|
|
|
|
return AV_CODEC_ID_AV1;
|
2023-02-03 19:35:37 +08:00
|
|
|
default:
|
|
|
|
LOGE("Unknown codec id 0x%08" PRIx32, codec_id);
|
|
|
|
return AV_CODEC_ID_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 01:48:20 +08:00
|
|
|
static bool
|
|
|
|
sc_demuxer_recv_codec_id(struct sc_demuxer *demuxer, uint32_t *codec_id) {
|
|
|
|
uint8_t data[4];
|
|
|
|
ssize_t r = net_recv_all(demuxer->socket, data, 4);
|
|
|
|
if (r < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*codec_id = sc_read32be(data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
2022-07-20 17:39:55 +08:00
|
|
|
// | `- key frame
|
|
|
|
// `-- config packet
|
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 16:12:46 +08:00
|
|
|
uint64_t pts_flags = sc_read64be(header);
|
|
|
|
uint32_t len = sc_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)) {
|
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) {
|
2022-02-03 04:03:24 +08:00
|
|
|
bool ok = push_packet_to_sinks(demuxer, packet);
|
|
|
|
if (!ok) {
|
2023-02-19 07:13:54 +08:00
|
|
|
LOGE("Demuxer '%s': could not process packet", demuxer->name);
|
2022-02-03 04:03:24 +08:00
|
|
|
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)) {
|
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
|
|
|
|
2023-02-11 01:54:46 +08:00
|
|
|
// Flag to report end-of-stream (i.e. device disconnected)
|
|
|
|
bool eos = false;
|
|
|
|
|
2023-02-11 01:48:20 +08:00
|
|
|
uint32_t raw_codec_id;
|
|
|
|
bool ok = sc_demuxer_recv_codec_id(demuxer, &raw_codec_id);
|
|
|
|
if (!ok) {
|
2023-02-11 01:54:46 +08:00
|
|
|
eos = true;
|
2023-02-11 01:48:20 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum AVCodecID codec_id = sc_demuxer_to_avcodec_id(raw_codec_id);
|
2023-02-03 19:35:37 +08:00
|
|
|
if (codec_id == AV_CODEC_ID_NONE) {
|
|
|
|
// Error already logged
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AVCodec *codec = avcodec_find_decoder(codec_id);
|
2019-03-02 23:43:43 +08:00
|
|
|
if (!codec) {
|
2023-02-19 07:13:54 +08:00
|
|
|
LOGE("Demuxer '%s': decoder not found", demuxer->name);
|
2019-03-02 23:43:43 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-02-03 03:51:07 +08:00
|
|
|
if (!sc_demuxer_open_sinks(demuxer, codec)) {
|
2023-02-11 01:45:31 +08:00
|
|
|
goto end;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2023-02-11 05:52:40 +08:00
|
|
|
struct sc_packet_merger merger;
|
|
|
|
sc_packet_merger_init(&merger);
|
|
|
|
|
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();
|
2023-02-11 06:18:09 +08:00
|
|
|
goto finally_close_sinks;
|
2021-06-14 15:07:49 +08:00
|
|
|
}
|
|
|
|
|
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
|
2023-02-11 01:54:46 +08:00
|
|
|
eos = true;
|
2019-07-31 07:55:32 +08:00
|
|
|
break;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2023-02-11 05:52:40 +08:00
|
|
|
// Prepend any config packet to the next media packet
|
|
|
|
ok = sc_packet_merger_merge(&merger, packet);
|
|
|
|
if (!ok) {
|
|
|
|
av_packet_unref(packet);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:13:54 +08:00
|
|
|
LOGD("Demuxer '%s': end of frames", demuxer->name);
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2023-02-11 05:52:40 +08:00
|
|
|
sc_packet_merger_destroy(&merger);
|
2019-07-31 07:55:32 +08:00
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
av_packet_free(&packet);
|
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-03-02 23:43:43 +08:00
|
|
|
end:
|
2023-02-11 01:54:46 +08:00
|
|
|
demuxer->cbs->on_ended(demuxer, eos, 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
|
2023-02-19 07:13:54 +08:00
|
|
|
sc_demuxer_init(struct sc_demuxer *demuxer, const char *name, sc_socket socket,
|
2022-02-03 03:51:07 +08:00
|
|
|
const struct sc_demuxer_callbacks *cbs, void *cbs_userdata) {
|
2023-02-19 07:13:54 +08:00
|
|
|
demuxer->name = name; // statically allocated
|
2022-02-03 03:51:07 +08:00
|
|
|
demuxer->socket = socket;
|
|
|
|
demuxer->sink_count = 0;
|
2021-05-16 21:32:31 +08:00
|
|
|
|
2023-02-11 01:54:46 +08:00
|
|
|
assert(cbs && cbs->on_ended);
|
2021-05-16 21:32:31 +08:00
|
|
|
|
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) {
|
2023-02-19 07:13:54 +08:00
|
|
|
LOGD("Demuxer '%s': starting thread", demuxer->name);
|
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) {
|
2023-02-19 07:13:54 +08:00
|
|
|
LOGE("Demuxer '%s': could not start thread", demuxer->name);
|
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
|
|
|
}
|