summarylogtreecommitdiffstats
path: root/bind_iface_eyeballed.patch
blob: 400d27ea2e7c5fdf62a33b13c74c560c3afe8a38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
From 0a1c4a3712d69e9aaa8d533b480b07abf3ec6d9e Mon Sep 17 00:00:00 2001
From: tytan652 <tytan652@tytanium.xyz>
Date: Thu, 14 Sep 2023 11:25:31 +0200
Subject: [PATCH 1/5] Revert "UI: Fix IP settings order"

---
 UI/forms/OBSBasicSettings.ui | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/UI/forms/OBSBasicSettings.ui b/UI/forms/OBSBasicSettings.ui
index c7c9119177d48..d3ffae94a210f 100644
--- a/UI/forms/OBSBasicSettings.ui
+++ b/UI/forms/OBSBasicSettings.ui
@@ -7638,30 +7638,30 @@
                     </widget>
                    </item>
                    <item row="1" column="0">
-                    <widget class="QLabel" name="ipFamilyLabel">
+                    <widget class="QLabel" name="bindToIPLabel">
                      <property name="text">
-                      <string>Basic.Settings.Advanced.Network.IPFamily</string>
+                      <string>Basic.Settings.Advanced.Network.BindToIP</string>
                      </property>
                      <property name="buddy">
-                      <cstring>ipFamily</cstring>
+                      <cstring>bindToIP</cstring>
                      </property>
                     </widget>
                    </item>
                    <item row="1" column="1">
-                    <widget class="QComboBox" name="ipFamily"/>
+                    <widget class="QComboBox" name="bindToIP"/>
                    </item>
                    <item row="2" column="0">
-                    <widget class="QLabel" name="bindToIPLabel">
+                    <widget class="QLabel" name="ipFamilyLabel">
                      <property name="text">
-                      <string>Basic.Settings.Advanced.Network.BindToIP</string>
+                      <string>Basic.Settings.Advanced.Network.IPFamily</string>
                      </property>
                      <property name="buddy">
-                      <cstring>bindToIP</cstring>
+                      <cstring>ipFamily</cstring>
                      </property>
                     </widget>
                    </item>
                    <item row="2" column="1">
-                    <widget class="QComboBox" name="bindToIP"/>
+                    <widget class="QComboBox" name="ipFamily"/>
                    </item>
                    <item row="4" column="1">
                     <widget class="QCheckBox" name="enableNewSocketLoop">

From b089076079ea0db434650dd7eae56caec7cba6ae Mon Sep 17 00:00:00 2001
From: tytan652 <tytan652@tytanium.xyz>
Date: Thu, 14 Sep 2023 11:25:50 +0200
Subject: [PATCH 2/5] Revert "UI: Add IPv4 / IPv6 selection setting"

This reverts commit 488a96bc4b0c27749b1ef143ed76a71c1830fe0a.
---
 UI/data/locale/en-US.ini             |  1 -
 UI/forms/OBSBasicSettings.ui         | 21 ++++-----------------
 UI/window-basic-auto-config-test.cpp |  4 ----
 UI/window-basic-main-outputs.cpp     |  6 ------
 UI/window-basic-main.cpp             |  2 --
 UI/window-basic-settings.cpp         | 19 +------------------
 6 files changed, 5 insertions(+), 48 deletions(-)

diff --git a/UI/data/locale/en-US.ini b/UI/data/locale/en-US.ini
index 9c3f387779133..a1967ea126bf0 100644
--- a/UI/data/locale/en-US.ini
+++ b/UI/data/locale/en-US.ini
@@ -1247,7 +1247,6 @@ Basic.Settings.Advanced.StreamDelay.MemoryUsage="Estimated Memory Usage: %1 MB"
 Basic.Settings.Advanced.Network="Network"
 Basic.Settings.Advanced.Network.Disabled="The currently selected streaming protocol does not support changing network settings."
 Basic.Settings.Advanced.Network.BindToIP="Bind to IP"
-Basic.Settings.Advanced.Network.IPFamily="IP Family"
 Basic.Settings.Advanced.Network.EnableNewSocketLoop="Enable network optimizations"
 Basic.Settings.Advanced.Network.EnableLowLatencyMode="Enable TCP pacing"
 Basic.Settings.Advanced.Network.TCPPacing.Tooltip="Attempts to make RTMP output friendlier to other latency sensitive applications on the network by regulating the rate of transmission.\nIt may increase the risk of dropped frames on unstable connections."
diff --git a/UI/forms/OBSBasicSettings.ui b/UI/forms/OBSBasicSettings.ui
index d3ffae94a210f..6f886811b2e6c 100644
--- a/UI/forms/OBSBasicSettings.ui
+++ b/UI/forms/OBSBasicSettings.ui
@@ -7650,27 +7650,14 @@
                    <item row="1" column="1">
                     <widget class="QComboBox" name="bindToIP"/>
                    </item>
-                   <item row="2" column="0">
-                    <widget class="QLabel" name="ipFamilyLabel">
-                     <property name="text">
-                      <string>Basic.Settings.Advanced.Network.IPFamily</string>
-                     </property>
-                     <property name="buddy">
-                      <cstring>ipFamily</cstring>
-                     </property>
-                    </widget>
-                   </item>
-                   <item row="2" column="1">
-                    <widget class="QComboBox" name="ipFamily"/>
-                   </item>
-                   <item row="4" column="1">
+                   <item row="3" column="1">
                     <widget class="QCheckBox" name="enableNewSocketLoop">
                      <property name="text">
                       <string>Basic.Settings.Advanced.Network.EnableNewSocketLoop</string>
                      </property>
                     </widget>
                    </item>
