Skip to content

Commit

Permalink
各種コーデックの情報を取得してくるところまで実装
Browse files Browse the repository at this point in the history
  • Loading branch information
melpon committed Jan 30, 2025
1 parent 5227f4c commit 8f8972c
Show file tree
Hide file tree
Showing 26 changed files with 852 additions and 183 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ target_sources(sora
src/sora_signaling.cpp
src/sora_video_decoder_factory.cpp
src/sora_video_encoder_factory.cpp
src/sora_video_codec.cpp
src/ssl_verifier.cpp
src/url_parts.cpp
src/version.cpp
src/vpl_session_impl.cpp
src/websocket.cpp
src/zlib_helper.cpp
)
Expand Down Expand Up @@ -258,6 +260,7 @@ if (SORA_TARGET_OS STREQUAL "windows")
if (USE_NVCODEC_ENCODER)
target_sources(sora
PRIVATE
src/hwenc_nvcodec/nvcodec_video_codec.cpp
src/hwenc_nvcodec/nvcodec_video_encoder.cpp
third_party/NvCodec/NvCodec/NvEncoder/NvEncoder.cpp
third_party/NvCodec/NvCodec/NvEncoder/NvEncoderD3D11.cpp
Expand All @@ -281,13 +284,15 @@ if (SORA_TARGET_OS STREQUAL "windows")

set_source_files_properties(
src/cuda_context_cuda.cpp
src/hwenc_nvcodec/nvcodec_video_codec_cuda.cpp
third_party/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp
src/hwenc_nvcodec/nvcodec_decoder_cuda.cpp
PROPERTIES
CUDA_SOURCE_PROPERTY_FORMAT OBJ
)
cuda_compile(CUDA_FILES
src/cuda_context_cuda.cpp
src/hwenc_nvcodec/nvcodec_video_codec_cuda.cpp
third_party/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp
src/hwenc_nvcodec/nvcodec_decoder_cuda.cpp
OPTIONS
Expand Down Expand Up @@ -329,7 +334,7 @@ if (SORA_TARGET_OS STREQUAL "windows")
find_package(VPL REQUIRED)
target_sources(sora
PRIVATE
src/hwenc_vpl/vpl_session_impl.cpp
src/hwenc_vpl/vpl_video_codec.cpp
src/hwenc_vpl/vpl_video_decoder.cpp
src/hwenc_vpl/vpl_video_encoder.cpp
)
Expand Down Expand Up @@ -472,6 +477,8 @@ elseif (SORA_TARGET_OS STREQUAL "ubuntu")
target_sources(sora
PRIVATE
src/cuda_context_cuda.cpp
src/hwenc_nvcodec/nvcodec_video_codec.cpp
src/hwenc_nvcodec/nvcodec_video_codec_cuda.cpp
src/hwenc_nvcodec/nvcodec_video_encoder.cpp
src/hwenc_nvcodec/nvcodec_v4l2_capturer.cpp
src/hwenc_nvcodec/nvcodec_video_encoder_cuda.cpp
Expand All @@ -489,6 +496,7 @@ elseif (SORA_TARGET_OS STREQUAL "ubuntu")
# これらのソースは CUDA としてコンパイルする
set_source_files_properties(
src/cuda_context_cuda.cpp
src/hwenc_nvcodec/nvcodec_video_codec_cuda.cpp
src/hwenc_nvcodec/nvcodec_video_encoder_cuda.cpp
src/hwenc_nvcodec/nvcodec_decoder_cuda.cpp
third_party/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp
Expand All @@ -506,7 +514,7 @@ elseif (SORA_TARGET_OS STREQUAL "ubuntu")
target_include_directories(sora PRIVATE /usr/local/cuda/include)
target_sources(sora
PRIVATE
src/hwenc_vpl/vpl_session_impl.cpp
src/hwenc_vpl/vpl_video_codec.cpp
src/hwenc_vpl/vpl_video_decoder.cpp
src/hwenc_vpl/vpl_video_encoder.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SORA_CPP_SDK_VERSION=2025.2.0-canary.0
WEBRTC_BUILD_VERSION=m132.6834.5.2
WEBRTC_BUILD_VERSION=m132.6834.5.4
BOOST_VERSION=1.87.0
CMAKE_VERSION=3.31.4
CUDA_VERSION=11.8.0-1
Expand Down
2 changes: 1 addition & 1 deletion examples/VERSION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SORA_CPP_SDK_VERSION=2025.2.0-canary.0
WEBRTC_BUILD_VERSION=m132.6834.5.2
WEBRTC_BUILD_VERSION=m132.6834.5.4
BOOST_VERSION=1.87.0
CMAKE_VERSION=3.31.4
SDL2_VERSION=2.30.11
Expand Down
4 changes: 0 additions & 4 deletions include/sora/cuda_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class CudaContext {
// CUDA コンテキスト生成する。
// CUDA に対応していないプラットフォームでは nullptr を返す。
static std::shared_ptr<CudaContext> Create();
void* Context() const;

private:
std::shared_ptr<void> impl_;
};

enum class CudaVideoCodec {
Expand Down
16 changes: 16 additions & 0 deletions include/sora/hwenc_nvcodec/nvcodec_video_codec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SORA_HWENC_NVCODEC_NVCODEC_VIDEO_CODEC_H_
#define SORA_HWENC_NVCODEC_NVCODEC_VIDEO_CODEC_H_

#include <memory>

#include "sora/cuda_context.h"
#include "sora/sora_video_codec.h"

namespace sora {

VideoCodecCapability::Engine GetNvCodecVideoCodecCapability(
std::shared_ptr<CudaContext> context);

} // namespace sora

#endif
14 changes: 0 additions & 14 deletions include/sora/hwenc_vpl/vpl_session.h

This file was deleted.

14 changes: 14 additions & 0 deletions include/sora/hwenc_vpl/vpl_video_codec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef SORA_HWENC_VPL_VPL_VIDEO_CODEC_H_
#define SORA_HWENC_VPL_VPL_VIDEO_CODEC_H_

#include "sora/sora_video_codec.h"
#include "sora/vpl_session.h"

namespace sora {

VideoCodecCapability::Engine GetVplVideoCodecCapability(
std::shared_ptr<VplSession> session);

} // namespace sora

#endif
2 changes: 1 addition & 1 deletion include/sora/hwenc_vpl/vpl_video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <api/video/video_codec_type.h>
#include <api/video_codecs/video_decoder.h>

#include "vpl_session.h"
#include "sora/vpl_session.h"

namespace sora {

Expand Down
2 changes: 1 addition & 1 deletion include/sora/hwenc_vpl/vpl_video_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <api/video/video_codec_type.h>
#include <api/video_codecs/video_encoder.h>

#include "vpl_session.h"
#include "sora/vpl_session.h"

namespace sora {

Expand Down
7 changes: 6 additions & 1 deletion include/sora/open_h264_video_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
#include <api/video_codecs/video_encoder.h>
#include <media/base/codec.h>

#include "sora_video_codec.h"

namespace sora {

std::unique_ptr<webrtc::VideoEncoder> CreateOpenH264VideoEncoder(
const webrtc::SdpVideoFormat& format,
std::string openh264);

}
VideoCodecCapability::Engine GetOpenH264VideoCodecCapability(
std::optional<std::string> openh264_path);

} // namespace sora

#endif
110 changes: 110 additions & 0 deletions include/sora/sora_video_codec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#ifndef SORA_SORA_VIDEO_CODEC_H_
#define SORA_SORA_VIDEO_CODEC_H_

// WebRTC
#include <api/video/video_codec_type.h>

// Boost
#include <boost/json.hpp>

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "cuda_context.h"
#include "vpl_session.h"

namespace webrtc {

// VideoCodecType
void tag_invoke(const boost::json::value_from_tag&,
boost::json::value& jv,
const VideoCodecType& v);
VideoCodecType tag_invoke(const boost::json::value_to_tag<VideoCodecType>&,
boost::json::value const& jv);

} // namespace webrtc

namespace sora {

enum class VideoCodecImplementation {
kInternal,
kCiscoOpenH264,
kIntelVpl,
kNvidiaVideoCodecSdk,
};

struct VideoCodecCapability {
struct Parameters {
std::optional<std::string> version;
std::optional<std::string> openh264_path;
std::optional<std::string> vpl_impl;
std::optional<int> vpl_impl_value;
std::optional<std::string> nvcodec_gpu_device_name;
};
struct Codec {
Codec(webrtc::VideoCodecType type, bool decoder, bool encoder)
: type(type), decoder(decoder), encoder(encoder) {}
webrtc::VideoCodecType type;
bool decoder;
bool encoder;
Parameters parameters;
};
struct Engine {
Engine(VideoCodecImplementation name) : name(name) {}
VideoCodecImplementation name;
std::vector<Codec> codecs;
Parameters parameters;
};
std::vector<Engine> engines;
};

// VideoCodecImplementation
void tag_invoke(const boost::json::value_from_tag&,
boost::json::value& jv,
const VideoCodecImplementation& v);
VideoCodecImplementation tag_invoke(
const boost::json::value_to_tag<VideoCodecImplementation>&,
boost::json::value const& jv);
// VideoCodecCapability::Parameters
void tag_invoke(const boost::json::value_from_tag&,
boost::json::value& jv,
const VideoCodecCapability::Parameters& v);
VideoCodecCapability::Parameters tag_invoke(
const boost::json::value_to_tag<VideoCodecCapability::Parameters>&,
boost::json::value const& jv);
// VideoCodecCapability::Codec
void tag_invoke(const boost::json::value_from_tag&,
boost::json::value& jv,
const VideoCodecCapability::Codec& v);
VideoCodecCapability::Codec tag_invoke(
const boost::json::value_to_tag<VideoCodecCapability::Codec>&,
boost::json::value const& jv);
// VideoCodecCapability::Engine
void tag_invoke(const boost::json::value_from_tag&,
boost::json::value& jv,
const VideoCodecCapability::Engine& v);
VideoCodecCapability::Engine tag_invoke(
const boost::json::value_to_tag<VideoCodecCapability::Engine>&,
boost::json::value const& jv);
// VideoCodecCapability
void tag_invoke(const boost::json::value_from_tag&,
boost::json::value& jv,
const VideoCodecCapability& v);
VideoCodecCapability tag_invoke(
const boost::json::value_to_tag<VideoCodecCapability>&,
boost::json::value const& jv);

struct VideoCodecCapabilityConfig {
std::shared_ptr<CudaContext> cuda_context;
std::shared_ptr<VplSession> vpl_session;
std::optional<std::string> openh264_path;
void* jni_env = nullptr;
};

VideoCodecCapability GetVideoCodecCapability(VideoCodecCapabilityConfig config);

} // namespace sora

#endif
16 changes: 16 additions & 0 deletions include/sora/vpl_session.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SORA_VPL_SESSION_H_
#define SORA_VPL_SESSION_H_

#include <memory>

namespace sora {

struct VplSession {
// Intel VPL のセッションを作成する
// 対応してない場合やエラーが発生した場合は nullptr を返す
static std::shared_ptr<VplSession> Create();
};

} // namespace sora

#endif
21 changes: 8 additions & 13 deletions src/cuda_context_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ simplelogger::Logger* logger =

namespace sora {

CUcontext GetCudaContext(std::shared_ptr<CudaContext> ctx) {
return static_cast<CUcontext>(ctx->Context());
}

struct CudaContextImpl {
struct CudaContextImpl : CudaContext {
CUdevice device;
CUcontext context;
~CudaContextImpl() { dyn::cuCtxDestroy(context); }
Expand All @@ -46,7 +42,6 @@ struct CudaContextImpl {
throw std::exception()

std::shared_ptr<CudaContext> CudaContext::Create() {
std::shared_ptr<CudaContext> ctx;
try {
CUdevice device;
CUcontext context;
Expand All @@ -62,21 +57,21 @@ std::shared_ptr<CudaContext> CudaContext::Create() {
//RTC_LOG(LS_INFO) << "GPU in use: " << device_name;
ckerror(dyn::cuCtxCreate(&context, 0, device));

std::shared_ptr<CudaContextImpl> impl(new CudaContextImpl());
auto impl = std::make_shared<CudaContextImpl>();
impl->device = device;
impl->context = context;

ctx.reset(new CudaContext());
ctx->impl_ = impl;
return impl;
} catch (std::exception&) {
return nullptr;
}
}

return ctx;
CUdevice GetCudaDevice(std::shared_ptr<CudaContext> ctx) {
return std::static_pointer_cast<CudaContextImpl>(ctx)->device;
}

void* CudaContext::Context() const {
return std::static_pointer_cast<CudaContextImpl>(impl_)->context;
CUcontext GetCudaContext(std::shared_ptr<CudaContext> ctx) {
return std::static_pointer_cast<CudaContextImpl>(ctx)->context;
}

} // namespace sora
Expand Down
3 changes: 2 additions & 1 deletion src/cuda_context_cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

namespace sora {

CUdevice GetCudaDevice(std::shared_ptr<CudaContext> ctx);
CUcontext GetCudaContext(std::shared_ptr<CudaContext> ctx);

}
} // namespace sora

#endif
Loading

0 comments on commit 8f8972c

Please sign in to comment.