Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jun 5, 2024
1 parent 3e01be4 commit 8ac769c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 67 deletions.
48 changes: 24 additions & 24 deletions src/modules/ffmpeg/util/audio_resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ extern "C" {

namespace caspar::ffmpeg {

AudioResampler::AudioResampler(int64_t sample_rate, AVSampleFormat in_sample_fmt)
: ctx(std::shared_ptr<SwrContext>(swr_alloc_set_opts(nullptr,
AV_CH_LAYOUT_7POINT1,
AV_SAMPLE_FMT_S32,
sample_rate,
AV_CH_LAYOUT_7POINT1,
in_sample_fmt,
sample_rate,
0,
nullptr),
[](SwrContext* ptr) { swr_free(&ptr); }))
{
if (!ctx)
FF_RET(AVERROR(ENOMEM), "swr_alloc_set_opts");
AudioResampler::AudioResampler(int64_t sample_rate, AVSampleFormat in_sample_fmt)
: ctx(std::shared_ptr<SwrContext>(swr_alloc_set_opts(nullptr,
AV_CH_LAYOUT_7POINT1,
AV_SAMPLE_FMT_S32,
sample_rate,
AV_CH_LAYOUT_7POINT1,
in_sample_fmt,
sample_rate,
0,
nullptr),
[](SwrContext* ptr) { swr_free(&ptr); }))
{
if (!ctx)
FF_RET(AVERROR(ENOMEM), "swr_alloc_set_opts");

FF_RET(swr_init(ctx.get()), "swr_init");
}
FF_RET(swr_init(ctx.get()), "swr_init");
}

caspar::array<int32_t> AudioResampler::convert(int frames, const void** src)
{
auto result = caspar::array<int32_t>(frames * 8 * sizeof(int32_t));
auto ptr = result.data();
auto ret = swr_convert(ctx.get(), (uint8_t**)&ptr, frames, reinterpret_cast<const uint8_t**>(src), frames);
caspar::array<int32_t> AudioResampler::convert(int frames, const void** src)
{
auto result = caspar::array<int32_t>(frames * 8 * sizeof(int32_t));
auto ptr = result.data();
auto ret = swr_convert(ctx.get(), (uint8_t**)&ptr, frames, reinterpret_cast<const uint8_t**>(src), frames);

return result;
}
return result;
}

}; // namespace caspar::ffmpeg
}; // namespace caspar::ffmpeg
22 changes: 12 additions & 10 deletions src/modules/ffmpeg/util/audio_resampler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <common/array.h>
#include <memory>

#pragma once

extern "C" {
#include <libavutil/samplefmt.h>
}
Expand All @@ -9,17 +11,17 @@ struct SwrContext;

namespace caspar::ffmpeg {

class AudioResampler
{
std::shared_ptr<SwrContext> ctx;
class AudioResampler
{
std::shared_ptr<SwrContext> ctx;

public:
AudioResampler(int64_t sample_rate, AVSampleFormat in_sample_fmt);
public:
AudioResampler(int64_t sample_rate, AVSampleFormat in_sample_fmt);

AudioResampler(const AudioResampler&) = delete;
AudioResampler& operator=(const AudioResampler&) = delete;
AudioResampler(const AudioResampler&) = delete;
AudioResampler& operator=(const AudioResampler&) = delete;

caspar::array<int32_t> convert(int frames, const void** src);
};
caspar::array<int32_t> convert(int frames, const void** src);
};

}; // namespace caspar::ffmpeg
}; // namespace caspar::ffmpeg
69 changes: 36 additions & 33 deletions src/modules/html/producer/html_producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,28 @@ namespace caspar { namespace html {
inline std::int_least64_t now()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count();
std::chrono::high_resolution_clock::now().time_since_epoch())
.count();
}