-                   <item row="5" column="1">
+                   <item row="4" column="1">
                     <widget class="QCheckBox" name="enableLowLatencyMode">
                      <property name="enabled">
                       <bool>false</bool>
@@ -7680,7 +7667,7 @@
                      </property>
                     </widget>
                    </item>
-                   <item row="4" column="0">
+                   <item row="3" column="0">
                     <spacer name="horizontalSpacer_7">
                      <property name="orientation">
                       <enum>Qt::Horizontal</enum>
@@ -7693,7 +7680,7 @@
                      </property>
                     </spacer>
                    </item>
-                   <item row="3" column="1">
+                   <item row="2" column="1">
                     <widget class="QCheckBox" name="dynBitrate">
                      <property name="toolTip">
                       <string>Basic.Settings.Output.DynamicBitrate.TT</string>
diff --git a/UI/window-basic-auto-config-test.cpp b/UI/window-basic-auto-config-test.cpp
index 7d7c4278ede57..45bf800b0483f 100644
--- a/UI/window-basic-auto-config-test.cpp
+++ b/UI/window-basic-auto-config-test.cpp
@@ -249,10 +249,6 @@ void AutoConfigTestPage::TestBandwidthThread()
 		config_get_string(main->Config(), "Output", "BindIP");
 	obs_data_set_string(output_settings, "bind_ip", bind_ip);
 
-	const char *ip_family =
-		config_get_string(main->Config(), "Output", "IPFamily");
-	obs_data_set_string(output_settings, "ip_family", ip_family);
-
 	/* -----------------------------------*/
 	/* determine which servers to test    */
 
diff --git a/UI/window-basic-main-outputs.cpp b/UI/window-basic-main-outputs.cpp
index e70a3d4adc105..b604aae7358ab 100644
--- a/UI/window-basic-main-outputs.cpp
+++ b/UI/window-basic-main-outputs.cpp
@@ -1208,8 +1208,6 @@ bool SimpleOutput::StartStreaming(obs_service_t *service)
 		config_get_bool(main->Config(), "Output", "DelayPreserve");
 	const char *bindIP =
 		config_get_string(main->Config(), "Output", "BindIP");
-	const char *ipFamily =
-		config_get_string(main->Config(), "Output", "IPFamily");
 #ifdef _WIN32
 	bool enableNewSocketLoop = config_get_bool(main->Config(), "Output",
 						   "NewSocketLoopEnable");
@@ -1221,7 +1219,6 @@ bool SimpleOutput::StartStreaming(obs_service_t *service)
 
 	OBSDataAutoRelease settings = obs_data_create();
 	obs_data_set_string(settings, "bind_ip", bindIP);
-	obs_data_set_string(settings, "ip_family", ipFamily);
 #ifdef _WIN32
 	obs_data_set_bool(settings, "new_socket_loop_enabled",
 			  enableNewSocketLoop);
@@ -2258,8 +2255,6 @@ bool AdvancedOutput::StartStreaming(obs_service_t *service)
 		config_get_bool(main->Config(), "Output", "DelayPreserve");
 	const char *bindIP =
 		config_get_string(main->Config(), "Output", "BindIP");
-	const char *ipFamily =
-		config_get_string(main->Config(), "Output", "IPFamily");
 #ifdef _WIN32
 	bool enableNewSocketLoop = config_get_bool(main->Config(), "Output",
 						   "NewSocketLoopEnable");
@@ -2280,7 +2275,6 @@ bool AdvancedOutput::StartStreaming(obs_service_t *service)
 
 	OBSDataAutoRelease settings = obs_data_create();
 	obs_data_set_string(settings, "bind_ip", bindIP);
-	obs_data_set_string(settings, "ip_family", ipFamily);
 #ifdef _WIN32
 	obs_data_set_bool(settings, "new_socket_loop_enabled",
 			  enableNewSocketLoop);
diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp
index bcebcf363fcb3..008e59ac9438f 100644
--- a/UI/window-basic-main.cpp
+++ b/UI/window-basic-main.cpp
@@ -1717,8 +1717,6 @@ bool OBSBasic::InitBasicConfigDefaults()
 	config_set_default_uint(basicConfig, "Output", "MaxRetries", 25);
 
 	config_set_default_string(basicConfig, "Output", "BindIP", "default");
-	config_set_default_string(basicConfig, "Output", "IPFamily",
-				  "IPv4+IPv6");
 	config_set_default_bool(basicConfig, "Output", "NewSocketLoopEnable",
 				false);
 	config_set_default_bool(basicConfig, "Output", "LowLatencyEnable",
diff --git a/UI/window-basic-settings.cpp b/UI/window-basic-settings.cpp
index 5aa1cf6583993..9314b2da4918f 100644
--- a/UI/window-basic-settings.cpp
+++ b/UI/window-basic-settings.cpp
@@ -568,7 +568,6 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	HookWidget(ui->processPriority,      COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->confirmOnExit,        CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->bindToIP,             COMBO_CHANGED,  ADV_CHANGED);
-	HookWidget(ui->ipFamily,             COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->enableNewSocketLoop,  CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->enableLowLatencyMode, CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->hotkeyFocusType,      COMBO_CHANGED,  ADV_CHANGED);
@@ -866,17 +865,6 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 		ui->bindToIP->addItem(QT_UTF8(name), val);
 	}
 
