Skip to content

Commit

Permalink
Beginnings of the Loopback endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Psychlist1972 committed Feb 10, 2024
1 parent dd9fbf0 commit e50b3d0
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 91 deletions.
2 changes: 1 addition & 1 deletion build/staging/version/BundleInfo.wxi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Include>
<?define SetupVersionName="Developer Preview 5" ?>
<?define SetupVersionNumber="1.0.24040.2018" ?>
<?define SetupVersionNumber="1.0.24041.0202" ?>
</Include>
10 changes: 5 additions & 5 deletions src/api/Abstraction/LoopbackMidiAbstraction/AbstractionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class AbstractionState
return m_configurationManager;
}

//std::shared_ptr<MidiEndpointTable> GetEndpointTable()
//{
// return m_endpointTable;
//}
std::shared_ptr<MidiEndpointTable> GetEndpointTable()
{
return m_endpointTable;
}


HRESULT Cleanup()
Expand All @@ -58,5 +58,5 @@ class AbstractionState
wil::com_ptr<CMidi2LoopbackMidiEndpointManager> m_endpointManager;
wil::com_ptr<CMidi2LoopbackMidiConfigurationManager> m_configurationManager;


std::shared_ptr<MidiEndpointTable> m_endpointTable = std::make_shared<MidiEndpointTable>();
};
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@
<ClInclude Include="Midi2.LoopbackMidiBidi.h" />
<ClInclude Include="Midi2.LoopbackMidiConfigurationManager.h" />
<ClInclude Include="Midi2.LoopbackMidiEndpointManager.h" />
<ClInclude Include="MidiLoopbackDevice.h" />
<ClInclude Include="MidiLoopbackDeviceDefinition.h" />
<ClInclude Include="MidiLoopbackDeviceTable.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Resource.h" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
<ClInclude Include="Midi2.LoopbackMidiEndpointManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MidiLoopbackDevice.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MidiLoopbackDeviceTable.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MidiLoopbackDeviceDefinition.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Midi2.LoopbackMidiAbstraction.rc">
Expand Down
134 changes: 62 additions & 72 deletions src/api/Abstraction/LoopbackMidiAbstraction/Midi2.LoopbackMidiBidi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,66 +31,57 @@ CMidi2LoopbackMidiBiDi::Initialize(
m_callbackContext = Context;
m_endpointId = internal::NormalizeEndpointInterfaceIdWStringCopy(endpointId);

//if (Context != MIDI_PROTOCOL_MANAGER_ENDPOINT_CREATION_CONTEXT)
{
HRESULT hr = S_OK;
HRESULT hr = S_OK;

// TODO: This should use SWD properties and not a string search
// TODO: This should use SWD properties and not a string search

if (internal::EndpointInterfaceIdContainsString(m_endpointId, MIDI_VIRT_INSTANCE_ID_DEVICE_PREFIX))
{
TraceLoggingWrite(
MidiLoopbackMidiAbstractionTelemetryProvider::Provider(),
__FUNCTION__,
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
TraceLoggingPointer(this, "this"),
TraceLoggingWideString(L"Initializing device-side BiDi", "message"),
TraceLoggingWideString(m_endpointId.c_str(), "endpoint id")
);
if (internal::EndpointInterfaceIdContainsString(m_endpointId, MIDI_LOOP_INSTANCE_ID_A_PREFIX))
{
TraceLoggingWrite(
MidiLoopbackMidiAbstractionTelemetryProvider::Provider(),
__FUNCTION__,
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
TraceLoggingPointer(this, "this"),
TraceLoggingWideString(L"Initializing Side-A BiDi", "message"),
TraceLoggingWideString(m_endpointId.c_str(), "endpoint id")
);

m_callback = Callback;
m_callback = Callback;

// LOG_IF_FAILED(hr = AbstractionState::Current().GetEndpointTable()->OnDeviceConnected(m_endpointId, this));
}
else if (internal::EndpointInterfaceIdContainsString(m_endpointId, MIDI_VIRT_INSTANCE_ID_CLIENT_PREFIX))
{
TraceLoggingWrite(
MidiLoopbackMidiAbstractionTelemetryProvider::Provider(),
__FUNCTION__,
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
TraceLoggingPointer(this, "this"),
TraceLoggingWideString(L"Initializing client-side BiDi", "message"),
TraceLoggingWideString(m_endpointId.c_str(), "endpoint id")
);
m_isEndpointA = true;
}
else if (internal::EndpointInterfaceIdContainsString(m_endpointId, MIDI_LOOP_INSTANCE_ID_B_PREFIX))
{
TraceLoggingWrite(
MidiLoopbackMidiAbstractionTelemetryProvider::Provider(),
__FUNCTION__,
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
TraceLoggingPointer(this, "this"),
TraceLoggingWideString(L"Initializing Side-B BiDi", "message"),
TraceLoggingWideString(m_endpointId.c_str(), "endpoint id")
);

m_callback = Callback;
m_callback = Callback;

// LOG_IF_FAILED(hr = AbstractionState::Current().GetEndpointTable()->OnClientConnected(m_endpointId, this));
}
else
{
TraceLoggingWrite(
MidiLoopbackMidiAbstractionTelemetryProvider::Provider(),
__FUNCTION__,
TraceLoggingLevel(WINEVENT_LEVEL_ERROR),
TraceLoggingPointer(this, "this"),
TraceLoggingWideString(L"We don't understand the endpoint Id", "message"),
TraceLoggingWideString(m_endpointId.c_str(), "endpoint id")
);

// we don't understand this endpoint id

hr = E_FAIL;
}
m_isEndpointA = false;
}
else
{
// we don't understand this endpoint id

TraceLoggingWrite(
MidiLoopbackMidiAbstractionTelemetryProvider::Provider(),
__FUNCTION__,
TraceLoggingLevel(WINEVENT_LEVEL_ERROR),
TraceLoggingPointer(this, "this"),
TraceLoggingWideString(L"We don't understand the endpoint Id", "message"),
TraceLoggingWideString(m_endpointId.c_str(), "endpoint id")
);

return hr;
hr = E_FAIL;
}
//else
//{
// // we're in protocol negotiation
// m_isDeviceSide = false;
// return S_OK;
//}

return hr;
}

HRESULT
Expand All @@ -105,7 +96,6 @@ CMidi2LoopbackMidiBiDi::Cleanup()
);

