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

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

1:使用GDI函數(shù)或者Windows Media API函數(shù)
2:使用DirectX技術(shù)
3:使用api hook技術(shù)(如D3D游戲捕捉)
4:使用圖形驅(qū)動技術(shù)

obs的窗口、桌面捕捉在win-capture插件中。它使用到的是GDI技術(shù)。
相比于api hook或者圖形驅(qū)動技術(shù),GDI在效率與性能方便確實略有差距。但確實不失為一種最簡單的方法。
它的本質(zhì)就是抓取窗口或桌面快照(其實桌面也是一種特殊的窗口,也包含窗口句柄)
。只要有句柄,我們就可以得到了設(shè)備上下文(DC),就可以利用blit(復(fù)制)它的內(nèi)容到我們創(chuàng)建的DC中。

步驟如下:

1:上層枚舉窗口或桌面,將選中的窗口名等屬性傳到此插件中。

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

2:根據(jù)參數(shù)找到對應(yīng)的窗口或桌面。

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

3:libobs底層不斷調(diào)用static void wc_tick(void *data, float seconds)來獲取數(shù)據(jù)
,而此函數(shù)調(diào)用了如下函數(shù)

//創(chuàng)建一個設(shè)備兼容的DC  
capture->hdc = CreateCompatibleDC(NULL);
//創(chuàng)建可以直接寫入的、與設(shè)備無關(guān)的位圖(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的圖象復(fù)制到兼容DC中   
BitBlt(hdc, 0, 0, capture->width, capture->height,hdc_target, capture->x, capture->y, SRCCOPY);
ReleaseDC(NULL, hdc_target);

5:釋放創(chuàng)建的對象

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

6:當(dāng)然很多時候需要捕捉鼠標,需要在此位圖上畫一個icon

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

有些特殊需求獲取捕捉的原始圖像,數(shù)據(jù)保存在capture->bits中,是rgb數(shù)據(jù)。當(dāng)然也可以直接保存為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++;
}
值得注意的是:

在桌面或窗口捕捉時,記得設(shè)置多適配器兼容模式,否則你回經(jīng)常發(fā)現(xiàn)抓的數(shù)據(jù)為空,即為黑色。

上層調(diào)用

既然知道了底層實現(xiàn),那上層如何調(diào)用呢?

    ui->pCBoxWindows->clear();
    //創(chuàng)建窗口捕捉資源
    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);
    //設(shè)置多適配器兼容
    obs_data_set_bool(settings, "compatibility", true);
    //更新設(shè)置,只有更新,底層才會生效
    obs_source_update(source, settings);
    obs_properties_destroy(pProperties);

以上就是obs中Windows窗口,桌面捕捉的實現(xiàn)。mac的原理類似,只是使用了不同的系統(tǒng)API。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容