obs-studio源码分析(一):窗口、显示器(桌面)捕捉

windows下窗口、桌面捕捉有多种方式。如下:

1:使用GDI函数或者Windows Media API函数
2:使用DirectX技术
3:使用api hook技术(如D3D游戏捕捉)
4:使用图形驱动技术

obs的窗口、桌面捕捉在win-capture插件中。它使用到的是GDI技术。
相比于api hook或者图形驱动技术,GDI在效率与性能方便确实略有差距。但确实不失为一种最简单的方法。
它的本质就是抓取窗口或桌面快照(其实桌面也是一种特殊的窗口,也包含窗口句柄)
。只要有句柄,我们就可以得到了设备上下文(DC),就可以利用blit(复制)它的内容到我们创建的DC中。

步骤如下:

1:上层枚举窗口或桌面,将选中的窗口名等属性传到此插件中。

static void update_settings(struct window_capture *wc, obs_data_t *s)

2:根据参数找到对应的窗口或桌面。

HWND find_window(enum window_search_mode mode,
        enum window_priority priority,
        const char *class,
        const char *title,
        const char *exe)

3:libobs底层不断调用static void wc_tick(void *data, float seconds)来获取数据
,而此函数调用了如下函数

//创建一个设备兼容的DC  
capture->hdc = CreateCompatibleDC(NULL);
//创建可以直接写入的、与设备无关的位图(DIB)
capture->bmp = CreateDIBSection(capture->hdc, &bi, DIB_RGB_COLORS, (void**)&capture->bits, NULL, 0);
//将位图选入到DC
capture->old_bmp = SelectObject(capture->hdc, capture->bmp);
//取得桌面窗口的DC
hdc_target = GetDC(window);
//将桌面窗口DC的图象复制到兼容DC中   
BitBlt(hdc, 0, 0, capture->width, capture->height,hdc_target, capture->x, capture->y, SRCCOPY);
ReleaseDC(NULL, hdc_target);

5:释放创建的对象

SelectObject(capture->hdc, capture->old_bmp);
DeleteDC(capture->hdc);
DeleteObject(capture->bmp);

6:当然很多时候需要捕捉鼠标,需要在此位图上画一个icon

static void draw_cursor(struct dc_capture *capture, HDC hdc, HWND window)
保存图像

有些特殊需求获取捕捉的原始图像,数据保存在capture->bits中,是rgb数据。当然也可以直接保存为bmp图片。如下(例子只保存5张,否则太多):

int nCount = 0;
void WriteBmp(void *data, int width, int height)
{
    if (nCount > 5)
        return;

    FILE *pFile = NULL;
    BITMAPFILEHEADER bmpheader;
    BITMAPINFO bmpinfo;

    char fileName[32];
    int bpp = 32;
    // open file
    sprintf(fileName, "frame%d.bmp", nCount);
    pFile = fopen(fileName, "wb");
    if (!pFile)
        return;

    bmpheader.bfType = ('M' << 8) | 'B';
    bmpheader.bfReserved1 = 0;
    bmpheader.bfReserved2 = 0;
    bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmpheader.bfSize = bmpheader.bfOffBits + width * height * bpp / 8;

    bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpinfo.bmiHeader.biWidth = width;
    bmpinfo.bmiHeader.biHeight = -height; //reverse the image
    bmpinfo.bmiHeader.biPlanes = 1;
    bmpinfo.bmiHeader.biBitCount = bpp;
    bmpinfo.bmiHeader.biCompression = BI_RGB;
    bmpinfo.bmiHeader.biSizeImage = 0;
    bmpinfo.bmiHeader.biXPelsPerMeter = 100;
    bmpinfo.bmiHeader.biYPelsPerMeter = 100;
    bmpinfo.bmiHeader.biClrUsed = 0;
    bmpinfo.bmiHeader.biClrImportant = 0;

    fwrite(&bmpheader, sizeof(BITMAPFILEHEADER), 1, pFile);
    fwrite(&bmpinfo.bmiHeader, sizeof(BITMAPINFOHEADER), 1, pFile);
    uint8_t *buffer = (uint8_t *)data;

    fwrite(buffer, 1, width * height * bpp / 8, pFile);

    fclose(pFile);
    nCount++;
}
值得注意的是:

在桌面或窗口捕捉时,记得设置多适配器兼容模式,否则你回经常发现抓的数据为空,即为黑色。

上层调用

既然知道了底层实现,那上层如何调用呢?

    ui->pCBoxWindows->clear();
    //创建窗口捕捉资源
    OBSSource source = obs_source_create("window_capture", "test", NULL, nullptr);
    if (source) {
        //添加到指定场景中
        OBSSceneItem sceneitem;
        sceneitem = obs_scene_add(scene, source);
        obs_sceneitem_set_visible(sceneitem, true);
    }
    //通过source获取属性
    obs_properties_t *pProperties = obs_source_properties(source);
    obs_property_t *property = obs_properties_first(pProperties);
    while (property) {
        //获取属性名字
        const char *name = obs_property_name(property);
        //属性为窗口
        if (strcmp("window", name) == 0){
            obs_combo_format format = obs_property_list_format(property);
            size_t           count = obs_property_list_item_count(property);
            for (size_t i = 0; i < count; i++){
                //获取窗口名字
                const char *name = obs_property_list_item_name(property, i);
                QVariant var;
                if (format == OBS_COMBO_FORMAT_STRING) {
                    var = obs_property_list_item_string(property, i);
                    ui->pCBoxWindows->addItem(QString::fromUtf8(name), var);
                }
            }
        }
        obs_property_next(&property);
    }
    OBSData settings = obs_source_get_settings(source);
    QString strWindow = ui->pCBoxWindows->currentData().toString();
    obs_data_set_string(settings, "window", strWindow.toLocal8Bit().data());
    obs_data_set_int(settings, "priority", 0);
    //设置多适配器兼容
    obs_data_set_bool(settings, "compatibility", true);
    //更新设置,只有更新,底层才会生效
    obs_source_update(source, settings);
    obs_properties_destroy(pProperties);

以上就是obs中Windows窗口,桌面捕捉的实现。mac的原理类似,只是使用了不同的系统API。

猜你喜欢

转载自blog.csdn.net/balijinyi/article/details/80284504