-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
MultiAudioNode.cpp
2388 lines (1945 loc) · 66.9 KB
/
MultiAudioNode.cpp
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) 2002, 2003 Jerome Duval (jerome.duval@free.fr)
* Distributed under the terms of the MIT License.
*/
//! Multi-audio replacement media addon for BeOS
#include "MultiAudioNode.h"
#include <stdio.h>
#include <string.h>
#include <Autolock.h>
#include <Buffer.h>
#include <BufferGroup.h>
#include <Catalog.h>
#include <ParameterWeb.h>
#include <String.h>
#include <Referenceable.h>
#include "MultiAudioUtility.h"
#ifdef DEBUG
# define PRINTING
#endif
#include "debug.h"
#include "Resampler.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MultiAudio"
#define PARAMETER_ID_INPUT_FREQUENCY 1
#define PARAMETER_ID_OUTPUT_FREQUENCY 2
//This represent an hardware output
class node_input {
public:
node_input(media_input& input, media_format format);
~node_input();
int32 fChannelId;
media_input fInput;
media_format fPreferredFormat;
media_format fFormat;
volatile uint32 fBufferCycle;
multi_buffer_info fOldBufferInfo;
BBuffer* fBuffer;
Resampler *fResampler;
};
//This represent an hardware input
class node_output {
public:
node_output(media_output& output, media_format format);
~node_output();
int32 fChannelId;
media_output fOutput;
media_format fPreferredFormat;
media_format fFormat;
BBufferGroup* fBufferGroup;
bool fOutputEnabled;
uint64 fSamplesSent;
volatile uint32 fBufferCycle;
multi_buffer_info fOldBufferInfo;
Resampler *fResampler;
};
struct FrameRateChangeCookie : public BReferenceable {
float oldFrameRate;
uint32 id;
};
struct sample_rate_info {
uint32 multiAudioRate;
const char* name;
};
static const sample_rate_info kSampleRateInfos[] = {
{B_SR_8000, "8000"},
{B_SR_11025, "11025"},
{B_SR_12000, "12000"},
{B_SR_16000, "16000"},
{B_SR_22050, "22050"},
{B_SR_24000, "24000"},
{B_SR_32000, "32000"},
{B_SR_44100, "44100"},
{B_SR_48000, "48000"},
{B_SR_64000, "64000"},
{B_SR_88200, "88200"},
{B_SR_96000, "96000"},
{B_SR_176400, "176400"},
{B_SR_192000, "192000"},
{B_SR_384000, "384000"},
{B_SR_1536000, "1536000"},
{}
};
const char* kMultiControlString[] = {
"NAME IS ATTACHED",
B_TRANSLATE("Output"), B_TRANSLATE("Input"), B_TRANSLATE("Setup"),
B_TRANSLATE("Tone control"), B_TRANSLATE("Extended Setup"),
B_TRANSLATE("Enhanced Setup"), B_TRANSLATE("Master"), B_TRANSLATE("Beep"),
B_TRANSLATE("Phone"), B_TRANSLATE("Mic"), B_TRANSLATE("Line"),
B_TRANSLATE("CD"), B_TRANSLATE("Video"), B_TRANSLATE("Aux"),
B_TRANSLATE("Wave"), B_TRANSLATE("Gain"), B_TRANSLATE("Level"),
B_TRANSLATE("Volume"), B_TRANSLATE("Mute"), B_TRANSLATE("Enable"),
B_TRANSLATE("Stereo mix"), B_TRANSLATE("Mono mix"),
B_TRANSLATE("Output stereo mix"), B_TRANSLATE("Output mono mix"),
B_TRANSLATE("Output bass"), B_TRANSLATE("Output treble"),
B_TRANSLATE("Output 3D center"), B_TRANSLATE("Output 3D depth"),
B_TRANSLATE("Headphones"), B_TRANSLATE("SPDIF")
};
// #pragma mark -
node_input::node_input(media_input& input, media_format format)
{
CALLED();
fInput = input;
fPreferredFormat = format;
fBufferCycle = 1;
fBuffer = NULL;
fResampler = NULL;
}
node_input::~node_input()
{
CALLED();
}
// #pragma mark -
node_output::node_output(media_output& output, media_format format)
:
fBufferGroup(NULL),
fOutputEnabled(true)
{
CALLED();
fOutput = output;
fPreferredFormat = format;
fBufferCycle = 1;
fResampler = NULL;
}
node_output::~node_output()
{
CALLED();
}
// #pragma mark -
MultiAudioNode::MultiAudioNode(BMediaAddOn* addon, const char* name,
MultiAudioDevice* device, int32 internalID, BMessage* config)
: BMediaNode(name), BBufferConsumer(B_MEDIA_RAW_AUDIO),
BBufferProducer(B_MEDIA_RAW_AUDIO),
fBufferLock("multi audio buffers"),
fThread(-1),
fDevice(device),
fTimeSourceStarted(false),
fWeb(NULL),
fConfig()
{
CALLED();
fInitStatus = B_NO_INIT;
if (!device)
return;
fAddOn = addon;
fId = internalID;
AddNodeKind(B_PHYSICAL_OUTPUT);
AddNodeKind(B_PHYSICAL_INPUT);
// initialize our preferred format objects
memset(&fOutputPreferredFormat, 0, sizeof(fOutputPreferredFormat)); // set everything to wildcard first
fOutputPreferredFormat.type = B_MEDIA_RAW_AUDIO;
fOutputPreferredFormat.u.raw_audio.format = MultiAudio::convert_to_media_format(fDevice->FormatInfo().output.format);
fOutputPreferredFormat.u.raw_audio.valid_bits = MultiAudio::convert_to_valid_bits(fDevice->FormatInfo().output.format);
fOutputPreferredFormat.u.raw_audio.channel_count = 2;
fOutputPreferredFormat.u.raw_audio.frame_rate = MultiAudio::convert_to_sample_rate(fDevice->FormatInfo().output.rate); // measured in Hertz
fOutputPreferredFormat.u.raw_audio.byte_order = B_MEDIA_HOST_ENDIAN;
// we'll use the consumer's preferred buffer size, if any
fOutputPreferredFormat.u.raw_audio.buffer_size = fDevice->BufferList().return_playback_buffer_size
* (fOutputPreferredFormat.u.raw_audio.format & media_raw_audio_format::B_AUDIO_SIZE_MASK)
* fOutputPreferredFormat.u.raw_audio.channel_count;
// initialize our preferred format objects
memset(&fInputPreferredFormat, 0, sizeof(fInputPreferredFormat)); // set everything to wildcard first
fInputPreferredFormat.type = B_MEDIA_RAW_AUDIO;
fInputPreferredFormat.u.raw_audio.format = MultiAudio::convert_to_media_format(fDevice->FormatInfo().input.format);
fInputPreferredFormat.u.raw_audio.valid_bits = MultiAudio::convert_to_valid_bits(fDevice->FormatInfo().input.format);
fInputPreferredFormat.u.raw_audio.channel_count = 2;
fInputPreferredFormat.u.raw_audio.frame_rate = MultiAudio::convert_to_sample_rate(fDevice->FormatInfo().input.rate); // measured in Hertz
fInputPreferredFormat.u.raw_audio.byte_order = B_MEDIA_HOST_ENDIAN;
// we'll use the consumer's preferred buffer size, if any
fInputPreferredFormat.u.raw_audio.buffer_size = fDevice->BufferList().return_record_buffer_size
* (fInputPreferredFormat.u.raw_audio.format & media_raw_audio_format::B_AUDIO_SIZE_MASK)
* fInputPreferredFormat.u.raw_audio.channel_count;
if (config != NULL) {
fConfig = *config;
PRINT_OBJECT(*config);
}
fInitStatus = B_OK;
}
MultiAudioNode::~MultiAudioNode()
{
CALLED();
fAddOn->GetConfigurationFor(this, NULL);
_StopOutputThread();
BMediaEventLooper::Quit();
fWeb = NULL;
}
status_t
MultiAudioNode::InitCheck() const
{
CALLED();
return fInitStatus;
}
void
MultiAudioNode::GetFlavor(flavor_info* info, int32 id)
{
CALLED();
if (info == NULL)
return;
info->flavor_flags = 0;
info->possible_count = 1; // one flavor at a time
info->in_format_count = 0; // no inputs
info->in_formats = 0;
info->out_format_count = 0; // no outputs
info->out_formats = 0;
info->internal_id = id;
info->name = (char*)"MultiAudioNode Node";
info->info = (char*)"The MultiAudioNode node outputs to multi_audio "
"drivers.";
info->kinds = B_BUFFER_CONSUMER | B_BUFFER_PRODUCER | B_TIME_SOURCE
| B_PHYSICAL_OUTPUT | B_PHYSICAL_INPUT | B_CONTROLLABLE;
info->in_format_count = 1; // 1 input
media_format* inFormats = new media_format[info->in_format_count];
GetFormat(&inFormats[0]);
info->in_formats = inFormats;
info->out_format_count = 1; // 1 output
media_format* outFormats = new media_format[info->out_format_count];
GetFormat(&outFormats[0]);
info->out_formats = outFormats;
}
void
MultiAudioNode::GetFormat(media_format* format)
{
CALLED();
if (format == NULL)
return;
format->type = B_MEDIA_RAW_AUDIO;
format->require_flags = B_MEDIA_MAUI_UNDEFINED_FLAGS;
format->deny_flags = B_MEDIA_MAUI_UNDEFINED_FLAGS;
format->u.raw_audio = media_raw_audio_format::wildcard;
}
//#pragma mark - BMediaNode
BMediaAddOn*
MultiAudioNode::AddOn(int32* _internalID) const
{
CALLED();
// BeBook says this only gets called if we were in an add-on.
if (fAddOn != 0 && _internalID != NULL)
*_internalID = fId;
return fAddOn;
}
void
MultiAudioNode::Preroll()
{
CALLED();
// XXX:Performance opportunity
BMediaNode::Preroll();
}
status_t
MultiAudioNode::HandleMessage(int32 message, const void* data, size_t size)
{
CALLED();
return B_ERROR;
}
void
MultiAudioNode::NodeRegistered()
{
CALLED();
if (fInitStatus != B_OK) {
ReportError(B_NODE_IN_DISTRESS);
return;
}
SetPriority(B_REAL_TIME_PRIORITY);
Run();
node_input *currentInput = NULL;
int32 currentId = 0;
for (int32 i = 0; i < fDevice->Description().output_channel_count; i++) {
if (currentInput == NULL
|| (fDevice->Description().channels[i].designations & B_CHANNEL_MONO_BUS)
|| (fDevice->Description().channels[currentId].designations & B_CHANNEL_STEREO_BUS
&& ( fDevice->Description().channels[i].designations & B_CHANNEL_LEFT ||
!(fDevice->Description().channels[i].designations & B_CHANNEL_STEREO_BUS)))
|| (fDevice->Description().channels[currentId].designations & B_CHANNEL_SURROUND_BUS
&& ( fDevice->Description().channels[i].designations & B_CHANNEL_LEFT ||
!(fDevice->Description().channels[i].designations & B_CHANNEL_SURROUND_BUS)))
) {
PRINT(("NodeRegistered() : creating an input for %li\n", i));
PRINT(("%ld\t%d\t0x%lx\t0x%lx\n",
fDevice->Description().channels[i].channel_id,
fDevice->Description().channels[i].kind,
fDevice->Description().channels[i].designations,
fDevice->Description().channels[i].connectors));
media_input* input = new media_input;
input->format = fOutputPreferredFormat;
input->destination.port = ControlPort();
input->destination.id = fInputs.CountItems();
input->node = Node();
sprintf(input->name, "output %ld", input->destination.id);
currentInput = new node_input(*input, fOutputPreferredFormat);
currentInput->fPreferredFormat.u.raw_audio.channel_count = 1;
currentInput->fInput.format = currentInput->fPreferredFormat;
delete currentInput->fResampler;
currentInput->fResampler = new
Resampler(currentInput->fPreferredFormat.AudioFormat(),
fOutputPreferredFormat.AudioFormat());
currentInput->fChannelId = fDevice->Description().channels[i].channel_id;
fInputs.AddItem(currentInput);
currentId = i;
} else {
PRINT(("NodeRegistered() : adding a channel\n"));
currentInput->fPreferredFormat.u.raw_audio.channel_count++;
currentInput->fInput.format = currentInput->fPreferredFormat;
}
currentInput->fInput.format.u.raw_audio.format = media_raw_audio_format::wildcard.format;
}
node_output *currentOutput = NULL;
currentId = 0;
for (int32 i = fDevice->Description().output_channel_count;
i < fDevice->Description().output_channel_count
+ fDevice->Description().input_channel_count; i++) {
if (currentOutput == NULL
|| (fDevice->Description().channels[i].designations & B_CHANNEL_MONO_BUS)
|| (fDevice->Description().channels[currentId].designations & B_CHANNEL_STEREO_BUS
&& ( fDevice->Description().channels[i].designations & B_CHANNEL_LEFT ||
!(fDevice->Description().channels[i].designations & B_CHANNEL_STEREO_BUS)))
|| (fDevice->Description().channels[currentId].designations & B_CHANNEL_SURROUND_BUS
&& ( fDevice->Description().channels[i].designations & B_CHANNEL_LEFT ||
!(fDevice->Description().channels[i].designations & B_CHANNEL_SURROUND_BUS)))
) {
PRINT(("NodeRegistered() : creating an output for %li\n", i));
PRINT(("%ld\t%d\t0x%lx\t0x%lx\n",fDevice->Description().channels[i].channel_id,
fDevice->Description().channels[i].kind,
fDevice->Description().channels[i].designations,
fDevice->Description().channels[i].connectors));
media_output *output = new media_output;
output->format = fInputPreferredFormat;
output->destination = media_destination::null;
output->source.port = ControlPort();
output->source.id = fOutputs.CountItems();
output->node = Node();
sprintf(output->name, "input %ld", output->source.id);
currentOutput = new node_output(*output, fInputPreferredFormat);
currentOutput->fPreferredFormat.u.raw_audio.channel_count = 1;
currentOutput->fOutput.format = currentOutput->fPreferredFormat;
delete currentOutput->fResampler;
currentOutput->fResampler = new
Resampler(fInputPreferredFormat.AudioFormat(),
currentOutput->fPreferredFormat.AudioFormat());
currentOutput->fChannelId = fDevice->Description().channels[i].channel_id;
fOutputs.AddItem(currentOutput);
currentId = i;
} else {
PRINT(("NodeRegistered() : adding a channel\n"));
currentOutput->fPreferredFormat.u.raw_audio.channel_count++;
currentOutput->fOutput.format = currentOutput->fPreferredFormat;
}
}
// Set up our parameter web
fWeb = MakeParameterWeb();
SetParameterWeb(fWeb);
// Apply configuration
#ifdef PRINTING
bigtime_t start = system_time();
#endif
int32 index = 0;
int32 parameterID = 0;
const void *data;
ssize_t size;
while (fConfig.FindInt32("parameterID", index, ¶meterID) == B_OK) {
if (fConfig.FindData("parameterData", B_RAW_TYPE, index, &data, &size)
== B_OK) {
SetParameterValue(parameterID, TimeSource()->Now(), data, size);
}
index++;
}
PRINT(("apply configuration in : %Ld\n", system_time() - start));
}
status_t
MultiAudioNode::RequestCompleted(const media_request_info& info)
{
CALLED();
if (info.what != media_request_info::B_REQUEST_FORMAT_CHANGE)
return B_OK;
FrameRateChangeCookie* cookie
= (FrameRateChangeCookie*)info.user_data;
if (cookie == NULL)
return B_OK;
BReference<FrameRateChangeCookie> cookieReference(cookie, true);
// if the request failed, we reset the frame rate
if (info.status != B_OK) {
if (cookie->id == PARAMETER_ID_INPUT_FREQUENCY) {
_SetNodeInputFrameRate(cookie->oldFrameRate);
if (fDevice->Description().output_rates & B_SR_SAME_AS_INPUT)
_SetNodeOutputFrameRate(cookie->oldFrameRate);
} else if (cookie->id == PARAMETER_ID_OUTPUT_FREQUENCY)
_SetNodeOutputFrameRate(cookie->oldFrameRate);
// TODO: If we have multiple connections, we should request to change
// the format back!
}
return B_OK;
}
void
MultiAudioNode::SetTimeSource(BTimeSource* timeSource)
{
CALLED();
}
// #pragma mark - BBufferConsumer
status_t
MultiAudioNode::AcceptFormat(const media_destination& dest,
media_format* format)
{
// Check to make sure the format is okay, then remove
// any wildcards corresponding to our requirements.
CALLED();
if (format == NULL)
return B_BAD_VALUE;
if (format->type != B_MEDIA_RAW_AUDIO)
return B_MEDIA_BAD_FORMAT;
node_input *channel = _FindInput(dest);
if (channel == NULL)
return B_MEDIA_BAD_DESTINATION;
/* media_format * myFormat = GetFormat();
fprintf(stderr,"proposed format: ");
print_media_format(format);
fprintf(stderr,"\n");
fprintf(stderr,"my format: ");
print_media_format(myFormat);
fprintf(stderr,"\n");*/
// Be's format_is_compatible doesn't work.
// if (!format_is_compatible(*format,*myFormat)) {
channel->fFormat = channel->fPreferredFormat;
/*if(format->u.raw_audio.format == media_raw_audio_format::B_AUDIO_FLOAT
&& channel->fPreferredFormat.u.raw_audio.format == media_raw_audio_format::B_AUDIO_SHORT)
format->u.raw_audio.format = media_raw_audio_format::B_AUDIO_FLOAT;
else*/
format->u.raw_audio.format = channel->fPreferredFormat.u.raw_audio.format;
format->u.raw_audio.valid_bits = channel->fPreferredFormat.u.raw_audio.valid_bits;
format->u.raw_audio.frame_rate = channel->fPreferredFormat.u.raw_audio.frame_rate;
format->u.raw_audio.channel_count = channel->fPreferredFormat.u.raw_audio.channel_count;
format->u.raw_audio.byte_order = B_MEDIA_HOST_ENDIAN;
format->u.raw_audio.buffer_size = fDevice->BufferList().return_playback_buffer_size
* (format->u.raw_audio.format & media_raw_audio_format::B_AUDIO_SIZE_MASK)
* format->u.raw_audio.channel_count;
/*media_format myFormat;
GetFormat(&myFormat);
if (!format_is_acceptible(*format,myFormat)) {
fprintf(stderr,"<- B_MEDIA_BAD_FORMAT\n");
return B_MEDIA_BAD_FORMAT;
}*/
//AddRequirements(format);
return B_OK;
}
status_t
MultiAudioNode::GetNextInput(int32* cookie, media_input* _input)
{
CALLED();
if (_input == NULL)
return B_BAD_VALUE;
if (*cookie >= fInputs.CountItems() || *cookie < 0)
return B_BAD_INDEX;
node_input *channel = (node_input *)fInputs.ItemAt(*cookie);
*_input = channel->fInput;
*cookie += 1;
PRINT(("input.format : %lu\n", channel->fInput.format.u.raw_audio.format));
return B_OK;
}
void
MultiAudioNode::DisposeInputCookie(int32 cookie)
{
CALLED();
// nothing to do since our cookies are just integers
}
void
MultiAudioNode::BufferReceived(BBuffer* buffer)
{
//CALLED();
switch (buffer->Header()->type) {
/*case B_MEDIA_PARAMETERS:
{
status_t status = ApplyParameterData(buffer->Data(),buffer->SizeUsed());
if (status != B_OK) {
fprintf(stderr,"ApplyParameterData in MultiAudioNode::BufferReceived failed\n");
}
buffer->Recycle();
}
break;*/
case B_MEDIA_RAW_AUDIO:
if (buffer->Flags() & BBuffer::B_SMALL_BUFFER) {
fprintf(stderr,"NOT IMPLEMENTED: B_SMALL_BUFFER in MultiAudioNode::BufferReceived\n");
// XXX: implement this part
buffer->Recycle();
} else {
media_timed_event event(buffer->Header()->start_time, BTimedEventQueue::B_HANDLE_BUFFER,
buffer, BTimedEventQueue::B_RECYCLE_BUFFER);
status_t status = EventQueue()->AddEvent(event);
if (status != B_OK) {
fprintf(stderr,"EventQueue()->AddEvent(event) in MultiAudioNode::BufferReceived failed\n");
buffer->Recycle();
}
}
break;
default:
fprintf(stderr,"unexpected buffer type in MultiAudioNode::BufferReceived\n");
buffer->Recycle();
break;
}
}
void
MultiAudioNode::ProducerDataStatus(const media_destination& forWhom,
int32 status, bigtime_t atPerformanceTime)
{
//CALLED();
node_input *channel = _FindInput(forWhom);
if (channel == NULL) {
fprintf(stderr,"invalid destination received in MultiAudioNode::ProducerDataStatus\n");
return;
}
media_timed_event event(atPerformanceTime, BTimedEventQueue::B_DATA_STATUS,
&channel->fInput, BTimedEventQueue::B_NO_CLEANUP, status, 0, NULL);
EventQueue()->AddEvent(event);
}
status_t
MultiAudioNode::GetLatencyFor(const media_destination& forWhom,
bigtime_t* _latency, media_node_id* _timeSource)
{
CALLED();
if (_latency == NULL || _timeSource == NULL)
return B_BAD_VALUE;
node_input *channel = _FindInput(forWhom);
if (channel == NULL)
return B_MEDIA_BAD_DESTINATION;
*_latency = EventLatency();
*_timeSource = TimeSource()->ID();
return B_OK;
}
status_t
MultiAudioNode::Connected(const media_source& producer,
const media_destination& where, const media_format& with_format,
media_input* out_input)
{
CALLED();
if (out_input == 0) {
fprintf(stderr, "<- B_BAD_VALUE\n");
return B_BAD_VALUE; // no crashing
}
node_input *channel = _FindInput(where);
if (channel == NULL) {
fprintf(stderr, "<- B_MEDIA_BAD_DESTINATION\n");
return B_MEDIA_BAD_DESTINATION;
}
_UpdateInternalLatency(with_format);
// record the agreed upon values
channel->fInput.source = producer;
channel->fInput.format = with_format;
*out_input = channel->fInput;
_StartOutputThreadIfNeeded();
return B_OK;
}
void
MultiAudioNode::Disconnected(const media_source& producer,
const media_destination& where)
{
CALLED();
node_input *channel = _FindInput(where);
if (channel == NULL || channel->fInput.source != producer)
return;
channel->fInput.source = media_source::null;
channel->fInput.format = channel->fPreferredFormat;
BAutolock locker(fBufferLock);
_FillWithZeros(*channel);
//GetFormat(&channel->fInput.format);
}
status_t
MultiAudioNode::FormatChanged(const media_source& producer,
const media_destination& consumer, int32 change_tag,
const media_format& format)
{
CALLED();
node_input *channel = _FindInput(consumer);
if(channel==NULL) {
fprintf(stderr,"<- B_MEDIA_BAD_DESTINATION\n");
return B_MEDIA_BAD_DESTINATION;
}
if (channel->fInput.source != producer) {
return B_MEDIA_BAD_SOURCE;
}
return B_ERROR;
}
status_t
MultiAudioNode::SeekTagRequested(const media_destination& destination,
bigtime_t in_target_time,
uint32 in_flags,
media_seek_tag * out_seek_tag,
bigtime_t * out_tagged_time,
uint32 * out_flags)
{
CALLED();
return BBufferConsumer::SeekTagRequested(destination,in_target_time,in_flags,
out_seek_tag,out_tagged_time,out_flags);
}
// #pragma mark - BBufferProducer
status_t
MultiAudioNode::FormatSuggestionRequested(media_type type, int32 /*quality*/,
media_format* format)
{
// FormatSuggestionRequested() is not necessarily part of the format negotiation
// process; it's simply an interrogation -- the caller wants to see what the node's
// preferred data format is, given a suggestion by the caller.
CALLED();
if (!format)
{
fprintf(stderr, "\tERROR - NULL format pointer passed in!\n");
return B_BAD_VALUE;
}
// this is the format we'll be returning (our preferred format)
*format = fInputPreferredFormat;
// a wildcard type is okay; we can specialize it
if (type == B_MEDIA_UNKNOWN_TYPE) type = B_MEDIA_RAW_AUDIO;
// we only support raw audio
if (type != B_MEDIA_RAW_AUDIO) return B_MEDIA_BAD_FORMAT;
else return B_OK;
}
status_t
MultiAudioNode::FormatProposal(const media_source& output, media_format* format)
{
// FormatProposal() is the first stage in the BMediaRoster::Connect() process. We hand
// out a suggested format, with wildcards for any variations we support.
CALLED();
node_output *channel = _FindOutput(output);
// is this a proposal for our select output?
if (channel == NULL)
{
fprintf(stderr, "MultiAudioNode::FormatProposal returning B_MEDIA_BAD_SOURCE\n");
return B_MEDIA_BAD_SOURCE;
}
// we only support floating-point raw audio, so we always return that, but we
// supply an error code depending on whether we found the proposal acceptable.
media_type requestedType = format->type;
*format = channel->fPreferredFormat;
if ((requestedType != B_MEDIA_UNKNOWN_TYPE) && (requestedType != B_MEDIA_RAW_AUDIO))
{
fprintf(stderr, "MultiAudioNode::FormatProposal returning B_MEDIA_BAD_FORMAT\n");
return B_MEDIA_BAD_FORMAT;
}
else return B_OK; // raw audio or wildcard type, either is okay by us
}
status_t
MultiAudioNode::FormatChangeRequested(const media_source& source,
const media_destination& destination, media_format* format,
int32* _deprecated_)
{
CALLED();
// we don't support any other formats, so we just reject any format changes.
return B_ERROR;
}
status_t
MultiAudioNode::GetNextOutput(int32* cookie, media_output* out_output)
{
CALLED();
if ((*cookie < fOutputs.CountItems()) && (*cookie >= 0)) {
node_output *channel = (node_output *)fOutputs.ItemAt(*cookie);
*out_output = channel->fOutput;
*cookie += 1;
return B_OK;
} else
return B_BAD_INDEX;
}
status_t
MultiAudioNode::DisposeOutputCookie(int32 cookie)
{
CALLED();
// do nothing because we don't use the cookie for anything special
return B_OK;
}
status_t
MultiAudioNode::SetBufferGroup(const media_source& for_source,
BBufferGroup* newGroup)
{
CALLED();
node_output *channel = _FindOutput(for_source);
// is this our output?
if (channel == NULL)
{
fprintf(stderr, "MultiAudioNode::SetBufferGroup returning B_MEDIA_BAD_SOURCE\n");
return B_MEDIA_BAD_SOURCE;
}
// Are we being passed the buffer group we're already using?
if (newGroup == channel->fBufferGroup) return B_OK;
// Ahh, someone wants us to use a different buffer group. At this point we delete
// the one we are using and use the specified one instead. If the specified group is
// NULL, we need to recreate one ourselves, and use *that*. Note that if we're
// caching a BBuffer that we requested earlier, we have to Recycle() that buffer
// *before* deleting the buffer group, otherwise we'll deadlock waiting for that
// buffer to be recycled!
delete channel->fBufferGroup; // waits for all buffers to recycle
if (newGroup != NULL)
{
// we were given a valid group; just use that one from now on
channel->fBufferGroup = newGroup;
}
else
{
// we were passed a NULL group pointer; that means we construct
// our own buffer group to use from now on
size_t size = channel->fOutput.format.u.raw_audio.buffer_size;
int32 count = int32(fLatency / BufferDuration() + 1 + 1);
channel->fBufferGroup = new BBufferGroup(size, count);
}
return B_OK;
}
status_t
MultiAudioNode::PrepareToConnect(const media_source& what,
const media_destination& where, media_format* format,
media_source* source, char* name)
{
CALLED();
// is this our output?
node_output* channel = _FindOutput(what);
if (channel == NULL) {
fprintf(stderr, "MultiAudioNode::PrepareToConnect returning B_MEDIA_BAD_SOURCE\n");
return B_MEDIA_BAD_SOURCE;
}
// are we already connected?
if (channel->fOutput.destination != media_destination::null)
return B_MEDIA_ALREADY_CONNECTED;
// the format may not yet be fully specialized (the consumer might have
// passed back some wildcards). Finish specializing it now, and return an
// error if we don't support the requested format.
if (format->type != B_MEDIA_RAW_AUDIO) {
fprintf(stderr, "\tnon-raw-audio format?!\n");
return B_MEDIA_BAD_FORMAT;
}
// !!! validate all other fields except for buffer_size here, because the
// consumer might have supplied different values from AcceptFormat()?
// check the buffer size, which may still be wildcarded
if (format->u.raw_audio.buffer_size
== media_raw_audio_format::wildcard.buffer_size) {
format->u.raw_audio.buffer_size = 2048;
// pick something comfortable to suggest
fprintf(stderr, "\tno buffer size provided, suggesting %lu\n",
format->u.raw_audio.buffer_size);
} else {
fprintf(stderr, "\tconsumer suggested buffer_size %lu\n",
format->u.raw_audio.buffer_size);
}
// Now reserve the connection, and return information about it
channel->fOutput.destination = where;
channel->fOutput.format = *format;
*source = channel->fOutput.source;
#ifdef __HAIKU__
strlcpy(name, channel->fOutput.name, B_MEDIA_NAME_LENGTH);
#else
strncpy(name, channel->fOutput.name, B_MEDIA_NAME_LENGTH);
#endif
return B_OK;
}
void
MultiAudioNode::Connect(status_t error, const media_source& source,
const media_destination& destination, const media_format& format,
char* name)
{
CALLED();
// is this our output?
node_output* channel = _FindOutput(source);
if (channel == NULL) {
fprintf(stderr, "MultiAudioNode::Connect returning (cause : B_MEDIA_BAD_SOURCE)\n");
return;
}
// If something earlier failed, Connect() might still be called, but with
// a non-zero error code. When that happens we simply unreserve the
// connection and do nothing else.
if (error) {
channel->fOutput.destination = media_destination::null;
channel->fOutput.format = channel->fPreferredFormat;
return;
}
// Okay, the connection has been confirmed. Record the destination and
// format that we agreed on, and report our connection name again.
channel->fOutput.destination = destination;
channel->fOutput.format = format;
#ifdef __HAIKU__
strlcpy(name, channel->fOutput.name, B_MEDIA_NAME_LENGTH);
#else
strncpy(name, channel->fOutput.name, B_MEDIA_NAME_LENGTH);
#endif
// reset our buffer duration, etc. to avoid later calculations
bigtime_t duration = channel->fOutput.format.u.raw_audio.buffer_size * 10000
/ ((channel->fOutput.format.u.raw_audio.format & media_raw_audio_format::B_AUDIO_SIZE_MASK)
* channel->fOutput.format.u.raw_audio.channel_count)
/ ((int32)(channel->fOutput.format.u.raw_audio.frame_rate / 100));
SetBufferDuration(duration);
// Now that we're connected, we can determine our downstream latency.
// Do so, then make sure we get our events early enough.
media_node_id id;
FindLatencyFor(channel->fOutput.destination, &fLatency, &id);
PRINT(("\tdownstream latency = %Ld\n", fLatency));
fInternalLatency = BufferDuration();
PRINT(("\tbuffer-filling took %Ld usec on this machine\n", fInternalLatency));
//SetEventLatency(fLatency + fInternalLatency);
// Set up the buffer group for our connection, as long as nobody handed us
// a buffer group (via SetBufferGroup()) prior to this. That can happen,
// for example, if the consumer calls SetOutputBuffersFor() on us from
// within its Connected() method.
if (!channel->fBufferGroup)
_AllocateBuffers(*channel);
_StartOutputThreadIfNeeded();
}
void
MultiAudioNode::Disconnect(const media_source& what,
const media_destination& where)
{
CALLED();