-	// Add IP Family options
-	p = obs_properties_get(ppts, "ip_family");
-
-	count = obs_property_list_item_count(p);
-	for (size_t i = 0; i < count; i++) {
-		const char *name = obs_property_list_item_name(p, i);
-		const char *val = obs_property_list_item_string(p, i);
-
-		ui->ipFamily->addItem(QT_UTF8(name), val);
-	}
-
 	obs_properties_destroy(ppts);
 
 	InitStreamPage();
@@ -2902,8 +2890,7 @@ void OBSBasicSettings::LoadAdvancedSettings()
 		App()->GlobalConfig(), "General", "HotkeyFocusType");
 	bool dynBitrate =
 		config_get_bool(main->Config(), "Output", "DynamicBitrate");
-	const char *ipFamily =
-		config_get_string(main->Config(), "Output", "IPFamily");
+
 	bool confirmOnExit =
 		config_get_bool(GetGlobalConfig(), "General", "ConfirmOnExit");
 
@@ -2943,7 +2930,6 @@ void OBSBasicSettings::LoadAdvancedSettings()
 	ui->sdrWhiteLevel->setValue(sdrWhiteLevel);
 	ui->hdrNominalPeakLevel->setValue(hdrNominalPeakLevel);
 
-	SetComboByValue(ui->ipFamily, ipFamily);
 	if (!SetComboByValue(ui->bindToIP, bindIP))
 		SetInvalidValue(ui->bindToIP, bindIP, bindIP);
 
@@ -3678,7 +3664,6 @@ void OBSBasicSettings::SaveAdvancedSettings()
 	SaveSpinBox(ui->reconnectRetryDelay, "Output", "RetryDelay");
 	SaveSpinBox(ui->reconnectMaxRetries, "Output", "MaxRetries");
 	SaveComboData(ui->bindToIP, "Output", "BindIP");
-	SaveComboData(ui->ipFamily, "Output", "IPFamily");
 	SaveCheckBox(ui->autoRemux, "Video", "AutoRemux");
 	SaveCheckBox(ui->dynBitrate, "Output", "DynamicBitrate");
 
@@ -6243,8 +6228,6 @@ void OBSBasicSettings::UpdateAdvNetworkGroup()
 	ui->bindToIPLabel->setVisible(enabled);
 	ui->bindToIP->setVisible(enabled);
 	ui->dynBitrate->setVisible(enabled);
-	ui->ipFamilyLabel->setVisible(enabled);
-	ui->ipFamily->setVisible(enabled);
 #ifdef _WIN32
 	ui->enableNewSocketLoop->setVisible(enabled);
 	ui->enableLowLatencyMode->setVisible(enabled);

From d0880abc09b9dded5195de613e2522a8daf25ba6 Mon Sep 17 00:00:00 2001
From: tytan652 <tytan652@tytanium.xyz>
Date: Thu, 11 Feb 2021 14:29:08 +0100
Subject: [PATCH 3/5] happy-eyeballs,librtmp: Add interface binding for Linux

---
 deps/happy-eyeballs/happy-eyeballs.c | 39 ++++++++++++++++++++++++++--
 deps/happy-eyeballs/happy-eyeballs.h | 10 +++++++
 plugins/obs-outputs/librtmp/rtmp.c   |  3 +++
 plugins/obs-outputs/librtmp/rtmp.h   |  1 +
 4 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/deps/happy-eyeballs/happy-eyeballs.c b/deps/happy-eyeballs/happy-eyeballs.c
index 0ed15376b0b69..3a3828743ddca 100644
--- a/deps/happy-eyeballs/happy-eyeballs.c
+++ b/deps/happy-eyeballs/happy-eyeballs.c
@@ -101,12 +101,22 @@ struct happy_eyeballs_ctx {
 	const char *error_message;
 
 	/**
-	 * Set along with bind_addr to hint which interface to use.
+	 * Set along with bind_iface to hint which interface to use.
+	 */
+	int bind_iface_len;
+
+	/**
+	 * Set along with bind_iface_len to hint which interface to use.
+	 */
+	char *bind_iface;
+
+	/**
+	 * Set along with bind_addr to hint which address to use.
 	 */
 	socklen_t bind_addr_len;
 
 	/**
-	 * Set along with bind_addr_len to hint which interface to use.
+	 * Set along with bind_addr_len to hint which address to use.
 	 */
 	struct sockaddr_storage bind_addr;
 
@@ -349,6 +359,14 @@ static void *happy_connect_worker(void *arg)
 	setsockopt(args->sockfd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1},
 		   sizeof(int));
 #endif
+
+#ifdef __linux__
+	if (setsockopt(args->sockfd, SOL_SOCKET, SO_BINDTODEVICE,
+		       context->bind_iface, context->bind_iface_len) < 0) {
+		goto failure;
+	}
+#endif
+
 	if (context->bind_addr.ss_family != 0 &&
 	    bind(args->sockfd, (const struct sockaddr *)&context->bind_addr,
 		 context->bind_addr_len) < 0) {
@@ -696,6 +714,7 @@ int happy_eyeballs_destroy(struct happy_eyeballs_ctx *context)
 		freeaddrinfo(context->addresses);
 
 	da_free(context->candidates);
+	bfree(context->bind_iface);
 	free(context);
 	return STATUS_SUCCESS;
 }
@@ -703,6 +722,22 @@ int happy_eyeballs_destroy(struct happy_eyeballs_ctx *context)
 /* ------------------------------------------------------------------------- */
 /* Setters & Getters                                                         */
 
