Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media related interfaces A2DP #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions Mono.BlueZ.Console/A2DP_Test/BlueZA2DP.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DBus;
using org.freedesktop.DBus;
using Mono.BlueZ;
using Mono.BlueZ.DBus;
using System.Threading;

namespace Mono.BlueZ.Console
{
public class BlueZA2DP
{
private Bus _system;
public Exception _startupException { get; private set; }
private ManualResetEvent _started = new ManualResetEvent(false);

private const string BlueZRootPath = "/org/bluez";
private const string BlueZService = "org.bluez";

private ObjectPath ProfilePath = new ObjectPath("/profiles");
private ObjectPath EndpointPath = new ObjectPath("/led_web/endpoint");
private ObjectPath BlueZPath = new ObjectPath("/org/bluez");

private ObjectManager _objectManager;
private AgentManager1 _agentManager;
private ProfileManager1 _profileManager;

private Endpoint _endpoint;
private FileDescriptor _filedescriptor;

public BlueZA2DP()
{
var t = new Thread(DBusLoop);
t.IsBackground = true;
t.Start();
_started.WaitOne(60 * 1000);
_started.Close();
if (_startupException != null)
{
throw _startupException;
}
else
{
_objectManager = _system.GetObject<org.freedesktop.DBus.ObjectManager>(BlueZService, ObjectPath.Root);
_objectManager.InterfacesAdded += _objectManager_InterfacesAdded;
_objectManager.InterfacesRemoved += _objectManager_InterfacesRemoved;
}
}

public void RegisterEndpoint()//A1. Application Startup
{
var adapter = BlueZUtils.find_adapter();
var path = adapter.Key;

var media = _system.GetObject<Media1>(BlueZService, path);
//_endpoint = _system.GetObject<MediaEndpoint1>(BlueZService, ObjectPath.Root);
_endpoint = new Endpoint();

_system.Register(EndpointPath, _endpoint);
media.RegisterEndpoint(EndpointPath, A2DP.MP3_SINK_PROPERTIES);

var audiosource = _system.GetObject<AudioSource>(BlueZService, ObjectPath.Root);
audiosource.PropertyChanged += AudioSourcePropertyChanged;
}

private void GetTransportStream()
{
ObjectPath transportPath = _endpoint.TransportPath;
var transport = _system.GetObject<MediaTransport1>(BlueZService, transportPath);

_filedescriptor = transport.Acquire();
}

public FileDescriptor AudioStream
{
get { return _filedescriptor; }
}

private void _objectManager_InterfacesRemoved(ObjectPath path, string[] interfaces)
{
}

private void _objectManager_InterfacesAdded(ObjectPath path, IDictionary<string, IDictionary<string, object>> interfaces)
{
}

private void AudioSourcePropertyChanged(string name, object value)//C1. Start streaming event.
{
if (name == "State")
{
switch (value as string)
{
case AudioState.Playing:
GetTransportStream();
break;
//TODO implement changes to stop stream
}
}
}

private void DBusLoop()
{
try
{
_system = Bus.System;
}
catch (Exception ex)
{
_startupException = ex;
return;
}
finally
{
_started.Set();
}

while (true)
{
_system.Iterate();
}
}

private ProfileManager1 ProfileManager
{
get
{
if (_profileManager == null)
{
_profileManager = _system.GetObject<ProfileManager1>(BlueZService, new ObjectPath(BlueZRootPath));
}
return _profileManager;
}
}

private ObjectPath GetObjectPathForLocalObject(object obj)
{
return new ObjectPath("/" + obj.GetHashCode());
}
}
}
51 changes: 51 additions & 0 deletions Mono.BlueZ.Console/A2DP_Test/Endpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mono.BlueZ.DBus;
using Mono.BlueZ;
using DBus;

