Skip to content

Commit

Permalink
obs-webrtc: prototype / playground
Browse files Browse the repository at this point in the history
  • Loading branch information
DDRBoxman committed Aug 26, 2022
1 parent a709bc4 commit 209ebd0
Show file tree
Hide file tree
Showing 22 changed files with 3,804 additions and 2 deletions.
8 changes: 6 additions & 2 deletions UI/window-basic-settings-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern QCefCookieManager *panel_cookies;
enum class ListOpt : int {
ShowAll = 1,
Custom,
WHIP,
};

enum class Section : int {
Expand All @@ -38,7 +39,8 @@ enum class Section : int {

inline bool OBSBasicSettings::IsCustomService() const
{
return ui->service->currentData().toInt() == (int)ListOpt::Custom;
return ui->service->currentData().toInt() == (int)ListOpt::Custom ||
ui->service->currentData().toInt() == (int)ListOpt::WHIP;
}

void OBSBasicSettings::InitStreamPage()
Expand Down Expand Up @@ -235,7 +237,7 @@ void OBSBasicSettings::SaveStream1Settings()
obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text()));

OBSServiceAutoRelease newService = obs_service_create(
service_id, "default_service", settings, hotkeyData);
"whip", "default_service", settings, hotkeyData);

if (!newService)
return;
Expand Down Expand Up @@ -349,6 +351,8 @@ void OBSBasicSettings::LoadServices(bool showAll)
for (QString &name : names)
ui->service->addItem(name);

ui->service->insertItem(0, QTStr("WHIP"), QVariant((int)ListOpt::WHIP));

if (!showAll) {
ui->service->addItem(
QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"),
Expand Down
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ add_subdirectory(obs-transitions)
add_subdirectory(rtmp-services)
add_subdirectory(text-freetype2)
add_subdirectory(aja)
add_subdirectory(obs-webrtc)
51 changes: 51 additions & 0 deletions plugins/obs-webrtc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
project(obs-webrtc)

include(FetchContent)

FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/AndrewGaspar/corrosion.git
GIT_TAG origin/master # Optionally specify a version tag or branch here
)

FetchContent_MakeAvailable(Corrosion)

# find_package(Libcurl REQUIRED)

include_directories(${LIBCURL_INCLUDE_DIRS})

include_directories(${OBS_JANSSON_INCLUDE_DIRS})

option(ENABLE_FFMPEG_LOGGING "Enables obs-ffmpeg logging" OFF)

find_package(
FFmpeg REQUIRED
COMPONENTS avcodec
avfilter
avdevice
avutil
swscale
avformat
swresample)
include_directories(${FFMPEG_INCLUDE_DIRS})

set(obs-webrtc_HEADERS webrtc-stream.h webrtc-source.h webrtc-call.h)
set(obs-webrtc_SOURCES whip.c webrtc-stream.c webrtc-source.c webrtc.c
webrtc-call.c)

if(WIN32)
set(MODULE_DESCRIPTION "OBS webrtc module")
configure_file(${CMAKE_SOURCE_DIR}/cmake/bundle/windows/obs-module.rc.in
obs-webrtc.rc)
list(APPEND obs-webrtc_SOURCES obs-webrtc.rc)
endif()

add_library(obs-webrtc MODULE ${obs-webrtc_SOURCES} ${obs-webrtc_HEADERS})

corrosion_import_crate(MANIFEST_PATH webrtc/Cargo.toml)

target_link_libraries(obs-webrtc libobs webrtc-rs-lib ${OBS_JANSSON_IMPORT}
${LIBCURL_LIBRARIES} ${FFMPEG_LIBRARIES})
set_target_properties(obs-webrtc PROPERTIES FOLDER "plugins")

setup_plugin_target(obs-webrtc data)
Empty file added plugins/obs-webrtc/data/.keepme
Empty file.
121 changes: 121 additions & 0 deletions plugins/obs-webrtc/webrtc-call.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>

#include <graphics/graphics.h>

#include "./webrtc/bindings.h"

#include <obs.h>

OBSWebRTCCall *call;
struct gs_texture *texture;

static inline enum video_format convert_pixel_format(int f)
{
switch (f) {
case AV_PIX_FMT_NONE:
return VIDEO_FORMAT_NONE;
case AV_PIX_FMT_YUV420P:
return VIDEO_FORMAT_I420;
case AV_PIX_FMT_NV12:
return VIDEO_FORMAT_NV12;
case AV_PIX_FMT_YUYV422:
return VIDEO_FORMAT_YUY2;
case AV_PIX_FMT_YUV444P:
return VIDEO_FORMAT_I444;
case AV_PIX_FMT_UYVY422:
return VIDEO_FORMAT_UYVY;
case AV_PIX_FMT_RGBA:
return VIDEO_FORMAT_RGBA;
case AV_PIX_FMT_BGRA:
return VIDEO_FORMAT_BGRA;
case AV_PIX_FMT_BGR0:
return VIDEO_FORMAT_BGRX;
case AV_PIX_FMT_YUVA420P:
return VIDEO_FORMAT_I40A;
case AV_PIX_FMT_YUVA422P:
return VIDEO_FORMAT_I42A;
case AV_PIX_FMT_YUVA444P:
return VIDEO_FORMAT_YUVA;
default:;
}

return VIDEO_FORMAT_NONE;
}

void packet_callback(void *user_data, uint8_t *packet_data, int length)
{

//blog(LOG_INFO, "%d", length);

AVCodecContext *context = (AVCodecContext *)user_data;

AVPacket *packet = av_packet_alloc();

av_new_packet(packet, 1024 * 10);

memcpy(packet->data, packet_data, length);

avcodec_send_packet(context, packet);

AVFrame *frame = av_frame_alloc();
int res = avcodec_receive_frame(context, frame);
if (res == 0) {
blog(LOG_INFO, "Width: %d, Height: %d", frame->width,
frame->height);
int format = convert_pixel_format(frame->format);

obs_enter_graphics();

struct gs_texture *texture =
gs_texture_create(frame->width, frame->height, format,
1, (const uint8_t **)&frame->data, 0);

obs_leave_graphics();
}

if (res == AVERROR(EAGAIN)) {
// NO FRAME
}

av_free(frame);
}

void setup_call()
{
AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);

AVDictionary *opts = NULL;
//av_dict_set(&opts, "b", "2.5M", 0);
if (!codec)
exit(1);
AVCodecContext *context = avcodec_alloc_context3(codec);
if (avcodec_open2(context, codec, &opts) < 0)
exit(1);

AVCodecParameters *codecpar = avcodec_parameters_alloc();
codecpar->codec_id = AV_CODEC_ID_H264;
codecpar->height = 720;
codecpar->width = 1280;

//codecpar->

//avcodec_parameters_to_context(context, codecpar);

call = obs_webrtc_call_init(context, packet_callback);
}

