[MMIXER]
[reactos.git] / reactos / lib / drivers / sound / mmixer / controls.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Kernel Streaming
4 * FILE: lib/drivers/sound/mmixer/controls.c
5 * PURPOSE: Mixer Control Iteration Functions
6 * PROGRAMMER: Johannes Anderwald
7 */
8
9 #include "priv.h"
10
11 MIXER_STATUS
12 MMixerGetTargetPinsByNodeConnectionIndex(
13 IN PMIXER_CONTEXT MixerContext,
14 IN PKSMULTIPLE_ITEM NodeConnections,
15 IN PKSMULTIPLE_ITEM NodeTypes,
16 IN ULONG bUpDirection,
17 IN ULONG NodeConnectionIndex,
18 OUT PULONG Pins)
19 {
20 PKSTOPOLOGY_CONNECTION Connection;
21 ULONG PinId, NodeConnectionCount, Index;
22 PULONG NodeConnection;
23 MIXER_STATUS Status;
24
25
26 /* sanity check */
27 ASSERT(NodeConnectionIndex < NodeConnections->Count);
28
29 Connection = (PKSTOPOLOGY_CONNECTION)(NodeConnections + 1);
30
31 //DPRINT("FromNode %u FromNodePin %u -> ToNode %u ToNodePin %u\n", Connection[NodeConnectionIndex].FromNode, Connection[NodeConnectionIndex].FromNodePin, Connection[NodeConnectionIndex].ToNode, Connection[NodeConnectionIndex].ToNodePin );
32
33 if ((Connection[NodeConnectionIndex].ToNode == KSFILTER_NODE && bUpDirection == FALSE) ||
34 (Connection[NodeConnectionIndex].FromNode == KSFILTER_NODE && bUpDirection == TRUE))
35 {
36 /* iteration stops here */
37 if (bUpDirection)
38 PinId = Connection[NodeConnectionIndex].FromNodePin;
39 else
40 PinId = Connection[NodeConnectionIndex].ToNodePin;
41
42 //DPRINT("GetTargetPinsByNodeIndex FOUND Target Pin %u Parsed %u\n", PinId, Pins[PinId]);
43
44 /* mark pin index as a target pin */
45 Pins[PinId] = TRUE;
46 return MM_STATUS_SUCCESS;
47 }
48
49 // get all node indexes referenced by that node
50 if (bUpDirection)
51 {
52 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, Connection[NodeConnectionIndex].FromNode, TRUE, FALSE, &NodeConnectionCount, &NodeConnection);
53 }
54 else
55 {
56 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, Connection[NodeConnectionIndex].ToNode, TRUE, TRUE, &NodeConnectionCount, &NodeConnection);
57 }
58
59 if (Status == MM_STATUS_SUCCESS)
60 {
61 for(Index = 0; Index < NodeConnectionCount; Index++)
62 {
63 // iterate recursively into the nodes
64 Status = MMixerGetTargetPinsByNodeConnectionIndex(MixerContext, NodeConnections, NodeTypes, bUpDirection, NodeConnection[Index], Pins);
65 ASSERT(Status == MM_STATUS_SUCCESS);
66 }
67 // free node connection indexes
68 MixerContext->Free(NodeConnection);
69 }
70
71 return Status;
72 }
73
74 MIXER_STATUS
75 MMixerGetControlsFromPinByConnectionIndex(
76 IN PMIXER_CONTEXT MixerContext,
77 IN PKSMULTIPLE_ITEM NodeConnections,
78 IN PKSMULTIPLE_ITEM NodeTypes,
79 IN ULONG bUpDirection,
80 IN ULONG NodeConnectionIndex,
81 OUT PULONG Nodes)
82 {
83 PKSTOPOLOGY_CONNECTION CurConnection;
84 LPGUID NodeType;
85 ULONG NodeIndex;
86 MIXER_STATUS Status;
87 ULONG NodeConnectionCount, Index;
88 PULONG NodeConnection;
89
90
91 /* get current connection */
92 CurConnection = MMixerGetConnectionByIndex(NodeConnections, NodeConnectionIndex);
93
94 if (bUpDirection)
95 NodeIndex = CurConnection->FromNode;
96 else
97 NodeIndex = CurConnection->ToNode;
98
99 /* get target node type of current connection */
100 NodeType = MMixerGetNodeType(NodeTypes, NodeIndex);
101
102 if (IsEqualGUIDAligned(NodeType, &KSNODETYPE_SUM) || IsEqualGUIDAligned(NodeType, &KSNODETYPE_MUX))
103 {
104 if (bUpDirection)
105 {
106 /* add the sum / mux node to destination line */
107 Nodes[NodeIndex] = TRUE;
108 }
109
110 return MM_STATUS_SUCCESS;
111 }
112
113 /* now add the node */
114 Nodes[NodeIndex] = TRUE;
115
116
117 /* get all node indexes referenced by that node */
118 if (bUpDirection)
119 {
120 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, NodeIndex, TRUE, FALSE, &NodeConnectionCount, &NodeConnection);
121 }
122 else
123 {
124 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, NodeIndex, TRUE, TRUE, &NodeConnectionCount, &NodeConnection);
125 }
126
127 if (Status != MM_STATUS_SUCCESS)
128 {
129 for(Index = 0; Index < NodeConnectionCount; Index++)
130 {
131 /* iterate recursively into the nodes */
132 Status = MMixerGetControlsFromPinByConnectionIndex(MixerContext, NodeConnections, NodeTypes, bUpDirection, NodeConnection[Index], Nodes);
133 ASSERT(Status == MM_STATUS_SUCCESS);
134 }
135 /* free node connection indexes */
136 MixerContext->Free(NodeConnection);
137 }
138
139 return Status;
140 }
141
142 MIXER_STATUS
143 MMixerAddMixerControl(
144 IN PMIXER_CONTEXT MixerContext,
145 IN LPMIXER_INFO MixerInfo,
146 IN HANDLE hDevice,
147 IN PKSMULTIPLE_ITEM NodeTypes,
148 IN ULONG NodeIndex,
149 IN LPMIXERLINE_EXT MixerLine,
150 OUT LPMIXERCONTROLW MixerControl)
151 {
152 LPGUID NodeType;
153 KSP_NODE Node;
154 ULONG BytesReturned;
155 MIXER_STATUS Status;
156 LPWSTR Name;
157
158 /* initialize mixer control */
159 MixerControl->cbStruct = sizeof(MIXERCONTROLW);
160 MixerControl->dwControlID = MixerInfo->ControlId;
161
162 /* get node type */
163 NodeType = MMixerGetNodeType(NodeTypes, NodeIndex);
164 /* store control type */
165 MixerControl->dwControlType = MMixerGetControlTypeFromTopologyNode(NodeType);
166
167 MixerControl->fdwControl = MIXERCONTROL_CONTROLF_UNIFORM; //FIXME
168 MixerControl->cMultipleItems = 0; //FIXME
169
170 if (MixerControl->dwControlType == MIXERCONTROL_CONTROLTYPE_MUTE)
171 {
172 MixerControl->Bounds.dwMinimum = 0;
173 MixerControl->Bounds.dwMaximum = 1;
174 }
175 else if (MixerControl->dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
176 {
177 MixerControl->Bounds.dwMinimum = 0;
178 MixerControl->Bounds.dwMaximum = 0xFFFF;
179 MixerControl->Metrics.cSteps = 0xC0; //FIXME
180 }
181
182 /* setup request to retrieve name */
183 Node.NodeId = NodeIndex;
184 Node.Property.Id = KSPROPERTY_TOPOLOGY_NAME;
185 Node.Property.Flags = KSPROPERTY_TYPE_GET;
186 Node.Property.Set = KSPROPSETID_Topology;
187 Node.Reserved = 0;
188
189 /* get node name size */
190 Status = MixerContext->Control(hDevice, IOCTL_KS_PROPERTY, (PVOID)&Node, sizeof(KSP_NODE), NULL, 0, &BytesReturned);
191
192 if (Status == MM_STATUS_MORE_ENTRIES)
193 {
194 ASSERT(BytesReturned != 0);
195 Name = (LPWSTR)MixerContext->Alloc(BytesReturned);
196 if (!Name)
197 {
198 /* not enough memory */
199 return MM_STATUS_NO_MEMORY;
200 }
201
202 /* get node name */
203 Status = MixerContext->Control(hDevice, IOCTL_KS_PROPERTY, (PVOID)&Node, sizeof(KSP_NODE), (LPVOID)Name, BytesReturned, &BytesReturned);
204 if (Status != MM_STATUS_SUCCESS)
205 {
206 MixerContext->Copy(MixerControl->szShortName, Name, (min(MIXER_SHORT_NAME_CHARS, wcslen(Name)+1)) * sizeof(WCHAR));
207 MixerControl->szShortName[MIXER_SHORT_NAME_CHARS-1] = L'\0';
208
209 MixerContext->Copy(MixerControl->szName, Name, (min(MIXER_LONG_NAME_CHARS, wcslen(Name)+1)) * sizeof(WCHAR));
210 MixerControl->szName[MIXER_LONG_NAME_CHARS-1] = L'\0';
211 }
212
213 /* free name buffer */
214 MixerContext->Free(Name);
215 }
216
217 MixerInfo->ControlId++;
218 #if 0
219 if (MixerControl->dwControlType == MIXERCONTROL_CONTROLTYPE_MUX)
220 {
221 KSNODEPROPERTY Property;
222 ULONG PinId = 2;
223
224 /* setup the request */
225 RtlZeroMemory(&Property, sizeof(KSNODEPROPERTY));
226
227 Property.NodeId = NodeIndex;
228 Property.Property.Id = KSPROPERTY_AUDIO_MUX_SOURCE;
229 Property.Property.Flags = KSPROPERTY_TYPE_SET;
230 Property.Property.Set = KSPROPSETID_Audio;
231
232 /* get node volume level info */
233 Status = MixerContext->Control(hDevice, IOCTL_KS_PROPERTY, (PVOID)&Property, sizeof(KSNODEPROPERTY), (PVOID)&PinId, sizeof(ULONG), &BytesReturned);
234
235 DPRINT1("Status %x NodeIndex %u PinId %u\n", Status, NodeIndex, PinId);
236 //DbgBreakPoint();
237 }else
238 #endif
239 if (MixerControl->dwControlType == MIXERCONTROL_CONTROLTYPE_VOLUME)
240 {
241 KSNODEPROPERTY_AUDIO_CHANNEL Property;
242 ULONG Length;
243 PKSPROPERTY_DESCRIPTION Desc;
244 PKSPROPERTY_MEMBERSHEADER Members;
245 PKSPROPERTY_STEPPING_LONG Range;
246
247 Length = sizeof(KSPROPERTY_DESCRIPTION) + sizeof(KSPROPERTY_MEMBERSHEADER) + sizeof(KSPROPERTY_STEPPING_LONG);
248 Desc = (PKSPROPERTY_DESCRIPTION)MixerContext->Alloc(Length);
249 ASSERT(Desc);
250
251 /* setup the request */
252 RtlZeroMemory(&Property, sizeof(KSNODEPROPERTY_AUDIO_CHANNEL));
253
254 Property.NodeProperty.NodeId = NodeIndex;
255 Property.NodeProperty.Property.Id = KSPROPERTY_AUDIO_VOLUMELEVEL;
256 Property.NodeProperty.Property.Flags = KSPROPERTY_TYPE_BASICSUPPORT;
257 Property.NodeProperty.Property.Set = KSPROPSETID_Audio;
258
259 /* get node volume level info */
260 Status = MixerContext->Control(hDevice, IOCTL_KS_PROPERTY, (PVOID)&Property, sizeof(KSNODEPROPERTY_AUDIO_CHANNEL), Desc, Length, &BytesReturned);
261
262 if (Status == MM_STATUS_SUCCESS)
263 {
264 LPMIXERVOLUME_DATA VolumeData;
265 ULONG Steps, MaxRange, Index;
266 LONG Value;
267
268 Members = (PKSPROPERTY_MEMBERSHEADER)(Desc + 1);
269 Range = (PKSPROPERTY_STEPPING_LONG)(Members + 1);
270
271 DPRINT("NodeIndex %u Range Min %d Max %d Steps %x UMin %x UMax %x\n", NodeIndex, Range->Bounds.SignedMinimum, Range->Bounds.SignedMaximum, Range->SteppingDelta, Range->Bounds.UnsignedMinimum, Range->Bounds.UnsignedMaximum);
272
273 MaxRange = Range->Bounds.UnsignedMaximum - Range->Bounds.UnsignedMinimum;
274
275 if (MaxRange)
276 {
277 ASSERT(MaxRange);
278 VolumeData = (LPMIXERVOLUME_DATA)MixerContext->Alloc(sizeof(MIXERVOLUME_DATA));
279 if (!VolumeData)
280 return MM_STATUS_NO_MEMORY;
281
282 Steps = MaxRange / Range->SteppingDelta + 1;
283
284 /* store mixer control info there */
285 VolumeData->Header.dwControlID = MixerControl->dwControlID;
286 VolumeData->SignedMaximum = Range->Bounds.SignedMaximum;
287 VolumeData->SignedMinimum = Range->Bounds.SignedMinimum;
288 VolumeData->SteppingDelta = Range->SteppingDelta;
289 VolumeData->ValuesCount = Steps;
290 VolumeData->InputSteppingDelta = 0x10000 / Steps;
291
292 VolumeData->Values = (PLONG)MixerContext->Alloc(sizeof(LONG) * Steps);
293 if (!VolumeData->Values)
294 {
295 MixerContext->Free(Desc);
296 MixerContext->Free(VolumeData);
297
298 return MM_STATUS_NO_MEMORY;
299 }
300
301 Value = Range->Bounds.SignedMinimum;
302 for(Index = 0; Index < Steps; Index++)
303 {
304 VolumeData->Values[Index] = Value;
305 Value += Range->SteppingDelta;
306 }
307 InsertTailList(&MixerLine->LineControlsExtraData, &VolumeData->Header.Entry);
308 }
309 }
310 MixerContext->Free(Desc);
311 }
312
313
314 DPRINT("Status %x Name %S\n", Status, MixerControl->szName);
315 return MM_STATUS_SUCCESS;
316 }
317
318 MIXER_STATUS
319 MMixerAddMixerSourceLine(
320 IN PMIXER_CONTEXT MixerContext,
321 IN OUT LPMIXER_INFO MixerInfo,
322 IN HANDLE hDevice,
323 IN PKSMULTIPLE_ITEM NodeConnections,
324 IN PKSMULTIPLE_ITEM NodeTypes,
325 IN ULONG PinId,
326 IN ULONG bBridgePin,
327 IN ULONG bTargetPin)
328 {
329 LPMIXERLINE_EXT SrcLine, DstLine;
330 MIXER_STATUS Status;
331 KSP_PIN Pin;
332 LPWSTR PinName;
333 GUID NodeType;
334 ULONG BytesReturned, ControlCount, Index;
335 PULONG Nodes;
336
337 if (!bTargetPin)
338 {
339 /* allocate src mixer line */
340 SrcLine = (LPMIXERLINE_EXT)MixerContext->Alloc(sizeof(MIXERLINE_EXT));
341
342 if (!SrcLine)
343 return MM_STATUS_NO_MEMORY;
344
345 /* zero struct */
346 RtlZeroMemory(SrcLine, sizeof(MIXERLINE_EXT));
347
348 }
349 else
350 {
351 ASSERT(!IsListEmpty(&MixerInfo->LineList));
352 SrcLine = MMixerGetSourceMixerLineByLineId(MixerInfo, DESTINATION_LINE);
353 }
354
355 /* get destination line */
356 DstLine = MMixerGetSourceMixerLineByLineId(MixerInfo, DESTINATION_LINE);
357 ASSERT(DstLine);
358
359
360 if (!bTargetPin)
361 {
362 /* initialize mixer src line */
363 SrcLine->hDevice = hDevice;
364 SrcLine->PinId = PinId;
365 SrcLine->Line.cbStruct = sizeof(MIXERLINEW);
366
367 /* initialize mixer destination line */
368 SrcLine->Line.cbStruct = sizeof(MIXERLINEW);
369 SrcLine->Line.dwDestination = 0;
370 SrcLine->Line.dwSource = DstLine->Line.cConnections;
371 SrcLine->Line.dwLineID = (DstLine->Line.cConnections * 0x10000);
372 SrcLine->Line.fdwLine = MIXERLINE_LINEF_ACTIVE | MIXERLINE_LINEF_SOURCE;
373 SrcLine->Line.dwUser = 0;
374 SrcLine->Line.cChannels = DstLine->Line.cChannels;
375 SrcLine->Line.cConnections = 0;
376 SrcLine->Line.Target.dwType = 1;
377 SrcLine->Line.Target.dwDeviceID = DstLine->Line.Target.dwDeviceID;
378 SrcLine->Line.Target.wMid = MixerInfo->MixCaps.wMid;
379 SrcLine->Line.Target.wPid = MixerInfo->MixCaps.wPid;
380 SrcLine->Line.Target.vDriverVersion = MixerInfo->MixCaps.vDriverVersion;
381 InitializeListHead(&SrcLine->LineControlsExtraData);
382 wcscpy(SrcLine->Line.Target.szPname, MixerInfo->MixCaps.szPname);
383
384 }
385
386 /* allocate a node arrary */
387 Nodes = (PULONG)MixerContext->Alloc(sizeof(ULONG) * NodeTypes->Count);
388
389 if (!Nodes)
390 {
391 /* not enough memory */
392 if (!bTargetPin)
393 {
394 MixerContext->Free(SrcLine);
395 }
396 return MM_STATUS_NO_MEMORY;
397 }
398
399 Status = MMixerGetControlsFromPin(MixerContext, NodeConnections, NodeTypes, PinId, bTargetPin, Nodes);
400 if (Status != MM_STATUS_SUCCESS)
401 {
402 /* something went wrong */
403 if (!bTargetPin)
404 {
405 MixerContext->Free(SrcLine);
406 }
407 MixerContext->Free(Nodes);
408 return Status;
409 }
410
411 /* now count all nodes controlled by that pin */
412 ControlCount = 0;
413 for(Index = 0; Index < NodeTypes->Count; Index++)
414 {
415 if (Nodes[Index])
416 {
417 // found a node
418 ControlCount++;
419 }
420 }
421
422 /* now allocate the line controls */
423 if (ControlCount)
424 {
425 SrcLine->LineControls = (LPMIXERCONTROLW)MixerContext->Alloc(sizeof(MIXERCONTROLW) * ControlCount);
426
427 if (!SrcLine->LineControls)
428 {
429 /* no memory available */
430 if (!bTargetPin)
431 {
432 MixerContext->Free(SrcLine);
433 }
434 MixerContext->Free(Nodes);
435 return MM_STATUS_NO_MEMORY;
436 }
437
438 SrcLine->NodeIds = (PULONG)MixerContext->Alloc(sizeof(ULONG) * ControlCount);
439 if (!SrcLine->NodeIds)
440 {
441 /* no memory available */
442 MixerContext->Free(SrcLine->LineControls);
443 if (!bTargetPin)
444 {
445 MixerContext->Free(SrcLine);
446 }
447 MixerContext->Free(Nodes);
448 return MM_STATUS_NO_MEMORY;
449 }
450
451 /* zero line controls */
452 RtlZeroMemory(SrcLine->LineControls, sizeof(MIXERCONTROLW) * ControlCount);
453 RtlZeroMemory(SrcLine->NodeIds, sizeof(ULONG) * ControlCount);
454
455 ControlCount = 0;
456 for(Index = 0; Index < NodeTypes->Count; Index++)
457 {
458 if (Nodes[Index])
459 {
460 /* store the node index for retrieving / setting details */
461 SrcLine->NodeIds[ControlCount] = Index;
462
463 Status = MMixerAddMixerControl(MixerContext, MixerInfo, hDevice, NodeTypes, Index, SrcLine, &SrcLine->LineControls[ControlCount]);
464 if (Status == MM_STATUS_SUCCESS)
465 {
466 /* increment control count on success */
467 ControlCount++;
468 }
469 }
470 }
471 /* store control count */
472 SrcLine->Line.cControls = ControlCount;
473 }
474
475 /* release nodes array */
476 MixerContext->Free(Nodes);
477
478 /* get pin category */
479 Pin.PinId = PinId;
480 Pin.Reserved = 0;
481 Pin.Property.Flags = KSPROPERTY_TYPE_GET;
482 Pin.Property.Set = KSPROPSETID_Pin;
483 Pin.Property.Id = KSPROPERTY_PIN_CATEGORY;
484
485 /* try get pin category */
486 Status = MixerContext->Control(hDevice, IOCTL_KS_PROPERTY, (PVOID)&Pin, sizeof(KSP_PIN), (LPVOID)&NodeType, sizeof(GUID), &BytesReturned);
487 if (Status != MM_STATUS_SUCCESS)
488 {
489 //FIXME
490 //map component type
491 }
492
493 /* retrieve pin name */
494 Pin.PinId = PinId;
495 Pin.Reserved = 0;
496 Pin.Property.Flags = KSPROPERTY_TYPE_GET;
497 Pin.Property.Set = KSPROPSETID_Pin;
498 Pin.Property.Id = KSPROPERTY_PIN_NAME;
499
500 /* try get pin name size */
501 Status = MixerContext->Control(hDevice, IOCTL_KS_PROPERTY, (PVOID)&Pin, sizeof(KSP_PIN), NULL, 0, &BytesReturned);
502
503 if (Status == MM_STATUS_MORE_ENTRIES)
504 {
505 PinName = (LPWSTR)MixerContext->Alloc(BytesReturned);
506 if (PinName)
507 {
508 /* try get pin name */
509 Status = MixerContext->Control(hDevice, IOCTL_KS_PROPERTY, (PVOID)&Pin, sizeof(KSP_PIN), (LPVOID)PinName, BytesReturned, &BytesReturned);
510
511 if (Status == MM_STATUS_SUCCESS)
512 {
513 MixerContext->Copy(SrcLine->Line.szShortName, PinName, (min(MIXER_SHORT_NAME_CHARS, wcslen(PinName)+1)) * sizeof(WCHAR));
514 SrcLine->Line.szShortName[MIXER_SHORT_NAME_CHARS-1] = L'\0';
515
516 MixerContext->Copy(SrcLine->Line.szName, PinName, (min(MIXER_LONG_NAME_CHARS, wcslen(PinName)+1)) * sizeof(WCHAR));
517 SrcLine->Line.szName[MIXER_LONG_NAME_CHARS-1] = L'\0';
518 }
519 MixerContext->Free(PinName);
520 }
521 }
522
523 /* insert src line */
524 if (!bTargetPin)
525 {
526 InsertTailList(&MixerInfo->LineList, &SrcLine->Entry);
527 DstLine->Line.cConnections++;
528 }
529
530 return MM_STATUS_SUCCESS;
531 }
532
533 MIXER_STATUS
534 MMixerCreateDestinationLine(
535 IN PMIXER_CONTEXT MixerContext,
536 IN LPMIXER_INFO MixerInfo,
537 IN ULONG bInputMixer)
538 {
539 LPMIXERLINE_EXT DestinationLine;
540
541 // allocate a mixer destination line
542 DestinationLine = (LPMIXERLINE_EXT) MixerContext->Alloc(sizeof(MIXERLINE_EXT));
543 if (!MixerInfo)
544 {
545 // no memory
546 return MM_STATUS_NO_MEMORY;
547 }
548
549 /* initialize mixer destination line */
550 DestinationLine->Line.cbStruct = sizeof(MIXERLINEW);
551 DestinationLine->Line.dwSource = MAXULONG;
552 DestinationLine->Line.dwLineID = DESTINATION_LINE;
553 DestinationLine->Line.fdwLine = MIXERLINE_LINEF_ACTIVE;
554 DestinationLine->Line.dwUser = 0;
555 DestinationLine->Line.dwComponentType = (bInputMixer == 0 ? MIXERLINE_COMPONENTTYPE_DST_SPEAKERS : MIXERLINE_COMPONENTTYPE_DST_WAVEIN);
556 DestinationLine->Line.cChannels = 2; //FIXME
557 wcscpy(DestinationLine->Line.szShortName, L"Summe"); //FIXME
558 wcscpy(DestinationLine->Line.szName, L"Summe"); //FIXME
559 DestinationLine->Line.Target.dwType = (bInputMixer == 0 ? MIXERLINE_TARGETTYPE_WAVEOUT : MIXERLINE_TARGETTYPE_WAVEIN);
560 DestinationLine->Line.Target.dwDeviceID = !bInputMixer;
561 DestinationLine->Line.Target.wMid = MixerInfo->MixCaps.wMid;
562 DestinationLine->Line.Target.wPid = MixerInfo->MixCaps.wPid;
563 DestinationLine->Line.Target.vDriverVersion = MixerInfo->MixCaps.vDriverVersion;
564 wcscpy(DestinationLine->Line.Target.szPname, MixerInfo->MixCaps.szPname);
565
566
567 // insert into mixer info
568 InsertHeadList(&MixerInfo->LineList, &DestinationLine->Entry);
569
570 // done
571 return MM_STATUS_SUCCESS;
572 }
573
574 MIXER_STATUS
575 MMixerGetControlsFromPin(
576 IN PMIXER_CONTEXT MixerContext,
577 IN PKSMULTIPLE_ITEM NodeConnections,
578 IN PKSMULTIPLE_ITEM NodeTypes,
579 IN ULONG PinId,
580 IN ULONG bUpDirection,
581 OUT PULONG Nodes)
582 {
583 ULONG NodeConnectionCount, Index;
584 MIXER_STATUS Status;
585 PULONG NodeConnection;
586
587 /* sanity check */
588 ASSERT(PinId != (ULONG)-1);
589
590 /* get all node indexes referenced by that pin */
591 if (bUpDirection)
592 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, PinId, FALSE, FALSE, &NodeConnectionCount, &NodeConnection);
593 else
594 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, PinId, FALSE, TRUE, &NodeConnectionCount, &NodeConnection);
595
596 for(Index = 0; Index < NodeConnectionCount; Index++)
597 {
598 /* get all associated controls */
599 Status = MMixerGetControlsFromPinByConnectionIndex(MixerContext, NodeConnections, NodeTypes, bUpDirection, NodeConnection[Index], Nodes);
600 }
601
602 MixerContext->Free(NodeConnection);
603
604 return Status;
605 }
606
607
608
609
610 MIXER_STATUS
611 MMixerAddMixerSourceLines(
612 IN PMIXER_CONTEXT MixerContext,
613 IN OUT LPMIXER_INFO MixerInfo,
614 IN HANDLE hDevice,
615 IN PKSMULTIPLE_ITEM NodeConnections,
616 IN PKSMULTIPLE_ITEM NodeTypes,
617 IN ULONG PinsCount,
618 IN ULONG BridgePinIndex,
619 IN ULONG TargetPinIndex,
620 IN PULONG Pins)
621 {
622 ULONG Index;
623
624 for(Index = PinsCount; Index > 0; Index--)
625 {
626 DPRINT("MMixerAddMixerSourceLines Index %lu Pin %lu\n", Index-1, Pins[Index-1]);
627 if (Pins[Index-1])
628 {
629 MMixerAddMixerSourceLine(MixerContext, MixerInfo, hDevice, NodeConnections, NodeTypes, Index-1, (Index -1 == BridgePinIndex), (Index -1 == TargetPinIndex));
630 }
631 }
632 return MM_STATUS_SUCCESS;
633 }
634
635
636 MIXER_STATUS
637 MMixerHandlePhysicalConnection(
638 IN PMIXER_CONTEXT MixerContext,
639 IN OUT LPMIXER_INFO MixerInfo,
640 IN ULONG bInput,
641 IN PKSPIN_PHYSICALCONNECTION OutConnection)
642 {
643 PULONG PinsRef = NULL, PinConnectionIndex = NULL, PinsSrcRef;
644 ULONG PinsRefCount, Index, PinConnectionIndexCount;
645 MIXER_STATUS Status;
646 HANDLE hDevice = NULL;
647 PKSMULTIPLE_ITEM NodeTypes = NULL;
648 PKSMULTIPLE_ITEM NodeConnections = NULL;
649 PULONG MixerControls;
650 ULONG MixerControlsCount;
651
652
653 // open the connected filter
654 Status = MixerContext->Open(OutConnection->SymbolicLinkName, &hDevice);
655 if (Status != MM_STATUS_SUCCESS)
656 {
657 DPRINT("OpenDevice failed with %x\n", Status);
658 return Status;
659 }
660
661 // get connected filter pin count
662 PinsRefCount = MMixerGetFilterPinCount(MixerContext, hDevice);
663 ASSERT(PinsRefCount);
664
665 PinsRef = (PULONG)MixerContext->Alloc(sizeof(ULONG) * PinsRefCount);
666 if (!PinsRef)
667 {
668 // no memory
669 MixerContext->Close(hDevice);
670 return MM_STATUS_UNSUCCESSFUL;
671 }
672
673 // get topology node types
674 Status = MMixerGetFilterTopologyProperty(MixerContext, hDevice, KSPROPERTY_TOPOLOGY_NODES, &NodeTypes);
675 if (Status != MM_STATUS_SUCCESS)
676 {
677 MixerContext->Close(hDevice);
678 MixerContext->Free(PinsRef);
679 return Status;
680 }
681
682 // get topology connections
683 Status = MMixerGetFilterTopologyProperty(MixerContext, hDevice, KSPROPERTY_TOPOLOGY_CONNECTIONS, &NodeConnections);
684 if (Status != MM_STATUS_SUCCESS)
685 {
686 MixerContext->Close(hDevice);
687 MixerContext->Free(PinsRef);
688 MixerContext->Free(NodeTypes);
689 return Status;
690 }
691 // gets connection index of the bridge pin which connects to a node
692 DPRINT("Pin %lu\n", OutConnection->Pin);
693
694 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, OutConnection->Pin, FALSE, !bInput, &PinConnectionIndexCount, &PinConnectionIndex);
695 if (Status != MM_STATUS_SUCCESS)
696 {
697 MixerContext->Close(hDevice);
698 MixerContext->Free(PinsRef);
699 MixerContext->Free(NodeTypes);
700 MixerContext->Free(NodeConnections);
701 return Status;
702 }
703
704 /* there should be no split in the bride pin */
705 ASSERT(PinConnectionIndexCount == 1);
706
707 /* find all target pins of this connection */
708 Status = MMixerGetTargetPinsByNodeConnectionIndex(MixerContext, NodeConnections, NodeTypes, FALSE, PinConnectionIndex[0], PinsRef);
709 if (Status != MM_STATUS_SUCCESS)
710 {
711 MixerContext->Close(hDevice);
712 MixerContext->Free(PinsRef);
713 MixerContext->Free(NodeTypes);
714 MixerContext->Free(NodeConnections);
715 MixerContext->Free(PinConnectionIndex);
716 return Status;
717 }
718
719 for(Index = 0; Index < PinsRefCount; Index++)
720 {
721 DPRINT("PinsRefCount %lu Index %lu Value %lu\n", PinsRefCount, Index, PinsRef[Index]);
722 if (PinsRef[Index])
723 {
724 // found a target pin, now get all references
725 Status = MMixerGetNodeIndexes(MixerContext, NodeConnections, Index, FALSE, FALSE, &MixerControlsCount, &MixerControls);
726 if (Status != MM_STATUS_SUCCESS)
727 {
728 DPRINT("MMixerGetNodeIndexes failed with %u\n", Status);
729 break;
730 }
731
732 /* sanity check */
733 ASSERT(MixerControlsCount == 1);
734
735 PinsSrcRef = (PULONG)MixerContext->Alloc(PinsRefCount * sizeof(ULONG));
736 if (!PinsSrcRef)
737 {
738 /* no memory */
739 MixerContext->Close(hDevice);
740 MixerContext->Free(PinsRef);
741 MixerContext->Free(NodeTypes);
742 MixerContext->Free(NodeConnections);
743 MixerContext->Free(PinConnectionIndex);
744 MixerContext->Free(MixerControls);
745 return MM_STATUS_NO_MEMORY;
746 }
747
748 // now get all connected source pins
749 Status = MMixerGetTargetPinsByNodeConnectionIndex(MixerContext, NodeConnections, NodeTypes, TRUE, MixerControls[0], PinsSrcRef);
750 if (Status != MM_STATUS_SUCCESS)
751 {
752 // failed */
753 MixerContext->Close(hDevice);
754 MixerContext->Free(PinsRef);
755 MixerContext->Free(NodeTypes);
756 MixerContext->Free(NodeConnections);
757 MixerContext->Free(PinConnectionIndex);
758 MixerContext->Free(MixerControls);
759 MixerContext->Free(PinsSrcRef);
760 return Status;
761 }
762
763 /* add pins from target line */
764 if (!bInput)
765 {
766 // dont add bridge pin for input mixers
767 PinsSrcRef[Index] = TRUE;
768 PinsSrcRef[OutConnection->Pin] = TRUE;
769 }
770 PinsSrcRef[OutConnection->Pin] = TRUE;
771
772 Status = MMixerAddMixerSourceLines(MixerContext, MixerInfo, hDevice, NodeConnections, NodeTypes, PinsRefCount, OutConnection->Pin, Index, PinsSrcRef);
773
774 MixerContext->Free(MixerControls);
775 MixerContext->Free(PinsSrcRef);
776 }
777 }
778
779 return Status;
780 }
781
782
783 MIXER_STATUS
784 MMixerInitializeFilter(
785 IN PMIXER_CONTEXT MixerContext,
786 IN PMIXER_LIST MixerList,
787 IN HANDLE hMixer,
788 IN LPWSTR DeviceName,
789 IN PKSMULTIPLE_ITEM NodeTypes,
790 IN PKSMULTIPLE_ITEM NodeConnections,
791 IN ULONG PinCount,
792 IN ULONG NodeIndex,
793 IN ULONG bInputMixer)
794 {
795 LPMIXER_INFO MixerInfo;
796 MIXER_STATUS Status;
797 PKSPIN_PHYSICALCONNECTION OutConnection;
798 ULONG Index;
799 ULONG * Pins;
800 ULONG bUsed;
801
802 // allocate a mixer info struct
803 MixerInfo = (LPMIXER_INFO) MixerContext->Alloc(sizeof(MIXER_INFO));
804 if (!MixerInfo)
805 {
806 // no memory
807 return MM_STATUS_NO_MEMORY;
808 }
809
810 // intialize mixer caps */
811 MixerInfo->MixCaps.wMid = MM_MICROSOFT; //FIXME
812 MixerInfo->MixCaps.wPid = MM_PID_UNMAPPED; //FIXME
813 MixerInfo->MixCaps.vDriverVersion = 1; //FIXME
814 MixerInfo->MixCaps.fdwSupport = 0;
815 MixerInfo->MixCaps.cDestinations = 1;
816 MixerInfo->hMixer = hMixer;
817
818 // initialize line list
819 InitializeListHead(&MixerInfo->LineList);
820
821 /* FIXME find mixer name */
822
823 Status = MMixerCreateDestinationLine(MixerContext, MixerInfo, bInputMixer);
824 if (Status != MM_STATUS_SUCCESS)
825 {
826 // failed to create destination line
827 MixerContext->Free(MixerInfo);
828 return Status;
829 }
830
831
832 // now allocate an array which will receive the indices of the pin
833 // which has a ADC / DAC nodetype in its path
834 Pins = (PULONG)MixerContext->Alloc(PinCount * sizeof(ULONG));
835
836 if (!Pins)
837 {
838 // no memory
839 MMixerFreeMixerInfo(MixerContext, MixerInfo);
840 return MM_STATUS_NO_MEMORY;
841 }
842
843 // now get the target pins of the ADC / DAC node
844 Status = MMixerGetTargetPins(MixerContext, NodeTypes, NodeConnections, NodeIndex, bInputMixer, Pins, PinCount);
845
846 if (Status != MM_STATUS_SUCCESS)
847 {
848 // failed to locate target pins
849 MixerContext->Free(Pins);
850 MMixerFreeMixerInfo(MixerContext, MixerInfo);
851 DPRINT("MMixerGetTargetPins failed with %u\n", Status);
852 return Status;
853 }
854
855 // filter hasnt been used
856 bUsed = FALSE;
857
858 // now check all pins and generate new lines for destination lines
859 for(Index = 0; Index < PinCount; Index++)
860 {
861 DPRINT("Index %lu TargetPin %lu\n", Index, Pins[Index]);
862 // is the current index a target pin
863 if (Pins[Index])
864 {
865 // check if the pin has a physical connection
866 Status = MMixerGetPhysicalConnection(MixerContext, hMixer, Index, &OutConnection);
867 if (Status == MM_STATUS_SUCCESS)
868 {
869 // the pin has a physical connection
870 Status = MMixerHandlePhysicalConnection(MixerContext, MixerInfo, bInputMixer, OutConnection);
871 DPRINT("MMixerHandlePhysicalConnection status %u\n", Status);
872 MixerContext->Free(OutConnection);
873 bUsed = TRUE;
874 }
875 }
876 }
877 MixerContext->Free(Pins);
878
879 if (bUsed)
880 {
881 // store mixer info in list
882 InsertTailList(&MixerList->MixerList, &MixerInfo->Entry);
883 MixerList->MixerListCount++;
884 DPRINT("New MixerCount %lu\n", MixerList->MixerListCount);
885 }
886 else
887 {
888 // TODO:
889 // filter exposes its topology on the same filter
890 MMixerFreeMixerInfo(MixerContext, MixerInfo);
891 }
892
893 // done
894 return Status;
895 }
896
897 MIXER_STATUS
898 MMixerSetupFilter(
899 IN PMIXER_CONTEXT MixerContext,
900 IN PMIXER_LIST MixerList,
901 IN HANDLE hMixer,
902 IN PULONG DeviceCount,
903 IN LPWSTR DeviceName)
904 {
905 PKSMULTIPLE_ITEM NodeTypes, NodeConnections;
906 MIXER_STATUS Status;
907 ULONG PinCount;
908 ULONG NodeIndex;
909
910 // get number of pins
911 PinCount = MMixerGetFilterPinCount(MixerContext, hMixer);
912 ASSERT(PinCount);
913 DPRINT("NumOfPins: %lu\n", PinCount);
914
915 // get filter node types
916 Status = MMixerGetFilterTopologyProperty(MixerContext, hMixer, KSPROPERTY_TOPOLOGY_NODES, &NodeTypes);
917 if (Status != MM_STATUS_SUCCESS)
918 {
919 // failed
920 return Status;
921 }
922
923 // get filter node connections
924 Status = MMixerGetFilterTopologyProperty(MixerContext, hMixer, KSPROPERTY_TOPOLOGY_CONNECTIONS, &NodeConnections);
925 if (Status != MM_STATUS_SUCCESS)
926 {
927 // failed
928 MixerContext->Free(NodeTypes);
929 return Status;
930 }
931
932 // check if the filter has an wave out node
933 NodeIndex = MMixerGetIndexOfGuid(NodeTypes, &KSNODETYPE_DAC);
934 if (NodeIndex != MAXULONG)
935 {
936 // it has
937
938 Status = MMixerInitializeFilter(MixerContext, MixerList, hMixer, DeviceName, NodeTypes, NodeConnections, PinCount, NodeIndex, FALSE);
939 DPRINT("MMixerInitializeFilter Status %u\n", Status);
940 // check for success
941 if (Status == MM_STATUS_SUCCESS)
942 {
943 // increment mixer count
944 (*DeviceCount)++;
945 }
946
947 }
948
949 // check if the filter has an wave in node
950 NodeIndex = MMixerGetIndexOfGuid(NodeTypes, &KSNODETYPE_ADC);
951 if (NodeIndex != MAXULONG)
952 {
953 // it has
954 Status = MMixerInitializeFilter(MixerContext, MixerList, hMixer, DeviceName, NodeTypes, NodeConnections, PinCount, NodeIndex, TRUE);
955 DPRINT("MMixerInitializeFilter Status %u\n", Status);
956 // check for success
957 if (Status == MM_STATUS_SUCCESS)
958 {
959 // increment mixer count
960 (*DeviceCount)++;
961 }
962
963 }
964
965 //free resources
966 MixerContext->Free((PVOID)NodeTypes);
967 MixerContext->Free((PVOID)NodeConnections);
968
969 // done
970 return Status;
971 }