Skip to content

Commit

Permalink
refactor decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenjianYang committed Jan 19, 2022
1 parent 6df642a commit cbf599a
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 152 deletions.
3 changes: 3 additions & 0 deletions solution/ed_voice/ed_voice.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<ClInclude Include="..\..\src\ed_voice\global\info.h" />
<ClInclude Include="..\..\src\ed_voice\global\signals.h" />
<ClInclude Include="..\..\src\ed_voice\player\decoder.h" />
<ClInclude Include="..\..\src\ed_voice\player\decoder_ogg.h" />
<ClInclude Include="..\..\src\ed_voice\player\decoder_wav.h" />
<ClInclude Include="..\..\src\ed_voice\player\player.h" />
<ClInclude Include="..\..\src\ed_voice\player\player_base.h" />
<ClInclude Include="..\..\src\ed_voice\player\sound_buffer.h" />
Expand Down Expand Up @@ -62,6 +64,7 @@
<ClCompile Include="..\..\src\ed_voice\core\sora_voice.cpp" />
<ClCompile Include="..\..\src\ed_voice\ed_voice.cpp" />
<ClCompile Include="..\..\src\ed_voice\global\global.cpp" />
<ClCompile Include="..\..\src\ed_voice\player\decoder.cpp" />
<ClCompile Include="..\..\src\ed_voice\player\decoder_ogg.cpp" />
<ClCompile Include="..\..\src\ed_voice\player\decoder_wav.cpp" />
<ClCompile Include="..\..\src\ed_voice\player\player.cpp" />
Expand Down
9 changes: 9 additions & 0 deletions solution/ed_voice/ed_voice.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@
<ClInclude Include="..\..\src\ed_voice\utils\win_title.h">
<Filter>src\utils</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ed_voice\player\decoder_ogg.h">
<Filter>src\player</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ed_voice\player\decoder_wav.h">
<Filter>src\player</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\ed_voice\ed_voice.cpp">
Expand Down Expand Up @@ -219,5 +225,8 @@
<ClCompile Include="..\..\src\ed_voice\utils\win_title.cpp">
<Filter>src\utils</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ed_voice\player\decoder.cpp">
<Filter>src\player</Filter>
</ClCompile>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion solution/googletest/gmock.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Test|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
2 changes: 1 addition & 1 deletion solution/googletest/gmock_main.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Test|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
2 changes: 1 addition & 1 deletion solution/googletest/gtest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Test|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
2 changes: 1 addition & 1 deletion solution/googletest/gtest_main.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Test|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>$(DefaultPlatformToolset)</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
4 changes: 2 additions & 2 deletions solution/test/player_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{8dcf0718-20e7-4bb9-a92e-00b3ae961945}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<ProjectName>player_test</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
Expand Down
4 changes: 2 additions & 2 deletions solution/unit_test/unit_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{d0b626ec-932d-4692-92c5-f49bff1f6213}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<ProjectName>unit_test</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
Expand Down
19 changes: 19 additions & 0 deletions src/ed_voice/player/decoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "player/decoder.h"

#include "player/decoder_ogg.h"
#include "player/decoder_wav.h"

bool player::Decoder::Init() {
return player::impl::Ogg::Init() && player::impl::Wav::Init();
}

std::unique_ptr<player::Decoder> player::Decoder::Get(std::string_view decoder_name) {
if (decoder_name == "ogg") {
return player::impl::Ogg::Get();
} else if (decoder_name == "wav") {
return player::impl::Wav::Get();
} else {
return nullptr;
}
}

11 changes: 2 additions & 9 deletions src/ed_voice/player/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,8 @@ class Decoder {
std::size_t samples_read_ = 0;

public:
static std::unique_ptr<Decoder> GetWav();
static std::unique_ptr<Decoder> GetOgg();
static bool InitAllDecoders() {
return InitWav() && InitOgg();
}

protected:
static bool InitWav();
static bool InitOgg();
static std::unique_ptr<Decoder> Get(std::string_view decoder_name);
static bool Init();
}; // Decoder
} // namespace player
#endif // __PLAYER_DECODER_H__
105 changes: 34 additions & 71 deletions src/ed_voice/player/decoder_ogg.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "player/decoder.h"
#include "player/decoder_ogg.h"

#include <stdio.h>
#include <vorbis/vorbisfile.h>
Expand All @@ -7,59 +7,31 @@
#include "utils/log.h"

namespace {
using player::BuffByte;
using player::Decoder;
using player::WaveFormat;
using std::int32_t;
using std::uint32_t;
using std::uint16_t;

class Ogg : public Decoder {
public:
bool Open(const char* file_name) override;
std::size_t Read(BuffByte* buff, std::size_t samples_count) override;
void Close() override;
decltype(::ov_open_callbacks)* _ov_open_callbacks;
decltype(::ov_info)* _ov_info;
decltype(::ov_read)* _ov_read;
decltype(::ov_clear)* _ov_clear;
decltype(::ov_pcm_total)* _ov_pcm_total;
bool _ready;
} // namespace

Ogg() = default;
~Ogg() override {
this->Close();
}

private:
OggVorbis_File ov_file{};

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

private:
static decltype(::ov_open_callbacks)* ov_open_callbacks;
static decltype(::ov_info)* ov_info;
static decltype(::ov_read)* ov_read;
static decltype(::ov_clear)* ov_clear;
static decltype(::ov_pcm_total)* ov_pcm_total;
static bool ready_;

public:
static bool Init();
static bool Ready() {
return ready_;
}
}; // Ogg