void start_call()
{
obs_webrtc_call_start(call);
}

void frame(AVCodecContext *context)
{
AVFrame frame;
avcodec_receive_frame(context, &frame);

struct gs_texture *texture =
gs_texture_create(frame.width, frame.height, frame.format, 1,
(const uint8_t **)&frame.data, 0);
}
4 changes: 4 additions & 0 deletions plugins/obs-webrtc/webrtc-call.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void setup_call();
void start_call();
42 changes: 42 additions & 0 deletions plugins/obs-webrtc/webrtc-services-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "util/text-lookup.h"
#include "util/threading.h"
#include "util/platform.h"
#include "util/dstr.h"
#include "obs-module.h"
#include "file-updater/file-updater.h"

OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("obs-webrtc-services", "en-US")
MODULE_EXPORT const char *obs_module_description(void)
{
return "OBS core WebRTC services";
}

#define RTMP_SERVICES_LOG_STR "[obs-webrtc-services plugin] "
#define RTMP_SERVICES_VER_STR \
"obs-webrtc-services plugin (libobs " OBS_VERSION ")"

extern struct obs_service_info whip_service;

static update_info_t *update_info = NULL;
static struct dstr module_name = {0};

const char *get_module_name(void)
{
return module_name.array;
}

bool obs_module_load(void)
{
dstr_copy(&module_name, "obs-webrtc-services plugin (libobs ");
dstr_cat(&module_name, obs_get_version_string());
dstr_cat(&module_name, ")");

obs_register_service(&whip_service);
return true;
}

void obs_module_unload(void)
{
dstr_free(&module_name);
}
25 changes: 25 additions & 0 deletions plugins/obs-webrtc/webrtc-source.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <obs-module.h>

static const char *webrtc_source_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("WebRTCSource");
}

static void *webrtc_source_create(obs_data_t *settings, obs_source_t *source) {}

static void webrtc_source_destroy(void *data) {}

static obs_properties_t *webrtc_source_get_properties(void *data) {}

struct obs_source_info webrtc_source_info = {
.id = "webrtc_source",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
OBS_SOURCE_DO_NOT_DUPLICATE,
.get_name = webrtc_source_getname,
.create = webrtc_source_create,
.destroy = webrtc_source_destroy,
.get_properties = webrtc_source_get_properties,
.icon_type = OBS_ICON_TYPE_MEDIA,
};
Empty file.
Loading

0 comments on commit 209ebd0

Please sign in to comment.