2019-03-02 23:43:43 +08:00
|
|
|
#include "stream.h"
|
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2019-03-02 23:43:43 +08:00
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavutil/time.h>
|
|
|
|
#include <SDL2/SDL_events.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "decoder.h"
|
|
|
|
#include "events.h"
|
|
|
|
#include "recorder.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/buffer_util.h"
|
|
|
|
#include "util/log.h"
|
2019-03-02 23:43:43 +08:00
|
|
|
|
|
|
|
#define BUFSIZE 0x10000
|
|
|
|
|
|
|
|
#define HEADER_SIZE 12
|
|
|
|
#define NO_PTS UINT64_C(-1)
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-07-31 07:55:32 +08:00
|
|
|
stream_recv_packet(struct stream *stream, AVPacket *packet) {
|
2019-03-02 23:43:43 +08:00
|
|
|
// The video stream contains raw packets, without time information. When we
|
|
|
|
// record, we retrieve the timestamps separately, from a "meta" header
|
|
|
|
// added by the server before each raw packet.
|
|
|
|
//
|
|
|
|
// The "meta" header length is 12 bytes:
|
|
|
|
// [. . . . . . . .|. . . .]. . . . . . . . . . . . . . . ...
|
|
|
|
// <-------------> <-----> <-----------------------------...
|
|
|
|
// PTS packet raw packet
|
|
|
|
// size
|
|
|
|
//
|
|
|
|
// It is followed by <packet_size> bytes containing the packet/frame.
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
uint8_t header[HEADER_SIZE];
|
|
|
|
ssize_t r = net_recv_all(stream->socket, header, HEADER_SIZE);
|
|
|
|
if (r < HEADER_SIZE) {
|
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
uint64_t pts = buffer_read64be(header);
|
|
|
|
uint32_t len = buffer_read32be(&header[8]);
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(pts == NO_PTS || (pts & 0x8000000000000000) == 0);
|
|
|
|
assert(len);
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
if (av_new_packet(packet, len)) {
|
|
|
|
LOGE("Could not allocate packet");
|
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
r = net_recv_all(stream->socket, packet->data, len);
|
2019-11-27 20:39:42 +08:00
|
|
|
if (r < 0 || ((uint32_t) r) < len) {
|
2019-07-31 07:55:32 +08:00
|
|
|
av_packet_unref(packet);
|
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-11-27 20:39:42 +08:00
|
|
|
packet->pts = pts != NO_PTS ? (int64_t) pts : AV_NOPTS_VALUE;
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
return true;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
notify_stopped(void) {
|
2019-03-02 23:43:43 +08:00
|
|
|
SDL_Event stop_event;
|
|
|
|
stop_event.type = EVENT_STREAM_STOPPED;
|
|
|
|
SDL_PushEvent(&stop_event);
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
static bool
|
|
|
|
process_config_packet(struct stream *stream, AVPacket *packet) {
|
2019-07-31 07:55:40 +08:00
|
|
|
if (stream->recorder && !recorder_push(stream->recorder, packet)) {
|
2019-07-31 07:55:32 +08:00
|
|
|
LOGE("Could not send config packet to recorder");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
static bool
|
|
|
|
process_frame(struct stream *stream, AVPacket *packet) {
|
|
|
|
if (stream->decoder && !decoder_push(stream->decoder, packet)) {
|
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
if (stream->recorder) {
|
|
|
|
packet->dts = packet->pts;
|
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
if (!recorder_push(stream->recorder, packet)) {
|
|
|
|
LOGE("Could not send packet to recorder");
|
2019-07-31 07:55:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
stream_parse(struct stream *stream, AVPacket *packet) {
|
|
|
|
uint8_t *in_data = packet->data;
|
|
|
|
int in_len = packet->size;
|
|
|
|
uint8_t *out_data = NULL;
|
|
|
|
int out_len = 0;
|
|
|
|
int r = av_parser_parse2(stream->parser, stream->codec_ctx,
|
|
|
|
&out_data, &out_len, in_data, in_len,
|
|
|
|
AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);
|
|
|
|
|
|
|
|
// PARSER_FLAG_COMPLETE_FRAMES is set
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(r == in_len);
|
|
|
|
(void) r;
|
|
|
|
assert(out_len == in_len);
|
2019-07-31 07:55:32 +08:00
|
|
|
|
|
|
|
if (stream->parser->key_frame == 1) {
|
|
|
|
packet->flags |= AV_PKT_FLAG_KEY;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
bool ok = process_frame(stream, packet);
|
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not process frame");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
stream_push_packet(struct stream *stream, AVPacket *packet) {
|
|
|
|
bool is_config = packet->pts == AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
// A config packet must not be decoded immetiately (it contains no
|
|
|
|
// frame); instead, it must be concatenated with the future data packet.
|
|
|
|
if (stream->has_pending || is_config) {
|
|
|
|
size_t offset;
|
|
|
|
if (stream->has_pending) {
|
|
|
|
offset = stream->pending.size;
|
|
|
|
if (av_grow_packet(&stream->pending, packet->size)) {
|
|
|
|
LOGE("Could not grow packet");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
offset = 0;
|
|
|
|
if (av_new_packet(&stream->pending, packet->size)) {
|
|
|
|
LOGE("Could not create packet");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stream->has_pending = true;
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
memcpy(stream->pending.data + offset, packet->data, packet->size);
|
|
|
|
|
|
|
|
if (!is_config) {
|
|
|
|
// prepare the concat packet to send to the decoder
|
|
|
|
stream->pending.pts = packet->pts;
|
|
|
|
stream->pending.dts = packet->dts;
|
|
|
|
stream->pending.flags = packet->flags;
|
|
|
|
packet = &stream->pending;
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
if (is_config) {
|
|
|
|
// config packet
|
|
|
|
bool ok = process_config_packet(stream, packet);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// data packet
|
|
|
|
bool ok = stream_parse(stream, packet);
|
|
|
|
|
|
|
|
if (stream->has_pending) {
|
|
|
|
// the pending packet must be discarded (consumed or error)
|
|
|
|
stream->has_pending = false;
|
|
|
|
av_packet_unref(&stream->pending);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
run_stream(void *data) {
|
|
|
|
struct stream *stream = data;
|
|
|
|
|
2019-03-02 23:43:43 +08:00
|
|
|
AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
|
|
|
|
if (!codec) {
|
|
|
|
LOGE("H.264 decoder not found");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
stream->codec_ctx = avcodec_alloc_context3(codec);
|
|
|
|
if (!stream->codec_ctx) {
|
|
|
|
LOGC("Could not allocate codec context");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2019-03-02 23:43:43 +08:00
|
|
|
if (stream->decoder && !decoder_open(stream->decoder, codec)) {
|
|
|
|
LOGE("Could not open decoder");
|
2019-07-31 07:55:32 +08:00
|
|
|
goto finally_free_codec_ctx;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:40 +08:00
|
|
|
if (stream->recorder) {
|
|
|
|
if (!recorder_open(stream->recorder, codec)) {
|
|
|
|
LOGE("Could not open recorder");
|
|
|
|
goto finally_close_decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recorder_start(stream->recorder)) {
|
|
|
|
LOGE("Could not start recorder");
|
|
|
|
goto finally_close_recorder;
|
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
stream->parser = av_parser_init(AV_CODEC_ID_H264);
|
|
|
|
if (!stream->parser) {
|
|
|
|
LOGE("Could not initialize parser");
|
2019-07-31 07:55:40 +08:00
|
|
|
goto finally_stop_and_join_recorder;
|
2019-07-31 07:55:32 +08:00
|
|
|
}
|
2019-03-02 23:43:43 +08:00
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
// We must only pass complete frames to av_parser_parse2()!
|
|
|
|
// It's more complicated, but this allows to reduce the latency by 1 frame!
|
|
|
|
stream->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
AVPacket packet;
|
|
|
|
bool ok = stream_recv_packet(stream, &packet);
|
|
|
|
if (!ok) {
|
|
|
|
// end of stream
|
|
|
|
break;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
ok = stream_push_packet(stream, &packet);
|
2019-03-02 23:43:43 +08:00
|
|
|
av_packet_unref(&packet);
|
2019-07-31 07:55:32 +08:00
|
|
|
if (!ok) {
|
|
|
|
// cannot process packet (error already logged)
|
2019-03-02 23:43:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("End of frames");
|
|
|
|
|
2019-07-31 07:55:32 +08:00
|
|
|
if (stream->has_pending) {
|
|
|
|
av_packet_unref(&stream->pending);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_parser_close(stream->parser);
|
2019-07-31 07:55:40 +08:00
|
|
|
finally_stop_and_join_recorder:
|
|
|
|
if (stream->recorder) {
|
|
|
|
recorder_stop(stream->recorder);
|
|
|
|
LOGI("Finishing recording...");
|
|
|
|
recorder_join(stream->recorder);
|
|
|
|
}
|
2019-07-31 07:55:32 +08:00
|
|
|
finally_close_recorder:
|
2019-03-02 23:43:43 +08:00
|
|
|
if (stream->recorder) {
|
|
|
|
recorder_close(stream->recorder);
|
|
|
|
}
|
2019-06-27 05:50:39 +08:00
|
|
|
finally_close_decoder:
|
|
|
|
if (stream->decoder) {
|
|
|
|
decoder_close(stream->decoder);
|
|
|
|
}
|
2019-07-31 07:55:32 +08:00
|
|
|
finally_free_codec_ctx:
|
|
|
|
avcodec_free_context(&stream->codec_ctx);
|
2019-03-02 23:43:43 +08:00
|
|
|
end:
|
|
|
|
notify_stopped();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
stream_init(struct stream *stream, socket_t socket,
|
|
|
|
struct decoder *decoder, struct recorder *recorder) {
|
2019-03-02 23:43:43 +08:00
|
|
|
stream->socket = socket;
|
|
|
|
stream->decoder = decoder,
|
|
|
|
stream->recorder = recorder;
|
2019-07-31 07:55:32 +08:00
|
|
|
stream->has_pending = false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
stream_start(struct stream *stream) {
|
2019-03-02 23:43:43 +08:00
|
|
|
LOGD("Starting stream thread");
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
bool ok = sc_thread_create(&stream->thread, run_stream, "stream", stream);
|
|
|
|
if (!ok) {
|
2019-03-02 23:43:43 +08:00
|
|
|
LOGC("Could not start stream thread");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
stream_join(struct stream *stream) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_thread_join(&stream->thread, NULL);
|
2019-03-02 23:43:43 +08:00
|
|
|
}
|