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