Different CPU & GPU usage between using API and Software

thomasWind

New Member
Sorry I post this thread at wrong place....
Hello!
I'm working to use obs api to push stream of specific window. The profile is 4k-4k-60fps.
When I use Api to do this work, the cpu usage is 6% higher than using obs-studio software. GPU stays in high usage. (If I use software, gpu usage would decrease slowly.

I read the source code of obs and set the same values.
Also I tried the transition to contain scene, but the usage is still high.

Here are the rough steps I use api :
Code:
// firstly start_up
auto res = obs_startup("en-US", nullptr, nullptr); //"en-US""zh-CN"
obs_data_t *settings = obs_data_create();
obs_data_set_bool(settings, "BrowserHWAccel", true);
obs_apply_private_data(settings);
obs_data_release(settings);

//load_module
obs_add_module_path("../Debug/obs-plugins/64bit", "../Debug/data/obs-plugins/%module%");
obs_load_all_modules();

//create scene and set output
obs_scene_t *scene = obs_scene_create(this->scene_name.c_str());
    if (!scene)
        return;
obs_set_output_source(0, obs_scene_get_source(scene));

//reset video
struct obs_video_info ovi;
ovi.adapter = 0;
ovi.fps_num = fps;
ovi.fps_den = 1;
ovi.graphics_module = "libobs-d3d11.dll";
ovi.output_format = VIDEO_FORMAT_NV12;
ovi.scale_type = OBS_SCALE_BICUBIC;
ovi.colorspace = VIDEO_CS_709;
ovi.range = VIDEO_RANGE_PARTIAL;
ovi.gpu_conversion = true;
ovi.base_width = base_width;
ovi.base_height = base_height;
ovi.output_width = output_width;
ovi.output_height = output_height;
std::cout << "start reset video" << std::endl;
int rtn = obs_reset_video(&ovi);

//create window-capture source and add to scene
this->addCaptureSource();
obs_scene_add(scene, this->m_window_source)

//create encoder
m_h264Streaming = obs_video_encoder_create("obs_x264",...)
m_AacStreaming = obs_audio_encoder_create("CoreAudio_AAC"...)

//create rtmp service
m_rtmpService = obs_service_create("rtmp_common",...)
m_streamOutput = obs_output_create("rtmp_output",...)

obs_encoder_set_video(m_h264Streaming, obs_get_video());
obs_encoder_set_audio(m_AacStreaming, obs_get_audio());

//start live
obs_output_set_video_encoder(m_streamOutput, m_h264Streaming);
obs_output_set_audio_encoder(m_streamOutput, m_AacStreaming, 0);
obs_output_set_service(m_streamOutput, m_rtmpService);

auto res = obs_output_start(m_streamOutput);
std::cout << "start_living res=" << res << std::endl;
if (!res)
{
    return false;
}
return true;
 

R1CH

Forum Admin
Developer
Hopefully a silly question, but:

Code:
obs_add_module_path("../Debug/obs-plugins/64bit", "../Debug/data/obs-plugins/%module%");

Are libobs and all the plugins built in release mode and only your app is built in debug?
 

thomasWind

New Member
Hopefully a silly question, but:

Code:
obs_add_module_path("../Debug/obs-plugins/64bit", "../Debug/data/obs-plugins/%module%");

Are libobs and all the plugins built in release mode and only your app is built in debug?

Thanks to reply me R1CH!
It's a small mistake, I post this thread with coping wrong code. I add the modules by compiling the source code in release.

I also used the obs-studio's source code logic to test, but the usage is still high.
1. I use obs default setting, let it init automaticlly.
2. then created the scene by code. And I can see the scene and window-capture in obs-gui.
1637117696258.png


1637117497180.png


3. If I use obs-gui's button to start stream, the usage is normal as the obs-studio software. (It launched well both in debug and release mode)
4. If I use api to start stream, the usage is still bad. The steps are here.



Code:
//create encoder
m_h264Streaming = obs_video_encoder_create("obs_x264",...)
m_AacStreaming = obs_audio_encoder_create("CoreAudio_AAC"...)

//create rtmp service
m_rtmpService = obs_service_create("rtmp_common",...)
m_streamOutput = obs_output_create("rtmp_output",...)

obs_encoder_set_video(m_h264Streaming, obs_get_video());
obs_encoder_set_audio(m_AacStreaming, obs_get_audio());

//start live
obs_output_set_video_encoder(m_streamOutput, m_h264Streaming);
obs_output_set_audio_encoder(m_streamOutput, m_AacStreaming, 0);
obs_output_set_service(m_streamOutput, m_rtmpService);

auto res = obs_output_start(m_streamOutput);
std::cout << "start_living res=" << res << std::endl;
if (!res)
{
    return false;
}
return true;
 

R1CH

Forum Admin
Developer
Perhaps the difference is in your x264 settings then. A small difference in settings can have a large impact in CPU use.
 
Top