+int happy_eyeballs_set_bind_iface(struct happy_eyeballs_ctx *context,
+				  int iface_len, const char *iface)
+{
+	if (!context)
+		return STATUS_INVALID_ARGUMENT;
+
+	if (iface && iface_len > 0) {
+		context->bind_iface = bstrdup(iface);
+		context->bind_iface_len = iface_len;
+	} else {
+		context->bind_iface_len = 0;
+		context->bind_iface = NULL;
+	}
+	return STATUS_SUCCESS;
+}
+
 int happy_eyeballs_set_bind_addr(struct happy_eyeballs_ctx *context,
 				 socklen_t addr_len,
 				 struct sockaddr_storage *addr_storage)
diff --git a/deps/happy-eyeballs/happy-eyeballs.h b/deps/happy-eyeballs/happy-eyeballs.h
index 3ef62e2de8e9e..b939868dba72e 100644
--- a/deps/happy-eyeballs/happy-eyeballs.h
+++ b/deps/happy-eyeballs/happy-eyeballs.h
@@ -82,6 +82,16 @@ int happy_eyeballs_create(struct happy_eyeballs_ctx **context);
 int happy_eyeballs_connect(struct happy_eyeballs_ctx *context,
 			   const char *hostname, int port);
 
+/**
+ * Optionally set the interface. You may pass 0 and NULL for these
+ * parameters, respectively, to clear this setting. This must be called before
+ * happy_eyeballs_connect.
+ *
+ * Returns 0 on success or -EINVAL if context is not set.
+ */
+int happy_eyeballs_set_bind_iface(struct happy_eyeballs_ctx *context,
+				  int iface_len, const char *iface);
+
 /**
  * Optionally set the interface address. You may pass 0 and NULL for these
  * parameters, respectively, to clear this setting. This must be called before
diff --git a/plugins/obs-outputs/librtmp/rtmp.c b/plugins/obs-outputs/librtmp/rtmp.c
index 014a594d7fd94..2cf97d7845e9a 100644
--- a/plugins/obs-outputs/librtmp/rtmp.c
+++ b/plugins/obs-outputs/librtmp/rtmp.c
@@ -1063,6 +1063,9 @@ RTMP_Connect(RTMP *r, RTMPPacket *cp)
         port = r->Link.port;
     }
 
+    /* Set local bind interface (if present) */
+    happy_eyeballs_set_bind_iface(happy_ctx, r->m_bindInterface.av_len, r->m_bindInterface.av_val);
+
     /* Set local bind address (if present) */
     happy_eyeballs_set_bind_addr(happy_ctx, r->m_bindIP.addrLen, &r->m_bindIP.addr);
 
diff --git a/plugins/obs-outputs/librtmp/rtmp.h b/plugins/obs-outputs/librtmp/rtmp.h
index d1cddd412c3c2..a1fd605504d3f 100644
--- a/plugins/obs-outputs/librtmp/rtmp.h
+++ b/plugins/obs-outputs/librtmp/rtmp.h
@@ -421,6 +421,7 @@ extern "C"
         void*   m_customSendParam;
         CUSTOMSEND m_customSendFunc;
 
+        AVal m_bindInterface;
         RTMP_BINDINFO m_bindIP;
 
         uint8_t m_bSendChunkSizeInfo;

From 2d7f060b9181d38edddd3441489e1d7e3867d98d Mon Sep 17 00:00:00 2001
From: tytan652 <tytan652@tytanium.xyz>
Date: Thu, 11 Feb 2021 14:47:45 +0100
Subject: [PATCH 4/5] obs-outputs: Add RTMP iface binding for Linux

---
 plugins/obs-outputs/net-if.c      | 32 +++++++++++++++++++++++++++++
 plugins/obs-outputs/net-if.h      | 15 ++++++++++++++
 plugins/obs-outputs/rtmp-stream.c | 34 +++++++++++++++++++++++++++++++
 plugins/obs-outputs/rtmp-stream.h |  2 ++
 4 files changed, 83 insertions(+)

diff --git a/plugins/obs-outputs/net-if.c b/plugins/obs-outputs/net-if.c
index 6ffb2679a9a87..ae89ffed46ef1 100644
--- a/plugins/obs-outputs/net-if.c
+++ b/plugins/obs-outputs/net-if.c
@@ -154,6 +154,38 @@ static inline bool is_loopback(struct ifaddrs *ifa)
 	return n && (strcmp(n, "lo") == 0 || strcmp(n, "lo0") == 0);
 }
 
