Created
May 19, 2016 06:08
-
-
Save nswutong/d1b2fb024bd2b962e37f3d887e71dc53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <time.h> | |
| #include <windows.h> | |
| #include <util/base.h> | |
| #include <graphics/vec2.h> | |
| #include <media-io/audio-resampler.h> | |
| #include <obs.h> | |
| #include <intrin.h> | |
| //#define WithTest | |
| static void do_log(int log_level, const char *msg, va_list args, void *param) | |
| { | |
| char bla[4096]; | |
| vsnprintf(bla, 4095, msg, args); | |
| OutputDebugStringA(bla); | |
| OutputDebugStringA("\n"); | |
| if (log_level < LOG_WARNING) | |
| { | |
| __debugbreak(); | |
| } | |
| UNUSED_PARAMETER(param); | |
| } | |
| #ifdef WithTest | |
| static LRESULT CALLBACK sceneProc(HWND hwnd, UINT message, WPARAM wParam, | |
| LPARAM lParam) | |
| { | |
| switch (message) { | |
| case WM_CLOSE: | |
| PostQuitMessage(0); | |
| break; | |
| default: | |
| return DefWindowProc(hwnd, message, wParam, lParam); | |
| } | |
| return 0; | |
| } | |
| #endif // WithTest | |
| #ifdef WithTest | |
| int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine, | |
| int numCmd) | |
| { | |
| UNUSED_PARAMETER(numCmd); | |
| UNUSED_PARAMETER(cmdLine); | |
| UNUSED_PARAMETER(prevInstance); | |
| #else | |
| int main() | |
| { | |
| #endif | |
| uint32_t width = 1920; | |
| uint32_t height = 1080; | |
| base_set_log_handler(do_log, nullptr); | |
| if (!obs_startup("en-US", nullptr, nullptr)) | |
| { | |
| blog(LOG_ERROR, "Couldn't create OBS"); | |
| } | |
| obs_load_all_modules(); | |
| { | |
| struct obs_video_info ovi; | |
| ovi.adapter = 0; | |
| ovi.base_width = width; | |
| ovi.base_height = height; | |
| ovi.fps_num = 24; | |
| ovi.fps_den = 1; | |
| ovi.graphics_module = DL_D3D11; | |
| ovi.output_format = VIDEO_FORMAT_RGBA; | |
| ovi.output_width = width; | |
| ovi.output_height = height; | |
| if (obs_reset_video(&ovi) != 0) | |
| { | |
| blog(LOG_ERROR, "Couldn't initialize video"); | |
| } | |
| } | |
| { | |
| struct obs_audio_info ai; | |
| ai.samples_per_sec = 44100;// 非常重要! | |
| ai.speakers = SPEAKERS_STEREO; | |
| if (obs_reset_audio(&ai) != true) | |
| { | |
| blog(LOG_ERROR, "Couldn't initialize audio"); | |
| } | |
| } | |
| obs_data_t *videoSourceSetting = obs_data_create(); | |
| { | |
| obs_data_set_string(videoSourceSetting, "window", ":UnrealWindow:"); | |
| obs_data_set_bool(videoSourceSetting, "capture_any_fullscreen", false); | |
| obs_data_set_bool(videoSourceSetting, "capture_cursor", false); | |
| } | |
| obs_source_t *videoSource = obs_source_create("game_capture", "", videoSourceSetting, nullptr); | |
| //obs_source_t *videoSource = obs_source_create("monitor_capture", "", nullptr, nullptr); | |
| obs_set_output_source(0, videoSource); | |
| obs_source_t *audioSource = obs_source_create("wasapi_output_capture", "", nullptr, nullptr); | |
| obs_set_output_source(1, audioSource); | |
| obs_encoder_t *h264Encoder = obs_video_encoder_create("obs_x264", "", nullptr, nullptr); | |
| obs_encoder_set_video(h264Encoder, obs_get_video()); | |
| obs_encoder_t *aacEncoder = obs_audio_encoder_create("ffmpeg_aac", "", nullptr, 0, nullptr); | |
| obs_data_t *aacSettings = obs_data_create(); | |
| { | |
| obs_data_set_int(aacSettings, "bitrate", 160); | |
| obs_data_set_bool(aacSettings, "cbr", true); | |
| obs_data_release(aacSettings); | |
| } | |
| obs_encoder_update(aacEncoder, aacSettings); | |
| obs_encoder_set_audio(aacEncoder, obs_get_audio()); | |
| obs_output_t *fileOutput = obs_output_create("ffmpeg_muxer", "", nullptr, nullptr); | |
| obs_output_set_video_encoder(fileOutput, h264Encoder); | |
| obs_output_set_audio_encoder(fileOutput, aacEncoder, 0); | |
| Sleep(3000); | |
| #ifdef WithTest | |
| { | |
| WNDCLASS wc; | |
| memset(&wc, 0, sizeof(wc)); | |
| wc.lpszClassName = TEXT("Test"); | |
| wc.hbrBackground = (HBRUSH)COLOR_WINDOW; | |
| wc.hInstance = instance; | |
| wc.hCursor = LoadCursor(NULL, IDC_ARROW); | |
| wc.lpfnWndProc = (WNDPROC)sceneProc; | |
| if (!RegisterClass(&wc)) | |
| { | |
| return 0; | |
| } | |
| HWND hwnd = CreateWindow(TEXT("Test"), TEXT("Test"), | |
| WS_OVERLAPPEDWINDOW | WS_VISIBLE, | |
| 0, 0, width, height, | |
| NULL, NULL, instance, NULL); | |
| RECT rc; | |
| GetWindowRect(hwnd, &rc); | |
| gs_init_data info = {}; | |
| { | |
| info.cx = rc.right; | |
| info.cy = rc.bottom; | |
| info.format = GS_RGBA; | |
| info.zsformat = GS_ZS_NONE; | |
| info.window.hwnd = hwnd; | |
| } | |
| obs_display_t *display = obs_display_create(&info); | |
| obs_display_add_draw_callback(display, [](void *, uint32_t, uint32_t) {obs_render_main_view(); }, nullptr); | |
| MSG msg; | |
| while (GetMessage(&msg, NULL, 0, 0)) | |
| { | |
| TranslateMessage(&msg); | |
| DispatchMessage(&msg); | |
| } | |
| DestroyWindow(hwnd); | |
| } | |
| #else | |
| obs_data_t *settings = obs_data_create(); | |
| { | |
| //obs_data_set_string(settings, "format_name", "mp4"); | |
| //obs_data_set_string(settings, "video_encoder", "utvideo"); | |
| //obs_data_set_string(settings, "audio_encoder", "pcm_s16le"); | |
| obs_data_set_string(settings, "path", "C:/Users/wutongfei/Videos/Test.mp4"); | |
| } | |
| obs_output_update(fileOutput, settings); | |
| obs_data_release(settings); | |
| //width = obs_source_get_width(videoSource); | |
| //height = obs_source_get_height(videoSource); | |
| { | |
| struct obs_video_info ovi; | |
| ovi.adapter = 0; | |
| ovi.base_width = width; | |
| ovi.base_height = height; | |
| ovi.fps_num = 30; | |
| ovi.fps_den = 1; | |
| ovi.graphics_module = DL_D3D11; | |
| ovi.output_format = VIDEO_FORMAT_RGBA; | |
| ovi.output_width = width; | |
| ovi.output_height = height; | |
| if (obs_reset_video(&ovi) != 0) | |
| { | |
| blog(LOG_ERROR, "Couldn't initialize video"); | |
| } | |
| } | |
| obs_output_start(fileOutput); | |
| SuspendThread(GetCurrentThread()); | |
| obs_output_stop(fileOutput); | |
| #endif // WithTest | |
| obs_shutdown(); | |
| blog(LOG_INFO, "Number of memory leaks: %ld", bnum_allocs()); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment