diff --git a/docs/sphinx/reference-outputs.rst b/docs/sphinx/reference-outputs.rst index dcfe16b620779f..c3687e6406ee56 100644 --- a/docs/sphinx/reference-outputs.rst +++ b/docs/sphinx/reference-outputs.rst @@ -74,6 +74,22 @@ Output Definition Structure (obs_output_info) frame. Audio data will be correctly truncated down to the exact audio sample according to that video frame timing. + - **OBS_OUTPUT_NO_INTERLEAVE** - Output does not want A/V interleaving. + + When this capability flag is used, encoded packets are delivered as + soon as each encoder produces them, instead of being held until a + packet of the opposing type establishes a monotonic muxed order. + This is intended for transports that carry each elementary stream + independently and whose receiver synchronises from media + timestamps; a muxed container should not use it. + + The output must then handle two things itself: video is no longer + gated on the first keyframe, so the first video packet may be a + delta frame, and packets arrive per encoder with no cross-type + ordering or timestamp offset normalisation. + + Ignored unless both video and audio are present. + .. member:: const char *(*obs_output_info.get_name)(void *type_data) Get the translated name of the output type. diff --git a/docs/sphinx/reference-services.rst b/docs/sphinx/reference-services.rst index 3ed58b5d985ab3..9485b41fd14850 100644 --- a/docs/sphinx/reference-services.rst +++ b/docs/sphinx/reference-services.rst @@ -71,6 +71,16 @@ Service Definition Structure :return: The properties of the service + The frontend displays these properties in the *Service Options* group + of the Stream settings page. In that context ``get_properties`` is + called with *data* set to ``NULL`` and ``get_properties2`` is not + used. Properties named after settings owned by the Stream page + (``server``, ``key``, ``bearer_token``, ``use_auth``, ``username``, + ``password``, ``service``, ``protocol``, ``using_custom_server`` and + ``bwtest``) are removed before the group is displayed. The remaining + values are saved along with their defaults as explicit values, as + services do not have their defaults applied on creation. + .. member:: void (*obs_service_info.update)(void *data, obs_data_t *settings) Updates the settings for this service. diff --git a/frontend/data/locale/en-US.ini b/frontend/data/locale/en-US.ini index 3046ec3ca9f99d..7f12828c62c4de 100644 --- a/frontend/data/locale/en-US.ini +++ b/frontend/data/locale/en-US.ini @@ -1042,6 +1042,7 @@ Basic.Settings.Stream.WHIPSimulcastLabel="Simulcast" Basic.Settings.Stream.WHIPSimulcastInfo="Simulcast allows you to encode and send multiple video qualities. Learn More" Basic.Settings.Stream.WHIPSimulcastTotalLayers="Total Layers" Basic.Settings.Stream.AdvancedOptions="Advanced Options" +Basic.Settings.Stream.ServiceOptions="Service Options" # basic mode 'output' settings Basic.Settings.Output="Output" diff --git a/frontend/forms/OBSBasicSettings.ui b/frontend/forms/OBSBasicSettings.ui index f0e191ce8b973b..4bc753a7b1cd43 100644 --- a/frontend/forms/OBSBasicSettings.ui +++ b/frontend/forms/OBSBasicSettings.ui @@ -1848,6 +1848,27 @@ + + + + Basic.Settings.Stream.ServiceOptions + + + + 8 + + + 2 + + + 8 + + + 8 + + + + diff --git a/frontend/settings/OBSBasicSettings.hpp b/frontend/settings/OBSBasicSettings.hpp index 092288030b9fd9..1e9756fed60c1c 100644 --- a/frontend/settings/OBSBasicSettings.hpp +++ b/frontend/settings/OBSBasicSettings.hpp @@ -37,6 +37,39 @@ struct OBSTheme; std::string DeserializeConfigText(const char *value); +// Structured data for service dropdown items +struct ServiceItemData { + enum class Type { + Custom, // rtmp_custom service + ShowAll, // "Show All" option in dropdown + RtmpCommon, // Standard rtmp_common service (Twitch, YouTube, etc.) + CustomServiceType // Custom service types (WHIP, MoQ, etc.) + }; + + Type type; + QString serviceId; // Service ID: for RtmpCommon this is the service name, + // for CustomServiceType this is the service type ID (e.g., "whip_custom") + QString displayName; // Human-readable display name + + ServiceItemData() : type(Type::Custom) {} + + ServiceItemData(Type t, const QString &id = QString(), const QString &name = QString()) + : type(t), + serviceId(id), + displayName(name) + { + } + + // Helper methods for easy type checking + bool isCustom() const { return type == Type::Custom; } + bool isShowAll() const { return type == Type::ShowAll; } + bool isRtmpCommon() const { return type == Type::RtmpCommon; } + bool isCustomServiceType() const { return type == Type::CustomServiceType; } +}; + +// Register with Qt's meta-type system so it can be stored in QVariant +Q_DECLARE_METATYPE(ServiceItemData) + class OBSBasicSettings : public QDialog { Q_OBJECT Q_PROPERTY(QIcon generalIcon READ GetGeneralIcon WRITE SetGeneralIcon DESIGNABLE true) @@ -87,7 +120,9 @@ class OBSBasicSettings : public QDialog { std::vector formats; - OBSPropertiesView *streamProperties = nullptr; + OBSPropertiesView *servicePropertiesView = nullptr; + QString servicePropertiesId; + OBSPropertiesView *streamEncoderProps = nullptr; OBSPropertiesView *recordEncoderProps = nullptr; @@ -204,8 +239,15 @@ class OBSBasicSettings : public QDialog { /* stream */ void InitStreamPage(); bool IsCustomService() const; + bool IsCustomServiceType() const; + QString GetCustomServiceTypeId() const; inline bool IsWHIP() const; + int FindService(const std::function &predicate); void LoadServices(bool showAll); + void LoadServicePropertiesView(); + void ClearServicePropertiesView(); + OBSData SeedServicePropertiesSettings(const char *serviceId) const; + void MergeServiceProperties(obs_data_t *settings); void OnOAuthStreamKeyConnected(); void OnAuthConnected(); QString lastService; diff --git a/frontend/settings/OBSBasicSettings_Stream.cpp b/frontend/settings/OBSBasicSettings_Stream.cpp index 1307e267ce9f95..0fe82033dc533b 100644 --- a/frontend/settings/OBSBasicSettings_Stream.cpp +++ b/frontend/settings/OBSBasicSettings_Stream.cpp @@ -9,10 +9,13 @@ #endif #include +#include #include #include +#include + static const QUuid &CustomServerUUID() { static const QUuid uuid = QUuid::fromString(QT_UTF8("{241da255-70f2-4bbb-bef7-509695bf8e65}")); @@ -26,12 +29,6 @@ extern QCef *cef; extern QCefCookieManager *panel_cookies; extern bool cef_js_avail; -enum class ListOpt : int { - ShowAll = 1, - Custom, - WHIP, -}; - enum class Section : int { Connect, StreamKey, @@ -39,12 +36,45 @@ enum class Section : int { bool OBSBasicSettings::IsCustomService() const { - return ui->service->currentData().toInt() == (int)ListOpt::Custom; + QVariant data = ui->service->currentData(); + ServiceItemData serviceData = data.value(); + return serviceData.isCustom(); +} + +bool OBSBasicSettings::IsCustomServiceType() const +{ + QVariant data = ui->service->currentData(); + ServiceItemData serviceData = data.value(); + return serviceData.isCustomServiceType(); +} + +QString OBSBasicSettings::GetCustomServiceTypeId() const +{ + QVariant data = ui->service->currentData(); + ServiceItemData serviceData = data.value(); + if (serviceData.isCustomServiceType()) { + return serviceData.serviceId; + } + return QString(); } inline bool OBSBasicSettings::IsWHIP() const { - return ui->service->currentData().toInt() == (int)ListOpt::WHIP; + return GetCustomServiceTypeId() == "whip_custom"; +} + +int OBSBasicSettings::FindService(const std::function &predicate) +{ + for (int i = 0; i < ui->service->count(); i++) { + QVariant data = ui->service->itemData(i); + if (data.canConvert()) { + ServiceItemData serviceData = data.value(); + if (predicate(serviceData)) { + return i; + } + } + } + return -1; } void OBSBasicSettings::InitStreamPage() @@ -59,6 +89,8 @@ void OBSBasicSettings::InitStreamPage() ui->connectedAccountLabel->setVisible(false); ui->connectedAccountText->setVisible(false); + ui->servicePropertiesGroupBox->setVisible(false); + int vertSpacing = ui->topStreamLayout->verticalSpacing(); QMargins m = ui->topStreamLayout->contentsMargins(); @@ -114,13 +146,17 @@ void OBSBasicSettings::LoadStream1Settings() protocol = QT_UTF8(obs_service_get_protocol(service_obj)); const char *bearer_token = obs_data_get_string(settings, "bearer_token"); - if (is_rtmp_custom || is_whip) { + if (!is_rtmp_common) { ui->customServer->setText(server); } if (is_rtmp_custom) { - ui->service->setCurrentIndex(0); - lastServiceIdx = 0; + auto idx = FindService([](const ServiceItemData &serviceData) { return serviceData.isCustom(); }); + if (idx == -1) { + idx = 0; + } + ui->service->setCurrentIndex(idx); + lastServiceIdx = idx; lastCustomServer = ui->customServer->text(); bool use_auth = obs_data_get_bool(settings, "use_auth"); @@ -129,11 +165,15 @@ void OBSBasicSettings::LoadStream1Settings() ui->authUsername->setText(QT_UTF8(username)); ui->authPw->setText(QT_UTF8(password)); ui->useAuth->setChecked(use_auth); - } else { - int idx = ui->service->findText(service); + } else if (is_rtmp_common) { + auto idx = FindService([&](const ServiceItemData &serviceData) { + return serviceData.isRtmpCommon() && serviceData.serviceId == service; + }); + if (idx == -1) { if (service && *service) { - ui->service->insertItem(1, service); + ServiceItemData newService(ServiceItemData::Type::RtmpCommon, service, service); + ui->service->insertItem(1, service, QVariant::fromValue(newService)); } idx = 1; } @@ -145,6 +185,24 @@ void OBSBasicSettings::LoadStream1Settings() idx = config_get_int(main->Config(), "Twitch", "AddonChoice"); ui->twitchAddonDropdown->setCurrentIndex(idx); + } else { + auto idx = FindService([&](const ServiceItemData &serviceData) { + return serviceData.isCustomServiceType() && serviceData.serviceId == type; + }); + + if (idx == -1) { + const char *display_name = obs_service_get_display_name(type); + if (!display_name) { + display_name = type; + } + ServiceItemData newService(ServiceItemData::Type::CustomServiceType, QString(type), + QT_UTF8(display_name)); + ui->service->insertItem(1, QT_UTF8(display_name), QVariant::fromValue(newService)); + idx = 1; + } + + ui->service->setCurrentIndex(idx); + lastServiceIdx = idx; } ui->enableMultitrackVideo->setChecked(config_get_bool(main->Config(), "Stream1", "EnableMultitrackVideo")); @@ -227,6 +285,8 @@ void OBSBasicSettings::LoadStream1Settings() ServiceChanged(true); + LoadServicePropertiesView(); + UpdateKeyLink(); UpdateMoreInfoLink(); UpdateVodTrackSetting(); @@ -267,13 +327,15 @@ void OBSBasicSettings::SwapMultiTrack(const char *protocol) void OBSBasicSettings::SaveStream1Settings() { bool customServer = IsCustomService(); - bool whip = IsWHIP(); + QString customServiceTypeId = GetCustomServiceTypeId(); const char *service_id = "rtmp_common"; + QByteArray serviceIdUtf8; if (customServer) { service_id = "rtmp_custom"; - } else if (whip) { - service_id = "whip_custom"; + } else if (!customServiceTypeId.isEmpty()) { + serviceIdUtf8 = customServiceTypeId.toUtf8(); + service_id = serviceIdUtf8.constData(); } obs_service_t *oldService = main->GetService(); @@ -281,7 +343,9 @@ void OBSBasicSettings::SaveStream1Settings() OBSDataAutoRelease settings = obs_data_create(); - if (!customServer && !whip) { + MergeServiceProperties(settings); + + if (!customServer && customServiceTypeId.isEmpty()) { obs_data_set_string(settings, "service", QT_TO_UTF8(ui->service->currentText())); obs_data_set_string(settings, "protocol", QT_TO_UTF8(protocol)); if (ui->server->currentData() == CustomServerUUID()) { @@ -316,8 +380,7 @@ void OBSBasicSettings::SaveStream1Settings() obs_data_set_bool(settings, "bwtest", false); } - if (whip) { - obs_data_set_string(settings, "service", "WHIP"); + if (IsWHIP()) { obs_data_set_string(settings, "bearer_token", QT_TO_UTF8(ui->key->text())); } else { obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text())); @@ -382,7 +445,7 @@ void OBSBasicSettings::SaveStream1Settings() void OBSBasicSettings::UpdateMoreInfoLink() { - if (IsCustomService() || IsWHIP()) { + if (IsCustomService() || IsCustomServiceType()) { ui->moreInfoButton->hide(); return; } @@ -487,20 +550,49 @@ void OBSBasicSettings::LoadServices(bool showAll) names.sort(Qt::CaseInsensitive); } + // Add rtmp_common services with ServiceItemData for (QString &name : names) { - ui->service->addItem(name); + ServiceItemData data(ServiceItemData::Type::RtmpCommon, name, name); + ui->service->addItem(name, QVariant::fromValue(data)); } - if (obs_is_output_protocol_registered("WHIP")) { - ui->service->addItem(QTStr("WHIP"), QVariant((int)ListOpt::WHIP)); + // Enumerate all available service types loaded in OBS + size_t idx = 0; + const char *service_id; + while (obs_enum_service_types(idx++, &service_id)) { + // Skip rtmp_common and rtmp_custom as they're handled separately + if (strcmp(service_id, "rtmp_common") == 0 || strcmp(service_id, "rtmp_custom") == 0) { + continue; + } + + // Get the display name for the service + const char *display_name = obs_service_get_display_name(service_id); + if (!display_name) { + display_name = service_id; + } + + // Check if the service has a registered protocol + OBSServiceAutoRelease temp_service = obs_service_create(service_id, "temp", nullptr, nullptr); + if (temp_service) { + const char *protocol = obs_service_get_protocol(temp_service); + if (protocol && obs_is_output_protocol_registered(protocol)) { + // Add the service to the dropdown with ServiceItemData + ServiceItemData data(ServiceItemData::Type::CustomServiceType, QString(service_id), + QT_UTF8(display_name)); + ui->service->addItem(QT_UTF8(display_name), QVariant::fromValue(data)); + } + } } if (!showAll) { + ServiceItemData showAllData(ServiceItemData::Type::ShowAll); ui->service->addItem(QTStr("Basic.AutoConfig.StreamPage.Service.ShowAll"), - QVariant((int)ListOpt::ShowAll)); + QVariant::fromValue(showAllData)); } - ui->service->insertItem(0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"), QVariant((int)ListOpt::Custom)); + ServiceItemData customData(ServiceItemData::Type::Custom); + ui->service->insertItem(0, QTStr("Basic.AutoConfig.StreamPage.Service.Custom"), + QVariant::fromValue(customData)); if (!lastService.isEmpty()) { int idx = ui->service->findText(lastService); @@ -512,6 +604,112 @@ void OBSBasicSettings::LoadServices(bool showAll) ui->service->blockSignals(false); } +namespace { + +// Mandatory keys, cannot be re-declared or overwritten by custom service props +constexpr std::array kFrontendOwnedServiceKeys{"server", "key", "bearer_token", + "use_auth", "username", "password", + "service", "protocol", "using_custom_server", + "bwtest"}; + +obs_properties_t *createFilteredServiceProperties(const char *serviceId) +{ + obs_properties_t *props = obs_get_service_properties(serviceId); + + for (const char *name : kFrontendOwnedServiceKeys) { + obs_properties_remove_by_name(props, name); + } + + return props; +} + +// Reload callback for OBSPropertiesView, which passes the service type id back as its opaque object +obs_properties_t *getServiceCustomProperties(void *typeData) +{ + return createFilteredServiceProperties(static_cast(typeData)); +} + +bool serviceHasCustomProperties(const char *serviceId) +{ + OBSProperties props = createFilteredServiceProperties(serviceId); + return obs_properties_first(props) != nullptr; +} + +} // namespace + +void OBSBasicSettings::LoadServicePropertiesView() +{ + ClearServicePropertiesView(); + + if (!IsCustomServiceType()) { + return; + } + + const QString serviceId = GetCustomServiceTypeId(); + const QByteArray serviceIdUtf8 = serviceId.toUtf8(); + + if (!serviceHasCustomProperties(serviceIdUtf8.constData())) { + return; + } + + OBSData settings = SeedServicePropertiesSettings(serviceIdUtf8.constData()); + + servicePropertiesView = + new OBSPropertiesView(settings, serviceIdUtf8.constData(), getServiceCustomProperties, 170); + + servicePropertiesView->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); + servicePropertiesView->setScrolling(false); + servicePropertiesId = serviceId; + + connect(servicePropertiesView, &OBSPropertiesView::Changed, this, &OBSBasicSettings::Stream1Changed); + + ui->servicePropertiesLayout->addWidget(servicePropertiesView); + ui->servicePropertiesGroupBox->setVisible(true); +} + +void OBSBasicSettings::ClearServicePropertiesView() +{ + delete servicePropertiesView; + servicePropertiesView = nullptr; + servicePropertiesId.clear(); + ui->servicePropertiesGroupBox->setVisible(false); +} + +OBSData OBSBasicSettings::SeedServicePropertiesSettings(const char *serviceId) const +{ + OBSDataAutoRelease settings = obs_service_defaults(serviceId); + if (!settings) { + settings = obs_data_create(); + } + + obs_service_t *activeService = main->GetService(); + const char *activeServiceId = activeService ? obs_service_get_type(activeService) : nullptr; + if (activeServiceId && strcmp(serviceId, activeServiceId) == 0) { + OBSDataAutoRelease activeSettings = obs_service_get_settings(activeService); + obs_data_apply(settings, activeSettings); + } + + for (const char *name : kFrontendOwnedServiceKeys) { + obs_data_erase(settings, name); + } + + return settings.Get(); +} + +void OBSBasicSettings::MergeServiceProperties(obs_data_t *settings) +{ + if (!servicePropertiesView || servicePropertiesId.isEmpty() || + servicePropertiesId != GetCustomServiceTypeId()) { + return; + } + + obs_data_t *props = servicePropertiesView->GetSettings(); + + OBSDataAutoRelease defaults = obs_data_get_defaults(props); + obs_data_apply(settings, defaults); + obs_data_apply(settings, props); +} + static inline bool is_auth_service(const std::string &service) { return Auth::AuthType(service) != Auth::Type::None; @@ -576,7 +774,10 @@ void OBSBasicSettings::UseStreamKeyAdvClicked() void OBSBasicSettings::on_service_currentIndexChanged(int idx) { - if (ui->service->currentData().toInt() == (int)ListOpt::ShowAll) { + // Check if "Show All" was selected + QVariant data = ui->service->currentData(); + ServiceItemData serviceData = data.value(); + if (serviceData.isShowAll()) { LoadServices(true); ui->service->showPopup(); return; @@ -584,6 +785,8 @@ void OBSBasicSettings::on_service_currentIndexChanged(int idx) ServiceChanged(); + LoadServicePropertiesView(); + UpdateMoreInfoLink(); UpdateServerList(); UpdateKeyLink(); @@ -634,7 +837,7 @@ void OBSBasicSettings::ServiceChanged(bool resetFields) { std::string service = ui->service->currentText().toStdString(); bool custom = IsCustomService(); - bool whip = IsWHIP(); + bool customServiceType = IsCustomServiceType(); ui->disconnectAccount->setVisible(false); ui->bandwidthTestEnable->setVisible(false); @@ -655,7 +858,7 @@ void OBSBasicSettings::ServiceChanged(bool resetFields) ui->authPwLabel->setVisible(custom); ui->authPwWidget->setVisible(custom); - if (custom || whip) { + if (custom || customServiceType) { ui->destinationLayout->insertRow(1, ui->serverLabel, ui->serverStackedWidget); ui->serverStackedWidget->setCurrentIndex(1); @@ -778,25 +981,29 @@ void OBSBasicSettings::on_authPwShow_clicked() OBSService OBSBasicSettings::SpawnTempService() { bool custom = IsCustomService(); - bool whip = IsWHIP(); + QString customServiceTypeId = GetCustomServiceTypeId(); const char *service_id = "rtmp_common"; + QByteArray serviceIdUtf8; if (custom) { service_id = "rtmp_custom"; - } else if (whip) { - service_id = "whip_custom"; + } else if (!customServiceTypeId.isEmpty()) { + serviceIdUtf8 = customServiceTypeId.toUtf8(); + service_id = serviceIdUtf8.constData(); } OBSDataAutoRelease settings = obs_data_create(); - if (!custom && !whip) { + MergeServiceProperties(settings); + + if (!custom && customServiceTypeId.isEmpty()) { obs_data_set_string(settings, "service", QT_TO_UTF8(ui->service->currentText())); obs_data_set_string(settings, "server", QT_TO_UTF8(ui->server->currentData().toString())); } else { obs_data_set_string(settings, "server", QT_TO_UTF8(ui->customServer->text().trimmed())); } - if (whip) { + if (IsWHIP()) { obs_data_set_string(settings, "bearer_token", QT_TO_UTF8(ui->key->text())); } else { obs_data_set_string(settings, "key", QT_TO_UTF8(ui->key->text())); diff --git a/libobs/obs-output.c b/libobs/obs-output.c index 3e915917a77ecc..a7cb006bafcf5f 100644 --- a/libobs/obs-output.c +++ b/libobs/obs-output.c @@ -97,6 +97,11 @@ static inline bool flag_audio(const struct obs_output *output) return (output->info.flags & OBS_OUTPUT_AUDIO) != 0; } +static inline bool flag_no_interleave(const struct obs_output *output) +{ + return (output->info.flags & OBS_OUTPUT_NO_INTERLEAVE) != 0; +} + static inline bool log_flag_audio(const struct obs_output *output, const char *func_name) { bool ret = flag_audio(output); @@ -2326,6 +2331,15 @@ static void default_encoded_callback(void *param, struct encoder_packet *packet, obs_encoder_packet_release(packet); } +static inline encoded_callback_t choose_encoded_callback(const struct obs_output *output, bool has_video, + bool has_audio) +{ + if (!has_video || !has_audio || flag_no_interleave(output)) + return default_encoded_callback; + + return interleave_packets; +} + static void default_raw_video_callback(void *param, struct video_data *frame) { struct obs_output *output = param; @@ -2512,7 +2526,7 @@ static void hook_data_capture(struct obs_output *output) reset_packet_data(output); pthread_mutex_unlock(&output->interleaved_mutex); - encoded_callback = (has_video && has_audio) ? interleave_packets : default_encoded_callback; + encoded_callback = choose_encoded_callback(output, has_video, has_audio); if (output->delay_sec) { output->active_delay_ns = (uint64_t)output->delay_sec * 1000000000ULL; @@ -2865,7 +2879,7 @@ static void *end_data_capture_thread(void *data) if (output->active_delay_ns) encoded_callback = process_delay; else - encoded_callback = (has_video && has_audio) ? interleave_packets : default_encoded_callback; + encoded_callback = choose_encoded_callback(output, has_video, has_audio); if (has_video) stop_video_encoders(output, encoded_callback); diff --git a/libobs/obs-output.h b/libobs/obs-output.h index d765e35e9e0b6d..2eacc953df9ca7 100644 --- a/libobs/obs-output.h +++ b/libobs/obs-output.h @@ -32,6 +32,7 @@ extern "C" { #define OBS_OUTPUT_MULTI_TRACK_AUDIO OBS_OUTPUT_MULTI_TRACK #define OBS_OUTPUT_MULTI_TRACK_VIDEO (1 << 6) #define OBS_OUTPUT_MULTI_TRACK_AV (OBS_OUTPUT_MULTI_TRACK_AUDIO | OBS_OUTPUT_MULTI_TRACK_VIDEO) +#define OBS_OUTPUT_NO_INTERLEAVE (1 << 7) #define MAX_OUTPUT_AUDIO_ENCODERS 6 #define MAX_OUTPUT_VIDEO_ENCODERS 10 diff --git a/plugins/mac-videotoolbox/encoder.c b/plugins/mac-videotoolbox/encoder.c index ca084ac6e568dc..6f796b7cd3a5db 100644 --- a/plugins/mac-videotoolbox/encoder.c +++ b/plugins/mac-videotoolbox/encoder.c @@ -64,6 +64,7 @@ struct vt_encoder { CMVideoCodecType codec_type; bool bframes; bool spatial_aq; + bool low_latency; int vt_pix_fmt; enum video_colorspace colorspace; @@ -273,7 +274,8 @@ static OSStatus session_set_prop(VTCompressionSessionRef session, CFStringRef ke } static OSStatus session_set_bitrate(VTCompressionSessionRef session, const char *rate_control, int new_bitrate, - float quality, bool limit_bitrate, int max_bitrate, double max_bitrate_window) + float quality, bool limit_bitrate, int max_bitrate, double max_bitrate_window, + bool low_latency) { OSStatus code; @@ -284,7 +286,11 @@ static OSStatus session_set_bitrate(VTCompressionSessionRef session, const char compressionPropertyKey = kVTCompressionPropertyKey_AverageBitRate; can_limit_bitrate = true; - if (__builtin_available(macOS 13.0, *)) { + if (low_latency) { + VT_LOG(LOG_WARNING, + "CBR is not supported with low latency rate control. Will use ABR instead."); + can_limit_bitrate = false; + } else if (__builtin_available(macOS 13.0, *)) { if (is_apple_silicon) { compressionPropertyKey = kVTCompressionPropertyKey_ConstantBitRate; can_limit_bitrate = false; @@ -422,15 +428,39 @@ void sample_encoded_callback(void *data, void *source, OSStatus status, VTEncode CFRelease(pixbuf); } -static inline CFDictionaryRef create_encoder_spec(const char *vt_encoder_id) +static char *copy_low_latency_encoder_id(struct vt_encoder *enc) { - CFStringRef id = CFStringCreateWithFileSystemRepresentation(NULL, vt_encoder_id); + CFTypeRef keys[1] = {kVTVideoEncoderSpecification_EnableLowLatencyRateControl}; + CFTypeRef values[1] = {kCFBooleanTrue}; + CFDictionaryRef spec = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); - CFTypeRef keys[1] = {kVTVideoEncoderSpecification_EncoderID}; - CFTypeRef values[1] = {id}; + CFStringRef id = NULL; + OSStatus code = VTCopySupportedPropertyDictionaryForEncoder((int32_t)enc->width, (int32_t)enc->height, + enc->codec_type, spec, &id, NULL); + CFRelease(spec); - CFDictionaryRef encoder_spec = CFDictionaryCreate( - kCFAllocatorDefault, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + if (code != noErr || id == NULL) + return NULL; + + char *str = cfstr_copy_cstr(id, kCFStringEncodingUTF8); + CFRelease(id); + + return str; +} + +static inline CFDictionaryRef create_encoder_spec(const char *vt_encoder_id, const char *low_latency_id) +{ + const bool low_latency = low_latency_id != NULL; + CFStringRef id = CFStringCreateWithFileSystemRepresentation(NULL, low_latency ? low_latency_id : vt_encoder_id); + + CFTypeRef keys[2] = {kVTVideoEncoderSpecification_EncoderID, + kVTVideoEncoderSpecification_EnableLowLatencyRateControl}; + CFTypeRef values[2] = {id, kCFBooleanTrue}; + + CFDictionaryRef encoder_spec = CFDictionaryCreate(kCFAllocatorDefault, keys, values, low_latency ? 2 : 1, + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks); CFRelease(id); @@ -494,12 +524,28 @@ static OSStatus create_encoder(struct vt_encoder *enc) const char *codec_name = obs_encoder_get_codec(enc->encoder); CFDictionaryRef encoder_spec; + char *low_latency_id = NULL; if (strcmp(codec_name, "prores") == 0) { struct vt_encoder_type_data *type_data = (struct vt_encoder_type_data *)obs_encoder_get_type_data(enc->encoder); encoder_spec = create_prores_encoder_spec(enc->codec_type, type_data->hardware_accelerated); } else { - encoder_spec = create_encoder_spec(enc->vt_encoder_id); + if (enc->low_latency) { + // Low latency rate control is only offered by dedicated encoders + // (e.g. "com.apple.videotoolbox.videoencoder.h264.rtvc"), so the + // selected encoder is swapped for the low latency one of the same codec. + low_latency_id = copy_low_latency_encoder_id(enc); + if (low_latency_id != NULL) { + VT_BLOG(LOG_INFO, "low latency: encoding with '%s' in place of '%s'", low_latency_id, + enc->vt_encoder_id); + } else { + VT_BLOG(LOG_WARNING, "no low latency encoder for %.4s at %ux%u, encoding normally", + codec_type_to_print_fmt(enc->codec_type), enc->width, enc->height); + enc->low_latency = false; + } + } + + encoder_spec = create_encoder_spec(enc->vt_encoder_id, low_latency_id); } CFDictionaryRef pixbuf_spec = create_pixbuf_spec(enc); @@ -513,6 +559,7 @@ static OSStatus create_encoder(struct vt_encoder *enc) CFRelease(encoder_spec); CFRelease(pixbuf_spec); + bfree(low_latency_id); CFBooleanRef b = NULL; code = VTSessionCopyProperty(s, kVTCompressionPropertyKey_UsingHardwareAcceleratedVideoEncoder, NULL, &b); @@ -572,28 +619,36 @@ static OSStatus create_encoder(struct vt_encoder *enc) } code = session_set_bitrate(s, enc->rate_control, enc->bitrate, enc->quality, enc->limit_bitrate, - enc->rc_max_bitrate, enc->rc_max_bitrate_window); + enc->rc_max_bitrate, enc->rc_max_bitrate_window, enc->low_latency); if (code != noErr) { return code; } - if (__builtin_available(macOS 15.0, *)) { - int spatial_aq = enc->spatial_aq ? kVTQPModulationLevel_Default : kVTQPModulationLevel_Disable; - CFNumberRef spatialAQ = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &spatial_aq); + // The low latency encoder rejects Spatial AQ with kVTPropertyNotSupportedErr, so skip it + if (!enc->low_latency) { + if (__builtin_available(macOS 15.0, *)) { + int spatial_aq = enc->spatial_aq ? kVTQPModulationLevel_Default + : kVTQPModulationLevel_Disable; + CFNumberRef spatialAQ = + CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &spatial_aq); - code = VTSessionSetProperty(s, kVTCompressionPropertyKey_SpatialAdaptiveQPLevel, spatialAQ); + code = VTSessionSetProperty(s, kVTCompressionPropertyKey_SpatialAdaptiveQPLevel, + spatialAQ); - if (code != noErr) { - log_osstatus(LOG_WARNING, enc, - "setting kVTCompressionPropertyKey_SpatialAdaptiveQPLevel failed", code); - } + if (code != noErr) { + log_osstatus(LOG_WARNING, enc, + "setting kVTCompressionPropertyKey_SpatialAdaptiveQPLevel failed", + code); + } - CFRelease(spatialAQ); + CFRelease(spatialAQ); + } } } // This can fail depending on hardware configuration - code = session_set_prop(s, kVTCompressionPropertyKey_RealTime, kCFBooleanFalse); + code = session_set_prop(s, kVTCompressionPropertyKey_RealTime, + enc->low_latency ? kCFBooleanTrue : kCFBooleanFalse); if (code != noErr) log_osstatus(LOG_WARNING, enc, "setting kVTCompressionPropertyKey_RealTime failed, " @@ -648,11 +703,13 @@ static void dump_encoder_info(struct vt_encoder *enc) "\trc_max_bitrate_window: %f (s)\n" "\thw_enc: %s\n" "\tspatial_aq: %s\n" + "\tlow_latency: %s\n" "\tprofile: %s\n" "\tcodec_type: %.4s\n", enc->vt_encoder_id, enc->rate_control, enc->bitrate, enc->quality, enc->fps_num, enc->fps_den, enc->width, enc->height, enc->keyint, enc->limit_bitrate ? "on" : "off", enc->rc_max_bitrate, enc->rc_max_bitrate_window, enc->hw_enc ? "on" : "off", enc->spatial_aq ? "on" : "off", + enc->low_latency ? "on" : "off", (enc->profile != NULL && !!strlen(enc->profile)) ? enc->profile : "default", codec_type_to_print_fmt(enc->codec_type)); } @@ -772,7 +829,8 @@ static bool vt_update(void *data, obs_data_t *settings) return true; OSStatus code = session_set_bitrate(enc->session, enc->rate_control, enc->bitrate, enc->quality, - enc->limit_bitrate, enc->rc_max_bitrate, enc->rc_max_bitrate_window); + enc->limit_bitrate, enc->rc_max_bitrate, enc->rc_max_bitrate_window, + enc->low_latency); if (code != noErr) VT_BLOG(LOG_WARNING, "Failed to set bitrate to session"); @@ -792,6 +850,21 @@ static void *vt_create(obs_data_t *settings, obs_encoder_t *encoder) if (!update_params(enc, settings)) goto fail; + // Low latency is part of the encoder specification and cannot change once + // the session exists, so it is decided here instead of in update_params(), + // which also runs on every vt_update(). + if (obs_data_get_bool(settings, "low_latency")) { + struct vt_encoder_type_data *type_data = + (struct vt_encoder_type_data *)obs_encoder_get_type_data(enc->encoder); + + if (type_data->hardware_accelerated && + (enc->codec_type == kCMVideoCodecType_H264 || enc->codec_type == kCMVideoCodecType_HEVC)) + enc->low_latency = true; + else + VT_BLOG(LOG_INFO, + "low latency was requested but this encoder does not support it, encoding normally"); + } + code = CMSimpleQueueCreate(NULL, 100, &enc->queue); if (code != noErr) { goto fail;