+#ifdef __linux__
+void netif_get_ifaces(struct netif_siface_data *ifaces)
+{
+	da_init(ifaces->ifaces);
+
+	struct ifaddrs *ifaddr, *ifa;
+	unsigned int family;
+
+	if (getifaddrs(&ifaddr) == -1) {
+		warn("getifaddrs() failed");
+		return;
+	}
+
+	for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
+		if (ifa->ifa_addr == NULL || is_loopback(ifa))
+			continue;
+		family = ifa->ifa_addr->sa_family;
+
+		if ((family == AF_INET) || (family == AF_INET6)) {
+			char *item;
+			char *iface_dup = bstrdup(ifa->ifa_name);
+
+			item = iface_dup;
+
+			da_push_back(ifaces->ifaces, &item);
+		}
+	}
+
+	freeifaddrs(ifaddr);
+}
+#endif
+
 static inline void netif_get_addrs_nix(struct netif_saddr_data *ifaddrs)
 {
 	struct ifaddrs *ifaddr, *ifa;
diff --git a/plugins/obs-outputs/net-if.h b/plugins/obs-outputs/net-if.h
index fe30eb7f23b56..68a334d4e31e9 100644
--- a/plugins/obs-outputs/net-if.h
+++ b/plugins/obs-outputs/net-if.h
@@ -53,6 +53,21 @@
 
 #endif
 
+#ifdef __linux__
+struct netif_siface_data {
+	DARRAY(char *) ifaces;
+};
+
+static inline void netif_siface_data_free(struct netif_siface_data *data)
+{
+	for (size_t i = 0; i < data->ifaces.num; i++)
+		bfree(data->ifaces.array[i]);
+	da_free(data->ifaces);
+}
+
+extern void netif_get_ifaces(struct netif_siface_data *ifaces);
+#endif
+
 struct netif_saddr_item {
 	char *name;
 	char *addr;
diff --git a/plugins/obs-outputs/rtmp-stream.c b/plugins/obs-outputs/rtmp-stream.c
index ac50535db4be8..24084b07f79ba 100644
--- a/plugins/obs-outputs/rtmp-stream.c
+++ b/plugins/obs-outputs/rtmp-stream.c
@@ -126,6 +126,7 @@ static void rtmp_stream_destroy(void *data)
 	dstr_free(&stream->username);
 	dstr_free(&stream->password);
 	dstr_free(&stream->encoder_name);
+	dstr_free(&stream->bind_interface);
 	dstr_free(&stream->bind_ip);
 	os_event_destroy(stream->stop_event);
 	os_sem_destroy(stream->send_sem);
@@ -1185,6 +1186,17 @@ static int try_connect(struct rtmp_stream *stream)
 	set_rtmp_dstr(&stream->rtmp.Link.flashVer, &stream->encoder_name);
 	stream->rtmp.Link.swfUrl = stream->rtmp.Link.tcUrl;
 
+	if (dstr_is_empty(&stream->bind_interface) ||
+	    dstr_cmp(&stream->bind_interface, "default") == 0) {
+		memset(&stream->rtmp.m_bindInterface, 0,
+		       sizeof(stream->rtmp.m_bindInterface));
+	} else {
+		set_rtmp_dstr(&stream->rtmp.m_bindInterface,
+			      &stream->bind_interface);
+		info("Binding to interface %s",
+		     stream->rtmp.m_bindInterface.av_val);
+	}
+
 	if (dstr_is_empty(&stream->bind_ip) ||
 	    dstr_cmp(&stream->bind_ip, "default") == 0) {
 		memset(&stream->rtmp.m_bindIP, 0,
@@ -1235,6 +1247,7 @@ static bool init_connect(struct rtmp_stream *stream)
 {
 	obs_service_t *service;
 	obs_data_t *settings;
+	const char *bind_interface;
 	const char *bind_ip;
 	const char *ip_family;
 	int64_t drop_p;
@@ -1320,6 +1333,9 @@ static bool init_connect(struct rtmp_stream *stream)
 	stream->drop_threshold_usec = 1000 * drop_b;
 	stream->pframe_drop_threshold_usec = 1000 * drop_p;
 
+	bind_interface = obs_data_get_string(settings, OPT_BIND_INTERFACE);
+	dstr_copy(&stream->bind_interface, bind_interface);
+
 	bind_ip = obs_data_get_string(settings, OPT_BIND_IP);
 	dstr_copy(&stream->bind_ip, bind_ip);
 
@@ -1741,6 +1757,9 @@ static obs_properties_t *rtmp_stream_properties(void *unused)
 	UNUSED_PARAMETER(unused);
 
 	obs_properties_t *props = obs_properties_create();
+#ifdef __linux__
+	struct netif_siface_data ifaces = {0};
+#endif
 	struct netif_saddr_data addrs = {0};
 	obs_property_t *p;
 
@@ -1748,6 +1767,21 @@ static obs_properties_t *rtmp_stream_properties(void *unused)
 				   obs_module_text("RTMPStream.DropThreshold"),
 				   200, 10000, 100);
 	obs_property_int_set_suffix(p, " ms");
+#ifdef __linux__
+	p = obs_properties_add_list(props, OPT_BIND_INTERFACE,
+				    obs_module_text("RTMPStream.BindInterface"),
+				    OBS_COMBO_TYPE_LIST,
+				    OBS_COMBO_FORMAT_STRING);
+
+	obs_property_list_add_string(p, obs_module_text("Default"), "default");
+
+	netif_get_ifaces(&ifaces);
+	for (size_t i = 0; i < ifaces.ifaces.num; i++) {
+		char *item = ifaces.ifaces.array[i];
+		obs_property_list_add_string(p, item, item);
+	}
+	netif_siface_data_free(&ifaces);
+#endif
 
 	p = obs_properties_add_list(props, OPT_IP_FAMILY,
 				    obs_module_text("IPFamily"),
diff --git a/plugins/obs-outputs/rtmp-stream.h b/plugins/obs-outputs/rtmp-stream.h
index 29912e5014cc0..2b7ddb712032e 100644
--- a/plugins/obs-outputs/rtmp-stream.h
+++ b/plugins/obs-outputs/rtmp-stream.h
@@ -27,6 +27,7 @@
 #define OPT_DROP_THRESHOLD "drop_threshold_ms"
 #define OPT_PFRAME_DROP_THRESHOLD "pframe_drop_threshold_ms"
 #define OPT_MAX_SHUTDOWN_TIME_SEC "max_shutdown_time_sec"
+#define OPT_BIND_INTERFACE "bind_interface"
 #define OPT_BIND_IP "bind_ip"
 #define OPT_IP_FAMILY "ip_family"
 #define OPT_NEWSOCKETLOOP_ENABLED "new_socket_loop_enabled"
@@ -81,6 +82,7 @@ struct rtmp_stream {
 	struct dstr path, key;
 	struct dstr username, password;
 	struct dstr encoder_name;
+	struct dstr bind_interface;
 	struct dstr bind_ip;
 	socklen_t addrlen_hint; /* hint IPv4 vs IPv6 */
 

From d04fb679b9beaed5db5b8e87d69d4ca8f674cb47 Mon Sep 17 00:00:00 2001
From: tytan652 <tytan652@tytanium.xyz>
Date: Thu, 11 Feb 2021 14:51:42 +0100
Subject: [PATCH 5/5] UI: Add interface binding property for Linux

---
 UI/data/locale/en-US.ini             |  1 +
 UI/forms/OBSBasicSettings.ui         | 24 +++++--
 UI/window-basic-auto-config-test.cpp |  3 +
 UI/window-basic-main-outputs.cpp     |  6 ++
 UI/window-basic-main.cpp             |  2 +
 UI/window-basic-settings.cpp         | 93 +++++++++++++++++++++++++++-
 UI/window-basic-settings.hpp         |  3 +
 7 files changed, 125 insertions(+), 7 deletions(-)

diff --git a/UI/data/locale/en-US.ini b/UI/data/locale/en-US.ini
index a1967ea126bf0..cc35a640bcf90 100644
--- a/UI/data/locale/en-US.ini
+++ b/UI/data/locale/en-US.ini
@@ -1246,6 +1246,7 @@ Basic.Settings.Advanced.StreamDelay.Preserve="Preserve cutoff point (increase de
 Basic.Settings.Advanced.StreamDelay.MemoryUsage="Estimated Memory Usage: %1 MB"
 Basic.Settings.Advanced.Network="Network"
 Basic.Settings.Advanced.Network.Disabled="The currently selected streaming protocol does not support changing network settings."
+Basic.Settings.Advanced.Network.BindToInterface="Bind to interface"
 Basic.Settings.Advanced.Network.BindToIP="Bind to IP"
 Basic.Settings.Advanced.Network.EnableNewSocketLoop="Enable network optimizations"
 Basic.Settings.Advanced.Network.EnableLowLatencyMode="Enable TCP pacing"
diff --git a/UI/forms/OBSBasicSettings.ui b/UI/forms/OBSBasicSettings.ui
index 6f886811b2e6c..8378d174300a2 100644
--- a/UI/forms/OBSBasicSettings.ui
+++ b/UI/forms/OBSBasicSettings.ui
@@ -7638,6 +7638,19 @@
                     </widget>
                    </item>
                    <item row="1" column="0">
+                    <widget class="QLabel" name="bindToIfaceLabel">
+                     <property name="text">
+                      <string>Basic.Settings.Advanced.Network.BindToInterface</string>
+                     </property>
+                     <property name="buddy">
+                      <cstring>bindToInterface</cstring>
+                     </property>
+                    </widget>
+                   </item>
+                   <item row="1" column="1">
+                    <widget class="QComboBox" name="bindToInterface"/>
+                   </item>
+                   <item row="2" column="0">
                     <widget class="QLabel" name="bindToIPLabel">
                      <property name="text">
                       <string>Basic.Settings.Advanced.Network.BindToIP</string>
@@ -7647,17 +7660,17 @@
                      </property>
                     </widget>
                    </item>
-                   <item row="1" column="1">
+                   <item row="2" column="1">
                     <widget class="QComboBox" name="bindToIP"/>
                    </item>
-                   <item row="3" column="1">
+                   <item row="4" column="1">
                     <widget class="QCheckBox" name="enableNewSocketLoop">
                      <property name="text">
                       <string>Basic.Settings.Advanced.Network.EnableNewSocketLoop</string>
                      </property>
                     </widget>
                    </item>
-                   <item row="4" column="1">
+                   <item row="5" column="1">
                     <widget class="QCheckBox" name="enableLowLatencyMode">
                      <property name="enabled">
                       <bool>false</bool>
@@ -7667,7 +7680,7 @@
                      </property>
                     </widget>
                    </item>
-                   <item row="3" column="0">
+                   <item row="4" column="0">
                     <spacer name="horizontalSpacer_7">
                      <property name="orientation">
                       <enum>Qt::Horizontal</enum>
@@ -7680,7 +7693,7 @@
                      </property>
                     </spacer>
                    </item>
-                   <item row="2" column="1">
+                   <item row="3" column="1">
                     <widget class="QCheckBox" name="dynBitrate">
                      <property name="toolTip">
                       <string>Basic.Settings.Output.DynamicBitrate.TT</string>
@@ -8015,6 +8028,7 @@
   <tabstop>reconnectRetryDelay</tabstop>
   <tabstop>reconnectMaxRetries</tabstop>
   <tabstop>bindToIP</tabstop>
+  <tabstop>bindToInterface</tabstop>
   <tabstop>dynBitrate</tabstop>
   <tabstop>enableNewSocketLoop</tabstop>
   <tabstop>enableLowLatencyMode</tabstop>
diff --git a/UI/window-basic-auto-config-test.cpp b/UI/window-basic-auto-config-test.cpp
index 45bf800b0483f..a7a87ab409b95 100644
--- a/UI/window-basic-auto-config-test.cpp
+++ b/UI/window-basic-auto-config-test.cpp
@@ -245,6 +245,9 @@ void AutoConfigTestPage::TestBandwidthThread()
 	obs_data_set_int(aencoder_settings, "bitrate", 32);
 
 	OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
+	const char *bind_interface =
+		config_get_string(main->Config(), "Output", "BindInterface");
+	obs_data_set_string(output_settings, "bind_interface", bind_interface);
 	const char *bind_ip =
 		config_get_string(main->Config(), "Output", "BindIP");
 	obs_data_set_string(output_settings, "bind_ip", bind_ip);
diff --git a/UI/window-basic-main-outputs.cpp b/UI/window-basic-main-outputs.cpp
index b604aae7358ab..d63292dc02905 100644
--- a/UI/window-basic-main-outputs.cpp
+++ b/UI/window-basic-main-outputs.cpp
@@ -1206,6 +1206,8 @@ bool SimpleOutput::StartStreaming(obs_service_t *service)
 	int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
 	bool preserveDelay =
 		config_get_bool(main->Config(), "Output", "DelayPreserve");
+	const char *bindInterface =
+		config_get_string(main->Config(), "Output", "BindInterface");
 	const char *bindIP =
 		config_get_string(main->Config(), "Output", "BindIP");
 #ifdef _WIN32
@@ -1218,6 +1220,7 @@ bool SimpleOutput::StartStreaming(obs_service_t *service)
 		config_get_bool(main->Config(), "Output", "DynamicBitrate");
 
 	OBSDataAutoRelease settings = obs_data_create();
+	obs_data_set_string(settings, "bind_interface", bindInterface);
 	obs_data_set_string(settings, "bind_ip", bindIP);
 #ifdef _WIN32
 	obs_data_set_bool(settings, "new_socket_loop_enabled",
@@ -2253,6 +2256,8 @@ bool AdvancedOutput::StartStreaming(obs_service_t *service)
 	int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
 	bool preserveDelay =
 		config_get_bool(main->Config(), "Output", "DelayPreserve");
+	const char *bindInterface =
+		config_get_string(main->Config(), "Output", "BindInterface");
 	const char *bindIP =
 		config_get_string(main->Config(), "Output", "BindIP");
 #ifdef _WIN32
@@ -2274,6 +2279,7 @@ bool AdvancedOutput::StartStreaming(obs_service_t *service)
 	}
 
 	OBSDataAutoRelease settings = obs_data_create();
+	obs_data_set_string(settings, "bind_interface", bindInterface);
 	obs_data_set_string(settings, "bind_ip", bindIP);
 #ifdef _WIN32
 	obs_data_set_bool(settings, "new_socket_loop_enabled",
diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp
index 008e59ac9438f..46cb2e797b27e 100644
--- a/UI/window-basic-main.cpp
+++ b/UI/window-basic-main.cpp
@@ -1716,6 +1716,8 @@ bool OBSBasic::InitBasicConfigDefaults()
 	config_set_default_uint(basicConfig, "Output", "RetryDelay", 2);
 	config_set_default_uint(basicConfig, "Output", "MaxRetries", 25);
 
+	config_set_default_string(basicConfig, "Output", "BindInterface",
+				  "default");
 	config_set_default_string(basicConfig, "Output", "BindIP", "default");
 	config_set_default_bool(basicConfig, "Output", "NewSocketLoopEnable",
 				false);
diff --git a/UI/window-basic-settings.cpp b/UI/window-basic-settings.cpp
index 9314b2da4918f..15ca97c09edb9 100644
--- a/UI/window-basic-settings.cpp
+++ b/UI/window-basic-settings.cpp
@@ -567,6 +567,9 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	HookWidget(ui->reconnectMaxRetries,  SCROLL_CHANGED, ADV_CHANGED);
 	HookWidget(ui->processPriority,      COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->confirmOnExit,        CHECK_CHANGED,  ADV_CHANGED);
+#ifdef __linux__
+	HookWidget(ui->bindToInterface,      COMBO_CHANGED,  ADV_CHANGED);
+#endif
 	HookWidget(ui->bindToIP,             COMBO_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->enableNewSocketLoop,  CHECK_CHANGED,  ADV_CHANGED);
 	HookWidget(ui->enableLowLatencyMode, CHECK_CHANGED,  ADV_CHANGED);
@@ -675,6 +678,13 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	ui->resetOSXVSync = nullptr;
 #endif
 
+#ifndef __linux__
+	delete ui->bindToIfaceLabel;
+	delete ui->bindToInterface;
+	ui->bindToIfaceLabel = nullptr;
+	ui->bindToInterface = nullptr;
+#endif
+
 	connect(ui->streamDelaySec, &QSpinBox::valueChanged, this,
 		&OBSBasicSettings::UpdateStreamDelayEstimate);
 	connect(ui->outputMode, &QComboBox::currentIndexChanged, this,
@@ -824,6 +834,7 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	connect(ui->advRBSecMax, &QSpinBox::valueChanged, this,
 		&OBSBasicSettings::AdvReplayBufferChanged);
 
+
 	// GPU scaling filters
 	auto addScaleFilter = [&](const char *string, int value) -> void {
 		ui->advOutRescaleFilter->addItem(QTStr(string), value);
@@ -853,8 +864,37 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 	connectScaleFilter(ui->advOutRescaleFilter, ui->advOutRescale);
 	connectScaleFilter(ui->advOutRecRescaleFilter, ui->advOutRecRescale);
 
-	// Get Bind to IP Addresses
+
+#ifdef __linux__
+	connect(ui->bindToInterface, &QComboBox::currentIndexChanged, this,
+		&OBSBasicSettings::UpdateAddrList);
+	ui->bindToIP->setEnabled(false);
+#endif
+
+	// Get Bind to interfaces Interfaces (Linux only)
+	// Get Bind to IP Addresses (Others)
 	obs_properties_t *ppts = obs_get_output_properties("rtmp_output");
+#ifdef __linux__
+	obs_property_t *p_iface = obs_properties_get(ppts, "bind_interface");
+	QStringList dedup_iface = {};
+
+	ui->bindToInterface->blockSignals(true);
+
+	size_t count_iface = obs_property_list_item_count(p_iface);
+	for (size_t i = 0; i < count_iface; i++) {
+		const char *name = obs_property_list_item_name(p_iface, i);
+		const char *val = obs_property_list_item_string(p_iface, i);
+
+		// Add interfaces without duplicates
+		if (!dedup_iface.contains(QT_UTF8(name))) {
+			dedup_iface.append(QT_UTF8(name));
+			ui->bindToInterface->addItem(QT_UTF8(name), val);
+		}
+	}
+
+	UpdateAddrList();
+	ui->bindToInterface->blockSignals(false);
+#else
 	obs_property_t *p = obs_properties_get(ppts, "bind_ip");
 
 	size_t count = obs_property_list_item_count(p);
@@ -864,7 +904,7 @@ OBSBasicSettings::OBSBasicSettings(QWidget *parent)
 
 		ui->bindToIP->addItem(QT_UTF8(name), val);
 	}
-
+#endif
 	obs_properties_destroy(ppts);
 
 	InitStreamPage();
@@ -2876,6 +2916,10 @@ void OBSBasicSettings::LoadAdvancedSettings()
 						 "FilenameFormatting");
 	bool overwriteIfExists =
 		config_get_bool(main->Config(), "Output", "OverwriteIfExists");
+#ifdef __linux__
+	const char *bindInterface =
+		config_get_string(main->Config(), "Output", "BindInterface");
+#endif
 	const char *bindIP =
 		config_get_string(main->Config(), "Output", "BindIP");
 	const char *rbPrefix = config_get_string(main->Config(), "SimpleOutput",
@@ -2930,6 +2974,12 @@ void OBSBasicSettings::LoadAdvancedSettings()
 	ui->sdrWhiteLevel->setValue(sdrWhiteLevel);
 	ui->hdrNominalPeakLevel->setValue(hdrNominalPeakLevel);
 
+#ifdef __linux__
+	if (!SetComboByValue(ui->bindToInterface, bindInterface))
+		SetInvalidValue(ui->bindToInterface, bindInterface,
+				bindInterface);
+#endif
+
 	if (!SetComboByValue(ui->bindToIP, bindIP))
 		SetInvalidValue(ui->bindToIP, bindIP, bindIP);
 
@@ -3663,6 +3713,9 @@ void OBSBasicSettings::SaveAdvancedSettings()
 	SaveCheckBox(ui->reconnectEnable, "Output", "Reconnect");
 	SaveSpinBox(ui->reconnectRetryDelay, "Output", "RetryDelay");
 	SaveSpinBox(ui->reconnectMaxRetries, "Output", "MaxRetries");
+#ifdef __linux__
+	SaveComboData(ui->bindToInterface, "Output", "BindInterface");
+#endif
 	SaveComboData(ui->bindToIP, "Output", "BindIP");
 	SaveCheckBox(ui->autoRemux, "Video", "AutoRemux");
 	SaveCheckBox(ui->dynBitrate, "Output", "DynamicBitrate");
@@ -6231,6 +6284,9 @@ void OBSBasicSettings::UpdateAdvNetworkGroup()
 #ifdef _WIN32
 	ui->enableNewSocketLoop->setVisible(enabled);
 	ui->enableLowLatencyMode->setVisible(enabled);
+#elif defined(__linux__)
+	ui->bindToIfaceLabel->setVisible(enabled);
+	ui->bindToInterface->setVisible(enabled);
 #endif
 }
 
@@ -6269,3 +6325,36 @@ void OBSBasicSettings::AdvAudioEncodersChanged()
 			       ui->advOutTrack6Bitrate},
 			      320);
 }
+
+#ifdef __linux__
+void OBSBasicSettings::UpdateAddrList()
+{
+	ui->bindToIP->clear();
+
+	// Get Bind to IP Addresses (Linux only)
+	obs_properties_t *ppts = obs_get_output_properties("rtmp_output");
+	obs_property_t *p = obs_properties_get(ppts, "bind_ip");
+
+	size_t count_addr = obs_property_list_item_count(p);
+	for (size_t i = 0; i < count_addr; i++) {
+		const char *name = obs_property_list_item_name(p, i);
+		const char *val = obs_property_list_item_string(p, i);
+
+		//Put only the addresses from the selected interface
+		if (QT_UTF8(name).contains(
+			    ui->bindToInterface->currentText()) ||
+		    QT_UTF8(val).contains("default"))
+			ui->bindToIP->addItem(QT_UTF8(name), val);
+	}
+
+	obs_properties_destroy(ppts);
+
+	if (ui->bindToInterface->currentIndex() > 0) {
+		ui->bindToIP->setEnabled(true);
+	} else {
+		ui->bindToIP->setEnabled(false);
+		if (!SetComboByValue(ui->bindToIP, "default"))
+			SetInvalidValue(ui->bindToIP, "default", "default");
+	}
+}
+#endif
diff --git a/UI/window-basic-settings.hpp b/UI/window-basic-settings.hpp
index 01bffeb494c20..fd66b413cc794 100644
--- a/UI/window-basic-settings.hpp
+++ b/UI/window-basic-settings.hpp
@@ -472,6 +472,9 @@ private slots:
 	void SetHotkeysIcon(const QIcon &icon);
 	void SetAccessibilityIcon(const QIcon &icon);
 	void SetAdvancedIcon(const QIcon &icon);
+#ifdef __linux__
+	void UpdateAddrList();
+#endif
 
 	void UseStreamKeyAdvClicked();