2022-02-03 02:37:28 +08:00
|
|
|
#ifndef SC_RECORDER_H
|
|
|
|
#define SC_RECORDER_H
|
2018-11-09 19:21:17 +08:00
|
|
|
|
2021-01-09 02:24:51 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
#include <stdbool.h>
|
2018-11-09 19:21:17 +08:00
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
2021-01-09 02:15:29 +08:00
|
|
|
#include "coords.h"
|
2021-10-28 00:43:47 +08:00
|
|
|
#include "options.h"
|
2021-04-11 21:01:05 +08:00
|
|
|
#include "trait/packet_sink.h"
|
2021-02-01 01:24:35 +08:00
|
|
|
#include "util/thread.h"
|
2023-03-02 04:42:51 +08:00
|
|
|
#include "util/vecdeque.h"
|
2018-11-09 19:21:17 +08:00
|
|
|
|
2023-03-02 04:42:51 +08:00
|
|
|
struct sc_recorder_queue SC_VECDEQUE(AVPacket *);
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2022-02-03 02:37:28 +08:00
|
|
|
struct sc_recorder {
|
2023-02-19 01:02:43 +08:00
|
|
|
struct sc_packet_sink video_packet_sink;
|
|
|
|
struct sc_packet_sink audio_packet_sink;
|
|
|
|
|
2023-02-19 01:09:18 +08:00
|
|
|
/* The audio flag is unprotected:
|
|
|
|
* - it is initialized from sc_recorder_init() from the main thread;
|
|
|
|
* - it may be reset once from the recorder thread if the audio is
|
|
|
|
* disabled dynamically.
|
|
|
|
*
|
|
|
|
* Therefore, once the recorder thread is started, only the recorder thread
|
|
|
|
* may access it without data races.
|
|
|
|
*/
|
2023-02-19 01:02:43 +08:00
|
|
|
bool audio;
|
2021-04-11 21:01:05 +08:00
|
|
|
|
2018-11-09 19:21:17 +08:00
|
|
|
char *filename;
|
2020-06-20 04:04:06 +08:00
|
|
|
enum sc_record_format format;
|
2018-11-09 19:21:17 +08:00
|
|
|
AVFormatContext *ctx;
|
2021-10-30 21:20:39 +08:00
|
|
|
struct sc_size declared_frame_size;
|
2019-07-31 07:55:40 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_thread thread;
|
|
|
|
sc_mutex mutex;
|
|
|
|
sc_cond queue_cond;
|
2023-02-14 16:25:50 +08:00
|
|
|
// set on sc_recorder_stop(), packet_sink close or recording failure
|
|
|
|
bool stopped;
|
2023-02-15 17:06:10 +08:00
|
|
|
struct sc_recorder_queue video_queue;
|
2023-02-19 01:02:43 +08:00
|
|
|
struct sc_recorder_queue audio_queue;
|
2023-02-11 01:10:24 +08:00
|
|
|
|
2023-02-19 01:02:43 +08:00
|
|
|
// wake up the recorder thread once the video or audio codec is known
|
2023-02-14 16:25:50 +08:00
|
|
|
sc_cond stream_cond;
|
2023-02-15 17:06:10 +08:00
|
|
|
const AVCodec *video_codec;
|
2023-02-19 01:02:43 +08:00
|
|
|
const AVCodec *audio_codec;
|
2023-02-19 01:09:18 +08:00
|
|
|
// Instead of providing an audio_codec, the demuxer may notify that the
|
|
|
|
// stream is disabled if the device could not capture audio
|
|
|
|
bool audio_disabled;
|
2023-02-19 01:02:43 +08:00
|
|
|
|
|
|
|
int video_stream_index;
|
|
|
|
int audio_stream_index;
|
2023-02-14 16:25:50 +08:00
|
|
|
|
2023-02-11 01:10:24 +08:00
|
|
|
const struct sc_recorder_callbacks *cbs;
|
|
|
|
void *cbs_userdata;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sc_recorder_callbacks {
|
|
|
|
void (*on_ended)(struct sc_recorder *recorder, bool success,
|
|
|
|
void *userdata);
|
2018-11-09 19:21:17 +08:00
|
|
|
};
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_init(struct sc_recorder *recorder, const char *filename,
|
2023-02-19 01:02:43 +08:00
|
|
|
enum sc_record_format format, bool audio,
|
2023-02-11 01:10:24 +08:00
|
|
|
struct sc_size declared_frame_size,
|
|
|
|
const struct sc_recorder_callbacks *cbs, void *cbs_userdata);
|
2019-02-09 22:20:07 +08:00
|
|
|
|
2023-02-23 18:00:34 +08:00
|
|
|
bool
|
|
|
|
sc_recorder_start(struct sc_recorder *recorder);
|
|
|
|
|
2023-02-14 16:25:50 +08:00
|
|
|
void
|
|
|
|
sc_recorder_stop(struct sc_recorder *recorder);
|
|
|
|
|
|
|
|
void
|
|
|
|
sc_recorder_join(struct sc_recorder *recorder);
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
2022-02-03 02:37:28 +08:00
|
|
|
sc_recorder_destroy(struct sc_recorder *recorder);
|
2018-11-09 19:21:17 +08:00
|
|
|
|
|
|
|
#endif
|