namespace Mono.BlueZ.Console
{
public class Endpoint : MediaEndpoint1
{
private ObjectPath transportPath;

public void ClearConfiguration(ObjectPath transport)//B2. Device Disconnection Events
{
transportPath = null;
return;
}

public void Release()
{
return;
}

public byte[] SelectConfiguration(byte[] capabilities)//B1. Device Connection Events
{
if(capabilities == A2DP.MP3_CAPABILITIES)
{
return A2DP.MP3_CONFIGURATION;
}
if(capabilities == A2DP.SBC_CAPABILITIES)
{
return A2DP.SBC_CONFIGURATION;
}
return null;
}

public void SetConfiguration(ObjectPath transport, IDictionary<string, object> properties)//B1. Device Connection Events
{
transportPath = transport;
return;
}

public ObjectPath TransportPath
{
get { return transportPath; }
}
}
}
6 changes: 4 additions & 2 deletions Mono.BlueZ.Console/Mono.BlueZ.Console.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -31,6 +31,8 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="A2DP_Test\BlueZA2DP.cs" />
<Compile Include="A2DP_Test\Endpoint.cs" />
<Compile Include="Program.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
Expand All @@ -55,4 +57,4 @@
<Name>dbus-sharp</Name>
</ProjectReference>
</ItemGroup>
</Project>
</Project>
14 changes: 9 additions & 5 deletions Mono.BlueZ.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ class MainClass
public static void Main (string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (GlobalHandler);
var bootstrap = new BlendMicroBootstrap ();
bootstrap.Run ();

//var bootstrap = new PebbleBootstrap ();
//bootstrap.Run (true, null);
}
//var bootstrap = new BlendMicroBootstrap ();
//bootstrap.Run ();

//var bootstrap = new PebbleBootstrap ();
//bootstrap.Run (true, null);

var a2dp = new BlueZA2DP();
a2dp.RegisterEndpoint();
}

static void GlobalHandler(object sender, UnhandledExceptionEventArgs args)
{
Expand Down
16 changes: 16 additions & 0 deletions Mono.BlueZ.DBus/Audio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using DBus;

namespace Mono.BlueZ.DBus
{
public delegate void PropertyChangedHandler(string name, object value);

public static class AudioState
{
public const string Disconnected = "disconnected";
public const string Connected = "connected";
public const string Connecting = "connecting";
public const string Playing = "playing";
}
}
17 changes: 17 additions & 0 deletions Mono.BlueZ.DBus/AudioSink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using DBus;

namespace Mono.BlueZ.DBus
{
[Interface("org.bluez.AudioSink")]
public interface AudioSink
{
void Connect();
void Disconnect();
IDictionary<string, object> GetProperties();
event PropertyChangedHandler PropertyChanged;
string State { get; }
bool Playing { get; }
}
}
16 changes: 16 additions & 0 deletions Mono.BlueZ.DBus/AudioSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using DBus;

namespace Mono.BlueZ.DBus
{
[Interface("org.bluez.AudioSource")]
public interface AudioSource
{
void Connect();
void Disconnect();
IDictionary<string, object> GetProperties();
event PropertyChangedHandler PropertyChanged;
string State { get; }
}
}
16 changes: 16 additions & 0 deletions Mono.BlueZ.DBus/MediaEndpoint1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using DBus;

namespace Mono.BlueZ.DBus
{
// on /org/bluez/hciX
[Interface("org.bluez.MediaEndpoint1")]
public interface MediaEndpoint1
{
void SetConfiguration(ObjectPath transport, IDictionary<string,object> properties);
byte[] SelectConfiguration(byte[] capabilities);
void ClearConfiguration(ObjectPath transport);
void Release();
}
}
31 changes: 31 additions & 0 deletions Mono.BlueZ.DBus/MediaTransport1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using DBus;

namespace Mono.BlueZ.DBus
{
// on /org/bluez/hciX
[Interface("org.bluez.MediaTransport1")]
public interface MediaTransport1
{
FileDescriptor Acquire();
//TODO fd, uint16, uint16 Acquire() "Acquire transport file descriptor and the MTU for readand write respectively."
FileDescriptor TryAcquire();
//TODO fd, uint16, uint16 TryAcquire()
void Release();
ObjectPath Device {get;}
string UUID {get;}
byte Codec {get;}
byte[] Configuration {get;}
string State {get;}
ushort Delay {get;set;}
ushort Volume {get;set;}
}

public static class TransportState
{
public const string Idle = "idle";
public const string Pending = "pending";
public const string Active = "active";
}
}
9 changes: 7 additions & 2 deletions Mono.BlueZ.DBus/Mono.BlueZ.DBus.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -31,6 +31,11 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Audio.cs" />
<Compile Include="AudioSink.cs" />
<Compile Include="AudioSource.cs" />
<Compile Include="MediaEndpoint1.cs" />
<Compile Include="MediaTransport1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleObjectManager.cs" />
<Compile Include="AgentManager1.cs" />
Expand Down Expand Up @@ -64,4 +69,4 @@
<Name>dbus-sharp</Name>
</ProjectReference>
</ItemGroup>
</Project>
</Project>
Loading