OBS源
+ -

OBS用户添加源实例

2024-03-22 62 0

上一节知道,在OBS中任右键,会弹出一菜单,可以添加通过插件模块注册的各种”输入源“
当在菜单中选中某种类型的”输入源“之后,就会调用函数AddSource。其中 id是各个模块注册输入源时提供的源id,该ID应全局惟一。

void OBSBasic::AddSource(const char *id)
{
    if (id && *id)
    {
        //创建QT对话框对象。OBSBasicSourceSelect继承于QDialog
        OBSBasicSourceSelect sourceSelect(this, id, undo_s);
        //弹出并显示对话框,等待用户输入信息
        sourceSelect.exec();

        //参数校验
        if (should_show_properties(sourceSelect.newSource, id)) 
        {
            //创建输入源实例
            CreatePropertiesWindow(sourceSelect.newSource);
        }
    }
}

AddSource函数分两步:

对话框OBSBasicSourceSelect

第一步是通过使用OBSBasicSourceSelect弹出结话框。OBSBasicSourceSelect继承于QDialog,故通过exec()函数显示对话框。
102550588878
用户配置弹出对话框做相应的配置选择后。点击确认后,执行的是函数OBSBasicSourceSelect的成员函数on_buttonBox_accepted。

该函数会根据是新建还是添加现在执行不同的流程:

void OBSBasicSourceSelect::on_buttonBox_accepted()
{
    bool useExisting = ui->selectExisting->isChecked();//新建按钮是否选中
    bool visible = ui->sourceVisible->isChecked();//便源可见选项

    if (useExisting){
        ...
    } 
    else //添加新源
    {
        //名称是否为空,默认名为媒体源
        if (ui->sourceName->text().isEmpty()) {
            OBSMessageBox::warning(this,
                           QTStr("NoNameEntered.Title"),//请输入一个有效的名称
                           QTStr("NoNameEntered.Text"));//您不能使用空白名称。
            return;
        }
        //添加新源
        if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),  visible, newSource))
            return;
        ...
    }
    done(DialogCode::Accepted);
}

AddNew函数的原理校验用户输入的名称重复性,如果不重复,就创建源实例,创建后的新源实例加入场景中。

bool AddNew(QWidget *parent, const char *id, const char *name,
        const bool visible, OBSSource &newSource)
{
    //获取主窗口
    OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
    //获取主窗口当前的场景
    OBSScene scene = main->GetCurrentScene();
    bool success = false;
    if (!scene)
        return false;

    //是否名字已经存在
    OBSSourceAutoRelease source = obs_get_source_by_name(name);
    if (source && parent) {
        OBSMessageBox::information(parent, QTStr("NameExists.Title"),
                       QTStr("NameExists.Text"));

    } 
    else 
    {
        const char *v_id = obs_get_latest_input_type_id(id);
        source = obs_source_create(v_id, name, NULL, nullptr);
        if (source) 
        {
            AddSourceData data;
            data.source = source;
            data.visible = visible;

            //添加到当彰场景中
            obs_enter_graphics();
            obs_scene_atomic_update(scene, AddSource, &data);
            obs_leave_graphics();
            ...
        }
    }

    return success;
}

创建源属性对话框

使用AddNew添加新源,而newSource以引用的方式返回,即sourceSelect.newSource

    if (should_show_properties(sourceSelect.newSource, id)) {
        CreatePropertiesWindow(sourceSelect.newSource);
    }

新创建的源是struct obs_source_t类型,新创建的源需要进行配置。所以这里会通过CreatePropertiesWindow函数创建并显示属性对话框。

void OBSBasic::CreatePropertiesWindow(obs_source_t *source)
{
    bool closed = true;
    if (properties)
        closed = properties->close();

    if (!closed)
        return;

    properties = new OBSBasicProperties(this, source);
    properties->Init();
    properties->setAttribute(Qt::WA_DeleteOnClose, true);
}

如对于视频媒体源加的属性对话框:
13211228098

0 篇笔记 写笔记

作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!