Skip to content

Commit

Permalink
capture audio from CEF
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaspandersson committed Oct 4, 2023
1 parent de95e02 commit d655390
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 58 deletions.
1 change: 1 addition & 0 deletions src/core/frame/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const array<std::uint8_t>& mutable_frame::image_data(std::size_t index) const {
const array<std::int32_t>& mutable_frame::audio_data() const { return impl_->audio_data_; }
array<std::uint8_t>& mutable_frame::image_data(std::size_t index) { return impl_->image_data_.at(index); }
array<std::int32_t>& mutable_frame::audio_data() { return impl_->audio_data_; }
void mutable_frame::set_audio_data(caspar::array<int32_t>&& audio) { impl_->audio_data_ = std::move(audio); }
std::size_t mutable_frame::width() const { return impl_->desc_.planes.at(0).width; }
std::size_t mutable_frame::height() const { return impl_->desc_.planes.at(0).height; }
const frame_geometry& mutable_frame::geometry() const { return impl_->geometry_; }
Expand Down
3 changes: 2 additions & 1 deletion src/core/frame/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class mutable_frame final
~mutable_frame();

mutable_frame& operator=(const mutable_frame&) = delete;
mutable_frame& operator =(mutable_frame&& other);
mutable_frame& operator=(mutable_frame&& other);

void swap(mutable_frame& other);

Expand All @@ -41,6 +41,7 @@ class mutable_frame final

array<std::int32_t>& audio_data();
const array<std::int32_t>& audio_data() const;
void set_audio_data(caspar::array<int32_t>&& audio);

std::size_t width() const;

Expand Down
2 changes: 2 additions & 0 deletions src/modules/ffmpeg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(SOURCES
producer/av_producer.cpp
producer/av_input.cpp
util/av_util.cpp
util/audio_resampler.cpp
producer/ffmpeg_producer.cpp
consumer/ffmpeg_consumer.cpp
ffmpeg.cpp
Expand All @@ -14,6 +15,7 @@ set(HEADERS
producer/av_producer.h
producer/av_input.h
util/av_util.h
util/audio_resampler.h
producer/ffmpeg_producer.h
consumer/ffmpeg_consumer.h
ffmpeg.h
Expand Down
38 changes: 38 additions & 0 deletions src/modules/ffmpeg/util/audio_resampler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "audio_resampler.h"
#include "av_assert.h"

extern "C" {
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
}

namespace caspar { namespace 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");

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);

return result;
}

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

extern "C" {
#include <libavutil/samplefmt.h>
}

struct SwrContext;

namespace caspar { namespace ffmpeg {

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

public:
AudioResampler(int64_t sample_rate, AVSampleFormat in_sample_fmt);

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

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

}}; // namespace caspar::ffmpeg
3 changes: 2 additions & 1 deletion src/modules/html/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include_directories(../..)
include_directories(${BOOST_INCLUDE_PATH})
include_directories(${TBB_INCLUDE_PATH})
include_directories(${CEF_INCLUDE_PATH})
include_directories(${FFMPEG_INCLUDE_PATH})

set_target_properties(html PROPERTIES FOLDER modules)
source_group(sources\\producer producer/*)
Expand All @@ -29,7 +30,7 @@ source_group(sources ./*)
target_link_libraries(html
common
core

ffmpeg
${CEF_BIN_PATH}/libcef.so
${CEF_BIN_PATH}/libcef_dll_wrapper.a
)
Expand Down
Loading

0 comments on commit d655390

Please sign in to comment.