mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-03-03 06:11:42 +00:00
refactoring
This commit is contained in:
85
src/ustreamer/h264/stream.c
Normal file
85
src/ustreamer/h264/stream.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/*****************************************************************************
|
||||
# #
|
||||
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
|
||||
# #
|
||||
# Copyright (C) 2018-2021 Maxim Devaev <mdevaev@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 3 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 <https://www.gnu.org/licenses/>. #
|
||||
# #
|
||||
*****************************************************************************/
|
||||
|
||||
|
||||
#include "stream.h"
|
||||
|
||||
|
||||
h264_stream_s *h264_stream_init(memsink_s *sink, unsigned bitrate, unsigned gop) {
|
||||
h264_stream_s *h264;
|
||||
A_CALLOC(h264, 1);
|
||||
h264->sink = sink;
|
||||
h264->tmp_src = frame_init("h264_tmp_src");
|
||||
h264->dest = frame_init("h264_dest");
|
||||
|
||||
// FIXME: 30 or 0? https://github.com/6by9/yavta/blob/master/yavta.c#L210
|
||||
if ((h264->enc = h264_encoder_init(bitrate, gop, 0)) == NULL) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return h264;
|
||||
|
||||
error:
|
||||
h264_stream_destroy(h264);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void h264_stream_destroy(h264_stream_s *h264) {
|
||||
if (h264->enc) {
|
||||
h264_encoder_destroy(h264->enc);
|
||||
}
|
||||
frame_destroy(h264->dest);
|
||||
frame_destroy(h264->tmp_src);
|
||||
free(h264);
|
||||
}
|
||||
|
||||
void h264_stream_process(h264_stream_s *h264, const frame_s *frame, int vcsm_handle, bool force_key) {
|
||||
long double now = get_now_monotonic();
|
||||
bool zero_copy = false;
|
||||
|
||||
if (is_jpeg(frame->format)) {
|
||||
assert(vcsm_handle <= 0);
|
||||
LOG_DEBUG("H264: Input frame is JPEG; decoding ...");
|
||||
if (unjpeg(frame, h264->tmp_src, true) < 0) {
|
||||
return;
|
||||
}
|
||||
frame = h264->tmp_src;
|
||||
LOG_VERBOSE("H264: JPEG decoded; time=%.3Lf", get_now_monotonic() - now);
|
||||
} else if (vcsm_handle > 0) {
|
||||
LOG_DEBUG("H264: Zero-copy available for the input");
|
||||
zero_copy = true;
|
||||
} else {
|
||||
LOG_DEBUG("H264: Copying source to tmp buffer ...");
|
||||
frame_copy(frame, h264->tmp_src);
|
||||
frame = h264->tmp_src;
|
||||
LOG_VERBOSE("H264: Source copied; time=%.3Lf", get_now_monotonic() - now);
|
||||
}
|
||||
|
||||
if (!h264_encoder_is_prepared_for(h264->enc, frame, zero_copy)) {
|
||||
h264_encoder_prepare(h264->enc, frame, zero_copy);
|
||||
}
|
||||
|
||||
if (h264->enc->ready) {
|
||||
if (h264_encoder_compress(h264->enc, frame, vcsm_handle, h264->dest, force_key) == 0) {
|
||||
memsink_server_put(h264->sink, h264->dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
src/ustreamer/h264/stream.h
Normal file
47
src/ustreamer/h264/stream.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*****************************************************************************
|
||||
# #
|
||||
# uStreamer - Lightweight and fast MJPG-HTTP streamer. #
|
||||
# #
|
||||
# Copyright (C) 2018-2021 Maxim Devaev <mdevaev@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 3 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 <https://www.gnu.org/licenses/>. #
|
||||
# #
|
||||
*****************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "../../libs/tools.h"
|
||||
#include "../../libs/logging.h"
|
||||
#include "../../libs/frame.h"
|
||||
#include "../../libs/memsink.h"
|
||||
#include "../../libs/unjpeg.h"
|
||||
|
||||
#include "encoder.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
memsink_s *sink;
|
||||
frame_s *tmp_src;
|
||||
frame_s *dest;
|
||||
h264_encoder_s *enc;
|
||||
} h264_stream_s;
|
||||
|
||||
|
||||
h264_stream_s *h264_stream_init(memsink_s *sink, unsigned bitrate, unsigned gop);
|
||||
void h264_stream_destroy(h264_stream_s *h264);
|
||||
void h264_stream_process(h264_stream_s *h264, const frame_s *frame, int vcsm_handle, bool force_key);
|
||||
@@ -27,12 +27,6 @@ static workers_pool_s *_stream_init_loop(stream_s *stream);
|
||||
static workers_pool_s *_stream_init_one(stream_s *stream);
|
||||
static bool _stream_expose_frame(stream_s *stream, frame_s *frame, unsigned captured_fps);
|
||||
|
||||
#ifdef WITH_OMX
|
||||
static h264_stream_s *_h264_stream_init(memsink_s *sink, unsigned bitrate, unsigned gop);
|
||||
static void _h264_stream_destroy(h264_stream_s *h264);
|
||||
static void _h264_stream_process(h264_stream_s *h264, const frame_s *frame, int vcsm_handle, bool force_key);
|
||||
#endif
|
||||
|
||||
|
||||
#define RUN(_next) stream->run->_next
|
||||
|
||||
@@ -80,7 +74,7 @@ void stream_loop(stream_s *stream) {
|
||||
|
||||
# ifdef WITH_OMX
|
||||
if (stream->h264_sink) {
|
||||
RUN(h264) = _h264_stream_init(stream->h264_sink, stream->h264_bitrate, stream->h264_gop);
|
||||
RUN(h264) = h264_stream_init(stream->h264_sink, stream->h264_bitrate, stream->h264_gop);
|
||||
}
|
||||
# endif
|
||||
|
||||
@@ -202,7 +196,7 @@ void stream_loop(stream_s *stream) {
|
||||
|
||||
# ifdef WITH_OMX
|
||||
if (RUN(h264)) {
|
||||
_h264_stream_process(RUN(h264), &hw->raw, hw->vcsm_handle, h264_force_key);
|
||||
h264_stream_process(RUN(h264), &hw->raw, hw->vcsm_handle, h264_force_key);
|
||||
}
|
||||
# endif
|
||||
}
|
||||
@@ -236,7 +230,7 @@ void stream_loop(stream_s *stream) {
|
||||
|
||||
# ifdef WITH_OMX
|
||||
if (RUN(h264)) {
|
||||
_h264_stream_destroy(RUN(h264));
|
||||
h264_stream_destroy(RUN(h264));
|
||||
}
|
||||
# endif
|
||||
}
|
||||
@@ -256,7 +250,7 @@ static workers_pool_s *_stream_init_loop(stream_s *stream) {
|
||||
if (_stream_expose_frame(stream, NULL, 0)) {
|
||||
# ifdef WITH_OMX
|
||||
if (RUN(h264)) {
|
||||
_h264_stream_process(RUN(h264), stream->blank, -1, false);
|
||||
h264_stream_process(RUN(h264), stream->blank, -1, false);
|
||||
}
|
||||
# endif
|
||||
}
|
||||
@@ -365,67 +359,4 @@ static bool _stream_expose_frame(stream_s *stream, frame_s *frame, unsigned capt
|
||||
# undef VID
|
||||
}
|
||||
|
||||
#ifdef WITH_OMX
|
||||
static h264_stream_s *_h264_stream_init(memsink_s *sink, unsigned bitrate, unsigned gop) {
|
||||
h264_stream_s *h264;
|
||||
A_CALLOC(h264, 1);
|
||||
h264->sink = sink;
|
||||
h264->tmp_src = frame_init("h264_tmp_src");
|
||||
h264->dest = frame_init("h264_dest");
|
||||
|
||||
// FIXME: 30 or 0? https://github.com/6by9/yavta/blob/master/yavta.c#L210
|
||||
if ((h264->enc = h264_encoder_init(bitrate, gop, 0)) == NULL) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return h264;
|
||||
|
||||
error:
|
||||
_h264_stream_destroy(h264);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void _h264_stream_destroy(h264_stream_s *h264) {
|
||||
if (h264->enc) {
|
||||
h264_encoder_destroy(h264->enc);
|
||||
}
|
||||
frame_destroy(h264->dest);
|
||||
frame_destroy(h264->tmp_src);
|
||||
free(h264);
|
||||
}
|
||||
|
||||
static void _h264_stream_process(h264_stream_s *h264, const frame_s *frame, int vcsm_handle, bool force_key) {
|
||||
long double now = get_now_monotonic();
|
||||
bool zero_copy = false;
|
||||
|
||||
if (is_jpeg(frame->format)) {
|
||||
assert(vcsm_handle <= 0);
|
||||
LOG_DEBUG("H264: Input frame is JPEG; decoding ...");
|
||||
if (unjpeg(frame, h264->tmp_src, true) < 0) {
|
||||
return;
|
||||
}
|
||||
frame = h264->tmp_src;
|
||||
LOG_VERBOSE("H264: JPEG decoded; time=%.3Lf", get_now_monotonic() - now);
|
||||
} else if (vcsm_handle > 0) {
|
||||
LOG_DEBUG("H264: Zero-copy available for the input");
|
||||
zero_copy = true;
|
||||
} else {
|
||||
LOG_DEBUG("H264: Copying source to tmp buffer ...");
|
||||
frame_copy(frame, h264->tmp_src);
|
||||
frame = h264->tmp_src;
|
||||
LOG_VERBOSE("H264: Source copied; time=%.3Lf", get_now_monotonic() - now);
|
||||
}
|
||||
|
||||
if (!h264_encoder_is_prepared_for(h264->enc, frame, zero_copy)) {
|
||||
h264_encoder_prepare(h264->enc, frame, zero_copy);
|
||||
}
|
||||
|
||||
if (h264->enc->ready) {
|
||||
if (h264_encoder_compress(h264->enc, frame, vcsm_handle, h264->dest, force_key) == 0) {
|
||||
memsink_server_put(h264->sink, h264->dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#undef RUN
|
||||
|
||||
@@ -43,8 +43,7 @@
|
||||
#include "encoder.h"
|
||||
#include "workers.h"
|
||||
#ifdef WITH_OMX
|
||||
# include "../libs/unjpeg.h"
|
||||
# include "h264/encoder.h"
|
||||
# include "h264/stream.h"
|
||||
#endif
|
||||
#ifdef WITH_GPIO
|
||||
# include "gpio/gpio.h"
|
||||
@@ -60,15 +59,6 @@ typedef struct {
|
||||
atomic_bool has_clients; // For slowdown
|
||||
} video_s;
|
||||
|
||||
#ifdef WITH_OMX
|
||||
typedef struct {
|
||||
memsink_s *sink;
|
||||
frame_s *tmp_src;
|
||||
frame_s *dest;
|
||||
h264_encoder_s *enc;
|
||||
} h264_stream_s;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
video_s *video;
|
||||
long double last_as_blank_ts;
|
||||
|
||||
Reference in New Issue
Block a user