forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs-encoder.c
1194 lines (965 loc) · 30.7 KB
/
obs-encoder.c
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
Copyright (C) 2013-2014 by Hugh Bailey <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "obs.h"
#include "obs-internal.h"
#define encoder_active(encoder) \
os_atomic_load_bool(&encoder->active)
#define set_encoder_active(encoder, val) \
os_atomic_set_bool(&encoder->active, val)
struct obs_encoder_info *find_encoder(const char *id)
{
for (size_t i = 0; i < obs->encoder_types.num; i++) {
struct obs_encoder_info *info = obs->encoder_types.array+i;
if (strcmp(info->id, id) == 0)
return info;
}
return NULL;
}
const char *obs_encoder_get_display_name(const char *id)
{
struct obs_encoder_info *ei = find_encoder(id);
return ei ? ei->get_name(ei->type_data) : NULL;
}
static bool init_encoder(struct obs_encoder *encoder, const char *name,
obs_data_t *settings, obs_data_t *hotkey_data)
{
pthread_mutexattr_t attr;
pthread_mutex_init_value(&encoder->init_mutex);
pthread_mutex_init_value(&encoder->callbacks_mutex);
pthread_mutex_init_value(&encoder->outputs_mutex);
if (pthread_mutexattr_init(&attr) != 0)
return false;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
return false;
if (!obs_context_data_init(&encoder->context, OBS_OBJ_TYPE_ENCODER,
settings, name, hotkey_data, false))
return false;
if (pthread_mutex_init(&encoder->init_mutex, &attr) != 0)
return false;
if (pthread_mutex_init(&encoder->callbacks_mutex, &attr) != 0)
return false;
if (pthread_mutex_init(&encoder->outputs_mutex, NULL) != 0)
return false;
if (encoder->info.get_defaults)
encoder->info.get_defaults(encoder->context.settings);
return true;
}
static struct obs_encoder *create_encoder(const char *id,
enum obs_encoder_type type, const char *name,
obs_data_t *settings, size_t mixer_idx, obs_data_t *hotkey_data)
{
struct obs_encoder *encoder;
struct obs_encoder_info *ei = find_encoder(id);
bool success;
if (ei && ei->type != type)
return NULL;
encoder = bzalloc(sizeof(struct obs_encoder));
encoder->mixer_idx = mixer_idx;
if (!ei) {
blog(LOG_ERROR, "Encoder ID '%s' not found", id);
encoder->info.id = bstrdup(id);
encoder->info.type = type;
encoder->owns_info_id = true;
} else {
encoder->info = *ei;
}
success = init_encoder(encoder, name, settings, hotkey_data);
if (!success) {
blog(LOG_ERROR, "creating encoder '%s' (%s) failed", name, id);
obs_encoder_destroy(encoder);
return NULL;
}
encoder->control = bzalloc(sizeof(obs_weak_encoder_t));
encoder->control->encoder = encoder;
obs_context_data_insert(&encoder->context,
&obs->data.encoders_mutex,
&obs->data.first_encoder);
blog(LOG_DEBUG, "encoder '%s' (%s) created", name, id);
return encoder;
}
obs_encoder_t *obs_video_encoder_create(const char *id, const char *name,
obs_data_t *settings, obs_data_t *hotkey_data)
{
if (!name || !id) return NULL;
return create_encoder(id, OBS_ENCODER_VIDEO, name, settings, 0,
hotkey_data);
}
obs_encoder_t *obs_audio_encoder_create(const char *id, const char *name,
obs_data_t *settings, size_t mixer_idx, obs_data_t *hotkey_data)
{
if (!name || !id) return NULL;
return create_encoder(id, OBS_ENCODER_AUDIO, name, settings, mixer_idx,
hotkey_data);
}
static void receive_video(void *param, struct video_data *frame);
static void receive_audio(void *param, size_t mix_idx, struct audio_data *data);
static inline void get_audio_info(const struct obs_encoder *encoder,
struct audio_convert_info *info)
{
const struct audio_output_info *aoi;
aoi = audio_output_get_info(encoder->media);
if (info->format == AUDIO_FORMAT_UNKNOWN)
info->format = aoi->format;
if (!info->samples_per_sec)
info->samples_per_sec = aoi->samples_per_sec;
if (info->speakers == SPEAKERS_UNKNOWN)
info->speakers = aoi->speakers;
if (encoder->info.get_audio_info)
encoder->info.get_audio_info(encoder->context.data, info);
}
static inline void get_video_info(struct obs_encoder *encoder,
struct video_scale_info *info)
{
const struct video_output_info *voi;
voi = video_output_get_info(encoder->media);
info->format = voi->format;
info->colorspace = voi->colorspace;
info->range = voi->range;
info->width = obs_encoder_get_width(encoder);
info->height = obs_encoder_get_height(encoder);
if (encoder->info.get_video_info)
encoder->info.get_video_info(encoder->context.data, info);
if (info->width != voi->width || info->height != voi->height)
obs_encoder_set_scaled_size(encoder, info->width, info->height);
}
static inline bool has_scaling(const struct obs_encoder *encoder)
{
uint32_t video_width = video_output_get_width(encoder->media);
uint32_t video_height = video_output_get_height(encoder->media);
return encoder->scaled_width && encoder->scaled_height &&
(video_width != encoder->scaled_width ||
video_height != encoder->scaled_height);
}
static void add_connection(struct obs_encoder *encoder)
{
if (encoder->info.type == OBS_ENCODER_AUDIO) {
struct audio_convert_info audio_info = {0};
get_audio_info(encoder, &audio_info);
audio_output_connect(encoder->media, encoder->mixer_idx,
&audio_info, receive_audio, encoder);
} else {
struct video_scale_info info = {0};
get_video_info(encoder, &info);
start_raw_video(encoder->media, &info, receive_video, encoder);
}
set_encoder_active(encoder, true);
}
static void remove_connection(struct obs_encoder *encoder)
{
if (encoder->info.type == OBS_ENCODER_AUDIO)
audio_output_disconnect(encoder->media, encoder->mixer_idx,
receive_audio, encoder);
else
stop_raw_video(encoder->media, receive_video, encoder);
obs_encoder_shutdown(encoder);
set_encoder_active(encoder, false);
}
static inline void free_audio_buffers(struct obs_encoder *encoder)
{
for (size_t i = 0; i < MAX_AV_PLANES; i++) {
circlebuf_free(&encoder->audio_input_buffer[i]);
bfree(encoder->audio_output_buffer[i]);
encoder->audio_output_buffer[i] = NULL;
}
}
static void obs_encoder_actually_destroy(obs_encoder_t *encoder)
{
if (encoder) {
pthread_mutex_lock(&encoder->outputs_mutex);
for (size_t i = 0; i < encoder->outputs.num; i++) {
struct obs_output *output = encoder->outputs.array[i];
obs_output_remove_encoder(output, encoder);
}
da_free(encoder->outputs);
pthread_mutex_unlock(&encoder->outputs_mutex);
blog(LOG_DEBUG, "encoder '%s' destroyed", encoder->context.name);
free_audio_buffers(encoder);
if (encoder->context.data)
encoder->info.destroy(encoder->context.data);
da_free(encoder->callbacks);
pthread_mutex_destroy(&encoder->init_mutex);
pthread_mutex_destroy(&encoder->callbacks_mutex);
pthread_mutex_destroy(&encoder->outputs_mutex);
obs_context_data_free(&encoder->context);
if (encoder->owns_info_id)
bfree((void*)encoder->info.id);
bfree(encoder);
}
}
/* does not actually destroy the encoder until all connections to it have been
* removed. (full reference counting really would have been superfluous) */
void obs_encoder_destroy(obs_encoder_t *encoder)
{
if (encoder) {
bool destroy;
obs_context_data_remove(&encoder->context);
pthread_mutex_lock(&encoder->callbacks_mutex);
destroy = encoder->callbacks.num == 0;
if (!destroy)
encoder->destroy_on_stop = true;
pthread_mutex_unlock(&encoder->callbacks_mutex);
if (destroy)
obs_encoder_actually_destroy(encoder);
}
}
const char *obs_encoder_get_name(const obs_encoder_t *encoder)
{
return obs_encoder_valid(encoder, "obs_encoder_get_name") ?
encoder->context.name : NULL;
}
void obs_encoder_set_name(obs_encoder_t *encoder, const char *name)
{
if (!obs_encoder_valid(encoder, "obs_encoder_set_name"))
return;
if (name && *name && strcmp(name, encoder->context.name) != 0)
obs_context_data_setname(&encoder->context, name);
}
static inline obs_data_t *get_defaults(const struct obs_encoder_info *info)
{
obs_data_t *settings = obs_data_create();
if (info->get_defaults)
info->get_defaults(settings);
return settings;
}
obs_data_t *obs_encoder_defaults(const char *id)
{
const struct obs_encoder_info *info = find_encoder(id);
return (info) ? get_defaults(info) : NULL;
}
obs_properties_t *obs_get_encoder_properties(const char *id)
{
const struct obs_encoder_info *ei = find_encoder(id);
if (ei && ei->get_properties) {
obs_data_t *defaults = get_defaults(ei);
obs_properties_t *properties;
properties = ei->get_properties(NULL);
obs_properties_apply_settings(properties, defaults);
obs_data_release(defaults);
return properties;
}
return NULL;
}
obs_properties_t *obs_encoder_properties(const obs_encoder_t *encoder)
{
if (!obs_encoder_valid(encoder, "obs_encoder_properties"))
return NULL;
if (encoder->info.get_properties) {
obs_properties_t *props;
props = encoder->info.get_properties(encoder->context.data);
obs_properties_apply_settings(props, encoder->context.settings);
return props;
}
return NULL;
}
void obs_encoder_update(obs_encoder_t *encoder, obs_data_t *settings)
{
if (!obs_encoder_valid(encoder, "obs_encoder_update"))
return;
obs_data_apply(encoder->context.settings, settings);
if (encoder->info.update && encoder->context.data)
encoder->info.update(encoder->context.data,
encoder->context.settings);
}
bool obs_encoder_get_extra_data(const obs_encoder_t *encoder,
uint8_t **extra_data, size_t *size)
{
if (!obs_encoder_valid(encoder, "obs_encoder_get_extra_data"))
return false;
if (encoder->info.get_extra_data && encoder->context.data)
return encoder->info.get_extra_data(encoder->context.data,
extra_data, size);
return false;
}
obs_data_t *obs_encoder_get_settings(const obs_encoder_t *encoder)
{
if (!obs_encoder_valid(encoder, "obs_encoder_get_settings"))
return NULL;
obs_data_addref(encoder->context.settings);
return encoder->context.settings;
}
static inline void reset_audio_buffers(struct obs_encoder *encoder)
{
free_audio_buffers(encoder);
for (size_t i = 0; i < encoder->planes; i++)
encoder->audio_output_buffer[i] =
bmalloc(encoder->framesize_bytes);
}
static void intitialize_audio_encoder(struct obs_encoder *encoder)
{
struct audio_convert_info info = {0};
get_audio_info(encoder, &info);
encoder->samplerate = info.samples_per_sec;
encoder->planes = get_audio_planes(info.format, info.speakers);
encoder->blocksize = get_audio_size(info.format, info.speakers, 1);
encoder->framesize = encoder->info.get_frame_size(
encoder->context.data);
encoder->framesize_bytes = encoder->blocksize * encoder->framesize;
reset_audio_buffers(encoder);
}
static inline bool obs_encoder_initialize_internal(obs_encoder_t *encoder)
{
if (encoder_active(encoder))
return true;
if (encoder->initialized)
return true;
obs_encoder_shutdown(encoder);
if (encoder->info.create)
encoder->context.data = encoder->info.create(
encoder->context.settings, encoder);
if (!encoder->context.data)
return false;
if (encoder->info.type == OBS_ENCODER_AUDIO)
intitialize_audio_encoder(encoder);
encoder->initialized = true;
return true;
}
bool obs_encoder_initialize(obs_encoder_t *encoder)
{
bool success;
if (!encoder) return false;
pthread_mutex_lock(&encoder->init_mutex);
success = obs_encoder_initialize_internal(encoder);
pthread_mutex_unlock(&encoder->init_mutex);
return success;
}
void obs_encoder_shutdown(obs_encoder_t *encoder)
{
pthread_mutex_lock(&encoder->init_mutex);
if (encoder->context.data) {
encoder->info.destroy(encoder->context.data);
encoder->context.data = NULL;
encoder->paired_encoder = NULL;
encoder->first_received = false;
encoder->offset_usec = 0;
encoder->start_ts = 0;
}
pthread_mutex_unlock(&encoder->init_mutex);
}
static inline size_t get_callback_idx(
const struct obs_encoder *encoder,
void (*new_packet)(void *param, struct encoder_packet *packet),
void *param)
{
for (size_t i = 0; i < encoder->callbacks.num; i++) {
struct encoder_callback *cb = encoder->callbacks.array+i;
if (cb->new_packet == new_packet && cb->param == param)
return i;
}
return DARRAY_INVALID;
}
static inline void obs_encoder_start_internal(obs_encoder_t *encoder,
void (*new_packet)(void *param, struct encoder_packet *packet),
void *param)
{
struct encoder_callback cb = {false, new_packet, param};
bool first = false;
if (!encoder->context.data)
return;
pthread_mutex_lock(&encoder->callbacks_mutex);
first = (encoder->callbacks.num == 0);
size_t idx = get_callback_idx(encoder, new_packet, param);
if (idx == DARRAY_INVALID)
da_push_back(encoder->callbacks, &cb);
pthread_mutex_unlock(&encoder->callbacks_mutex);
if (first) {
encoder->cur_pts = 0;
add_connection(encoder);
}
}
void obs_encoder_start(obs_encoder_t *encoder,
void (*new_packet)(void *param, struct encoder_packet *packet),
void *param)
{
if (!obs_encoder_valid(encoder, "obs_encoder_start"))
return;
if (!obs_ptr_valid(new_packet, "obs_encoder_start"))
return;
pthread_mutex_lock(&encoder->init_mutex);
obs_encoder_start_internal(encoder, new_packet, param);
pthread_mutex_unlock(&encoder->init_mutex);
}
static inline bool obs_encoder_stop_internal(obs_encoder_t *encoder,
void (*new_packet)(void *param, struct encoder_packet *packet),
void *param)
{
bool last = false;
size_t idx;
pthread_mutex_lock(&encoder->callbacks_mutex);
idx = get_callback_idx(encoder, new_packet, param);
if (idx != DARRAY_INVALID) {
da_erase(encoder->callbacks, idx);
last = (encoder->callbacks.num == 0);
}
pthread_mutex_unlock(&encoder->callbacks_mutex);
if (last) {
remove_connection(encoder);
encoder->initialized = false;
if (encoder->destroy_on_stop) {
pthread_mutex_unlock(&encoder->init_mutex);
obs_encoder_actually_destroy(encoder);
return true;
}
}
return false;
}
void obs_encoder_stop(obs_encoder_t *encoder,
void (*new_packet)(void *param, struct encoder_packet *packet),
void *param)
{
bool destroyed;
if (!obs_encoder_valid(encoder, "obs_encoder_stop"))
return;
if (!obs_ptr_valid(new_packet, "obs_encoder_stop"))
return;
pthread_mutex_lock(&encoder->init_mutex);
destroyed = obs_encoder_stop_internal(encoder, new_packet, param);
if (!destroyed)
pthread_mutex_unlock(&encoder->init_mutex);
}
const char *obs_encoder_get_codec(const obs_encoder_t *encoder)
{
return obs_encoder_valid(encoder, "obs_encoder_get_codec") ?
encoder->info.codec : NULL;
}
const char *obs_get_encoder_codec(const char *id)
{
struct obs_encoder_info *info = find_encoder(id);
return info ? info->codec : NULL;
}
enum obs_encoder_type obs_encoder_get_type(const obs_encoder_t *encoder)
{
return obs_encoder_valid(encoder, "obs_encoder_get_type") ?
encoder->info.type : OBS_ENCODER_AUDIO;
}
enum obs_encoder_type obs_get_encoder_type(const char *id)
{
struct obs_encoder_info *info = find_encoder(id);
return info ? info->type : OBS_ENCODER_AUDIO;
}
void obs_encoder_set_scaled_size(obs_encoder_t *encoder, uint32_t width,
uint32_t height)
{
if (!obs_encoder_valid(encoder, "obs_encoder_set_scaled_size"))
return;
if (encoder->info.type != OBS_ENCODER_VIDEO) {
blog(LOG_WARNING, "obs_encoder_set_scaled_size: "
"encoder '%s' is not a video encoder",
obs_encoder_get_name(encoder));
return;
}
if (encoder_active(encoder)) {
blog(LOG_WARNING, "encoder '%s': Cannot set the scaled "
"resolution while the encoder is active",
obs_encoder_get_name(encoder));
return;
}
encoder->scaled_width = width;
encoder->scaled_height = height;
}
uint32_t obs_encoder_get_width(const obs_encoder_t *encoder)
{
if (!obs_encoder_valid(encoder, "obs_encoder_get_width"))
return 0;
if (encoder->info.type != OBS_ENCODER_VIDEO) {
blog(LOG_WARNING, "obs_encoder_get_width: "
"encoder '%s' is not a video encoder",
obs_encoder_get_name(encoder));
return 0;
}
if (!encoder->media)
return 0;
return encoder->scaled_width != 0 ?
encoder->scaled_width :
video_output_get_width(encoder->media);
}
uint32_t obs_encoder_get_height(const obs_encoder_t *encoder)
{
if (!obs_encoder_valid(encoder, "obs_encoder_get_height"))
return 0;
if (encoder->info.type != OBS_ENCODER_VIDEO) {
blog(LOG_WARNING, "obs_encoder_get_height: "
"encoder '%s' is not a video encoder",
obs_encoder_get_name(encoder));
return 0;
}
if (!encoder->media)
return 0;
return encoder->scaled_height != 0 ?
encoder->scaled_height :
video_output_get_height(encoder->media);
}
uint32_t obs_encoder_get_sample_rate(const obs_encoder_t *encoder)
{
if (!obs_encoder_valid(encoder, "obs_encoder_get_sample_rate"))
return 0;
if (encoder->info.type != OBS_ENCODER_AUDIO) {
blog(LOG_WARNING, "obs_encoder_get_sample_rate: "
"encoder '%s' is not an audio encoder",
obs_encoder_get_name(encoder));
return 0;
}
if (!encoder->media)
return 0;
return encoder->samplerate != 0 ?
encoder->samplerate :
audio_output_get_sample_rate(encoder->media);
}
void obs_encoder_set_video(obs_encoder_t *encoder, video_t *video)
{
const struct video_output_info *voi;
if (!obs_encoder_valid(encoder, "obs_encoder_set_video"))
return;
if (encoder->info.type != OBS_ENCODER_VIDEO) {
blog(LOG_WARNING, "obs_encoder_set_video: "
"encoder '%s' is not a video encoder",
obs_encoder_get_name(encoder));
return;
}
if (!video)
return;
voi = video_output_get_info(video);
encoder->media = video;
encoder->timebase_num = voi->fps_den;
encoder->timebase_den = voi->fps_num;
}
void obs_encoder_set_audio(obs_encoder_t *encoder, audio_t *audio)
{
if (!obs_encoder_valid(encoder, "obs_encoder_set_audio"))
return;
if (encoder->info.type != OBS_ENCODER_AUDIO) {
blog(LOG_WARNING, "obs_encoder_set_audio: "
"encoder '%s' is not an audio encoder",
obs_encoder_get_name(encoder));
return;
}
if (!audio)
return;
encoder->media = audio;
encoder->timebase_num = 1;
encoder->timebase_den = audio_output_get_sample_rate(audio);
}
video_t *obs_encoder_video(const obs_encoder_t *encoder)
{
if (!obs_encoder_valid(encoder, "obs_encoder_video"))
return NULL;
if (encoder->info.type != OBS_ENCODER_VIDEO) {
blog(LOG_WARNING, "obs_encoder_set_video: "
"encoder '%s' is not a video encoder",
obs_encoder_get_name(encoder));
return NULL;
}
return encoder->media;
}
audio_t *obs_encoder_audio(const obs_encoder_t *encoder)
{
if (!obs_encoder_valid(encoder, "obs_encoder_audio"))
return NULL;
if (encoder->info.type != OBS_ENCODER_AUDIO) {
blog(LOG_WARNING, "obs_encoder_set_audio: "
"encoder '%s' is not an audio encoder",
obs_encoder_get_name(encoder));
return NULL;
}
return encoder->media;
}
bool obs_encoder_active(const obs_encoder_t *encoder)
{
return obs_encoder_valid(encoder, "obs_encoder_active") ?
encoder_active(encoder) : false;
}
static inline bool get_sei(const struct obs_encoder *encoder,
uint8_t **sei, size_t *size)
{
if (encoder->info.get_sei_data)
return encoder->info.get_sei_data(encoder->context.data, sei,
size);
return false;
}
static void send_first_video_packet(struct obs_encoder *encoder,
struct encoder_callback *cb, struct encoder_packet *packet)
{
struct encoder_packet first_packet;
DARRAY(uint8_t) data;
uint8_t *sei;
size_t size;
/* always wait for first keyframe */
if (!packet->keyframe)
return;
da_init(data);
if (!get_sei(encoder, &sei, &size) || !sei || !size) {
cb->new_packet(cb->param, packet);
cb->sent_first_packet = true;
return;
}
da_push_back_array(data, sei, size);
da_push_back_array(data, packet->data, packet->size);
first_packet = *packet;
first_packet.data = data.array;
first_packet.size = data.num;
cb->new_packet(cb->param, &first_packet);
cb->sent_first_packet = true;
da_free(data);
}
static inline void send_packet(struct obs_encoder *encoder,
struct encoder_callback *cb, struct encoder_packet *packet)
{
/* include SEI in first video packet */
if (encoder->info.type == OBS_ENCODER_VIDEO && !cb->sent_first_packet)
send_first_video_packet(encoder, cb, packet);
else
cb->new_packet(cb->param, packet);
}
static void full_stop(struct obs_encoder *encoder)
{
if (encoder) {
pthread_mutex_lock(&encoder->callbacks_mutex);
da_free(encoder->callbacks);
remove_connection(encoder);
pthread_mutex_unlock(&encoder->callbacks_mutex);
}
}
static const char *do_encode_name = "do_encode";
static inline void do_encode(struct obs_encoder *encoder,
struct encoder_frame *frame)
{
profile_start(do_encode_name);
if (!encoder->profile_encoder_encode_name)
encoder->profile_encoder_encode_name =
profile_store_name(obs_get_profiler_name_store(),
"encode(%s)", encoder->context.name);
struct encoder_packet pkt = {0};
bool received = false;
bool success;
pkt.timebase_num = encoder->timebase_num;
pkt.timebase_den = encoder->timebase_den;
pkt.encoder = encoder;
profile_start(encoder->profile_encoder_encode_name);
success = encoder->info.encode(encoder->context.data, frame, &pkt,
&received);
profile_end(encoder->profile_encoder_encode_name);
if (!success) {
full_stop(encoder);
blog(LOG_ERROR, "Error encoding with encoder '%s'",
encoder->context.name);
goto error;
}
if (received) {
if (!encoder->first_received) {
encoder->offset_usec = packet_dts_usec(&pkt);
encoder->first_received = true;
}
/* we use system time here to ensure sync with other encoders,
* you do not want to use relative timestamps here */
pkt.dts_usec = encoder->start_ts / 1000 +
packet_dts_usec(&pkt) - encoder->offset_usec;
pkt.sys_dts_usec = pkt.dts_usec;
pthread_mutex_lock(&encoder->callbacks_mutex);
for (size_t i = encoder->callbacks.num; i > 0; i--) {
struct encoder_callback *cb;
cb = encoder->callbacks.array+(i-1);
send_packet(encoder, cb, &pkt);
}
pthread_mutex_unlock(&encoder->callbacks_mutex);
}
error:
profile_end(do_encode_name);
}
static const char *receive_video_name = "receive_video";
static void receive_video(void *param, struct video_data *frame)
{
profile_start(receive_video_name);
struct obs_encoder *encoder = param;
struct obs_encoder *pair = encoder->paired_encoder;
struct encoder_frame enc_frame;
if (!encoder->first_received && pair) {
if (!pair->first_received ||
pair->first_raw_ts > frame->timestamp) {
goto wait_for_audio;
}
}
memset(&enc_frame, 0, sizeof(struct encoder_frame));
for (size_t i = 0; i < MAX_AV_PLANES; i++) {
enc_frame.data[i] = frame->data[i];
enc_frame.linesize[i] = frame->linesize[i];
}
if (!encoder->start_ts)
encoder->start_ts = frame->timestamp;
enc_frame.frames = 1;
enc_frame.pts = encoder->cur_pts;
do_encode(encoder, &enc_frame);
encoder->cur_pts += encoder->timebase_num;
wait_for_audio:
profile_end(receive_video_name);
}
static void clear_audio(struct obs_encoder *encoder)
{
for (size_t i = 0; i < encoder->planes; i++)
circlebuf_free(&encoder->audio_input_buffer[i]);
}
static inline void push_back_audio(struct obs_encoder *encoder,
struct audio_data *data, size_t size, size_t offset_size)
{
size -= offset_size;
/* push in to the circular buffer */
if (size)
for (size_t i = 0; i < encoder->planes; i++)
circlebuf_push_back(&encoder->audio_input_buffer[i],
data->data[i] + offset_size, size);
}
static inline size_t calc_offset_size(struct obs_encoder *encoder,
uint64_t v_start_ts, uint64_t a_start_ts)
{
uint64_t offset = v_start_ts - a_start_ts;
offset = (uint64_t)offset * (uint64_t)encoder->samplerate /
1000000000ULL;
return (size_t)offset * encoder->blocksize;
}
static void start_from_buffer(struct obs_encoder *encoder, uint64_t v_start_ts)
{
size_t size = encoder->audio_input_buffer[0].size;
struct audio_data audio = {0};
size_t offset_size = 0;
for (size_t i = 0; i < MAX_AV_PLANES; i++) {
audio.data[i] = encoder->audio_input_buffer[i].data;
memset(&encoder->audio_input_buffer[i], 0,
sizeof(struct circlebuf));
}
if (encoder->first_raw_ts < v_start_ts)
offset_size = calc_offset_size(encoder, v_start_ts,
encoder->first_raw_ts);
push_back_audio(encoder, &audio, size, offset_size);
for (size_t i = 0; i < MAX_AV_PLANES; i++)
bfree(audio.data[i]);
}
static const char *buffer_audio_name = "buffer_audio";
static bool buffer_audio(struct obs_encoder *encoder, struct audio_data *data)
{
profile_start(buffer_audio_name);
size_t size = data->frames * encoder->blocksize;
size_t offset_size = 0;
bool success = true;
if (!encoder->start_ts && encoder->paired_encoder) {
uint64_t end_ts = data->timestamp;
uint64_t v_start_ts = encoder->paired_encoder->start_ts;
/* no video yet, so don't start audio */
if (!v_start_ts) {
success = false;
goto fail;
}
/* audio starting point still not synced with video starting
* point, so don't start audio */
end_ts += (uint64_t)data->frames * 1000000000ULL /
(uint64_t)encoder->samplerate;
if (end_ts <= v_start_ts) {
success = false;
goto fail;
}
/* ready to start audio, truncate if necessary */
if (data->timestamp < v_start_ts)
offset_size = calc_offset_size(encoder, v_start_ts,
data->timestamp);
if (data->timestamp <= v_start_ts)
clear_audio(encoder);
encoder->start_ts = v_start_ts;
/* use currently buffered audio instead */
if (v_start_ts < data->timestamp) {
start_from_buffer(encoder, v_start_ts);
}
} else if (!encoder->start_ts && !encoder->paired_encoder) {
encoder->start_ts = data->timestamp;
}
fail:
push_back_audio(encoder, data, size, offset_size);
profile_end(buffer_audio_name);
return success;
}
static void send_audio_data(struct obs_encoder *encoder)
{
struct encoder_frame enc_frame;
memset(&enc_frame, 0, sizeof(struct encoder_frame));
for (size_t i = 0; i < encoder->planes; i++) {
circlebuf_pop_front(&encoder->audio_input_buffer[i],
encoder->audio_output_buffer[i],
encoder->framesize_bytes);
enc_frame.data[i] = encoder->audio_output_buffer[i];
enc_frame.linesize[i] = (uint32_t)encoder->framesize_bytes;
}
enc_frame.frames = (uint32_t)encoder->framesize;
enc_frame.pts = encoder->cur_pts;
do_encode(encoder, &enc_frame);
encoder->cur_pts += encoder->framesize;
}
static const char *receive_audio_name = "receive_audio";
static void receive_audio(void *param, size_t mix_idx, struct audio_data *data)
{
profile_start(receive_audio_name);
struct obs_encoder *encoder = param;
if (!encoder->first_received) {
encoder->first_raw_ts = data->timestamp;
encoder->first_received = true;
clear_audio(encoder);
}