bool Ogg::Open(const char* file_name) {
bool player::impl::Ogg::Open(const char* file_name) {
FILE* file = fopen(file_name, "rb");
if (file == NULL) {
return false;
}

if (ov_open_callbacks(file, &ov_file, nullptr, 0, OV_CALLBACKS_DEFAULT) != 0) {
if (_ov_open_callbacks(file, &this->ov_file, nullptr, 0, OV_CALLBACKS_DEFAULT) != 0) {
fclose(file);
return false;
}

vorbis_info* info = ov_info(&ov_file, -1);
vorbis_info* info = _ov_info(&this->ov_file, -1);

wave_format_.format_tag = 1;
wave_format_.channels = static_cast<uint16_t>(info->channels);
Expand All @@ -69,12 +41,12 @@ bool Ogg::Open(const char* file_name) {
wave_format_.avg_bytes_per_sec = wave_format_.samples_per_sec * wave_format_.block_align;

samples_read_ = 0;
samples_total_ = static_cast<std::size_t>(ov_pcm_total(&ov_file, -1));
samples_total_ = static_cast<std::size_t>(_ov_pcm_total(&this->ov_file, -1));

return true;
}

std::size_t Ogg::Read(BuffByte* buff, std::size_t samples_count) {
std::size_t player::impl::Ogg::Read(BuffByte* buff, std::size_t samples_count) {
if (!buff || !samples_count) {
return 0;
}
Expand All @@ -89,53 +61,44 @@ std::size_t Ogg::Read(BuffByte* buff, std::size_t samples_count) {
int bitstream = 0;
while (read < request) {
int block_bytes = std::min(static_cast<int>(request - read) * wave_format_.block_align, block);
int bytes_read = ov_read(&ov_file, reinterpret_cast<char*>(buff), block_bytes, 0, 2, 1, &bitstream);
int bytes_read = _ov_read(&this->ov_file, reinterpret_cast<char*>(buff), block_bytes, 0, 2, 1, &bitstream);
read += bytes_read / wave_format_.block_align;
buff += bytes_read;
}
samples_read_ += read;
return read;
}

void Ogg::Close() {
ov_clear(&ov_file);
memset(&ov_file, 0, sizeof(ov_file));
void player::impl::Ogg::Close() {
_ov_clear(&this->ov_file);
memset(&this->ov_file, 0, sizeof(this->ov_file));
}

decltype(::ov_open_callbacks)* Ogg::ov_open_callbacks;
decltype(::ov_info)* Ogg::ov_info;
decltype(::ov_read)* Ogg::ov_read;
decltype(::ov_clear)* Ogg::ov_clear;
decltype(::ov_pcm_total)* Ogg::ov_pcm_total;
bool Ogg::ready_;

bool Ogg::Init() {
if (!ready_) {

bool player::impl::Ogg::Init() {
if (!_ready) {
LOG("Loading ogg APIs...");
ready_ = utils::LoadOggApis(reinterpret_cast<void**>(&ov_open_callbacks),
reinterpret_cast<void**>(&ov_info),
reinterpret_cast<void**>(&ov_read),
reinterpret_cast<void**>(&ov_clear),
reinterpret_cast<void**>(&ov_pcm_total));
if (ready_) {
_ready = utils::LoadOggApis(reinterpret_cast<void**>(&_ov_open_callbacks),
reinterpret_cast<void**>(&_ov_info),
reinterpret_cast<void**>(&_ov_read),
reinterpret_cast<void**>(&_ov_clear),
reinterpret_cast<void**>(&_ov_pcm_total));
if (_ready) {
LOG("Load ogg APIs Finished.");
} else {
LOG("Load ogg APIs Failed.");
}
LOG("Loaded ov_open_callbacks = 0x%08X", (unsigned)ov_open_callbacks);
LOG("Loaded ov_info = 0x%08X", (unsigned)ov_info);
LOG("Loaded ov_read = 0x%08X", (unsigned)ov_read);
LOG("Loaded ov_clear = 0x%08X", (unsigned)ov_clear);
LOG("Loaded ov_pcm_total = 0x%08X", (unsigned)ov_pcm_total);
LOG("Loaded _ov_open_callbacks = 0x%08X", (unsigned)_ov_open_callbacks);
LOG("Loaded _ov_info = 0x%08X", (unsigned)_ov_info);
LOG("Loaded _ov_read = 0x%08X", (unsigned)_ov_read);
LOG("Loaded _ov_clear = 0x%08X", (unsigned)_ov_clear);
LOG("Loaded _ov_pcm_total = 0x%08X", (unsigned)_ov_pcm_total);
}
return ready_;
return _ready;
}
} // namesapce

std::unique_ptr<Decoder> player::Decoder::GetOgg() {
return Ogg::Ready() ? std::make_unique<Ogg>() : nullptr;
std::unique_ptr<player::Decoder> player::impl::Ogg::Get() {
return _ready ? std::make_unique<Ogg>() : nullptr;
}

bool player::Decoder::InitOgg() {
return Ogg::Init();
}
30 changes: 30 additions & 0 deletions src/ed_voice/player/decoder_ogg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "player/decoder.h"

#include <vorbis/vorbisfile.h>

namespace player::impl {

class Ogg : public Decoder {
public:
bool Open(const char* file_name) override;
std::size_t Read(BuffByte* buff, std::size_t samples_count) override;
void Close() override;

Ogg() = default;
~Ogg() override {
this->Close();
}

private:
OggVorbis_File ov_file{};

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

public:
static std::unique_ptr<Decoder> Get();
static bool Init();
}; // Ogg
} // namesapce player::impl
Loading

0 comments on commit cbf599a

Please sign in to comment.