Skip to content

Commit

Permalink
Long Term Stability Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sr99622 committed Jul 1, 2024
1 parent d5683af commit dd249be
Show file tree
Hide file tree
Showing 21 changed files with 258 additions and 103 deletions.
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ list(PREPEND CMAKE_MODULE_PATH ${MY_DIR_VAR}/cmake)

if(WIN32)
list(PREPEND CMAKE_MODULE_PATH ${MY_DIR_VAR}/cmake-win)
add_compile_options("/EHsc")
add_compile_options(/EHsc /MT)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
set(BUILD_SHARED_LIBS TRUE)
endif()
Expand Down Expand Up @@ -113,4 +113,19 @@ if (NOT WITHOUT_PYTHON)
target_include_directories(avio SYSTEM PUBLIC
include
)
endif()

IF (BUILD_TEST)
add_executable(avio-test
test/avio-test.cpp
)

target_link_libraries(avio-test PRIVATE
libavio
)

target_include_directories(avio-test PUBLIC
include
)

endif()
9 changes: 4 additions & 5 deletions include/Clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ namespace avio
class Clock
{
public:
uint64_t milliseconds();
uint64_t update(uint64_t rts);
uint64_t stream_time();
int sync(uint64_t rts);
int64_t milliseconds();
int64_t update(int64_t rts);
int64_t stream_time();
void pause(bool paused);

long long correction = 0;
int64_t correction = 0;

private:
std::chrono::steady_clock clock;
Expand Down
2 changes: 0 additions & 2 deletions include/Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class Decoder
void flush();

int sample_rate() { return dec_ctx->sample_rate; }
int channels() { return dec_ctx->channels; }
int frame_size() { return reader->fmt_ctx->streams[stream_index]->codecpar->frame_size; }
uint64_t channel_layout() { return dec_ctx->channel_layout; }
AVSampleFormat sample_format() { return dec_ctx->sample_fmt; }
int bit_rate() { return dec_ctx->bit_rate; }
int width() { return dec_ctx->width; }
Expand Down
13 changes: 10 additions & 3 deletions include/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,22 @@ class Encoder
AVDictionary* opts = NULL;

AVSampleFormat sample_fmt = AV_SAMPLE_FMT_NONE;
uint64_t channel_layout = 0;
int audio_bit_rate = 0;
int sample_rate = 0;
int nb_samples = 0;
int channels = 0;
int64_t total_samples = 0;
AVRational audio_time_base = av_make_q(0, 0);

#if LIBAVCODEC_VERSION_MAJOR < 61
uint64_t channel_layout = 0;
int channels = 0;
void set_channel_layout_mono() { channel_layout = AV_CH_LAYOUT_MONO; }
void set_channel_layout_stereo() { channel_layout = AV_CH_LAYOUT_STEREO; }
int64_t total_samples = 0;
#else
AVChannelLayout ch_layout;
void set_channel_layout_mono() { ch_layout = AV_CHANNEL_LAYOUT_MONO; }
void set_channel_layout_stereo() { ch_layout = AV_CHANNEL_LAYOUT_STEREO; }
#endif

bool show_frames = false;

Expand Down
4 changes: 3 additions & 1 deletion include/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class Filter
Frame tmp;
std::string desc;

#if LIBAVCODEC_VERSION_MAJOR < 61
uint64_t m_channel_layout = 0;
int64_t channel_layout() { return av_buffersink_get_channel_layout(sink_ctx); }
#endif

int width() { return av_buffersink_get_w(sink_ctx); }
int height() { return av_buffersink_get_h(sink_ctx); }
Expand All @@ -68,7 +71,6 @@ class Filter
AVRational frame_rate() { return av_buffersink_get_frame_rate(sink_ctx); }
int sample_rate() { return av_buffersink_get_sample_rate(sink_ctx); }
int channels() { return av_buffersink_get_channels(sink_ctx); }
int64_t channel_layout() { return av_buffersink_get_channel_layout(sink_ctx); }
AVSampleFormat sample_format() { return (AVSampleFormat)av_buffersink_get_format(sink_ctx); }
int frame_size() { return decoder->frame_size(); }

Expand Down
4 changes: 2 additions & 2 deletions include/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Frame
bool isValid() const { return m_frame ? true : false; }
void invalidate();
AVMediaType mediaType() const;
uint64_t pts() { return m_frame ? m_frame->pts : AV_NOPTS_VALUE; }
int64_t pts() { return m_frame ? m_frame->pts : AV_NOPTS_VALUE; }
void set_rts(AVStream* stream); // called from Decoder::decode
void set_pts(AVStream* stream); // called from Encoder::encode
std::string description() const;
Expand All @@ -62,7 +62,7 @@ class Frame
bool isPlanar();

AVFrame* m_frame = nullptr;
uint64_t m_rts;
int64_t m_rts;

};

Expand Down
2 changes: 2 additions & 0 deletions include/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ class Player
std::string getAudioChannelLayout() const;
int getAudioBitrate();
int getAudioSampleRate();
int getAudioFrameSize();
int getCacheSize();
void clearCache();
std::string getStreamInfo() const;
std::string getFFMPEGVersions() const;
bool sync_audio = false;

};

Expand Down
7 changes: 6 additions & 1 deletion include/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ class Reader
bool seeking();
void start_from(int milliseconds);
void end_at(int milliseconds);
//void showStreamParameters();
std::string getStreamInfo();

int64_t start_time();
int64_t duration();
int64_t bit_rate();

time_t timeout_start = time(nullptr);
std::string timeout_msg;

bool has_video();
int width();
int height();
Expand All @@ -84,7 +86,10 @@ class Reader
int sample_rate();
int frame_size();

#if LIBAVCODEC_VERSION_MAJOR < 61
uint64_t channel_layout();
#endif

