2018-11-09 19:21:17 +08:00
|
|
|
#include "recorder.h"
|
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2021-09-21 00:27:37 +08:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
2018-11-09 19:21:17 +08:00
|
|
|
#include <libavutil/time.h>
|
|
|
|
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
2021-11-13 06:12:51 +08:00
|
|
|
#include "util/str.h"
|
2018-11-09 19:21:17 +08:00
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
/** Downcast video packet_sink to recorder */
|
|
|
|
#define DOWNCAST_VIDEO(SINK) \
|
|
|
|
container_of(SINK, struct sc_recorder, video_packet_sink)
|
2021-04-11 21:01:05 +08:00
|
|
|
|
2019-02-09 22:08:36 +08:00
|
|
|
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static const AVOutputFormat *
|
|
|
|
find_muxer(const char *name) {
|
2019-02-16 22:04:32 +08:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API
|
2018-11-09 19:21:17 +08:00
|
|
|
void *opaque = NULL;
|
2018-11-10 19:00:30 +08:00
|
|
|
#endif
|
|
|
|
const AVOutputFormat *oformat = NULL;
|
2018-11-09 19:21:17 +08:00
|
|
|
do {
|
2019-02-16 22:04:32 +08:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API
|
2018-11-09 19:21:17 +08:00
|
|
|
oformat = av_muxer_iterate(&opaque);
|
2018-11-10 19:00:30 +08:00
|
|
|
#else
|
|
|
|
oformat = av_oformat_next(oformat);
|
|
|
|
#endif
|
2021-04-19 15:28:28 +08:00
|
|
|
// until null or containing the requested name
|
2021-11-13 06:08:19 +08:00
|
|
|
} while (oformat && !sc_str_list_contains(oformat->name, ',', name));
|
2018-11-09 19:21:17 +08:00
|
|
|
return oformat;
|
|
|
|
}
|
|
|
|
|
2022-02-03 02:37:28 +08:00
|
|
|
static struct sc_record_packet *
|
|
|
|
sc_record_packet_new(const AVPacket *packet) {
|
|
|
|
struct sc_record_packet *rec = malloc(sizeof(*rec));
|
2019-07-31 07:55:40 +08:00
|
|
|
if (!rec) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2019-07-31 07:55:40 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-08-14 00:49:25 +08:00
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
rec->packet = av_packet_alloc();
|
|
|
|
if (!rec->packet) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-06-14 15:07:49 +08:00
|
|
|
free(rec);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-08-14 00:49:25 +08:00
|
|
|
|
2021-06-14 15:07:49 +08:00
|
|
|
if (av_packet_ref(rec->packet, packet)) {
|
|
|
|
av_packet_free(&rec->packet);
|
2021-01-24 22:14:53 +08:00
|
|
|
free(rec);
|
2019-07-31 07:55:40 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_record_packet_delete(struct sc_record_packet *rec) {
|
2021-06-14 15:07:49 +08:00
|
|
|
av_packet_free(&rec->packet);
|
2021-01-24 22:14:53 +08:00
|
|
|
free(rec);
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_queue_clear(struct sc_recorder_queue *queue) {
|
2021-07-04 21:05:08 +08:00
|
|
|
while (!sc_queue_is_empty(queue)) {
|
2022-02-03 02:37:28 +08:00
|
|
|
struct sc_record_packet *rec;
|
2021-07-04 21:05:08 +08:00
|
|
|
sc_queue_take(queue, next, &rec);
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_record_packet_delete(rec);
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:20:07 +08:00
|
|
|
static const char *
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_get_format_name(enum sc_record_format format) {
|
2019-02-09 22:20:07 +08:00
|
|
|
switch (format) {
|
2020-06-20 04:04:06 +08:00
|
|
|
case SC_RECORD_FORMAT_MP4: return "mp4";
|
|
|
|
case SC_RECORD_FORMAT_MKV: return "matroska";
|
2019-02-09 22:20:07 +08:00
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_write_header(struct sc_recorder *recorder, const AVPacket *packet) {
|
2019-02-09 19:54:12 +08:00
|
|
|
AVStream *ostream = recorder->ctx->streams[0];
|
|
|
|
|
2019-02-16 07:50:14 +08:00
|
|
|
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
2019-02-09 19:54:12 +08:00
|
|
|
if (!extradata) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2019-02-09 19:54:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy the first packet to the extra data
|
|
|
|
memcpy(extradata, packet->data, packet->size);
|
|
|
|
|
|
|
|
ostream->codecpar->extradata = extradata;
|
|
|
|
ostream->codecpar->extradata_size = packet->size;
|
|
|
|
|
2023-02-18 19:07:30 +08:00
|
|
|
return avformat_write_header(recorder->ctx, NULL) >= 0;
|
2019-02-09 19:54:12 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 22:08:36 +08:00
|
|
|
static void
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_rescale_packet(struct sc_recorder *recorder, AVPacket *packet) {
|
2019-02-09 22:08:36 +08:00
|
|
|
AVStream *ostream = recorder->ctx->streams[0];
|
|
|
|
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base);
|
|
|
|
}
|
|
|
|
|
2021-04-07 22:41:57 +08:00
|
|
|
static bool
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_write(struct sc_recorder *recorder, AVPacket *packet) {
|
|
|
|
sc_recorder_rescale_packet(recorder, packet);
|
2018-11-09 19:21:17 +08:00
|
|
|
return av_write_frame(recorder->ctx, packet) >= 0;
|
|
|
|
}
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
static bool
|
|
|
|
sc_recorder_open_output_file(struct sc_recorder *recorder) {
|
|
|
|
const char *format_name = sc_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;
|
|
|
|
}
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
recorder->ctx = avformat_alloc_context();
|
|
|
|
if (!recorder->ctx) {
|
|
|
|
LOG_OOM();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = avio_open(&recorder->ctx->pb, recorder->filename,
|
|
|
|
AVIO_FLAG_WRITE);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGE("Failed to open output file: %s", recorder->filename);
|
|
|
|
avformat_free_context(recorder->ctx);
|
|
|
|
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);
|
|
|
|
|
|
|
|
LOGI("Recording started to %s file: %s", format_name, recorder->filename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sc_recorder_close_output_file(struct sc_recorder *recorder) {
|
|
|
|
avio_close(recorder->ctx->pb);
|
|
|
|
avformat_free_context(recorder->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_recorder_wait_video_stream(struct sc_recorder *recorder) {
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2023-02-15 17:06:10 +08:00
|
|
|
while (!recorder->video_codec && !recorder->stopped) {
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_cond_wait(&recorder->stream_cond, &recorder->mutex);
|
|
|
|
}
|
2023-02-15 17:06:10 +08:00
|
|
|
const AVCodec *codec = recorder->video_codec;
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
|
|
|
|
if (codec) {
|
|
|
|
AVStream *ostream = avformat_new_stream(recorder->ctx, codec);
|
|
|
|
if (!ostream) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
ostream->codecpar->codec_id = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-18 19:07:30 +08:00
|
|
|
static bool
|
|
|
|
sc_recorder_process_header(struct sc_recorder *recorder) {
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
while (!recorder->stopped && sc_queue_is_empty(&recorder->video_queue)) {
|
2023-02-18 19:07:30 +08:00
|
|
|
sc_cond_wait(&recorder->queue_cond, &recorder->mutex);
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
if (recorder->stopped && sc_queue_is_empty(&recorder->video_queue)) {
|
2023-02-18 19:07:30 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sc_record_packet *rec;
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_queue_take(&recorder->video_queue, next, &rec);
|
2023-02-18 19:07:30 +08:00
|
|
|
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
|
|
|
|
if (rec->packet->pts != AV_NOPTS_VALUE) {
|
|
|
|
LOGE("The first packet is not a config packet");
|
|
|
|
sc_record_packet_delete(rec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok = sc_recorder_write_header(recorder, rec->packet);
|
|
|
|
sc_record_packet_delete(rec);
|
|
|
|
if (!ok) {
|
|
|
|
LOGE("Failed to write header to %s", recorder->filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
static bool
|
|
|
|
sc_recorder_process_packets(struct sc_recorder *recorder) {
|
2023-02-17 15:46:11 +08:00
|
|
|
int64_t pts_origin = AV_NOPTS_VALUE;
|
|
|
|
|
2023-02-17 16:01:48 +08:00
|
|
|
// We can write a packet only once we received the next one so that we can
|
|
|
|
// set its duration (next_pts - current_pts)
|
|
|
|
struct sc_record_packet *previous = NULL;
|
|
|
|
|
2023-02-18 19:07:30 +08:00
|
|
|
bool header_written = sc_recorder_process_header(recorder);
|
|
|
|
if (!header_written) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
for (;;) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
while (!recorder->stopped
|
|
|
|
&& sc_queue_is_empty(&recorder->video_queue)) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_wait(&recorder->queue_cond, &recorder->mutex);
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if stopped is set, continue to process the remaining events (to
|
|
|
|
// finish the recording) before actually stopping
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
if (recorder->stopped && sc_queue_is_empty(&recorder->video_queue)) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-31 07:55:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-03 02:37:28 +08:00
|
|
|
struct sc_record_packet *rec;
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_queue_take(&recorder->video_queue, next, &rec);
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2023-02-18 19:07:30 +08:00
|
|
|
if (rec->packet->pts == AV_NOPTS_VALUE) {
|
|
|
|
// Ignore further config packets (e.g. on device orientation
|
|
|
|
// change). The next non-config packet will have the config packet
|
|
|
|
// data prepended.
|
|
|
|
sc_record_packet_delete(rec);
|
|
|
|
} else {
|
|
|
|
assert(rec->packet->pts != AV_NOPTS_VALUE);
|
|
|
|
|
|
|
|
if (!previous) {
|
|
|
|
// This is the first non-config packet
|
|
|
|
assert(pts_origin == AV_NOPTS_VALUE);
|
|
|
|
pts_origin = rec->packet->pts;
|
|
|
|
rec->packet->pts = 0;
|
|
|
|
rec->packet->dts = 0;
|
|
|
|
previous = rec;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(previous);
|
|
|
|
assert(pts_origin != AV_NOPTS_VALUE);
|
2023-02-02 04:56:43 +08:00
|
|
|
|
2023-02-17 15:46:11 +08:00
|
|
|
rec->packet->pts -= pts_origin;
|
2023-02-02 04:56:43 +08:00
|
|
|
rec->packet->dts = rec->packet->pts;
|
|
|
|
|
2019-11-13 23:04:16 +08:00
|
|
|
// we now know the duration of the previous packet
|
2021-06-14 15:07:49 +08:00
|
|
|
previous->packet->duration =
|
|
|
|
rec->packet->pts - previous->packet->pts;
|
2019-08-09 00:57:20 +08:00
|
|
|
|
2023-02-18 19:07:30 +08:00
|
|
|
bool ok = sc_recorder_write(recorder, previous->packet);
|
|
|
|
sc_record_packet_delete(previous);
|
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not record packet");
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2023-02-18 19:07:30 +08:00
|
|
|
previous = rec;
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2023-02-18 19:07:05 +08:00
|
|
|
// Write the last packet
|
|
|
|
struct sc_record_packet *last = previous;
|
|
|
|
if (last) {
|
|
|
|
// assign an arbitrary duration to the last packet
|
|
|
|
last->packet->duration = 100000;
|
|
|
|
bool ok = sc_recorder_write(recorder, last->packet);
|
|
|
|
if (!ok) {
|
|
|
|
// 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
|
|
|
|
LOGW("Could not record last packet");
|
|
|
|
}
|
|
|
|
sc_record_packet_delete(last);
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
int ret = av_write_trailer(recorder->ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGE("Failed to write trailer to %s", recorder->filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_recorder_record(struct sc_recorder *recorder) {
|
|
|
|
bool ok = sc_recorder_open_output_file(recorder);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = sc_recorder_wait_video_stream(recorder);
|
|
|
|
if (!ok) {
|
|
|
|
sc_recorder_close_output_file(recorder);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If recorder->stopped, process any queued packet anyway
|
|
|
|
|
|
|
|
ok = sc_recorder_process_packets(recorder);
|
|
|
|
sc_recorder_close_output_file(recorder);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
run_recorder(void *data) {
|
|
|
|
struct sc_recorder *recorder = data;
|
|
|
|
|
|
|
|
bool success = sc_recorder_record(recorder);
|
|
|
|
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
// Prevent the producer to push any new packet
|
|
|
|
recorder->stopped = true;
|
|
|
|
// Discard pending packets
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_recorder_queue_clear(&recorder->video_queue);
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
|
|
|
|
if (success) {
|
2022-02-03 02:37:28 +08:00
|
|
|
const char *format_name = sc_recorder_get_format_name(recorder->format);
|
2021-07-16 00:07:39 +08:00
|
|
|
LOGI("Recording complete to %s file: %s", format_name,
|
|
|
|
recorder->filename);
|
2023-02-14 16:25:50 +08:00
|
|
|
} else {
|
|
|
|
LOGE("Recording failed to %s", recorder->filename);
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("Recorder thread ended");
|
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
recorder->cbs->on_ended(recorder, success, recorder->cbs_userdata);
|
2023-02-11 01:10:24 +08:00
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
static bool
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_recorder_video_packet_sink_open(struct sc_packet_sink *sink,
|
|
|
|
const AVCodec *codec) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_VIDEO(sink);
|
2023-02-14 16:37:36 +08:00
|
|
|
assert(codec);
|
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
if (recorder->stopped) {
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2023-02-14 15:40:44 +08:00
|
|
|
return false;
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
recorder->video_codec = codec;
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_cond_signal(&recorder->stream_cond);
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2021-04-11 21:01:05 +08:00
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
static void
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_recorder_video_packet_sink_close(struct sc_packet_sink *sink) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_VIDEO(sink);
|
2023-02-14 16:37:36 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2023-02-14 16:25:50 +08:00
|
|
|
// EOS also stops the recorder
|
2019-07-31 07:55:40 +08:00
|
|
|
recorder->stopped = true;
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_signal(&recorder->queue_cond);
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
static bool
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_recorder_video_packet_sink_push(struct sc_packet_sink *sink,
|
|
|
|
const AVPacket *packet) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_VIDEO(sink);
|
2023-02-14 16:37:36 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
if (recorder->stopped) {
|
|
|
|
// reject any new packet
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-31 07:55:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-03 02:37:28 +08:00
|
|
|
struct sc_record_packet *rec = sc_record_packet_new(packet);
|
2019-08-02 05:15:47 +08:00
|
|
|
if (!rec) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-08-02 05:15:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_queue_push(&recorder->video_queue, next, rec);
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_signal(&recorder->queue_cond);
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-08-02 05:15:47 +08:00
|
|
|
return true;
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
2021-04-11 21:01:05 +08:00
|
|
|
|
|
|
|
bool
|
2023-02-11 01:10:24 +08:00
|
|
|
sc_recorder_init(struct sc_recorder *recorder, const char *filename,
|
2022-02-03 02:37:28 +08:00
|
|
|
enum sc_record_format format,
|
2023-02-11 01:10:24 +08:00
|
|
|
struct sc_size declared_frame_size,
|
|
|
|
const struct sc_recorder_callbacks *cbs, void *cbs_userdata) {
|
2021-04-11 21:01:05 +08:00
|
|
|
recorder->filename = strdup(filename);
|
|
|
|
if (!recorder->filename) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-04-11 21:01:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-14 15:40:44 +08:00
|
|
|
bool ok = sc_mutex_init(&recorder->mutex);
|
|
|
|
if (!ok) {
|
|
|
|
goto error_free_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = sc_cond_init(&recorder->queue_cond);
|
|
|
|
if (!ok) {
|
|
|
|
goto error_mutex_destroy;
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
ok = sc_cond_init(&recorder->stream_cond);
|
|
|
|
if (!ok) {
|
|
|
|
goto error_queue_cond_destroy;
|
|
|
|
}
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
sc_queue_init(&recorder->video_queue);
|
2023-02-14 15:40:44 +08:00
|
|
|
recorder->stopped = false;
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
recorder->video_codec = NULL;
|
2023-02-14 16:25:50 +08:00
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
recorder->format = format;
|
|
|
|
recorder->declared_frame_size = declared_frame_size;
|
|
|
|
|
2023-02-11 01:10:24 +08:00
|
|
|
assert(cbs && cbs->on_ended);
|
|
|
|
recorder->cbs = cbs;
|
|
|
|
recorder->cbs_userdata = cbs_userdata;
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
static const struct sc_packet_sink_ops video_ops = {
|
|
|
|
.open = sc_recorder_video_packet_sink_open,
|
|
|
|
.close = sc_recorder_video_packet_sink_close,
|
|
|
|
.push = sc_recorder_video_packet_sink_push,
|
2021-04-11 21:01:05 +08:00
|
|
|
};
|
|
|
|
|
2023-02-15 17:06:10 +08:00
|
|
|
recorder->video_packet_sink.ops = &video_ops;
|
2021-04-11 21:01:05 +08:00
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
return true;
|
2023-02-14 15:40:44 +08:00
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
error_queue_cond_destroy:
|
|
|
|
sc_cond_destroy(&recorder->queue_cond);
|
2023-02-14 15:40:44 +08:00
|
|
|
error_mutex_destroy:
|
|
|
|
sc_mutex_destroy(&recorder->mutex);
|
|
|
|
error_free_filename:
|
|
|
|
free(recorder->filename);
|
|
|
|
|
|
|
|
return false;
|
2021-04-11 21:01:05 +08:00
|
|
|
}
|
|
|
|
|
2023-02-23 18:00:34 +08:00
|
|
|
bool
|
|
|
|
sc_recorder_start(struct sc_recorder *recorder) {
|
|
|
|
bool ok = sc_thread_create(&recorder->thread, run_recorder,
|
|
|
|
"scrcpy-recorder", recorder);
|
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not start recorder thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
void
|
|
|
|
sc_recorder_stop(struct sc_recorder *recorder) {
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
recorder->stopped = true;
|
|
|
|
sc_cond_signal(&recorder->queue_cond);
|
|
|
|
sc_cond_signal(&recorder->stream_cond);
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_recorder_join(struct sc_recorder *recorder) {
|
|
|
|
sc_thread_join(&recorder->thread, NULL);
|
|
|
|
}
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
void
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_destroy(struct sc_recorder *recorder) {
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_cond_destroy(&recorder->stream_cond);
|
2023-02-14 15:40:44 +08:00
|
|
|
sc_cond_destroy(&recorder->queue_cond);
|
|
|
|
sc_mutex_destroy(&recorder->mutex);
|
2021-04-11 21:01:05 +08:00
|
|
|
free(recorder->filename);
|
|
|
|
}
|