From 77f0f6802a22cd08bfb80ee75ce628bf25d48923 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Thu, 18 Dec 2014 15:33:35 -0800 Subject: [PATCH] win-dshow: Put device id mangling in separate file This is going to be used for other code as well, so may as well put it in its own file. --- plugins/win-dshow/CMakeLists.txt | 1 + plugins/win-dshow/encode-dstr.hpp | 76 +++++++++++++++++++++++++++++++ plugins/win-dshow/win-dshow.cpp | 58 +---------------------- 3 files changed, 79 insertions(+), 56 deletions(-) create mode 100644 plugins/win-dshow/encode-dstr.hpp diff --git a/plugins/win-dshow/CMakeLists.txt b/plugins/win-dshow/CMakeLists.txt index eed18e759..f6e663d49 100644 --- a/plugins/win-dshow/CMakeLists.txt +++ b/plugins/win-dshow/CMakeLists.txt @@ -4,6 +4,7 @@ find_package(FFMpeg REQUIRED COMPONENTS avcodec avutil) include_directories(${FFMPEG_INCLUDE_DIRS}) set(win-dshow_HEADERS + encode-dstr.hpp ffmpeg-decode.h) set(win-dshow_SOURCES diff --git a/plugins/win-dshow/encode-dstr.hpp b/plugins/win-dshow/encode-dstr.hpp new file mode 100644 index 000000000..a8e77d211 --- /dev/null +++ b/plugins/win-dshow/encode-dstr.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include + +static inline void encode_dstr(struct dstr *str) +{ + dstr_replace(str, "#", "#22"); + dstr_replace(str, ":", "#3A"); +} + +static inline void decode_dstr(struct dstr *str) +{ + dstr_replace(str, "#3A", ":"); + dstr_replace(str, "#22", "#"); +} + +static inline void EncodeDeviceId(struct dstr *encodedStr, + const wchar_t *name_str, const wchar_t *path_str) +{ + DStr name; + DStr path; + + dstr_from_wcs(name, name_str); + dstr_from_wcs(path, path_str); + + encode_dstr(name); + encode_dstr(path); + + dstr_copy_dstr(encodedStr, name); + dstr_cat(encodedStr, ":"); + dstr_cat_dstr(encodedStr, path); +} + +static inline bool DecodeDeviceDStr(DStr &name, DStr &path, + const char *device_id) +{ + const char *path_str; + + if (!device_id || !*device_id) + return false; + + path_str = strchr(device_id, ':'); + if (!path_str) + return false; + + dstr_copy(path, path_str+1); + dstr_copy(name, device_id); + + size_t len = path_str - device_id; + name->array[len] = 0; + name->len = len; + + decode_dstr(name); + decode_dstr(path); + + return true; +} + +static inline bool DecodeDeviceId(DShow::DeviceId &out, const char *device_id) +{ + DStr name, path; + + if (!DecodeDeviceDStr(name, path, device_id)) + return false; + + BPtr wname = dstr_to_wcs(name); + out.name = wname; + + if (!dstr_is_empty(path)) { + BPtr wpath = dstr_to_wcs(path); + out.path = wpath; + } + + return true; +} + diff --git a/plugins/win-dshow/win-dshow.cpp b/plugins/win-dshow/win-dshow.cpp index 9abc53bfe..7bf2882ae 100644 --- a/plugins/win-dshow/win-dshow.cpp +++ b/plugins/win-dshow/win-dshow.cpp @@ -3,11 +3,11 @@ #include #include #include -#include #include #include #include "libdshowcapture/dshowcapture.hpp" #include "ffmpeg-decode.h" +#include "encode-dstr.hpp" #include #include @@ -317,18 +317,6 @@ static long long FrameRateInterval(const VideoInfo &cap, min(desired_interval, cap.maxInterval); } -static inline void encode_dstr(struct dstr *str) -{ - dstr_replace(str, "#", "#22"); - dstr_replace(str, ":", "#3A"); -} - -static inline void decode_dstr(struct dstr *str) -{ - dstr_replace(str, "#3A", ":"); - dstr_replace(str, "#22", "#"); -} - static inline video_format ConvertVideoFormat(VideoFormat format) { switch (format) { @@ -496,48 +484,6 @@ void DShowInput::OnAudioData(const AudioConfig &config, UNUSED_PARAMETER(endTime); } -static bool DecodeDeviceId(DStr &name, DStr &path, const char *device_id) -{ - const char *path_str; - - if (!device_id || !*device_id) - return false; - - path_str = strchr(device_id, ':'); - if (!path_str) - return false; - - dstr_copy(path, path_str+1); - dstr_copy(name, device_id); - - size_t len = path_str - device_id; - name->array[len] = 0; - name->len = len; - - decode_dstr(name); - decode_dstr(path); - - return true; -} - -static bool DecodeDeviceId(DeviceId &out, const char *device_id) -{ - DStr name, path; - - if (!DecodeDeviceId(name, path, device_id)) - return false; - - BPtr wname = dstr_to_wcs(name); - out.name = wname; - - if (!dstr_is_empty(path)) { - BPtr wpath = dstr_to_wcs(path); - out.path = wpath; - } - - return true; -} - struct PropertiesData { vector devices; vector audioDevices; @@ -1032,7 +978,7 @@ static bool ResTypeChanged(obs_properties_t *props, obs_property_t *p, static size_t AddDevice(obs_property_t *device_list, const string &id) { DStr name, path; - if (!DecodeDeviceId(name, path, id.c_str())) + if (!DecodeDeviceDStr(name, path, id.c_str())) return numeric_limits::max(); return obs_property_list_add_string(device_list, name, id.c_str());