struct presentation_frame
{
std::int_least64_t timestamp = now();
core::draw_frame frame = core::draw_frame::empty();
bool has_video = false;
bool has_audio = false;
core::draw_frame frame = core::draw_frame::empty();
bool has_video = false;
bool has_audio = false;

explicit presentation_frame(core::draw_frame video = {})
{
if (video) {
frame = std::move(video);
frame = std::move(video);
has_video = true;
}
}

presentation_frame(presentation_frame&& other)
noexcept : timestamp(other.timestamp),frame(std::move(other.frame))
presentation_frame(presentation_frame&& other) noexcept
: timestamp(other.timestamp)
, frame(std::move(other.frame))
{
}

Expand All @@ -105,30 +106,33 @@ struct presentation_frame

~presentation_frame() {}

void add_audio(core::mutable_frame audio) {
if (has_audio) return;
void add_audio(core::mutable_frame audio)
{
if (has_audio)
return;
has_audio = true;

if (frame) {
frame = core::draw_frame::over(frame, core::draw_frame(std::move (audio)));
frame = core::draw_frame::over(frame, core::draw_frame(std::move(audio)));
} else {
frame = core::draw_frame(std::move (audio));
frame = core::draw_frame(std::move(audio));
}
}

void add_video(core::draw_frame video) {
if (has_video) return;
void add_video(core::draw_frame video)
{
if (has_video)
return;
has_video = true;

if (frame) {
frame = core::draw_frame::over(frame, std::move (video));
frame = core::draw_frame::over(frame, std::move(video));
} else {
frame = std::move (video);
frame = std::move(video);
}
}
};


class html_client
: public CefClient
, public CefRenderHandler
Expand All @@ -146,16 +150,16 @@ class html_client
caspar::timer paint_timer_;
caspar::timer test_timer_;

spl::shared_ptr<core::frame_factory> frame_factory_;
core::video_format_desc format_desc_;
bool gpu_enabled_;
tbb::concurrent_queue<std::wstring> javascript_before_load_;
std::atomic<bool> loaded_;
std::queue<presentation_frame> frames_;
core::draw_frame last_generated_frame_;
mutable std::mutex frames_mutex_;
const size_t frames_max_size_ = 4;
std::atomic<bool> closing_;
spl::shared_ptr<core::frame_factory> frame_factory_;
core::video_format_desc format_desc_;
bool gpu_enabled_;
tbb::concurrent_queue<std::wstring> javascript_before_load_;
std::atomic<bool> loaded_;
std::queue<presentation_frame> frames_;
core::draw_frame last_generated_frame_;
mutable std::mutex frames_mutex_;
const size_t frames_max_size_ = 4;
std::atomic<bool> closing_;

std::unique_ptr<ffmpeg::AudioResampler> audioResampler_;

Expand Down Expand Up @@ -315,8 +319,6 @@ class html_client
}

private:


void GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect) override
{
CASPAR_ASSERT(CefCurrentlyOn(TID_UI));
Expand Down Expand Up @@ -368,7 +370,7 @@ class html_client
std::lock_guard<std::mutex> lock(frames_mutex_);

core::draw_frame new_frame = core::draw_frame(std::move(frame));
last_generated_frame_ = new_frame;
last_generated_frame_ = new_frame;

frames_.push(presentation_frame(std::move(new_frame)));
while (frames_.size() > 4) {
Expand Down Expand Up @@ -421,7 +423,7 @@ class html_client

CefRefPtr<CefRenderHandler> GetRenderHandler() override { return this; }

CefRefPtr<CefAudioHandler> GetAudioHandler() override { return this; }
CefRefPtr<CefAudioHandler> GetAudioHandler() override { return this; }

CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override { return this; }

Expand Down Expand Up @@ -483,9 +485,10 @@ class html_client
}
void OnAudioStreamPacket(CefRefPtr<CefBrowser> browser, const float** data, int samples, int64_t pts) override
{
if (!audioResampler_) return;
if (!audioResampler_)
return;

auto audio = audioResampler_->convert(samples, reinterpret_cast<const void**>(data));
auto audio = audioResampler_->convert(samples, reinterpret_cast<const void**>(data));
auto audio_frame = core::mutable_frame(this, {}, std::move(audio), core::pixel_format_desc());

{
Expand Down

0 comments on commit 8ac769c

Please sign in to comment.