/
filter_pid.c
9482 lines (8489 loc) · 320 KB
/
filter_pid.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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom ParisTech 2017-2024
* All rights reserved
*
* This file is part of GPAC / filters sub-project
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terfsess of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GPAC 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "filter_session.h"
#include <gpac/constants.h>
#include <gpac/bitstream.h>
static void free_evt(GF_FilterEvent *evt);
static void pcki_del(GF_FilterPacketInstance *pcki)
{
gf_assert(pcki->pck->reference_count);
if (safe_int_dec(&pcki->pck->reference_count) == 0) {
gf_filter_packet_destroy(pcki->pck);
}
gf_free(pcki);
}
void gf_filter_pid_inst_reset(GF_FilterPidInst *pidinst)
{
gf_assert(pidinst);
while (gf_fq_count(pidinst->packets)) {
GF_FilterPacketInstance *pcki = gf_fq_pop(pidinst->packets);
pcki_del(pcki);
}
while (gf_list_count(pidinst->pck_reassembly)) {
GF_FilterPacketInstance *pcki = gf_list_pop_back(pidinst->pck_reassembly);
pcki_del(pcki);
}
}
void gf_filter_pid_inst_del(GF_FilterPidInst *pidinst)
{
gf_assert(pidinst);
gf_filter_pid_inst_reset(pidinst);
gf_fq_del(pidinst->packets, (gf_destruct_fun) pcki_del);
gf_mx_del(pidinst->pck_mx);
gf_list_del(pidinst->pck_reassembly);
if (pidinst->props) {
gf_assert(pidinst->props->reference_count);
gf_mx_p(pidinst->pid->filter->tasks_mx);
//not in parent pid, may happen when reattaching a pid inst to a different pid
//in this case do NOT delete the props
if (gf_list_find(pidinst->pid->properties, pidinst->props)>=0) {
if (safe_int_dec(&pidinst->props->reference_count) == 0) {
//see \ref gf_filter_pid_merge_properties_internal for mutex
gf_list_del_item(pidinst->pid->properties, pidinst->props);
gf_props_del(pidinst->props);
}
}
gf_mx_v(pidinst->pid->filter->tasks_mx);
}
#ifdef GPAC_ENABLE_DEBUG
if (pidinst->prop_dump) {
while (1) {
GF_PropCheck *p = gf_list_pop_back(pidinst->prop_dump);
if (!p) break;
gf_free(p);
}
gf_list_del(pidinst->prop_dump);
}
#endif
gf_free(pidinst);
}
static GF_FilterPidInst *gf_filter_pid_inst_new(GF_Filter *filter, GF_FilterPid *pid)
{
GF_FilterPidInst *pidinst;
GF_SAFEALLOC(pidinst, GF_FilterPidInst);
if (!pidinst) return NULL;
pidinst->pid = pid;
pidinst->filter = filter;
if (filter->session->use_locks) {
char szName[200];
u32 pid_idx = 1 + gf_list_find(pid->filter->output_pids, pid);
u32 dst_idx = 1 + pid->num_destinations;
snprintf(szName, 200, "F%sPid%dDest%dPackets", filter->name, pid_idx, dst_idx);
pidinst->pck_mx = gf_mx_new(szName);
}
pidinst->packets = gf_fq_new(pidinst->pck_mx);
pidinst->pck_reassembly = gf_list_new();
pidinst->last_block_ended = GF_TRUE;
return pidinst;
}
static void gf_filter_pid_check_unblock(GF_FilterPid *pid)
{
Bool unblock;
if (pid->ignore_blocking) {
return;
}
//if we are in end of stream state and done with all packets, stay blocked
if (pid->has_seen_eos && !pid->nb_buffer_unit && !pid->eos_keepalive) {
if (!pid->would_block) {
safe_int_inc(&pid->would_block);
safe_int_inc(&pid->filter->would_block);
gf_assert(pid->filter->would_block + pid->filter->num_out_pids_not_connected <= pid->filter->num_output_pids);
}
return;
}
unblock=GF_FALSE;
gf_assert(pid->playback_speed_scaler);
//we block according to the number of dispatched units (decoder output) or to the requested buffer duration
//for other streams - unblock accordingly
if (pid->max_buffer_unit) {
if (pid->nb_buffer_unit * GF_FILTER_SPEED_SCALER < pid->max_buffer_unit * pid->playback_speed_scaler) {
unblock=GF_TRUE;
}
} else if (pid->buffer_duration * GF_FILTER_SPEED_SCALER < pid->max_buffer_time * pid->playback_speed_scaler) {
unblock=GF_TRUE;
}
if (!unblock) {
return;
}
gf_mx_p(pid->filter->tasks_mx);
unblock = GF_FALSE;
//unblock pid
if (pid->would_block) {
safe_int_dec(&pid->would_block);
gf_assert(pid->filter->would_block);
safe_int_dec(&pid->filter->would_block);
gf_assert((s32)pid->filter->would_block>=0);
gf_assert(pid->filter->would_block + pid->filter->num_out_pids_not_connected <= pid->filter->num_output_pids);
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Filter %s PID %s unblocked (filter has %d blocking pids)\n", pid->pid->filter->name, pid->pid->name, pid->pid->filter->would_block));
//check filter unblock
unblock = GF_TRUE;
}
//pid was not blocking but filter is no longer scheduled (might happen in multi-threaded modes), check filter unblock
else if (!pid->filter->process_task_queued) {
unblock = GF_TRUE;
}
if (unblock && (pid->filter->would_block + pid->filter->num_out_pids_not_connected < pid->filter->num_output_pids)) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Filter %s has only %d / %d blocked pids, requesting process task (%d queued)\n", pid->filter->name, pid->filter->would_block + pid->filter->num_out_pids_not_connected, pid->filter->num_output_pids, pid->filter->process_task_queued));
//post a process task
gf_filter_post_process_task(pid->filter);
}
gf_mx_v(pid->filter->tasks_mx);
}
static void gf_filter_pid_inst_check_dependencies(GF_FilterPidInst *pidi)
{
const GF_PropertyValue *p;
u32 i, dep_id = 0;
GF_FilterPid *pid = pidi->pid;
GF_Filter *filter = pid->filter;
//check pid dependency
p = gf_filter_pid_get_property_first(pid, GF_PROP_PID_DEPENDENCY_ID);
if (p) dep_id = p->value.uint;
if (!dep_id) return;
for (i=0; i<filter->num_output_pids; i++) {
u32 j;
GF_FilterPid *a_pid = gf_list_get(filter->output_pids, i);
if (a_pid==pid) continue;
p = gf_filter_pid_get_property_first(a_pid, GF_PROP_PID_ID);
if (!p) p = gf_filter_pid_get_property_first(a_pid, GF_PROP_PID_ESID);
if (!p || (p->value.uint != dep_id)) continue;
for (j=0; j<a_pid->num_destinations; j++) {
GF_FilterPidInst *a_pidi = gf_list_get(a_pid->destinations, j);
if (a_pidi == pidi) continue;
if (! a_pidi->is_decoder_input) continue;
if (a_pidi->filter == pidi->filter) continue;
GF_LOG(GF_LOG_WARNING, GF_LOG_FILTER, ("Filter %s PID %s connected to decoder %s, but dependent stream %s connected to %s - switching pid destination\n", a_pid->filter->name, a_pid->name, a_pidi->filter->name, pidi->pid->name, pidi->filter->name));
//disconnect this pid instance from its current decoder
gf_fs_post_task(filter->session, gf_filter_pid_disconnect_task, a_pidi->filter, a_pid, "pidinst_disconnect", NULL);
//reconnect this pid instance to the new decoder
safe_int_inc(&pid->filter->out_pid_connection_pending);
gf_filter_pid_post_connect_task(pidi->filter, a_pid);
}
}
}
static void gf_filter_pid_update_caps(GF_FilterPid *pid)
{
u32 mtype=0, codecid=0;
u32 i;
const GF_PropertyValue *p;
pid->raw_media = GF_FALSE;
p = gf_filter_pid_get_property_first(pid, GF_PROP_PID_CODECID);
if (p) codecid = p->value.uint;
p = gf_filter_pid_get_property_first(pid, GF_PROP_PID_STREAM_TYPE);
if (p) mtype = p->value.uint;
Bool was_sparse = pid->is_sparse;
p = gf_filter_pid_get_property_first(pid, GF_PROP_PID_SPARSE);
if (p) {
pid->is_sparse = p->value.boolean;
} else {
u32 otype = mtype;
p = gf_filter_pid_get_property_first(pid, GF_PROP_PID_ORIG_STREAM_TYPE);
if (p) otype = p->value.uint;
switch (otype) {
case GF_STREAM_AUDIO:
case GF_STREAM_VISUAL:
case GF_STREAM_FILE:
pid->is_sparse = GF_FALSE;
break;
default:
pid->is_sparse = GF_TRUE;
break;
}
}
if (was_sparse && !pid->is_sparse)
safe_int_dec(&pid->filter->nb_sparse_pids);
else if (!was_sparse && pid->is_sparse)
safe_int_inc(&pid->filter->nb_sparse_pids);
pid->stream_type = mtype;
pid->codecid = codecid;
u32 buffer_us = pid->filter->pid_buffer_max_us ? pid->filter->pid_buffer_max_us : pid->filter->session->default_pid_buffer_max_us;
if (pid->user_max_buffer_time) {
pid->max_buffer_time = MAX(pid->user_max_buffer_time, buffer_us);
pid->max_buffer_unit = 0;
} else {
pid->max_buffer_time = buffer_us;
pid->max_buffer_unit = pid->filter->pid_buffer_max_units ? pid->filter->pid_buffer_max_units : pid->filter->session->default_pid_buffer_max_units;
}
pid->raw_media = GF_FALSE;
if (codecid!=GF_CODECID_RAW) {
gf_mx_p(pid->filter->tasks_mx);
for (i=0; i<pid->filter->num_input_pids; i++) {
u32 i_codecid=0, i_type=0;
GF_FilterPidInst *pidi = gf_list_get(pid->filter->input_pids, i);
if (!pidi->pid) continue;
p = gf_filter_pid_get_property_first(pidi->pid, GF_PROP_PID_STREAM_TYPE);
if (p) i_type = p->value.uint;
p = gf_filter_pid_get_property_first(pidi->pid, GF_PROP_PID_CODECID);
if (p) i_codecid = p->value.uint;
//same stream type but changing from raw to not raw: this is an encoder input pid
if ((mtype==i_type) && (i_codecid==GF_CODECID_RAW)) {
pidi->is_encoder_input = GF_TRUE;
}
}
gf_mx_v(pid->filter->tasks_mx);
return;
}
//output is a decoded raw stream: if some input has same type but different codecid this is a decoder
//set input buffer size
gf_mx_p(pid->filter->tasks_mx);
for (i=0; i<pid->filter->num_input_pids; i++) {
u32 i_codecid=0, i_type=0;
GF_FilterPidInst *pidi = gf_list_get(pid->filter->input_pids, i);
if (!pidi->pid) continue;
p = gf_filter_pid_get_property_first(pidi->pid, GF_PROP_PID_STREAM_TYPE);
if (p) i_type = p->value.uint;
p = gf_filter_pid_get_property_first(pidi->pid, GF_PROP_PID_CODECID);
if (p) i_codecid = p->value.uint;
//same stream type but changing format type: this is a decoder input pid, set buffer req
if ((mtype==i_type) && (codecid != i_codecid)) {
buffer_us = pid->filter->pid_decode_buffer_max_us ? pid->filter->pid_decode_buffer_max_us : pid->filter->session->decoder_pid_buffer_max_us;
//default decoder buffer
pidi->pid->max_buffer_time = MAX(pidi->pid->user_max_buffer_time, buffer_us);
pidi->pid->max_buffer_unit = 0;
//composition buffer
if (pid->filter->pid_buffer_max_units) {
pid->max_buffer_unit = pid->filter->pid_buffer_max_units;
} else if (mtype==GF_STREAM_VISUAL) {
pid->max_buffer_unit = 4;
} else if (mtype==GF_STREAM_AUDIO) {
pid->max_buffer_unit = 20;
}
if (!pidi->is_decoder_input) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Filter %s pid instance %s marked as decoder input\n", pidi->pid->filter->name, pidi->pid->name));
pidi->is_decoder_input = GF_TRUE;
safe_int_inc(&pidi->pid->nb_decoder_inputs);
if ((i_type == GF_STREAM_AUDIO) || (i_type == GF_STREAM_VISUAL))
gf_filter_pid_inst_check_dependencies(pidi);
}
}
//same media type, same codec: raw stream
else if (mtype==i_type) {
pid->raw_media = GF_TRUE;
//special case if single input pid filter, copy buffer props from input
if (pid->filter->num_input_pids==1) {
GF_FilterPidInst *pid_in = gf_list_get(pid->filter->input_pids, i);
if ((pid_in->pid->max_buffer_unit>1) || pid_in->pid->max_buffer_time) {
pid->max_buffer_unit = pid_in->pid->max_buffer_unit;
pid->filter->pid_buffer_max_units = pid_in->pid->max_buffer_unit;
pid->max_buffer_time = pid_in->pid->max_buffer_time;
pid->filter->pid_buffer_max_us = (u32) pid_in->pid->max_buffer_time;
}
//if input has a single destination, reset buffer props to default
if (pid_in->pid->num_destinations==1) {
pid_in->pid->max_buffer_unit = 1;
pid_in->pid->max_buffer_time = 0;
}
}
//if we have max_buffer_time set, don't use buffer units
if (pid->max_buffer_time)
pid->max_buffer_unit = 0;
}
//input is file, output is not and codec ID is raw, this is a raw media pid
else if ((i_type==GF_STREAM_FILE) && (mtype!=GF_STREAM_FILE) && (codecid==GF_CODECID_RAW) ) {
pid->raw_media = GF_TRUE;
}
}
//source pid, mark raw media
if (!pid->filter->num_input_pids && pid->num_destinations) {
pid->raw_media = GF_TRUE;
}
gf_mx_v(pid->filter->tasks_mx);
}
#define TASK_REQUEUE(_t) \
_t->requeue_request = GF_TRUE; \
_t->schedule_next_time = gf_sys_clock_high_res() + 50; \
void gf_filter_pid_inst_delete_task(GF_FSTask *task)
{
GF_FilterPid *pid = task->pid;
GF_FilterPidInst *pidinst = task->udta;
GF_Filter *filter = pid->filter;
Bool pid_still_alive = GF_FALSE;
gf_assert(filter);
//reset in process
if ((pidinst->filter && pidinst->discard_packets) || filter->stream_reset_pending || filter->abort_pending) {
TASK_REQUEUE(task)
return;
}
//reset PID instance buffers before checking number of output shared packets
//otherwise we may block because some of the shared packets are in the
//pid instance buffer (not consumed)
gf_filter_pid_inst_reset(pidinst);
//we still have packets out there!
if (pidinst->pid->nb_shared_packets_out) {
//special case for disconnect of fanouts, destroy even if shared packets out
if (!pid->num_destinations || ((pid->num_destinations>=1) && (gf_list_find(pid->destinations, pidinst)>=0))) {
TASK_REQUEUE(task)
return;
}
}
//WARNING at this point pidinst->filter may be destroyed
GF_LOG(GF_LOG_INFO, GF_LOG_FILTER, ("Filter %s pid instance %s destruction (%d fan-out)\n", filter->name, pid->name, pid->num_destinations));
gf_mx_p(filter->tasks_mx);
gf_list_del_item(pid->destinations, pidinst);
pid->num_destinations = gf_list_count(pid->destinations);
if (pidinst->pid->num_pidinst_del_pending) {
pidinst->pid->num_pidinst_del_pending--;
if (pidinst->pid->num_pidinst_del_pending)
pid_still_alive = GF_TRUE;
}
gf_mx_v(filter->tasks_mx);
if (pidinst->is_decoder_input) {
gf_assert(pid->nb_decoder_inputs);
safe_int_dec(&pid->nb_decoder_inputs);
}
gf_filter_pid_inst_del(pidinst);
//recompute max buf dur and nb units to update blocking state
if (pid->num_destinations) {
u32 i;
u32 nb_pck = 0;
s64 buf_dur = 0;
for (i = 0; i < pid->num_destinations; i++) {
GF_FilterPidInst *apidi = gf_list_get(pid->destinations, i);
u32 npck = gf_fq_count(apidi->packets);
if (npck > nb_pck) nb_pck = npck;
if (apidi->buffer_duration > buf_dur) buf_dur = apidi->buffer_duration;
}
pid->nb_buffer_unit = nb_pck;
pid->buffer_duration = buf_dur;
} else {
pid->nb_buffer_unit = 0;
pid->buffer_duration = 0;
}
gf_assert(pid->filter == filter);
if (pid_still_alive)
return;
//some more destinations on pid, update blocking state
if (pid->num_destinations || pid->init_task_pending) {
if (pid->would_block)
gf_filter_pid_check_unblock(pid);
else
gf_filter_pid_would_block(pid);
return;
}
gf_mx_p(filter->tasks_mx);
//still some input to the filter, cannot destroy the output pid
if (gf_list_count(filter->input_pids)) {
gf_mx_v(filter->tasks_mx);
return;
}
//no more destinations on pid, unblock if blocking
if (pid->would_block) {
gf_assert(pid->filter->would_block);
safe_int_dec(&pid->filter->would_block);
}
//we cannot remove an output pid since the filter may still check status on that pid or try to dispatch packets
//removal/destruction must come from the filter
//we only count the number of output pids that have been internally discarded by this function, and trigger filter removal if last
pid->removed = GF_TRUE;
//filter still active and has no input, check if there are no more output pids valid. If so, remove filter
if (!gf_list_count(filter->input_pids) && !filter->finalized) {
u32 i, nb_opid_rem=0;
for (i=0; i<filter->num_output_pids; i++) {
GF_FilterPid *apid = gf_list_get(filter->output_pids, i);
if (apid->removed) nb_opid_rem++;
}
if (gf_list_count(filter->output_pids)==nb_opid_rem) {
gf_filter_post_remove(filter);
}
}
gf_mx_v(filter->tasks_mx);
}
static void gf_filter_pid_inst_swap_delete(GF_Filter *filter, GF_FilterPid *pid, GF_FilterPidInst *pidinst, GF_FilterPidInst *dst_swapinst)
{
u32 i, j;
//reset PID instance buffers before checking number of output shared packets
//otherwise we may block because some of the shared packets are in the
//pid instance buffer (not consumed)
gf_filter_pid_inst_reset(pidinst);
GF_LOG(GF_LOG_INFO, GF_LOG_FILTER, ("Filter %s pid instance %s swap destruction\n", filter->name, pidinst->pid ? pidinst->pid->name : pid->name));
gf_mx_p(filter->tasks_mx);
gf_list_del_item(filter->input_pids, pidinst);
filter->num_input_pids = gf_list_count(filter->input_pids);
if (!filter->num_input_pids)
filter->single_source = NULL;
gf_mx_v(filter->tasks_mx);
gf_mx_p(pid->filter->tasks_mx);
gf_list_del_item(pid->destinations, pidinst);
pid->num_destinations = gf_list_count(pid->destinations);
gf_mx_v(pid->filter->tasks_mx);
if (pidinst->is_decoder_input) {
gf_assert(pid->nb_decoder_inputs);
safe_int_dec(&pid->nb_decoder_inputs);
}
//this pid instance is registered to destination filter for chain reconfigure, don't discard it
if (filter->detached_pid_inst && (gf_list_find(filter->detached_pid_inst, pidinst)>=0) )
return;
gf_filter_pid_inst_del(pidinst);
if (filter->num_input_pids) return;
//we still have other pid instances registered for chain reconfigure, don't discard the filter
if (filter->detached_pid_inst) return;
//filter no longer used, disconnect chain
for (i=0; i<filter->num_output_pids; i++) {
GF_FilterPid *a_pid = gf_list_get(filter->output_pids, i);
for (j=0; j<a_pid->num_destinations; j++) {
GF_FilterPidInst *a_pidi = gf_list_get(a_pid->destinations, j);
if (a_pidi == dst_swapinst) continue;
gf_filter_pid_inst_swap_delete(a_pidi->filter, a_pid, a_pidi, dst_swapinst);
}
}
filter->swap_pidinst_dst = NULL;
filter->swap_pidinst_src = NULL;
gf_filter_post_remove(filter);
}
static void gf_filter_pid_inst_swap_delete_task(GF_FSTask *task)
{
GF_FilterPidInst *pidinst = task->udta;
GF_Filter *filter = pidinst->filter;
GF_FilterPid *pid = task->pid ? task->pid : pidinst->pid;
GF_FilterPidInst *dst_swapinst = pidinst->filter->swap_pidinst_dst;
//reset in process
if ((pidinst->filter && pidinst->discard_packets)
|| filter->stream_reset_pending
|| filter->nb_shared_packets_out
) {
TASK_REQUEUE(task)
return;
}
if (pidinst->filter)
pidinst->filter->swap_pidinst_dst = NULL;
gf_filter_pid_inst_swap_delete(filter, pid, pidinst, dst_swapinst);
}
static void gf_filter_pid_inst_swap(GF_Filter *filter, GF_FilterPidInst *dst)
{
GF_PropertyMap *prev_dst_props;
u32 nb_pck_transfer=0;
GF_FilterPidInst *src = filter->swap_pidinst_src;
if (!src) src = filter->swap_pidinst_dst;
if (src) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Filter %s swaping PID %s to PID %s\n", filter->name, src->pid->name, dst->pid->name));
}
if (filter->swap_needs_init) {
//we are in detach state, the packet queue of the old PID is never read
if (!filter->swap_pidinst_dst || !filter->swap_pidinst_dst->detach_pending) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Filter %s swaping PID %s failed\n", filter->name, src->pid->name));
return;
}
//we are in pending state, the origin of the old PID is never dispatching
// gf_assert(dst->pid && dst->pid->filter && dst->pid->filter->out_pid_connection_pending);
//we can therefore swap the packet queues safely and other important info
}
//otherwise we actually swap the pid instance on the same PID
else {
gf_mx_p(dst->pid->filter->tasks_mx);
if (src)
gf_list_del_item(dst->pid->destinations, src);
if (gf_list_find(dst->pid->destinations, dst)<0)
gf_list_add(dst->pid->destinations, dst);
dst->pid->num_destinations = gf_list_count(dst->pid->destinations);
if (gf_list_find(dst->filter->input_pids, dst)<0) {
gf_list_add(dst->filter->input_pids, dst);
dst->filter->num_input_pids = gf_list_count(dst->filter->input_pids);
if (dst->filter->num_input_pids==1) {
dst->filter->single_source = dst->pid->filter;
} else if (dst->filter->single_source != dst->pid->filter) {
dst->filter->single_source = NULL;
}
}
gf_mx_v(dst->pid->filter->tasks_mx);
}
if (src) {
GF_FilterPacketInstance *pcki;
while (1) {
pcki = gf_fq_pop(src->packets);
if (!pcki) break;
gf_assert(src->filter->pending_packets);
safe_int_dec(&src->filter->pending_packets);
if (pcki->pck->info.flags & GF_PCKF_FORCE_MAIN) {
gf_assert(src->filter->nb_main_thread_forced);
safe_int_dec(&src->filter->nb_main_thread_forced);
safe_int_inc(&dst->filter->nb_main_thread_forced);
}
pcki->pid = dst;
gf_fq_add(dst->packets, pcki);
safe_int_inc(&dst->filter->pending_packets);
nb_pck_transfer++;
}
if (src->requires_full_data_block && gf_list_count(src->pck_reassembly)) {
dst->requires_full_data_block = src->requires_full_data_block;
dst->last_block_ended = src->last_block_ended;
dst->first_block_started = src->first_block_started;
if (!dst->pck_reassembly) dst->pck_reassembly = gf_list_new();
while (gf_list_count(src->pck_reassembly)) {
pcki = gf_list_pop_front(src->pck_reassembly);
pcki->pid = dst;
gf_list_add(dst->pck_reassembly, pcki);
}
}
//copy over state
dst->is_end_of_stream = src->is_end_of_stream;
dst->nb_eos_signaled = src->nb_eos_signaled;
dst->buffer_duration = src->buffer_duration;
dst->nb_clocks_signaled = src->nb_clocks_signaled;
//switch previous src property map to this new pid (this avoids rewriting props of already dispatched packets)
//it may happen that we already have props on dest, due to configure of the pid
//use the old props as new ones and merge the previous props of dst in the new props
prev_dst_props = dst->props;
dst->props = src->props;
dst->force_reconfig = GF_TRUE;
src->force_reconfig = GF_TRUE;
src->props = NULL;
if (prev_dst_props) {
if (dst->props) {
gf_props_merge_property(dst->props, prev_dst_props, NULL, NULL);
gf_assert(prev_dst_props->reference_count);
if (safe_int_dec(&prev_dst_props->reference_count) == 0) {
gf_props_del(prev_dst_props);
}
} else {
dst->props = prev_dst_props;
}
}
if (nb_pck_transfer && !dst->filter->process_task_queued) {
gf_filter_post_process_task(dst->filter);
}
}
src = filter->swap_pidinst_dst;
if (src) {
if (filter->swap_needs_init) {
//exit out special handling of the pid since we are ready to detach
gf_assert(src->filter->stream_reset_pending);
safe_int_dec(&src->filter->stream_reset_pending);
//post detach task, we will reset the swap_pidinst only once truly deconnected from filter
safe_int_inc(&src->pid->filter->detach_pid_tasks_pending);
safe_int_inc(&filter->detach_pid_tasks_pending);
gf_fs_post_task(filter->session, gf_filter_pid_detach_task_no_flush, src->filter, src->pid, "pidinst_detach", filter);
} else {
GF_Filter *src_filter = src->filter;
gf_assert(!src->filter->sticky);
gf_assert(src->filter->num_input_pids==1);
gf_mx_p(src_filter->tasks_mx);
gf_list_del_item(src_filter->input_pids, src);
src_filter->num_input_pids = gf_list_count(src_filter->input_pids);
if (!src_filter->num_input_pids)
src_filter->single_source = NULL;
gf_mx_v(src_filter->tasks_mx);
gf_list_del_item(src->pid->destinations, src);
src->pid->num_destinations = gf_list_count(src->pid->destinations);
gf_filter_pid_inst_del(src);
filter->swap_pidinst_dst = NULL;
filter->swap_pidinst_src = NULL;
gf_filter_post_remove(src_filter);
}
}
if (filter->swap_pidinst_src) {
src = filter->swap_pidinst_src;
gf_assert(!src->filter->swap_pidinst_dst);
src->filter->swap_pidinst_dst = filter->swap_pidinst_dst;
src->filter->swap_pending = GF_TRUE;
gf_fs_post_task(filter->session, gf_filter_pid_inst_swap_delete_task, src->filter, src->pid, "pid_inst_delete", src);
}
}
//check all packets scheduled on main thread, unflag dest filter nb_main_thread_forced and set dest to NULL
//we must do that because the packets may be destroyed after the pid instance is detached
//so the destination filter would be NULL by then
void gf_filter_instance_detach_pid(GF_FilterPidInst *pidinst)
{
u32 i, count;
if (!pidinst->filter) return;
count = gf_fq_count(pidinst->packets);
for (i=0; i<count; i++) {
GF_FilterPacketInstance *pcki = gf_fq_get(pidinst->packets, i);
if (!pcki) break;
if (pcki->pck->info.flags & GF_PCKF_FORCE_MAIN) {
gf_assert(pidinst->filter->nb_main_thread_forced);
safe_int_dec(&pidinst->filter->nb_main_thread_forced);
}
}
count = gf_list_count(pidinst->pck_reassembly);
for (i=0; i<count; i++) {
GF_FilterPacketInstance *pcki = gf_list_get(pidinst->pck_reassembly, i);
if (!pcki) break;
if (pcki->pck->info.flags & GF_PCKF_FORCE_MAIN) {
gf_assert(pidinst->filter->nb_main_thread_forced);
safe_int_dec(&pidinst->filter->nb_main_thread_forced);
}
}
pidinst->filter = NULL;
}
void task_canceled(GF_FSTask *task)
{
if (task->class_type==TASK_TYPE_EVENT) {
GF_FilterEvent *evt = task->udta;
free_evt(evt);
}
else if (task->class_type==TASK_TYPE_SETUP) {
gf_free(task->udta);
}
else if (task->class_type==TASK_TYPE_USER) {
gf_free(task->udta);
gf_free((char *)task->log_name);
task->log_name = NULL;
}
}
typedef enum {
GF_PID_CONF_CONNECT,
GF_PID_CONF_RECONFIG,
GF_PID_CONF_REMOVE,
} GF_PidConnectType;
static void gf_filter_pid_connect_failure(GF_FilterPid *pid)
{
GF_FilterEvent evt;
GF_FEVT_INIT(evt, GF_FEVT_PLAY, pid);
gf_filter_pid_send_event_internal(pid, &evt, GF_TRUE);
GF_FEVT_INIT(evt, GF_FEVT_STOP, pid);
gf_filter_pid_send_event_internal(pid, &evt, GF_TRUE);
gf_filter_pid_set_eos(pid);
if (pid->filter->freg->process_event) {
GF_FEVT_INIT(evt, GF_FEVT_CONNECT_FAIL, pid);
gf_filter_pid_send_event_internal(pid, &evt, GF_TRUE);
}
}
static GF_Err gf_filter_pid_configure(GF_Filter *filter, GF_FilterPid *pid, GF_PidConnectType ctype)
{
u32 i, count;
GF_Err e;
Bool is_pid_swap = GF_FALSE;
Bool refire_events = GF_FALSE;
Bool new_pid_inst=GF_FALSE;
Bool remove_filter=GF_FALSE;
GF_FilterPidInst *pidinst=NULL;
GF_Filter *alias_orig = NULL;
if (filter->multi_sink_target) {
alias_orig = filter;
filter = filter->multi_sink_target;
}
gf_assert(filter->freg->configure_pid);
if (filter->finalized) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Trying to configure PID %s in finalized filter %s\n", pid->name, filter->name));
if (ctype==GF_PID_CONF_CONNECT) {
gf_assert(pid->filter->out_pid_connection_pending);
safe_int_dec(&pid->filter->out_pid_connection_pending);
}
return GF_SERVICE_ERROR;
}
if (filter->detached_pid_inst) {
count = gf_list_count(filter->detached_pid_inst);
for (i=0; i<count; i++) {
pidinst = gf_list_get(filter->detached_pid_inst, i);
if (pidinst->filter==filter) {
gf_list_rem(filter->detached_pid_inst, i);
//reattach new filter and pid
pidinst->filter = filter;
pidinst->pid = pid;
gf_assert(!pidinst->props);
//and treat as new pid inst
if (ctype == GF_PID_CONF_CONNECT) {
new_pid_inst=GF_TRUE;
if (!pid->filter->nb_pids_playing && (pidinst->is_playing || pidinst->is_paused))
refire_events = GF_TRUE;
}
gf_assert(pidinst->detach_pending);
safe_int_dec(&pidinst->detach_pending);
//revert temp sticky flag
if (filter->sticky == 2)
filter->sticky = 0;
break;
}
pidinst=NULL;
}
if (! gf_list_count(filter->detached_pid_inst)) {
gf_list_del(filter->detached_pid_inst);
filter->detached_pid_inst = NULL;
}
}
if (!pidinst) {
count = pid->num_destinations;
for (i=0; i<count; i++) {
pidinst = gf_list_get(pid->destinations, i);
if (pidinst->filter==filter) {
break;
}
pidinst=NULL;
}
}
//first connection of this PID to this filter
if (!pidinst) {
if (ctype != GF_PID_CONF_CONNECT) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Trying to disconnect PID %s not present in filter %s inputs\n", pid->name, filter->name));
return GF_SERVICE_ERROR;
}
pidinst = gf_filter_pid_inst_new(filter, pid);
new_pid_inst=GF_TRUE;
}
if (!pidinst->alias_orig)
pidinst->alias_orig = alias_orig;
//if new, add the PID to input/output before calling configure
if (new_pid_inst) {
if (!pidinst) return GF_OUT_OF_MEM;
gf_mx_p(pid->filter->tasks_mx);
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Registering %s:%s as destination for %s:%s\n", pid->filter->name, pid->name, pidinst->filter->name, pidinst->pid->name));
gf_list_add(pid->destinations, pidinst);
pid->num_destinations = gf_list_count(pid->destinations);
gf_mx_v(pid->filter->tasks_mx);
gf_mx_p(filter->tasks_mx);
if (!filter->input_pids) filter->input_pids = gf_list_new();
gf_list_add(filter->input_pids, pidinst);
filter->num_input_pids = gf_list_count(filter->input_pids);
if (filter->num_input_pids==1) {
filter->single_source = pidinst->pid->filter;
} else if (filter->single_source != pidinst->pid->filter) {
filter->single_source = NULL;
}
gf_mx_v(filter->tasks_mx);
//new connection, update caps in case we have events using caps (buffer req) being sent
//while processing the configure (they would be dispatched on the source filter, not the dest one being
//processed here)
gf_filter_pid_update_caps(pid);
}
//we are swaping a PID instance (dyn insert of a filter), do it before reconnecting
//in order to have properties in place
//TODO: handle error case, we might need to re-switch the pid inst!
if (filter->swap_pending) {
gf_filter_pid_inst_swap(filter, pidinst);
filter->swap_pending = GF_FALSE;
is_pid_swap = GF_TRUE;
}
filter->in_connect_err = GF_EOS;
//commented out: audio thread may be pulling packets out of the pid but not in the compositor:process, which
//could be called for video at the same time...
#if 0
FSESS_CHECK_THREAD(filter)
#endif
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Filter %s PID %s reconfigure\n", pidinst->filter->name, pidinst->pid->name));
e = filter->freg->configure_pid(filter, (GF_FilterPid*) pidinst, (ctype==GF_PID_CONF_REMOVE) ? GF_TRUE : GF_FALSE);
#ifdef GPAC_MEMORY_TRACKING
if (filter->session->check_allocs) {
if (filter->nb_consecutive_process >= filter->max_nb_consecutive_process) {
filter->max_nb_consecutive_process = filter->nb_consecutive_process;
filter->max_nb_process = filter->nb_process_since_reset;
filter->max_stats_nb_alloc = filter->stats_nb_alloc;
filter->max_stats_nb_calloc = filter->stats_nb_calloc;
filter->max_stats_nb_realloc = filter->stats_nb_realloc;
filter->max_stats_nb_free = filter->stats_nb_free;
}
filter->stats_mem_allocated = 0;
filter->stats_nb_alloc = filter->stats_nb_realloc = filter->stats_nb_free = 0;
filter->nb_process_since_reset = filter->nb_consecutive_process = 0;
}
#endif
if ((e==GF_OK) && (filter->in_connect_err<GF_OK))
e = filter->in_connect_err;
filter->in_connect_err = GF_OK;
if (e==GF_OK) {
//if new, register the new pid instance, and the source pid as input to this filter
if (new_pid_inst) {
GF_LOG(GF_LOG_INFO, GF_LOG_FILTER, ("Filter %s (%p) PID %s (%p) (%d fan-out) connected to filter %s (%p)\n", pid->filter->name, pid->filter, pid->name, pid, pid->num_destinations, filter->name, filter));
}
//reset blacklist on source if connect OK - this is required when reconfiguring multiple times to the same filter, eg
//jpeg->raw->jpeg, the first jpeg->raw would blacklist jpeg dec from source, preventing resolution to work at raw->jpeg switch
gf_list_reset(pidinst->pid->filter->blacklisted);
}
//failure on reconfigure, try reloading a filter chain
else if ((ctype==GF_PID_CONF_RECONFIG) && (e != GF_FILTER_NOT_SUPPORTED)) {
//mark pid as end of stream to let filter flush
pidinst->is_end_of_stream = GF_TRUE;
if (e==GF_BAD_PARAM) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Failed to reconfigure PID %s:%s in filter %s: %s\n", pid->filter->name, pid->name, filter->name, gf_error_to_string(e) ));
filter->session->last_connect_error = e;
} else {
GF_LOG(GF_LOG_INFO, GF_LOG_FILTER, ("Failed to reconfigure PID %s:%s in filter %s: %s, reloading filter graph\n", pid->filter->name, pid->name, filter->name, gf_error_to_string(e) ));
gf_list_add(pid->filter->blacklisted, (void *) filter->freg);
gf_filter_relink_dst(pidinst, e);
}
} else {
//error, remove from input and output
gf_mx_p(filter->tasks_mx);
gf_list_del_item(filter->input_pids, pidinst);
filter->num_input_pids = gf_list_count(filter->input_pids);
if (!filter->num_input_pids)
filter->single_source = NULL;
filter->freg->configure_pid(filter, (GF_FilterPid *) pidinst, GF_TRUE);
gf_mx_v(filter->tasks_mx);
gf_mx_p(pidinst->pid->filter->tasks_mx);
gf_list_del_item(pidinst->pid->destinations, pidinst);
pidinst->pid->num_destinations = gf_list_count(pidinst->pid->destinations);
//detach filter from pid instance
gf_filter_instance_detach_pid(pidinst);
gf_mx_v(pidinst->pid->filter->tasks_mx);
//if connect and error, direct delete of pid
if (new_pid_inst) {
gf_mx_p(pid->filter->tasks_mx);
gf_list_del_item(pid->destinations, pidinst);
pid->num_destinations = gf_list_count(pid->destinations);
//cancel all tasks targeting this pid
gf_mx_p(pid->filter->tasks_mx);
count = gf_fq_count(pid->filter->tasks);
for (i=0; i<count; i++) {
GF_FSTask *t = gf_fq_get(pid->filter->tasks, i);
if (t->pid == (GF_FilterPid *) pidinst) {
t->run_task = task_canceled;
}
}
gf_mx_v(pid->filter->tasks_mx);
//destroy pid instance
gf_filter_pid_inst_del(pidinst);
pidinst = NULL;
gf_mx_v(pid->filter->tasks_mx);
}
if (e==GF_REQUIRES_NEW_INSTANCE) {
//TODO: copy over args from current filter
//we already created a clone for a new instance, forward the task to the clone.
//This allows solving fan-in connections requiring new instances (tileagg, scalable dec)
//which cannot be solved at graph resolution stage
//we disable this for non fan-in cases
if (filter->max_extra_pids && filter->cloned_instance) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Reuse cloned filter %s for pid %s\n", filter->name, pid->name));
gf_filter_pid_post_connect_task(filter->cloned_instance, pid);
return GF_OK;
}
GF_Filter *new_filter = gf_filter_clone(filter, pid->filter);
if (new_filter) {
if (filter->max_extra_pids)
filter->cloned_instance = new_filter;
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Clone filter %s, new instance for pid %s\n", filter->name, pid->name));
gf_filter_pid_post_connect_task(new_filter, pid);
return GF_OK;
} else {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Failed to clone filter %s\n", filter->name));