m_callback = nullptr;
m_callbackContext = 0;

return S_OK;
}
Expand All @@ -115,7 +105,7 @@ HRESULT
CMidi2LoopbackMidiBiDi::SendMidiMessage(
PVOID Message,
UINT Size,
LONGLONG /*Position*/
LONGLONG Position
)
{
TraceLoggingWrite(
Expand All @@ -126,31 +116,31 @@ CMidi2LoopbackMidiBiDi::SendMidiMessage(
TraceLoggingWideString(m_endpointId.c_str(), "endpoint id")
);

// message received from the device

RETURN_HR_IF_NULL(E_INVALIDARG, Message);
RETURN_HR_IF(E_INVALIDARG, Size < sizeof(uint32_t));

//for (auto connection : m_linkedBiDiConnections)
//{
// auto cb = connection->GetCallback();
//
// if (cb != nullptr)
// {
// return cb->Callback(Message, Size, Position, m_callbackContext);
// }
//}
if (m_isEndpointA)
{
// send endpoint A to B
auto device = AbstractionState::Current().GetEndpointTable()->GetDevice(m_associationId);

if (device)
{
return device->SendMessageAToB(Message, Size, Position, m_callbackContext);
}
}
else
{
// send endpoint B to A
auto device = AbstractionState::Current().GetEndpointTable()->GetDevice(m_associationId);

//// if there's no linked bidi, it's not a failure. We just lose the message
//if (m_linkedBiDiCallback != nullptr)
//{
// //return m_linkedBiDi->SendMidiMessage(Message, Size, Position);
// return m_linkedBiDiCallback->Callback(Message, Size, Position, m_callbackContext);
//}
if (device)
{
return device->SendMessageBToA(Message, Size, Position, m_callbackContext);
}
}

return S_OK;

}

_Use_decl_annotations_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,30 @@ class CMidi2LoopbackMidiBiDi :
STDMETHOD(Callback)(_In_ PVOID, _In_ UINT, _In_ LONGLONG, _In_ LONGLONG);
STDMETHOD(Cleanup)();

void AssociationId(_In_ std::wstring newId) { m_associationId = newId; }

//void LinkAssociatedBiDi(_In_ wil::com_ptr_nothrow<CMidi2LoopbackMidiBiDi> associatedBiDi)
//{
// m_associatedBiDi = associatedBiDi;

// LOG_IF_FAILED(associatedBiDi->QueryInterface(__uuidof(IMidiCallback), (void**)&m_associatedBiDiCallback));
//}

//void UnlinkAssociatedBiDi()
//{
// m_associatedBiDi = nullptr;
// m_associatedBiDiCallback = nullptr;
//}

private:
//wil::com_ptr_nothrow<IMidiBiDi> m_linkedBiDi;
bool m_isEndpointA = false;

// std::vector<wil::com_ptr_nothrow<CMidi2LoopbackMidiBiDi>> m_linkedBiDiConnections{};
std::wstring m_associationId{};
//wil::com_ptr_nothrow<CMidi2LoopbackMidiBiDi> m_associatedBiDi;
//wil::com_ptr_nothrow<IMidiCallback> m_associatedBiDiCallback;


//wil::com_ptr_nothrow<IMidiCallback> m_linkedBiDiCallback;
wil::com_ptr_nothrow <IMidiCallback> m_callback;

LONGLONG m_callbackContext;

std::wstring m_endpointId{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ CMidi2LoopbackMidiEndpointManager::Initialize(

_Use_decl_annotations_
HRESULT
CMidi2LoopbackMidiEndpointManager::DeleteEndpointPair(std::wstring associationId)
CMidi2LoopbackMidiEndpointManager::DeleteEndpointPair(std::shared_ptr<MidiLoopbackDeviceDefinition> definition)
{

return S_OK;
Expand Down Expand Up @@ -111,7 +111,7 @@ CMidi2LoopbackMidiEndpointManager::CreateParentDevice()
_Use_decl_annotations_
HRESULT
CMidi2LoopbackMidiEndpointManager::CreateEndpointPair(
std::wstring associationId
std::shared_ptr<MidiLoopbackDeviceDefinition> definition
)
{
TraceLoggingWrite(
Expand All @@ -122,6 +122,36 @@ CMidi2LoopbackMidiEndpointManager::CreateEndpointPair(
);


// workflow:
// Configuration manager adds the entries to the endpoint table
// Then calls this function to create the pair of endpoints
//
// When endpoints BiDi are instantiated, they look at the endpoint
// table to see what they are supposed to connect to.
//
// They can be created in any order, so that will need to be
// checked for each created endpoint.
//
// Perhaps the table itself should just contain IMidiBiDi-like
// functions that are called by the endpoints? Then the endpoints
// don't need to know anything other than where to send the message.
// That would also be a pattern we can use for other types of
// routing.
//
// So perhaps a set of definitions associated by association id and
// then a related table of routing which has the required functions
// and pointers. Or maybe it all stays in the same definition class?
// and just rename it to MidiLoopbackEndpointDevice ?










return S_OK;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class CMidi2LoopbackMidiEndpointManager :
//));


HRESULT CreateEndpointPair(_In_ std::wstring associationId);
HRESULT DeleteEndpointPair(_In_ std::wstring associationId);
HRESULT CreateEndpointPair(_In_ std::shared_ptr<MidiLoopbackDeviceDefinition> definition);

HRESULT DeleteEndpointPair(_In_ std::shared_ptr<MidiLoopbackDeviceDefinition> definition);


//HRESULT DeleteEndpointPair(
Expand Down
Loading

0 comments on commit e50b3d0

Please sign in to comment.