std::string str_channel_layout();
AVSampleFormat sample_format();
const char* str_sample_format();
Expand Down
28 changes: 13 additions & 15 deletions include/avio.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void read(Reader* reader)
}

if (reader->vpq) {
if (player->isCameraStream() && (reader->vpq->size() == reader->vpq->max_size())) {
if (player->isCameraStream() && (reader->vpq->size() == reader->vpq->max_size()) && !pkt->flags) {
av_packet_free(&pkt);
player->infoCallback("dropping frames due to buffer overflow", player->uri);
if (player->packetDrop) player->packetDrop(player->uri);
Expand Down Expand Up @@ -97,13 +97,6 @@ static void read(Reader* reader)
reader->fill_pkts_cache(pkt);
}

if (reader->stop_play_at_pts != AV_NOPTS_VALUE && pkt->stream_index == reader->seek_stream_index()) {
if (pkt->pts > reader->stop_play_at_pts) {
av_packet_free(&pkt);
break;
}
}

Packet p(pkt);
if (!player->hidden) {
if (pkt->stream_index == reader->video_stream_index) {
Expand All @@ -122,20 +115,20 @@ static void read(Reader* reader)
}
}
catch (const QueueClosedException& e) {}
catch (const Exception& e) {
reader->close_pipe();
reader->clear_pkts_cache(0);
catch (const Exception& e) {
std::string msg(e.what());
std::string mark("-138");
if (msg.find(mark) != std::string::npos)
msg = "No connection to server";
std::stringstream str;
std::cout << str.str() << std::endl;
str << "read error: " << msg;
if (player->errorCallback) player->errorCallback(str.str(), player->uri, true);
else std::cout << str.str() << std::endl;
}

try {
reader->close_pipe();
reader->clear_pkts_cache(0);
if (reader->vpq) reader->vpq->push_move(Packet(nullptr));
if (reader->apq) reader->apq->push_move(Packet(nullptr));
}
Expand All @@ -162,7 +155,10 @@ static void decode(Decoder* decoder)
if (decoder->errorCallback) decoder->errorCallback(str.str(), "", false);
}

decoder->frame_q->push_move(Frame(nullptr));
try {
decoder->frame_q->push_move(Frame(nullptr));
}
catch (const QueueClosedException& e) {}
}

static void filter(Filter* filter)
Expand All @@ -183,8 +179,10 @@ static void filter(Filter* filter)
str << "filter loop exception: " << e.what();
if (filter->errorCallback) filter->errorCallback(str.str(), "", false);
}

filter->frame_out_q->push_move(Frame(nullptr));
try {
filter->frame_out_q->push_move(Frame(nullptr));
}
catch (const QueueClosedException& e) {}
}

static void write(Writer* writer, Encoder* encoder)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "avio"
version = "3.1.2"
version = "3.2.0"
authors = [
{ name="Stephen Rhodes", email="[email protected]" },
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from setuptools.command.build_ext import build_ext

PKG_NAME = "avio"
VERSION = "3.1.2"
VERSION = "3.2.0"

class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
Expand Down
25 changes: 6 additions & 19 deletions src/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ constexpr auto MAX_CLOCK_DIFF = 1000;
namespace avio
{

uint64_t Clock::milliseconds()
int64_t Clock::milliseconds()
{
if (!started) {
play_start = clock.now();
Expand All @@ -35,7 +35,7 @@ namespace avio
return std::chrono::duration_cast<std::chrono::milliseconds>(current - play_start).count();
}

uint64_t Clock::stream_time()
int64_t Clock::stream_time()
{
if (!started) {
play_start = clock.now();
Expand All @@ -45,24 +45,24 @@ uint64_t Clock::stream_time()
return milliseconds() - correction;
}

uint64_t Clock::update(uint64_t rts)
int64_t Clock::update(int64_t rts)
{
if (!started) {
play_start = clock.now();
started = true;
}

uint64_t current = milliseconds() - correction;
int64_t current = milliseconds() - correction;

if (current > rts) {
uint64_t diff = current - rts;
int64_t diff = current - rts;
if (diff > MAX_CLOCK_DIFF) {
correction -= (rts - current);
}
return 0;
}
else {
uint64_t diff = rts - current;
int64_t diff = rts - current;
if (diff > MAX_CLOCK_DIFF) {
correction += (current - rts);
diff = 0;
Expand All @@ -71,19 +71,6 @@ uint64_t Clock::update(uint64_t rts)
}
}

int Clock::sync(uint64_t rts)
{
if (!started) {
play_start = clock.now();
started = true;
}

uint64_t current = milliseconds() - correction;
int diff = rts - current;
correction -= diff;
return diff;
}

void Clock::pause(bool paused)
{
if (paused) {
Expand Down
10 changes: 7 additions & 3 deletions src/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ Decoder::Decoder(Reader* reader, AVMediaType mediaType, AVHWDeviceType hw_device
}
catch (const Exception& e) {
std::stringstream str;
str << "Decoder constructor exception: " << e.what();
std::string msg = e.what();
if (msg.find("av_hwdevice_ctx_create") != std::string::npos)
msg = "Hardware decoder not supported";

str << "Decoder constructor exception: " << msg;
((Player*)reader->player)->request_reconnect = false;
throw Exception(str.str());
}
}
Expand All @@ -145,8 +150,7 @@ int Decoder::decode(AVPacket* pkt)
if (!dec_ctx) throw Exception("dec_ctx null");

if (dec_ctx->opaque && pkt) {
const char* status = (const char *)dec_ctx->opaque;
if (strcmp("good", status))
if (strcmp(good, (const char *)dec_ctx->opaque))
throw Exception("incompatible hardware decoder");
}

Expand Down
Loading

0 comments on commit dd249be

Please sign in to comment.