2016-02-23 23:16:51 +00:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <obs-module.h>
|
|
|
|
#include <obs-avc.h>
|
|
|
|
#include <util/platform.h>
|
|
|
|
#include <util/dstr.h>
|
|
|
|
#include <util/threading.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include "flv-mux.h"
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
#define do_log(level, format, ...) \
|
2016-02-23 23:16:51 +00:00
|
|
|
blog(level, "[flv output: '%s'] " format, \
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_output_get_name(stream->output), ##__VA_ARGS__)
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
#define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
|
|
|
|
#define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
struct flv_output {
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_output_t *output;
|
|
|
|
struct dstr path;
|
|
|
|
FILE *file;
|
|
|
|
volatile bool active;
|
|
|
|
volatile bool stopping;
|
|
|
|
uint64_t stop_ts;
|
|
|
|
bool sent_headers;
|
|
|
|
int64_t last_packet_ts;
|
2018-02-19 19:54:37 +00:00
|
|
|
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
bool got_first_video;
|
|
|
|
int32_t start_dts_offset;
|
2016-02-23 23:16:51 +00:00
|
|
|
};
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
static inline bool stopping(struct flv_output *stream)
|
|
|
|
{
|
|
|
|
return os_atomic_load_bool(&stream->stopping);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool active(struct flv_output *stream)
|
|
|
|
{
|
|
|
|
return os_atomic_load_bool(&stream->active);
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
static const char *flv_output_getname(void *unused)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
return obs_module_text("FLVOutput");
|
|
|
|
}
|
|
|
|
|
2016-08-28 12:07:43 +00:00
|
|
|
static void flv_output_stop(void *data, uint64_t ts);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
static void flv_output_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct flv_output *stream = data;
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
pthread_mutex_destroy(&stream->mutex);
|
2016-02-23 23:16:51 +00:00
|
|
|
dstr_free(&stream->path);
|
|
|
|
bfree(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *flv_output_create(obs_data_t *settings, obs_output_t *output)
|
|
|
|
{
|
|
|
|
struct flv_output *stream = bzalloc(sizeof(struct flv_output));
|
|
|
|
stream->output = output;
|
2018-02-19 19:54:37 +00:00
|
|
|
pthread_mutex_init(&stream->mutex, NULL);
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
UNUSED_PARAMETER(settings);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_packet(struct flv_output *stream,
|
2019-09-22 21:19:10 +00:00
|
|
|
struct encoder_packet *packet, bool is_header)
|
2016-02-23 23:16:51 +00:00
|
|
|
{
|
|
|
|
uint8_t *data;
|
2019-09-22 21:19:10 +00:00
|
|
|
size_t size;
|
|
|
|
int ret = 0;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
stream->last_packet_ts = get_ms_time(packet, packet->dts);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
flv_packet_mux(packet, is_header ? 0 : stream->start_dts_offset, &data,
|
|
|
|
&size, is_header);
|
2016-02-23 23:16:51 +00:00
|
|
|
fwrite(data, 1, size, stream->file);
|
|
|
|
bfree(data);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_meta_data(struct flv_output *stream)
|
|
|
|
{
|
|
|
|
uint8_t *meta_data;
|
2019-09-22 21:19:10 +00:00
|
|
|
size_t meta_data_size;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2020-10-01 20:15:25 +00:00
|
|
|
flv_meta_data(stream->output, &meta_data, &meta_data_size, true);
|
2016-02-23 23:16:51 +00:00
|
|
|
fwrite(meta_data, 1, meta_data_size, stream->file);
|
|
|
|
bfree(meta_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_audio_header(struct flv_output *stream)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_output_t *context = stream->output;
|
2016-02-23 23:16:51 +00:00
|
|
|
obs_encoder_t *aencoder = obs_output_get_audio_encoder(context, 0);
|
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
struct encoder_packet packet = {.type = OBS_ENCODER_AUDIO,
|
|
|
|
.timebase_den = 1};
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
obs_encoder_get_extra_data(aencoder, &packet.data, &packet.size);
|
2016-02-23 23:16:51 +00:00
|
|
|
write_packet(stream, &packet, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_video_header(struct flv_output *stream)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_output_t *context = stream->output;
|
2016-02-23 23:16:51 +00:00
|
|
|
obs_encoder_t *vencoder = obs_output_get_video_encoder(context);
|
2019-09-22 21:19:10 +00:00
|
|
|
uint8_t *header;
|
|
|
|
size_t size;
|
2016-02-23 23:16:51 +00:00
|
|
|
|
2019-09-22 21:19:10 +00:00
|
|
|
struct encoder_packet packet = {
|
|
|
|
.type = OBS_ENCODER_VIDEO, .timebase_den = 1, .keyframe = true};
|
2016-02-23 23:16:51 +00:00
|
|
|
|
|
|
|
obs_encoder_get_extra_data(vencoder, &header, &size);
|
|
|
|
packet.size = obs_parse_avc_header(&packet.data, header, size);
|
|
|
|
write_packet(stream, &packet, true);
|
2018-02-19 19:54:37 +00:00
|
|
|
bfree(packet.data);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_headers(struct flv_output *stream)
|
|
|
|
{
|
|
|
|
write_meta_data(stream);
|
|
|
|
write_video_header(stream);
|
2018-02-19 19:54:37 +00:00
|
|
|
write_audio_header(stream);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool flv_output_start(void *data)
|
|
|
|
{
|
|
|
|
struct flv_output *stream = data;
|
|
|
|
obs_data_t *settings;
|
|
|
|
const char *path;
|
|
|
|
|
|
|
|
if (!obs_output_can_begin_data_capture(stream->output, 0))
|
|
|
|
return false;
|
|
|
|
if (!obs_output_initialize_encoders(stream->output, 0))
|
|
|
|
return false;
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
stream->got_first_video = false;
|
|
|
|
stream->sent_headers = false;
|
|
|
|
os_atomic_set_bool(&stream->stopping, false);
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
/* get path */
|
|
|
|
settings = obs_output_get_settings(stream->output);
|
|
|
|
path = obs_data_get_string(settings, "path");
|
|
|
|
dstr_copy(&stream->path, path);
|
|
|
|
obs_data_release(settings);
|
|
|
|
|
|
|
|
stream->file = os_fopen(stream->path.array, "wb");
|
|
|
|
if (!stream->file) {
|
|
|
|
warn("Unable to open FLV file '%s'", stream->path.array);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write headers and start capture */
|
2018-02-19 19:54:37 +00:00
|
|
|
os_atomic_set_bool(&stream->active, true);
|
2016-02-23 23:16:51 +00:00
|
|
|
obs_output_begin_data_capture(stream->output, 0);
|
|
|
|
|
|
|
|
info("Writing FLV file '%s'...", stream->path.array);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
static void flv_output_stop(void *data, uint64_t ts)
|
|
|
|
{
|
|
|
|
struct flv_output *stream = data;
|
|
|
|
stream->stop_ts = ts / 1000;
|
|
|
|
os_atomic_set_bool(&stream->stopping, true);
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
static void flv_output_actual_stop(struct flv_output *stream, int code)
|
2018-02-19 19:54:37 +00:00
|
|
|
{
|
|
|
|
os_atomic_set_bool(&stream->active, false);
|
|
|
|
|
|
|
|
if (stream->file) {
|
|
|
|
write_file_info(stream->file, stream->last_packet_ts,
|
|
|
|
os_ftelli64(stream->file));
|
|
|
|
|
|
|
|
fclose(stream->file);
|
|
|
|
}
|
2019-07-27 12:47:10 +00:00
|
|
|
if (code) {
|
|
|
|
obs_output_signal_stop(stream->output, code);
|
|
|
|
} else {
|
|
|
|
obs_output_end_data_capture(stream->output);
|
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
|
|
|
info("FLV file output complete");
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
static void flv_output_data(void *data, struct encoder_packet *packet)
|
|
|
|
{
|
2019-09-22 21:19:10 +00:00
|
|
|
struct flv_output *stream = data;
|
2016-02-23 23:16:51 +00:00
|
|
|
struct encoder_packet parsed_packet;
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
pthread_mutex_lock(&stream->mutex);
|
|
|
|
|
|
|
|
if (!active(stream))
|
|
|
|
goto unlock;
|
|
|
|
|
2019-07-27 12:47:10 +00:00
|
|
|
if (!packet) {
|
|
|
|
flv_output_actual_stop(stream, OBS_OUTPUT_ENCODE_ERROR);
|
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
|
2018-02-19 19:54:37 +00:00
|
|
|
if (stopping(stream)) {
|
|
|
|
if (packet->sys_dts_usec >= (int64_t)stream->stop_ts) {
|
2019-07-27 12:47:10 +00:00
|
|
|
flv_output_actual_stop(stream, 0);
|
2018-02-19 19:54:37 +00:00
|
|
|
goto unlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
if (!stream->sent_headers) {
|
|
|
|
write_headers(stream);
|
|
|
|
stream->sent_headers = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packet->type == OBS_ENCODER_VIDEO) {
|
2018-02-19 19:54:37 +00:00
|
|
|
if (!stream->got_first_video) {
|
|
|
|
stream->start_dts_offset =
|
|
|
|
get_ms_time(packet, packet->dts);
|
|
|
|
stream->got_first_video = true;
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:16:51 +00:00
|
|
|
obs_parse_avc_packet(&parsed_packet, packet);
|
|
|
|
write_packet(stream, &parsed_packet, false);
|
2017-04-19 19:54:15 +00:00
|
|
|
obs_encoder_packet_release(&parsed_packet);
|
2016-02-23 23:16:51 +00:00
|
|
|
} else {
|
|
|
|
write_packet(stream, packet, false);
|
|
|
|
}
|
2018-02-19 19:54:37 +00:00
|
|
|
|
|
|
|
unlock:
|
|
|
|
pthread_mutex_unlock(&stream->mutex);
|
2016-02-23 23:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static obs_properties_t *flv_output_properties(void *unused)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
|
|
|
|
obs_properties_t *props = obs_properties_create();
|
|
|
|
|
|
|
|
obs_properties_add_text(props, "path",
|
2019-09-22 21:19:10 +00:00
|
|
|
obs_module_text("FLVOutput.FilePath"),
|
|
|
|
OBS_TEXT_DEFAULT);
|
2016-02-23 23:16:51 +00:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct obs_output_info flv_output_info = {
|
2019-09-22 21:19:10 +00:00
|
|
|
.id = "flv_output",
|
|
|
|
.flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED,
|
2018-02-19 19:54:37 +00:00
|
|
|
.encoded_video_codecs = "h264",
|
|
|
|
.encoded_audio_codecs = "aac",
|
2019-09-22 21:19:10 +00:00
|
|
|
.get_name = flv_output_getname,
|
|
|
|
.create = flv_output_create,
|
|
|
|
.destroy = flv_output_destroy,
|
|
|
|
.start = flv_output_start,
|
|
|
|
.stop = flv_output_stop,
|
|
|
|
.encoded_packet = flv_output_data,
|
|
|
|
.get_properties = flv_output_properties,
|
2016-02-23 23:16:51 +00:00
|
|
|
};
|