Skip to content

Commit

Permalink
#45 and remove manual_video_source
Browse files Browse the repository at this point in the history
  • Loading branch information
UMU618 committed Oct 22, 2022
1 parent 2aad26b commit 1757919
Show file tree
Hide file tree
Showing 17 changed files with 731 additions and 754 deletions.
23 changes: 13 additions & 10 deletions src/cge/cge/audio_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int AudioEncoder::EncodingThread() {

size_t frame_bytes = codec_context_->frame_size *
(codec_context_->bits_per_raw_sample + 7) / 8 *
codec_context_->channels;
codec_context_->ch_layout.nb_channels;
size_t shared_mem_size = sizeof(SharedAudioFrames) +
sizeof(PackedAudioFrame) +
frame_bytes * kNumberOfSharedFrames;
Expand All @@ -132,7 +132,7 @@ int AudioEncoder::EncodingThread() {
}
SharedAudioFrames* saf = shared_frames_;
strcpy_s(saf->codec_name, codec_name_.data());
saf->channels = codec_context_->channels;
saf->channels = codec_context_->ch_layout.nb_channels;
saf->frame_size = codec_context_->frame_size;
saf->sample_bits = codec_context_->bits_per_raw_sample;
saf->sample_format = codec_context_->sample_fmt;
Expand Down Expand Up @@ -249,8 +249,9 @@ int AudioEncoder::AddStream(const AVCodec*& codec) {
// std::cerr << "invaild channels " << source_audio_info_.channels << ".\n";
// return -1;
// }
codec_context_->channels = kTargetChannels;
codec_context_->channel_layout = kTargetChannelLayout;
codec_context_->ch_layout = AV_CHANNEL_LAYOUT_STEREO;
//codec_context_->channels = kTargetChannels;
//codec_context_->channel_layout = kTargetChannelLayout;
if (codec_name_ == "opus") {
codec_context_->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
}
Expand Down Expand Up @@ -280,19 +281,21 @@ int AudioEncoder::Open(const AVCodec* codec, AVDictionary** opts) {
}

sound_capturer_.SetOutputInfo(
codec_context_->channel_layout, codec_context_->sample_fmt,
codec_context_->ch_layout, codec_context_->sample_fmt,
codec_context_->sample_rate, codec_context_->frame_size);

#if _DEBUG
APP_DEBUG() << "channel layout: " << codec_context_->channel_layout << '\n';
APP_DEBUG() << "channel layout: " << codec_context_->ch_layout.u.mask << '\n';
APP_DEBUG() << "sample format: " << codec_context_->sample_fmt << '\n';
APP_DEBUG() << "sample rate: " << codec_context_->sample_rate << '\n';
APP_DEBUG() << "frame size: " << codec_context_->frame_size << '\n';

if (source_audio_info_.channel_layout != codec_context_->channel_layout) {
if (0 != av_channel_layout_compare(&source_audio_info_.channel_layout,
&codec_context_->ch_layout)) {

APP_DEBUG() << "resample channel layout "
<< source_audio_info_.channel_layout << " to "
<< codec_context_->channel_layout << ".\n";
<< source_audio_info_.channel_layout.u.mask << " to "
<< codec_context_->ch_layout.u.mask << ".\n";
}
if (source_audio_info_.sample_format != codec_context_->sample_fmt) {
APP_DEBUG() << "resample format " << source_audio_info_.sample_format
Expand Down Expand Up @@ -334,7 +337,7 @@ int AudioEncoder::InitializeFrame(AVFrame*& frame) const noexcept {
frame->nb_samples = codec_context_->frame_size;
frame->format = codec_context_->sample_fmt;
frame->sample_rate = codec_context_->sample_rate;
frame->channel_layout = codec_context_->channel_layout;
av_channel_layout_copy(&frame->ch_layout, &codec_context_->ch_layout);

int error_code = av_frame_get_buffer(frame, 0);
if (error_code < 0) {
Expand Down
29 changes: 15 additions & 14 deletions src/cge/cge/audio_resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#include "audio_resampler.h"

int AudioResampler::Initialize(int64_t in_channel_layout,
int AudioResampler::Initialize(const AVChannelLayout* in_channel_layout,
AVSampleFormat in_sample_format,
int in_sample_rate,
int64_t out_channel_layout,
AVChannelLayout* out_channel_layout,
AVSampleFormat out_sample_format,
int out_sample_rate,
int frame_size) noexcept {
Expand All @@ -30,22 +30,24 @@ int AudioResampler::Initialize(int64_t in_channel_layout,

std::lock_guard<std::mutex> lock(mutex_);

out_channel_layout_ = out_channel_layout;
av_channel_layout_copy(&out_channel_layout_, out_channel_layout);
out_sample_format_ = out_sample_format;
out_sample_rate_ = out_sample_rate;
frame_size_ = frame_size;

if (in_channel_layout != out_channel_layout ||
if (0 != av_channel_layout_compare(in_channel_layout, out_channel_layout) ||
in_sample_format != out_sample_format ||
in_sample_rate != out_sample_rate) {
in_sample_rate_ = in_sample_rate;

resampler_context_ = swr_alloc_set_opts(
nullptr, out_channel_layout, out_sample_format, out_sample_rate,
in_channel_layout, in_sample_format, in_sample_rate, 0, nullptr);
// resampler_context_ = swr_alloc_set_opts(nullptr, ...);
int ec = swr_alloc_set_opts2(&resampler_context_, out_channel_layout,
out_sample_format, out_sample_rate,
in_channel_layout, in_sample_format,
in_sample_rate, 0, nullptr);
if (nullptr == resampler_context_) {
ATLTRACE2(atlTraceException, 0, "!swr_alloc_set_opts()\n");
return AVERROR(ENOMEM);
ATLTRACE2(atlTraceException, 0, "!swr_alloc_set_opts2(), #%d\n", ec);
return ec;
}

int error = swr_init(resampler_context_);
Expand All @@ -63,9 +65,8 @@ int AudioResampler::Initialize(int64_t in_channel_layout,
}
}

fifo_ = av_audio_fifo_alloc(
out_sample_format_,
av_get_channel_layout_nb_channels(out_channel_layout_), frame_size_ * 3);
fifo_ = av_audio_fifo_alloc(out_sample_format_,
out_channel_layout_.nb_channels, frame_size_ * 3);
if (nullptr == fifo_) {
ATLTRACE2(atlTraceException, 0, "!av_audio_fifo_alloc()\n");
return AVERROR(ENOMEM);
Expand Down Expand Up @@ -109,7 +110,7 @@ int AudioResampler::Store(const uint8_t* in,
return AVERROR_EXIT;
}
} else {
int channel_count = av_get_channel_layout_nb_channels(out_channel_layout_);
int channel_count = out_channel_layout_.nb_channels;
auto converted_samples =
static_cast<uint8_t**>(uint8_ptr_pool_.ordered_malloc(channel_count));
if (nullptr == converted_samples) {
Expand Down Expand Up @@ -196,7 +197,7 @@ int AudioResampler::InitializeFrame(AVFrame*& frame,
frame->nb_samples = frame_size;
frame->format = out_sample_format_;
frame->sample_rate = out_sample_rate_;
frame->channel_layout = out_channel_layout_;
frame->ch_layout = out_channel_layout_;

if (0 == frame_size) {
return 0;
Expand Down
6 changes: 3 additions & 3 deletions src/cge/cge/audio_resampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class AudioResampler {
AudioResampler() noexcept {}
~AudioResampler() noexcept { Free(); }

int Initialize(int64_t in_channel_layout,
int Initialize(const AVChannelLayout* in_channel_layout,
enum AVSampleFormat in_sample_format,
int in_sample_rate,
int64_t out_channel_layout,
AVChannelLayout* out_channel_layout,
enum AVSampleFormat out_sample_format,
int out_sample_rate,
int frame_size) noexcept;
Expand All @@ -38,7 +38,7 @@ class AudioResampler {
private:
int in_sample_rate_ = 0;

int64_t out_channel_layout_ = 0;
AVChannelLayout out_channel_layout_{};
AVSampleFormat out_sample_format_ = AV_SAMPLE_FMT_NONE;
int out_sample_rate_ = 0;
int frame_size_ = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/cge/cge/cge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace po = boost::program_options;

using namespace std::literals::string_view_literals;

constexpr auto kProgramInfo{"KSYUN Edge Cloud Gaming Engine v0.4 Beta"sv};
constexpr auto kProgramInfo{"KSYUN Edge Cloud Gaming Engine v0.5 Beta"sv};

constexpr uint64_t kDefaultAudioBitrate = 128000;
constexpr auto kDefaultBindAddress{"::"sv};
Expand Down
2 changes: 1 addition & 1 deletion src/cge/cge/game_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "game_service.h"
#include "user_manager.h"

#define USER_MANAGER 1
#define USER_MANAGER !_DEBUG

using namespace std::literals::chrono_literals;

Expand Down
27 changes: 17 additions & 10 deletions src/cge/cge/sound_capturer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ constexpr REFERENCE_TIME kReferenceTimePerSecond = 10000000;
constexpr REFERENCE_TIME kReferenceTimePerMilliSecond = 10000;

namespace {
int64_t GetChannelLayout(const WAVEFORMATEX* wave_format) noexcept {
void GetChannelLayout(const WAVEFORMATEX* wave_format,
AVChannelLayout* channel_layout) noexcept {
assert(nullptr != wave_format);

if (WAVE_FORMAT_EXTENSIBLE == wave_format->wFormatTag) {
return reinterpret_cast<const WAVEFORMATEXTENSIBLE*>(wave_format)
->dwChannelMask;
av_channel_layout_from_mask(
channel_layout,
reinterpret_cast<const WAVEFORMATEXTENSIBLE*>(wave_format)
->dwChannelMask);
} else {
av_channel_layout_default(channel_layout, wave_format->nChannels);
}
return av_get_default_channel_layout(wave_format->nChannels);
}

AVSampleFormat GetSampleFormat(const WAVEFORMATEX* wave_format) noexcept {
Expand Down Expand Up @@ -172,8 +176,9 @@ HRESULT SoundCapturer::GetAudioInfo(AudioInfo* info) {

info->sample_rate = wave_format->nSamplesPerSec;
info->sample_bits = wave_format->wBitsPerSample;
info->channel_layout = GetChannelLayout(wave_format);
info->channels = wave_format->nChannels;
// info->channel_layout = GetChannelLayout(wave_format);
// info->channels = wave_format->nChannels;
GetChannelLayout(wave_format, &info->channel_layout);
info->sample_format = GetSampleFormat(wave_format);
return hr;
}
Expand Down Expand Up @@ -254,12 +259,14 @@ HRESULT SoundCapturer::CaptureThread() {
kReferenceTimePerMilliSecond); // convert to milliseconds
ATLTRACE2(atlTraceUtil, 0, "time_between_fires = %d\n", time_between_fires);

assert(0 != out_channel_layout_);
// assert(0 != out_channel_layout_);
assert(AV_SAMPLE_FMT_NONE != out_sample_format_);
assert(0 != out_sample_rate_);
AVChannelLayout in_channel_layout;
GetChannelLayout(wave_format, &in_channel_layout);
int error_code = audio_resampler_.Initialize(
GetChannelLayout(wave_format), GetSampleFormat(wave_format),
wave_format->nSamplesPerSec, out_channel_layout_, out_sample_format_,
&in_channel_layout, GetSampleFormat(wave_format),
wave_format->nSamplesPerSec, &out_channel_layout_, out_sample_format_,
out_sample_rate_, frame_size_);
if (error_code < 0) {
APP_ERROR() << "Initialize resampler failed with " << error_code << ".\n";
Expand All @@ -273,7 +280,7 @@ HRESULT SoundCapturer::CaptureThread() {
return hr;
}
SharedAudioFrames* saf = shared_frame_;
if (saf->channels != av_get_channel_layout_nb_channels(out_channel_layout_) ||
if (saf->channels != out_channel_layout_.nb_channels ||
saf->frame_size != frame_size_ ||
saf->sample_format != out_sample_format_) {
APP_ERROR() << "Invalid audio frame format.\n";
Expand Down
9 changes: 5 additions & 4 deletions src/cge/cge/sound_capturer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ _CRT_END_C_HEADER
struct AudioInfo {
int sample_rate;
int sample_bits;
int64_t channel_layout;
int channels;
//int64_t channel_layout;
//int channels;
AVChannelLayout channel_layout;
AVSampleFormat sample_format;
};

Expand All @@ -47,7 +48,7 @@ class SoundCapturer {
bool Initialize() noexcept;
void Run();
void Stop();
void SetOutputInfo(int64_t channel_layout,
void SetOutputInfo(AVChannelLayout channel_layout,
AVSampleFormat sample_format,
int sample_rate,
int frame_size) noexcept {
Expand All @@ -74,7 +75,7 @@ class SoundCapturer {
CHandle shared_frame_ready_event_;
UINT32 frames_ = 0;

int64_t out_channel_layout_ = 0;
AVChannelLayout out_channel_layout_{};
AVSampleFormat out_sample_format_ = AV_SAMPLE_FMT_NONE;
int out_sample_rate_ = 0;
int frame_size_ = 0;
Expand Down
Loading

0 comments on commit 1757919

Please sign in to comment.