2018-11-09 19:21:17 +08:00
|
|
|
#include "recorder.h"
|
|
|
|
|
|
|
|
#include <libavutil/time.h>
|
2019-02-09 22:20:07 +08:00
|
|
|
#include <SDL2/SDL_assert.h>
|
2018-11-09 19:21:17 +08:00
|
|
|
|
|
|
|
#include "config.h"
|
2019-09-30 04:36:56 +08:00
|
|
|
#include "compat.h"
|
2019-07-31 07:55:40 +08:00
|
|
|
#include "lock_util.h"
|
2018-11-09 19:21:17 +08:00
|
|
|
#include "log.h"
|
|
|
|
|
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
|
2018-11-09 19:21:17 +08:00
|
|
|
// until null or with name "mp4"
|
2019-02-09 22:20:07 +08:00
|
|
|
} while (oformat && strcmp(oformat->name, name));
|
2018-11-09 19:21:17 +08:00
|
|
|
return oformat;
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
static struct record_packet *
|
|
|
|
record_packet_new(const AVPacket *packet) {
|
|
|
|
struct record_packet *rec = SDL_malloc(sizeof(*rec));
|
|
|
|
if (!rec) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-08-14 00:49:25 +08:00
|
|
|
|
|
|
|
// av_packet_ref() does not initialize all fields in old FFmpeg versions
|
|
|
|
// See <https://github.com/Genymobile/scrcpy/issues/707>
|
|
|
|
av_init_packet(&rec->packet);
|
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
if (av_packet_ref(&rec->packet, packet)) {
|
|
|
|
SDL_free(rec);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
record_packet_delete(struct record_packet *rec) {
|
|
|
|
av_packet_unref(&rec->packet);
|
|
|
|
SDL_free(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recorder_queue_clear(struct recorder_queue *queue) {
|
2019-08-02 05:15:47 +08:00
|
|
|
while (!queue_is_empty(queue)) {
|
|
|
|
struct record_packet *rec;
|
|
|
|
queue_take(queue, next, &rec);
|
|
|
|
record_packet_delete(rec);
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
recorder_init(struct recorder *recorder,
|
|
|
|
const char *filename,
|
|
|
|
enum recorder_format format,
|
|
|
|
struct size declared_frame_size) {
|
2018-11-09 19:21:17 +08:00
|
|
|
recorder->filename = SDL_strdup(filename);
|
|
|
|
if (!recorder->filename) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGE("Could not strdup filename");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
recorder->mutex = SDL_CreateMutex();
|
|
|
|
if (!recorder->mutex) {
|
|
|
|
LOGC("Could not create mutex");
|
|
|
|
SDL_free(recorder->filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
recorder->queue_cond = SDL_CreateCond();
|
|
|
|
if (!recorder->queue_cond) {
|
|
|
|
LOGC("Could not create cond");
|
|
|
|
SDL_DestroyMutex(recorder->mutex);
|
|
|
|
SDL_free(recorder->filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:15:47 +08:00
|
|
|
queue_init(&recorder->queue);
|
2019-07-31 07:55:40 +08:00
|
|
|
recorder->stopped = false;
|
|
|
|
recorder->failed = false;
|
2019-02-09 22:20:07 +08:00
|
|
|
recorder->format = format;
|
2018-11-09 19:21:17 +08:00
|
|
|
recorder->declared_frame_size = declared_frame_size;
|
2019-03-03 06:52:22 +08:00
|
|
|
recorder->header_written = false;
|
2019-08-09 00:57:20 +08:00
|
|
|
recorder->previous = NULL;
|
2018-11-09 19:21:17 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
recorder_destroy(struct recorder *recorder) {
|
2019-07-31 07:55:40 +08:00
|
|
|
SDL_DestroyCond(recorder->queue_cond);
|
|
|
|
SDL_DestroyMutex(recorder->mutex);
|
2018-11-09 19:21:17 +08:00
|
|
|
SDL_free(recorder->filename);
|
|
|
|
}
|
|
|
|
|
2019-02-09 22:20:07 +08:00
|
|
|
static const char *
|
|
|
|
recorder_get_format_name(enum recorder_format format) {
|
|
|
|
switch (format) {
|
|
|
|
case RECORDER_FORMAT_MP4: return "mp4";
|
|
|
|
case RECORDER_FORMAT_MKV: return "matroska";
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 18:59:31 +08:00
|
|
|
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
|
2019-02-09 22:20:07 +08:00
|
|
|
const char *format_name = recorder_get_format_name(recorder->format);
|
|
|
|
SDL_assert(format_name);
|
|
|
|
const AVOutputFormat *format = find_muxer(format_name);
|
|
|
|
if (!format) {
|
|
|
|
LOGE("Could not find muxer");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
recorder->ctx = avformat_alloc_context();
|
|
|
|
if (!recorder->ctx) {
|
|
|
|
LOGE("Could not allocate output context");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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>
|
2019-02-09 22:20:07 +08:00
|
|
|
recorder->ctx->oformat = (AVOutputFormat *) format;
|
2018-11-09 19:21:17 +08:00
|
|
|
|
2019-11-01 03:57:57 +08:00
|
|
|
av_dict_set(&recorder->ctx->metadata, "comment",
|
|
|
|
"Recorded by scrcpy " SCRCPY_VERSION, 0);
|
|
|
|
|
2018-11-09 19:21:17 +08:00
|
|
|
AVStream *ostream = avformat_new_stream(recorder->ctx, input_codec);
|
|
|
|
if (!ostream) {
|
|
|
|
avformat_free_context(recorder->ctx);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
2019-02-16 22:04:32 +08:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API
|
2018-11-09 19:21:17 +08:00
|
|
|
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;
|
2018-11-11 06:49:19 +08:00
|
|
|
#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
|
2018-11-09 19:21:17 +08:00
|
|
|
|
|
|
|
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);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
2019-02-10 18:29:34 +08:00
|
|
|
LOGI("Recording started to %s file: %s", format_name, recorder->filename);
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
recorder_close(struct recorder *recorder) {
|
2019-11-08 04:58:57 +08:00
|
|
|
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
|
2019-07-31 17:04:38 +08:00
|
|
|
recorder->failed = true;
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
avio_close(recorder->ctx->pb);
|
|
|
|
avformat_free_context(recorder->ctx);
|
2019-02-10 18:29:34 +08:00
|
|
|
|
2019-07-31 17:04:38 +08:00
|
|
|
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);
|
|
|
|
}
|
2018-11-09 19:21:17 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 18:59:31 +08:00
|
|
|
recorder_write_header(struct 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) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGC("Could not allocate extradata");
|
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);
|
|
|
|
|
2019-02-16 22:04:32 +08:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_CODEC_PARAMS_API
|
2019-02-09 19:54:12 +08:00
|
|
|
ostream->codecpar->extradata = extradata;
|
|
|
|
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);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGE("Failed to write header to %s", recorder->filename);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2019-02-09 19:54:12 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-02-09 19:54:12 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 22:08:36 +08:00
|
|
|
static void
|
|
|
|
recorder_rescale_packet(struct recorder *recorder, AVPacket *packet) {
|
|
|
|
AVStream *ostream = recorder->ctx->streams[0];
|
|
|
|
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, ostream->time_base);
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
recorder_write(struct recorder *recorder, AVPacket *packet) {
|
2019-02-09 19:54:12 +08:00
|
|
|
if (!recorder->header_written) {
|
2019-07-31 07:55:32 +08:00
|
|
|
if (packet->pts != AV_NOPTS_VALUE) {
|
|
|
|
LOGE("The first packet is not a config packet");
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-03 06:52:22 +08:00
|
|
|
bool ok = recorder_write_header(recorder, packet);
|
2019-02-09 19:54:12 +08:00
|
|
|
if (!ok) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2019-02-09 19:54:12 +08:00
|
|
|
}
|
2019-03-03 06:52:22 +08:00
|
|
|
recorder->header_written = true;
|
2019-07-31 07:55:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->pts == AV_NOPTS_VALUE) {
|
|
|
|
// ignore config packets
|
|
|
|
return true;
|
2019-02-09 19:54:12 +08:00
|
|
|
}
|
|
|
|
|
2019-02-09 22:08:36 +08:00
|
|
|
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
|
|
|
|
|
|
|
static int
|
|
|
|
run_recorder(void *data) {
|
|
|
|
struct recorder *recorder = data;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
mutex_lock(recorder->mutex);
|
|
|
|
|
2019-08-02 05:15:47 +08:00
|
|
|
while (!recorder->stopped && queue_is_empty(&recorder->queue)) {
|
2019-07-31 07:55:40 +08:00
|
|
|
cond_wait(recorder->queue_cond, recorder->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if stopped is set, continue to process the remaining events (to
|
|
|
|
// finish the recording) before actually stopping
|
|
|
|
|
2019-08-02 05:15:47 +08:00
|
|
|
if (recorder->stopped && queue_is_empty(&recorder->queue)) {
|
2019-07-31 07:55:40 +08:00
|
|
|
mutex_unlock(recorder->mutex);
|
2019-08-09 00:57:20 +08:00
|
|
|
struct record_packet *last = recorder->previous;
|
|
|
|
if (last) {
|
|
|
|
// assign an arbitrary duration to the last packet
|
|
|
|
last->packet.duration = 100000;
|
|
|
|
bool ok = 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");
|
|
|
|
}
|
|
|
|
record_packet_delete(last);
|
|
|
|
}
|
2019-07-31 07:55:40 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:15:47 +08:00
|
|
|
struct record_packet *rec;
|
|
|
|
queue_take(&recorder->queue, next, &rec);
|
2019-07-31 07:55:40 +08:00
|
|
|
|
|
|
|
mutex_unlock(recorder->mutex);
|
|
|
|
|
2019-08-09 00:57:20 +08:00
|
|
|
// recorder->previous is only written from this thread, no need to lock
|
|
|
|
struct record_packet *previous = recorder->previous;
|
|
|
|
recorder->previous = rec;
|
|
|
|
|
|
|
|
if (!previous) {
|
|
|
|
// we just received the first packet
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we now know the duration of the previous packet
|
|
|
|
previous->packet.duration = rec->packet.pts - previous->packet.pts;
|
|
|
|
|
|
|
|
bool ok = recorder_write(recorder, &previous->packet);
|
|
|
|
record_packet_delete(previous);
|
2019-07-31 07:55:40 +08:00
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not record packet");
|
|
|
|
|
|
|
|
mutex_lock(recorder->mutex);
|
|
|
|
recorder->failed = true;
|
|
|
|
// discard pending packets
|
|
|
|
recorder_queue_clear(&recorder->queue);
|
|
|
|
mutex_unlock(recorder->mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("Recorder thread ended");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
recorder_start(struct recorder *recorder) {
|
|
|
|
LOGD("Starting recorder thread");
|
|
|
|
|
|
|
|
recorder->thread = SDL_CreateThread(run_recorder, "recorder", recorder);
|
|
|
|
if (!recorder->thread) {
|
|
|
|
LOGC("Could not start recorder thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recorder_stop(struct recorder *recorder) {
|
|
|
|
mutex_lock(recorder->mutex);
|
|
|
|
recorder->stopped = true;
|
|
|
|
cond_signal(recorder->queue_cond);
|
|
|
|
mutex_unlock(recorder->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recorder_join(struct recorder *recorder) {
|
|
|
|
SDL_WaitThread(recorder->thread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
recorder_push(struct recorder *recorder, const AVPacket *packet) {
|
|
|
|
mutex_lock(recorder->mutex);
|
|
|
|
SDL_assert(!recorder->stopped);
|
|
|
|
|
|
|
|
if (recorder->failed) {
|
|
|
|
// reject any new packet (this will stop the stream)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:15:47 +08:00
|
|
|
struct record_packet *rec = record_packet_new(packet);
|
|
|
|
if (!rec) {
|
|
|
|
LOGC("Could not allocate record packet");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_push(&recorder->queue, next, rec);
|
2019-07-31 07:55:40 +08:00
|
|
|
cond_signal(recorder->queue_cond);
|
|
|
|
|
|
|
|
mutex_unlock(recorder->mutex);
|
2019-08-02 05:15:47 +08:00
|
|
|
return true;
|
2019-07-31 07:55:40 +08:00
|
|
|
}
|