2018-11-09 11:21:17 +00:00
|
|
|
#include "recorder.h"
|
|
|
|
|
2019-11-27 20:11:40 +00:00
|
|
|
#include <assert.h>
|
2021-09-20 16:27:37 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
2018-11-09 11:21:17 +00:00
|
|
|
#include <libavutil/time.h>
|
|
|
|
|
2019-11-24 10:53:00 +00:00
|
|
|
#include "util/log.h"
|
2021-11-12 22:12:51 +00:00
|
|
|
#include "util/str.h"
|
2018-11-09 11:21:17 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
/** Downcast packet sinks to recorder */
|
2023-02-15 09:06:10 +00:00
|
|
|
#define DOWNCAST_VIDEO(SINK) \
|
|
|
|
container_of(SINK, struct sc_recorder, video_packet_sink)
|
2023-02-18 17:02:43 +00:00
|
|
|
#define DOWNCAST_AUDIO(SINK) \
|
|
|
|
container_of(SINK, struct sc_recorder, audio_packet_sink)
|
2021-04-11 13:01:05 +00:00
|
|
|
|
2019-02-09 14:08:36 +00:00
|
|
|
static const AVRational SCRCPY_TIME_BASE = {1, 1000000}; // timestamps in us
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
static const AVOutputFormat *
|
|
|
|
find_muxer(const char *name) {
|
2019-02-16 14:04:32 +00:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API
|
2018-11-09 11:21:17 +00:00
|
|
|
void *opaque = NULL;
|
2018-11-10 11:00:30 +00:00
|
|
|
#endif
|
|
|
|
const AVOutputFormat *oformat = NULL;
|
2018-11-09 11:21:17 +00:00
|
|
|
do {
|
2019-02-16 14:04:32 +00:00
|
|
|
#ifdef SCRCPY_LAVF_HAS_NEW_MUXER_ITERATOR_API
|
2018-11-09 11:21:17 +00:00
|
|
|
oformat = av_muxer_iterate(&opaque);
|
2018-11-10 11:00:30 +00:00
|
|
|
#else
|
|
|
|
oformat = av_oformat_next(oformat);
|
|
|
|
#endif
|
2021-04-19 07:28:28 +00:00
|
|
|
// until null or containing the requested name
|
2021-11-12 22:08:19 +00:00
|
|
|
} while (oformat && !sc_str_list_contains(oformat->name, ',', name));
|
2018-11-09 11:21:17 +00:00
|
|
|
return oformat;
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
static AVPacket *
|
|
|
|
sc_recorder_packet_ref(const AVPacket *packet) {
|
|
|
|
AVPacket *p = av_packet_alloc();
|
|
|
|
if (!p) {
|
2021-11-24 21:06:11 +00:00
|
|
|
LOG_OOM();
|
2021-06-14 07:07:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-08-13 16:49:25 +00:00
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
if (av_packet_ref(p, packet)) {
|
|
|
|
av_packet_free(&p);
|
2019-07-30 23:55:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
return p;
|
2019-07-30 23:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-02 18:37:28 +00:00
|
|
|
sc_recorder_queue_clear(struct sc_recorder_queue *queue) {
|
2023-03-01 20:42:51 +00:00
|
|
|
while (!sc_vecdeque_is_empty(queue)) {
|
|
|
|
AVPacket *p = sc_vecdeque_pop(queue);
|
|
|
|
av_packet_free(&p);
|
2019-07-30 23:55:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:20:07 +00:00
|
|
|
static const char *
|
2022-02-02 18:37:28 +00:00
|
|
|
sc_recorder_get_format_name(enum sc_record_format format) {
|
2019-02-09 14:20:07 +00:00
|
|
|
switch (format) {
|
2023-05-07 10:23:51 +00:00
|
|
|
case SC_RECORD_FORMAT_MP4:
|
|
|
|
case SC_RECORD_FORMAT_M4A:
|
2023-05-07 10:38:32 +00:00
|
|
|
case SC_RECORD_FORMAT_AAC:
|
2023-05-07 10:23:51 +00:00
|
|
|
return "mp4";
|
|
|
|
case SC_RECORD_FORMAT_MKV:
|
|
|
|
case SC_RECORD_FORMAT_MKA:
|
|
|
|
return "matroska";
|
2023-05-07 10:35:27 +00:00
|
|
|
case SC_RECORD_FORMAT_OPUS:
|
|
|
|
return "opus";
|
2023-11-07 12:09:47 +00:00
|
|
|
case SC_RECORD_FORMAT_FLAC:
|
|
|
|
return "flac";
|
2023-11-13 08:35:18 +00:00
|
|
|
case SC_RECORD_FORMAT_WAV:
|
|
|
|
return "wav";
|
2023-05-07 10:23:51 +00:00
|
|
|
default:
|
|
|
|
return NULL;
|
2019-02-09 14:20:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
static bool
|
2023-02-18 17:02:43 +00:00
|
|
|
sc_recorder_set_extradata(AVStream *ostream, const AVPacket *packet) {
|
2019-02-15 23:50:14 +00:00
|
|
|
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
|
2019-02-09 11:54:12 +00:00
|
|
|
if (!extradata) {
|
2021-11-24 21:06:11 +00:00
|
|
|
LOG_OOM();
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2019-02-09 11:54:12 +00: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 17:02:43 +00:00
|
|
|
return true;
|
2019-02-09 11:54:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
static inline void
|
|
|
|
sc_recorder_rescale_packet(AVStream *stream, AVPacket *packet) {
|
|
|
|
av_packet_rescale_ts(packet, SCRCPY_TIME_BASE, stream->time_base);
|
2019-02-09 14:08:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 14:41:57 +00:00
|
|
|
static bool
|
2023-06-03 12:41:40 +00:00
|
|
|
sc_recorder_write_stream(struct sc_recorder *recorder,
|
|
|
|
struct sc_recorder_stream *st, AVPacket *packet) {
|
|
|
|
AVStream *stream = recorder->ctx->streams[st->index];
|
2023-02-18 17:02:43 +00:00
|
|
|
sc_recorder_rescale_packet(stream, packet);
|
2023-06-03 12:52:53 +00:00
|
|
|
if (st->last_pts != AV_NOPTS_VALUE && packet->pts <= st->last_pts) {
|
2023-11-14 08:33:59 +00:00
|
|
|
LOGD("Fixing PTS non monotonically increasing in stream %d "
|
2023-06-03 12:52:53 +00:00
|
|
|
"(%" PRIi64 " >= %" PRIi64 ")",
|
|
|
|
st->index, st->last_pts, packet->pts);
|
|
|
|
packet->pts = ++st->last_pts;
|
|
|
|
packet->dts = packet->pts;
|
|
|
|
} else {
|
|
|
|
st->last_pts = packet->pts;
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
return av_interleaved_write_frame(recorder->ctx, packet) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
sc_recorder_write_video(struct sc_recorder *recorder, AVPacket *packet) {
|
2023-06-03 12:41:40 +00:00
|
|
|
return sc_recorder_write_stream(recorder, &recorder->video_stream, packet);
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
sc_recorder_write_audio(struct sc_recorder *recorder, AVPacket *packet) {
|
2023-06-03 12:41:40 +00:00
|
|
|
return sc_recorder_write_stream(recorder, &recorder->audio_stream, packet);
|
2018-11-09 11:21:17 +00:00
|
|
|
}
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2023-02-14 08:25:50 +00: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-30 23:55:40 +00:00
|
|
|
|
2023-02-14 08:25:50 +00: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);
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
static inline bool
|
2023-11-13 08:35:18 +00:00
|
|
|
sc_recorder_must_wait_for_config_packets(struct sc_recorder *recorder) {
|
2023-05-07 10:08:50 +00:00
|
|
|
if (recorder->video && sc_vecdeque_is_empty(&recorder->video_queue)) {
|
2023-02-18 17:02:43 +00:00
|
|
|
// The video queue is empty
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-13 08:35:18 +00:00
|
|
|
if (recorder->audio && recorder->audio_expects_config_packet
|
|
|
|
&& sc_vecdeque_is_empty(&recorder->audio_queue)) {
|
2023-02-18 17:02:43 +00:00
|
|
|
// The audio queue is empty (when audio is enabled)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No queue is empty
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-18 11:07:30 +00:00
|
|
|
static bool
|
|
|
|
sc_recorder_process_header(struct sc_recorder *recorder) {
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
|
2023-06-03 13:09:46 +00:00
|
|
|
while (!recorder->stopped &&
|
|
|
|
((recorder->video && !recorder->video_init)
|
|
|
|
|| (recorder->audio && !recorder->audio_init)
|
2023-11-13 08:35:18 +00:00
|
|
|
|| sc_recorder_must_wait_for_config_packets(recorder))) {
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_wait(&recorder->cond, &recorder->mutex);
|
2023-02-18 11:07:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
if (recorder->video && sc_vecdeque_is_empty(&recorder->video_queue)) {
|
2023-02-18 17:02:43 +00:00
|
|
|
assert(recorder->stopped);
|
2023-03-01 20:42:51 +00:00
|
|
|
// If the recorder is stopped, don't process anything if there are not
|
|
|
|
// at least video packets
|
2023-02-18 11:07:30 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
AVPacket *video_pkt = NULL;
|
|
|
|
if (!sc_vecdeque_is_empty(&recorder->video_queue)) {
|
|
|
|
assert(recorder->video);
|
|
|
|
video_pkt = sc_vecdeque_pop(&recorder->video_queue);
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
AVPacket *audio_pkt = NULL;
|
2023-11-13 08:35:18 +00:00
|
|
|
if (recorder->audio_expects_config_packet &&
|
|
|
|
!sc_vecdeque_is_empty(&recorder->audio_queue)) {
|
2023-02-18 23:55:36 +00:00
|
|
|
assert(recorder->audio);
|
2023-03-01 20:42:51 +00:00
|
|
|
audio_pkt = sc_vecdeque_pop(&recorder->audio_queue);
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
2023-02-18 11:07:30 +00:00
|
|
|
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
int ret = false;
|
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
if (video_pkt) {
|
|
|
|
if (video_pkt->pts != AV_NOPTS_VALUE) {
|
|
|
|
LOGE("The first video packet is not a config packet");
|
|
|
|
goto end;
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
assert(recorder->video_stream.index >= 0);
|
2023-05-07 10:08:50 +00:00
|
|
|
AVStream *video_stream =
|
2023-06-03 12:41:40 +00:00
|
|
|
recorder->ctx->streams[recorder->video_stream.index];
|
2023-05-07 10:08:50 +00:00
|
|
|
bool ok = sc_recorder_set_extradata(video_stream, video_pkt);
|
|
|
|
if (!ok) {
|
|
|
|
goto end;
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 23:55:36 +00:00
|
|
|
if (audio_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
if (audio_pkt->pts != AV_NOPTS_VALUE) {
|
2023-02-18 17:02:43 +00:00
|
|
|
LOGE("The first audio packet is not a config packet");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
assert(recorder->audio_stream.index >= 0);
|
2023-02-18 17:02:43 +00:00
|
|
|
AVStream *audio_stream =
|
2023-06-03 12:41:40 +00:00
|
|
|
recorder->ctx->streams[recorder->audio_stream.index];
|
2023-05-07 10:08:50 +00:00
|
|
|
bool ok = sc_recorder_set_extradata(audio_stream, audio_pkt);
|
2023-02-18 17:02:43 +00:00
|
|
|
if (!ok) {
|
|
|
|
goto end;
|
|
|
|
}
|
2023-02-18 11:07:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
bool ok = avformat_write_header(recorder->ctx, NULL) >= 0;
|
2023-02-18 11:07:30 +00:00
|
|
|
if (!ok) {
|
|
|
|
LOGE("Failed to write header to %s", recorder->filename);
|
2023-02-18 17:02:43 +00:00
|
|
|
goto end;
|
2023-02-18 11:07:30 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
end:
|
2023-05-07 10:08:50 +00:00
|
|
|
if (video_pkt) {
|
|
|
|
av_packet_free(&video_pkt);
|
|
|
|
}
|
2023-02-18 23:55:36 +00:00
|
|
|
if (audio_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
av_packet_free(&audio_pkt);
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2023-02-18 11:07:30 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 08:25:50 +00:00
|
|
|
static bool
|
|
|
|
sc_recorder_process_packets(struct sc_recorder *recorder) {
|
2023-02-17 07:46:11 +00:00
|
|
|
int64_t pts_origin = AV_NOPTS_VALUE;
|
|
|
|
|
2023-02-18 11:07:30 +00:00
|
|
|
bool header_written = sc_recorder_process_header(recorder);
|
|
|
|
if (!header_written) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
AVPacket *video_pkt = NULL;
|
|
|
|
AVPacket *audio_pkt = NULL;
|
2023-02-18 17:02:43 +00:00
|
|
|
|
|
|
|
// We can write a video packet only once we received the next one so that
|
|
|
|
// we can set its duration (next_pts - current_pts)
|
2023-03-01 20:42:51 +00:00
|
|
|
AVPacket *video_pkt_previous = NULL;
|
2023-02-18 17:02:43 +00:00
|
|
|
|
|
|
|
bool error = false;
|
|
|
|
|
2019-07-30 23:55:40 +00:00
|
|
|
for (;;) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
while (!recorder->stopped) {
|
2023-05-07 10:08:50 +00:00
|
|
|
if (recorder->video && !video_pkt &&
|
|
|
|
!sc_vecdeque_is_empty(&recorder->video_queue)) {
|
2023-02-18 17:02:43 +00:00
|
|
|
// A new packet may be assigned to video_pkt and be processed
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (recorder->audio && !audio_pkt
|
2023-03-01 20:42:51 +00:00
|
|
|
&& !sc_vecdeque_is_empty(&recorder->audio_queue)) {
|
2023-02-18 17:02:43 +00:00
|
|
|
// A new packet may be assigned to audio_pkt and be processed
|
|
|
|
break;
|
|
|
|
}
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_wait(&recorder->cond, &recorder->mutex);
|
2019-07-30 23:55:40 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
// If stopped is set, continue to process the remaining events (to
|
|
|
|
// finish the recording) before actually stopping.
|
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
// If there is no video, then the video_queue will remain empty forever
|
|
|
|
// and video_pkt will always be NULL.
|
|
|
|
assert(recorder->video || (!video_pkt
|
|
|
|
&& sc_vecdeque_is_empty(&recorder->video_queue)));
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
// If there is no audio, then the audio_queue will remain empty forever
|
|
|
|
// and audio_pkt will always be NULL.
|
2023-03-01 20:42:51 +00:00
|
|
|
assert(recorder->audio || (!audio_pkt
|
|
|
|
&& sc_vecdeque_is_empty(&recorder->audio_queue)));
|
2023-02-18 17:02:43 +00:00
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
if (!video_pkt && !sc_vecdeque_is_empty(&recorder->video_queue)) {
|
|
|
|
video_pkt = sc_vecdeque_pop(&recorder->video_queue);
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
if (!audio_pkt && !sc_vecdeque_is_empty(&recorder->audio_queue)) {
|
|
|
|
audio_pkt = sc_vecdeque_pop(&recorder->audio_queue);
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
if (recorder->stopped && !video_pkt && !audio_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
assert(sc_vecdeque_is_empty(&recorder->video_queue));
|
|
|
|
assert(sc_vecdeque_is_empty(&recorder->audio_queue));
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-30 23:55:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
assert(video_pkt || audio_pkt); // at least one
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
// Ignore further config packets (e.g. on device orientation
|
|
|
|
// change). The next non-config packet will have the config packet
|
|
|
|
// data prepended.
|
2023-03-01 20:42:51 +00:00
|
|
|
if (video_pkt && video_pkt->pts == AV_NOPTS_VALUE) {
|
|
|
|
av_packet_free(&video_pkt);
|
2023-02-18 17:02:43 +00:00
|
|
|
video_pkt = NULL;
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
if (audio_pkt && audio_pkt->pts == AV_NOPTS_VALUE) {
|
|
|
|
av_packet_free(&audio_pkt);
|
|
|
|
audio_pkt = NULL;
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pts_origin == AV_NOPTS_VALUE) {
|
|
|
|
if (!recorder->audio) {
|
|
|
|
assert(video_pkt);
|
2023-03-01 20:42:51 +00:00
|
|
|
pts_origin = video_pkt->pts;
|
2023-05-07 10:08:50 +00:00
|
|
|
} else if (!recorder->video) {
|
|
|
|
assert(audio_pkt);
|
|
|
|
pts_origin = audio_pkt->pts;
|
2023-02-18 17:02:43 +00:00
|
|
|
} else if (video_pkt && audio_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
pts_origin = MIN(video_pkt->pts, audio_pkt->pts);
|
2023-02-18 23:55:36 +00:00
|
|
|
} else if (recorder->stopped) {
|
|
|
|
if (video_pkt) {
|
|
|
|
// The recorder is stopped without audio, record the video
|
|
|
|
// packets
|
2023-03-01 20:42:51 +00:00
|
|
|
pts_origin = video_pkt->pts;
|
2023-02-18 23:55:36 +00:00
|
|
|
} else {
|
|
|
|
// Fail if there is no video
|
|
|
|
error = true;
|
|
|
|
goto end;
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
} else {
|
|
|
|
// We need both video and audio packets to initialize pts_origin
|
2023-02-18 11:07:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
2023-02-18 11:07:30 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
assert(pts_origin != AV_NOPTS_VALUE);
|
|
|
|
|
|
|
|
if (video_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
video_pkt->pts -= pts_origin;
|
|
|
|
video_pkt->dts = video_pkt->pts;
|
2023-02-18 17:02:43 +00:00
|
|
|
|
|
|
|
if (video_pkt_previous) {
|
|
|
|
// we now know the duration of the previous packet
|
2023-03-01 20:42:51 +00:00
|
|
|
video_pkt_previous->duration = video_pkt->pts
|
|
|
|
- video_pkt_previous->pts;
|
2023-02-18 17:02:43 +00:00
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
bool ok = sc_recorder_write_video(recorder, video_pkt_previous);
|
|
|
|
av_packet_free(&video_pkt_previous);
|
2023-02-18 17:02:43 +00:00
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not record video packet");
|
|
|
|
error = true;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 20:56:43 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
video_pkt_previous = video_pkt;
|
|
|
|
video_pkt = NULL;
|
|
|
|
}
|
2023-02-01 20:56:43 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
if (audio_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
audio_pkt->pts -= pts_origin;
|
|
|
|
audio_pkt->dts = audio_pkt->pts;
|
2019-08-08 16:57:20 +00:00
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
bool ok = sc_recorder_write_audio(recorder, audio_pkt);
|
2023-02-18 11:07:30 +00:00
|
|
|
if (!ok) {
|
2023-02-18 17:02:43 +00:00
|
|
|
LOGE("Could not record audio packet");
|
|
|
|
error = true;
|
|
|
|
goto end;
|
2023-02-18 11:07:30 +00:00
|
|
|
}
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
av_packet_free(&audio_pkt);
|
2023-02-18 17:02:43 +00:00
|
|
|
audio_pkt = NULL;
|
2019-07-30 23:55:40 +00:00
|
|
|
}
|
2021-04-11 13:01:05 +00:00
|
|
|
}
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
// Write the last video packet
|
2023-03-01 20:42:51 +00:00
|
|
|
AVPacket *last = video_pkt_previous;
|
2023-02-18 11:07:05 +00:00
|
|
|
if (last) {
|
|
|
|
// assign an arbitrary duration to the last packet
|
2023-03-01 20:42:51 +00:00
|
|
|
last->duration = 100000;
|
|
|
|
bool ok = sc_recorder_write_video(recorder, last);
|
2023-02-18 11:07:05 +00:00
|
|
|
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");
|
|
|
|
}
|
2023-03-01 20:42:51 +00:00
|
|
|
av_packet_free(&last);
|
2023-02-18 11:07:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 08:25:50 +00:00
|
|
|
int ret = av_write_trailer(recorder->ctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOGE("Failed to write trailer to %s", recorder->filename);
|
2023-02-18 17:02:43 +00:00
|
|
|
error = false;
|
2023-02-14 08:25:50 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
end:
|
|
|
|
if (video_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
av_packet_free(&video_pkt);
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
|
|
|
if (audio_pkt) {
|
2023-03-01 20:42:51 +00:00
|
|
|
av_packet_free(&audio_pkt);
|
2023-02-18 17:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return !error;
|
2023-02-14 08:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_recorder_record(struct sc_recorder *recorder) {
|
|
|
|
bool ok = sc_recorder_open_output_file(recorder);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-03-08 20:40:39 +00:00
|
|
|
// Recording is a background task
|
|
|
|
bool ok = sc_thread_set_priority(SC_THREAD_PRIORITY_LOW);
|
|
|
|
(void) ok; // We don't care if it worked
|
|
|
|
|
2023-02-14 08:25:50 +00:00
|
|
|
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 09:06:10 +00:00
|
|
|
sc_recorder_queue_clear(&recorder->video_queue);
|
2023-02-18 17:02:43 +00:00
|
|
|
sc_recorder_queue_clear(&recorder->audio_queue);
|
2023-02-14 08:25:50 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
|
|
|
|
if (success) {
|
2022-02-02 18:37:28 +00:00
|
|
|
const char *format_name = sc_recorder_get_format_name(recorder->format);
|
2021-07-15 16:07:39 +00:00
|
|
|
LOGI("Recording complete to %s file: %s", format_name,
|
|
|
|
recorder->filename);
|
2023-02-14 08:25:50 +00:00
|
|
|
} else {
|
|
|
|
LOGE("Recording failed to %s", recorder->filename);
|
2019-07-30 23:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("Recorder thread ended");
|
|
|
|
|
2023-02-14 08:25:50 +00:00
|
|
|
recorder->cbs->on_ended(recorder, success, recorder->cbs_userdata);
|
2023-02-10 17:10:24 +00:00
|
|
|
|
2019-07-30 23:55:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:01:05 +00:00
|
|
|
static bool
|
2023-02-15 09:06:10 +00:00
|
|
|
sc_recorder_video_packet_sink_open(struct sc_packet_sink *sink,
|
2023-03-10 18:25:45 +00:00
|
|
|
AVCodecContext *ctx) {
|
2023-02-15 09:06:10 +00:00
|
|
|
struct sc_recorder *recorder = DOWNCAST_VIDEO(sink);
|
2023-03-10 20:50:34 +00:00
|
|
|
// only written from this thread, no need to lock
|
|
|
|
assert(!recorder->video_init);
|
2023-02-14 08:37:36 +00:00
|
|
|
|
2023-02-14 08:25:50 +00:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
if (recorder->stopped) {
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2023-02-14 07:40:44 +00:00
|
|
|
return false;
|
2021-04-11 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:50:34 +00:00
|
|
|
AVStream *stream = avformat_new_stream(recorder->ctx, ctx->codec);
|
|
|
|
if (!stream) {
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-10 21:46:56 +00:00
|
|
|
int r = avcodec_parameters_from_context(stream->codecpar, ctx);
|
|
|
|
if (r < 0) {
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
recorder->video_stream.index = stream->index;
|
2023-03-10 20:50:34 +00:00
|
|
|
|
|
|
|
recorder->video_init = true;
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2023-02-14 08:25:50 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2021-04-11 13:01:05 +00:00
|
|
|
|
2019-07-30 23:55:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:01:05 +00:00
|
|
|
static void
|
2023-02-15 09:06:10 +00:00
|
|
|
sc_recorder_video_packet_sink_close(struct sc_packet_sink *sink) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_VIDEO(sink);
|
2023-03-10 20:50:34 +00:00
|
|
|
// only written from this thread, no need to lock
|
|
|
|
assert(recorder->video_init);
|
2023-02-14 08:37:36 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2023-02-14 08:25:50 +00:00
|
|
|
// EOS also stops the recorder
|
2019-07-30 23:55:40 +00:00
|
|
|
recorder->stopped = true;
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-30 23:55:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 13:01:05 +00:00
|
|
|
static bool
|
2023-02-15 09:06:10 +00:00
|
|
|
sc_recorder_video_packet_sink_push(struct sc_packet_sink *sink,
|
|
|
|
const AVPacket *packet) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_VIDEO(sink);
|
2023-03-10 20:50:34 +00:00
|
|
|
// only written from this thread, no need to lock
|
|
|
|
assert(recorder->video_init);
|
2023-02-14 08:37:36 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2023-02-14 08:25:50 +00:00
|
|
|
if (recorder->stopped) {
|
|
|
|
// reject any new packet
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-07-30 23:55:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
AVPacket *rec = sc_recorder_packet_ref(packet);
|
2019-08-01 21:15:47 +00:00
|
|
|
if (!rec) {
|
2021-11-24 21:06:11 +00:00
|
|
|
LOG_OOM();
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-08-01 21:15:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
rec->stream_index = recorder->video_stream.index;
|
2023-03-01 20:42:51 +00:00
|
|
|
|
|
|
|
bool ok = sc_vecdeque_push(&recorder->video_queue, rec);
|
|
|
|
if (!ok) {
|
|
|
|
LOG_OOM();
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2019-07-30 23:55:40 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
2019-08-01 21:15:47 +00:00
|
|
|
return true;
|
2019-07-30 23:55:40 +00:00
|
|
|
}
|
2021-04-11 13:01:05 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
static bool
|
|
|
|
sc_recorder_audio_packet_sink_open(struct sc_packet_sink *sink,
|
2023-03-10 18:25:45 +00:00
|
|
|
AVCodecContext *ctx) {
|
2023-02-18 17:02:43 +00:00
|
|
|
struct sc_recorder *recorder = DOWNCAST_AUDIO(sink);
|
|
|
|
assert(recorder->audio);
|
2023-02-18 17:09:18 +00:00
|
|
|
// only written from this thread, no need to lock
|
2023-03-10 20:50:34 +00:00
|
|
|
assert(!recorder->audio_init);
|
2023-02-18 17:02:43 +00:00
|
|
|
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2023-03-10 20:50:34 +00:00
|
|
|
|
|
|
|
AVStream *stream = avformat_new_stream(recorder->ctx, ctx->codec);
|
|
|
|
if (!stream) {
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-10 21:46:56 +00:00
|
|
|
int r = avcodec_parameters_from_context(stream->codecpar, ctx);
|
|
|
|
if (r < 0) {
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-10 20:50:34 +00:00
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
recorder->audio_stream.index = stream->index;
|
2023-03-10 20:50:34 +00:00
|
|
|
|
2023-11-13 08:35:18 +00:00
|
|
|
// A config packet is provided for all supported formats except raw audio
|
|
|
|
recorder->audio_expects_config_packet =
|
|
|
|
ctx->codec_id != AV_CODEC_ID_PCM_S16LE;
|
|
|
|
|
2023-03-10 20:50:34 +00:00
|
|
|
recorder->audio_init = true;
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2023-02-18 17:02:43 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sc_recorder_audio_packet_sink_close(struct sc_packet_sink *sink) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_AUDIO(sink);
|
|
|
|
assert(recorder->audio);
|
2023-02-18 17:09:18 +00:00
|
|
|
// only written from this thread, no need to lock
|
2023-03-10 20:50:34 +00:00
|
|
|
assert(recorder->audio_init);
|
2023-02-18 17:02:43 +00:00
|
|
|
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
// EOS also stops the recorder
|
|
|
|
recorder->stopped = true;
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2023-02-18 17:02:43 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sc_recorder_audio_packet_sink_push(struct sc_packet_sink *sink,
|
|
|
|
const AVPacket *packet) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_AUDIO(sink);
|
|
|
|
assert(recorder->audio);
|
2023-02-18 17:09:18 +00:00
|
|
|
// only written from this thread, no need to lock
|
2023-03-10 20:50:34 +00:00
|
|
|
assert(recorder->audio_init);
|
2023-02-18 17:02:43 +00:00
|
|
|
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
|
|
|
|
if (recorder->stopped) {
|
|
|
|
// reject any new packet
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
AVPacket *rec = sc_recorder_packet_ref(packet);
|
2023-02-18 17:02:43 +00:00
|
|
|
if (!rec) {
|
|
|
|
LOG_OOM();
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
rec->stream_index = recorder->audio_stream.index;
|
2023-03-01 20:42:51 +00:00
|
|
|
|
|
|
|
bool ok = sc_vecdeque_push(&recorder->audio_queue, rec);
|
|
|
|
if (!ok) {
|
|
|
|
LOG_OOM();
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-18 17:02:43 +00:00
|
|
|
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2023-02-18 17:02:43 +00:00
|
|
|
|
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:09:18 +00:00
|
|
|
static void
|
|
|
|
sc_recorder_audio_packet_sink_disable(struct sc_packet_sink *sink) {
|
|
|
|
struct sc_recorder *recorder = DOWNCAST_AUDIO(sink);
|
|
|
|
assert(recorder->audio);
|
|
|
|
// only written from this thread, no need to lock
|
2023-03-10 20:50:34 +00:00
|
|
|
assert(!recorder->audio_init);
|
2023-02-18 17:09:18 +00:00
|
|
|
|
|
|
|
LOGW("Audio stream recording disabled");
|
|
|
|
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
2023-03-10 20:50:34 +00:00
|
|
|
recorder->audio = false;
|
|
|
|
recorder->audio_init = true;
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2023-02-18 17:09:18 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
}
|
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
static void
|
|
|
|
sc_recorder_stream_init(struct sc_recorder_stream *stream) {
|
|
|
|
stream->index = -1;
|
2023-06-03 12:52:53 +00:00
|
|
|
stream->last_pts = AV_NOPTS_VALUE;
|
2023-06-03 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 13:01:05 +00:00
|
|
|
bool
|
2023-02-10 17:10:24 +00:00
|
|
|
sc_recorder_init(struct sc_recorder *recorder, const char *filename,
|
2023-05-07 10:08:50 +00:00
|
|
|
enum sc_record_format format, bool video, bool audio,
|
2023-02-10 17:10:24 +00:00
|
|
|
const struct sc_recorder_callbacks *cbs, void *cbs_userdata) {
|
2021-04-11 13:01:05 +00:00
|
|
|
recorder->filename = strdup(filename);
|
|
|
|
if (!recorder->filename) {
|
2021-11-24 21:06:11 +00:00
|
|
|
LOG_OOM();
|
2021-04-11 13:01:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-14 07:40:44 +00:00
|
|
|
bool ok = sc_mutex_init(&recorder->mutex);
|
|
|
|
if (!ok) {
|
|
|
|
goto error_free_filename;
|
|
|
|
}
|
|
|
|
|
2023-06-03 13:05:23 +00:00
|
|
|
ok = sc_cond_init(&recorder->cond);
|
2023-02-14 07:40:44 +00:00
|
|
|
if (!ok) {
|
|
|
|
goto error_mutex_destroy;
|
|
|
|
}
|
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
assert(video || audio);
|
|
|
|
recorder->video = video;
|
2023-02-18 17:02:43 +00:00
|
|
|
recorder->audio = audio;
|
|
|
|
|
2023-03-01 20:42:51 +00:00
|
|
|
sc_vecdeque_init(&recorder->video_queue);
|
|
|
|
sc_vecdeque_init(&recorder->audio_queue);
|
2023-02-14 07:40:44 +00:00
|
|
|
recorder->stopped = false;
|
|
|
|
|
2023-03-10 20:50:34 +00:00
|
|
|
recorder->video_init = false;
|
|
|
|
recorder->audio_init = false;
|
2023-02-18 17:02:43 +00:00
|
|
|
|
2023-11-13 08:35:18 +00:00
|
|
|
recorder->audio_expects_config_packet = false;
|
|
|
|
|
2023-06-03 12:41:40 +00:00
|
|
|
sc_recorder_stream_init(&recorder->video_stream);
|
|
|
|
sc_recorder_stream_init(&recorder->audio_stream);
|
2023-02-14 08:25:50 +00:00
|
|
|
|
2021-04-11 13:01:05 +00:00
|
|
|
recorder->format = format;
|
|
|
|
|
2023-02-10 17:10:24 +00:00
|
|
|
assert(cbs && cbs->on_ended);
|
|
|
|
recorder->cbs = cbs;
|
|
|
|
recorder->cbs_userdata = cbs_userdata;
|
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
if (video) {
|
|
|
|
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 13:01:05 +00:00
|
|
|
|
2023-05-07 10:08:50 +00:00
|
|
|
recorder->video_packet_sink.ops = &video_ops;
|
|
|
|
}
|
2021-04-11 13:01:05 +00:00
|
|
|
|
2023-02-18 17:02:43 +00:00
|
|
|
if (audio) {
|
|
|
|
static const struct sc_packet_sink_ops audio_ops = {
|
|
|
|
.open = sc_recorder_audio_packet_sink_open,
|
|
|
|
.close = sc_recorder_audio_packet_sink_close,
|
|
|
|
.push = sc_recorder_audio_packet_sink_push,
|
2023-02-18 17:09:18 +00:00
|
|
|
.disable = sc_recorder_audio_packet_sink_disable,
|
2023-02-18 17:02:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
recorder->audio_packet_sink.ops = &audio_ops;
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:01:05 +00:00
|
|
|
return true;
|
2023-02-14 07:40:44 +00:00
|
|
|
|
|
|
|
error_mutex_destroy:
|
|
|
|
sc_mutex_destroy(&recorder->mutex);
|
|
|
|
error_free_filename:
|
|
|
|
free(recorder->filename);
|
|
|
|
|
|
|
|
return false;
|
2021-04-11 13:01:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 10:00:34 +00: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 08:25:50 +00:00
|
|
|
void
|
|
|
|
sc_recorder_stop(struct sc_recorder *recorder) {
|
|
|
|
sc_mutex_lock(&recorder->mutex);
|
|
|
|
recorder->stopped = true;
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_signal(&recorder->cond);
|
2023-02-14 08:25:50 +00:00
|
|
|
sc_mutex_unlock(&recorder->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_recorder_join(struct sc_recorder *recorder) {
|
|
|
|
sc_thread_join(&recorder->thread, NULL);
|
|
|
|
}
|
|
|
|
|
2021-04-11 13:01:05 +00:00
|
|
|
void
|
2022-02-02 18:37:28 +00:00
|
|
|
sc_recorder_destroy(struct sc_recorder *recorder) {
|
2023-06-03 13:05:23 +00:00
|
|
|
sc_cond_destroy(&recorder->cond);
|
2023-02-14 07:40:44 +00:00
|
|
|
sc_mutex_destroy(&recorder->mutex);
|
2021-04-11 13:01:05 +00:00
|
|
|
free(recorder->filename);
|
|
|
|
}
|