Skip to content

Commit b283668

Browse files
beginning dlna server
1 parent 54de03a commit b283668

File tree

107 files changed

+11184
-305
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+11184
-305
lines changed
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using MediaBrowser.Controller.Dlna;
2+
using ServiceStack;
3+
using ServiceStack.Web;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
8+
namespace MediaBrowser.Api.Dlna
9+
{
10+
[Route("/Dlna/{UuId}/description.xml", "GET", Summary = "Gets dlna server info")]
11+
[Route("/Dlna/{UuId}/description", "GET", Summary = "Gets dlna server info")]
12+
public class GetDescriptionXml
13+
{
14+
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
15+
public string UuId { get; set; }
16+
}
17+
18+
[Route("/Dlna/{UuId}/contentdirectory.xml", "GET", Summary = "Gets dlna content directory xml")]
19+
[Route("/Dlna/{UuId}/contentdirectory", "GET", Summary = "Gets the content directory xml")]
20+
public class GetContentDirectory
21+
{
22+
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
23+
public string UuId { get; set; }
24+
}
25+
26+
[Route("/Dlna/{UuId}/control", "POST", Summary = "Processes a control request")]
27+
public class ProcessControlRequest : IRequiresRequestStream
28+
{
29+
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
30+
public string UuId { get; set; }
31+
32+
public Stream RequestStream { get; set; }
33+
}
34+
35+
public class DlnaServerService : BaseApiService
36+
{
37+
private readonly IDlnaManager _dlnaManager;
38+
39+
public DlnaServerService(IDlnaManager dlnaManager)
40+
{
41+
_dlnaManager = dlnaManager;
42+
}
43+
44+
public object Get(GetDescriptionXml request)
45+
{
46+
var xml = _dlnaManager.GetServerDescriptionXml(GetRequestHeaders(), request.UuId);
47+
48+
return ResultFactory.GetResult(xml, "text/xml");
49+
}
50+
51+
public object Get(GetContentDirectory request)
52+
{
53+
var xml = _dlnaManager.GetContentDirectoryXml(GetRequestHeaders());
54+
55+
return ResultFactory.GetResult(xml, "text/xml");
56+
}
57+
58+
public object Post(ProcessControlRequest request)
59+
{
60+
var response = PostAsync(request).Result;
61+
62+
return ResultFactory.GetResult(response.Xml, "text/xml");
63+
}
64+
65+
private async Task<ControlResponse> PostAsync(ProcessControlRequest request)
66+
{
67+
using (var reader = new StreamReader(request.RequestStream))
68+
{
69+
return _dlnaManager.ProcessControlRequest(new ControlRequest
70+
{
71+
Headers = GetRequestHeaders(),
72+
InputXml = await reader.ReadToEndAsync().ConfigureAwait(false)
73+
});
74+
}
75+
}
76+
77+
private IDictionary<string, string> GetRequestHeaders()
78+
{
79+
var headers = new Dictionary<string, string>();
80+
81+
foreach (var key in Request.Headers.AllKeys)
82+
{
83+
headers[key] = Request.Headers[key];
84+
}
85+
86+
return headers;
87+
}
88+
}
89+
}

MediaBrowser.Api/DlnaService.cs MediaBrowser.Api/Dlna/DlnaService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66

7-
namespace MediaBrowser.Api
7+
namespace MediaBrowser.Api.Dlna
88
{
99
[Route("/Dlna/ProfileInfos", "GET", Summary = "Gets a list of profiles")]
1010
public class GetProfileInfos : IReturn<List<DeviceProfileInfo>>

MediaBrowser.Api/MediaBrowser.Api.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
<Link>Properties\SharedVersion.cs</Link>
6767
</Compile>
6868
<Compile Include="ChannelService.cs" />
69-
<Compile Include="DlnaService.cs" />
69+
<Compile Include="Dlna\DlnaServerService.cs" />
70+
<Compile Include="Dlna\DlnaService.cs" />
7071
<Compile Include="Movies\CollectionService.cs" />
7172
<Compile Include="Music\AlbumsService.cs" />
7273
<Compile Include="AppThemeService.cs" />

MediaBrowser.Api/Playback/BaseStreamingService.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,12 @@ protected async Task<StreamState> GetState(StreamRequest request, CancellationTo
13411341
RequestedUrl = url
13421342
};
13431343

1344+
if (!string.IsNullOrWhiteSpace(request.AudioCodec))
1345+
{
1346+
state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
1347+
state.Request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault();
1348+
}
1349+
13441350
var item = string.IsNullOrEmpty(request.MediaSourceId) ?
13451351
DtoService.GetItemByDtoId(request.Id) :
13461352
DtoService.GetItemByDtoId(request.MediaSourceId);
@@ -1492,10 +1498,10 @@ protected async Task<StreamState> GetState(StreamRequest request, CancellationTo
14921498
videoRequest.VideoCodec = "copy";
14931499
}
14941500

1495-
//if (state.AudioStream != null && CanStreamCopyAudio(request, state.AudioStream))
1496-
//{
1497-
// request.AudioCodec = "copy";
1498-
//}
1501+
if (state.AudioStream != null && CanStreamCopyAudio(request, state.AudioStream, state.SupportedAudioCodecs))
1502+
{
1503+
request.AudioCodec = "copy";
1504+
}
14991505
}
15001506

15011507
return state;
@@ -1587,10 +1593,10 @@ private bool CanStreamCopyVideo(VideoStreamRequest request, MediaStream videoStr
15871593
return SupportsAutomaticVideoStreamCopy;
15881594
}
15891595

1590-
private bool CanStreamCopyAudio(StreamRequest request, MediaStream audioStream)
1596+
private bool CanStreamCopyAudio(StreamRequest request, MediaStream audioStream, List<string> supportedAudioCodecs)
15911597
{
15921598
// Source and target codecs must match
1593-
if (string.IsNullOrEmpty(request.AudioCodec) || !string.Equals(request.AudioCodec, audioStream.Codec, StringComparison.OrdinalIgnoreCase))
1599+
if (string.IsNullOrEmpty(audioStream.Codec) || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparer.OrdinalIgnoreCase))
15941600
{
15951601
return false;
15961602
}
@@ -1623,7 +1629,7 @@ private bool CanStreamCopyAudio(StreamRequest request, MediaStream audioStream)
16231629
}
16241630
}
16251631

1626-
return SupportsAutomaticVideoStreamCopy;
1632+
return true;
16271633
}
16281634

16291635
protected virtual bool SupportsAutomaticVideoStreamCopy

MediaBrowser.Api/Playback/StreamState.cs

+3
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ public VideoStreamRequest VideoRequest
6767
public string AudioSync = "1";
6868
public string VideoSync = "vfr";
6969

70+
public List<string> SupportedAudioCodecs { get; set; }
71+
7072
public StreamState(ILiveTvManager liveTvManager, ILogger logger)
7173
{
7274
_liveTvManager = liveTvManager;
7375
_logger = logger;
76+
SupportedAudioCodecs = new List<string>();
7477
}
7578

7679
public string InputAudioSync { get; set; }

MediaBrowser.Common.Implementations/Networking/BaseNetworkManager.cs

+40-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,44 @@ public abstract class BaseNetworkManager
1515
/// </summary>
1616
/// <returns>IPAddress.</returns>
1717
public IEnumerable<string> GetLocalIpAddresses()
18+
{
19+
var list = GetIPsDefault().Where(i => !IPAddress.IsLoopback(i)).Select(i => i.ToString()).ToList();
20+
21+
if (list.Count > 0)
22+
{
23+
return list;
24+
}
25+
26+
return GetLocalIpAddressesFallback();
27+
}
28+
29+
private IEnumerable<IPAddress> GetIPsDefault()
30+
{
31+
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
32+
{
33+
var props = adapter.GetIPProperties();
34+
var gateways = from ga in props.GatewayAddresses
35+
where !ga.Address.Equals(IPAddress.Any)
36+
select true;
37+
38+
if (!gateways.Any())
39+
{
40+
continue;
41+
}
42+
43+
foreach (var uni in props.UnicastAddresses)
44+
{
45+
var address = uni.Address;
46+
if (address.AddressFamily != AddressFamily.InterNetwork)
47+
{
48+
continue;
49+
}
50+
yield return address;
51+
}
52+
}
53+
}
54+
55+
private IEnumerable<string> GetLocalIpAddressesFallback()
1856
{
1957
var host = Dns.GetHostEntry(Dns.GetHostName());
2058

@@ -25,7 +63,7 @@ public IEnumerable<string> GetLocalIpAddresses()
2563
.Select(i => i.ToString())
2664
.Reverse();
2765
}
28-
66+
2967
/// <summary>
3068
/// Gets a random port number that is currently available
3169
/// </summary>
@@ -50,6 +88,7 @@ public string GetMacAddress()
5088
.Select(i => BitConverter.ToString(i.GetPhysicalAddress().GetAddressBytes()))
5189
.FirstOrDefault();
5290
}
91+
5392
/// <summary>
5493
/// Parses the specified endpointstring.
5594
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
3+
namespace MediaBrowser.Controller.Dlna
4+
{
5+
public class ControlRequest
6+
{
7+
public IDictionary<string, string> Headers { get; set; }
8+
9+
public string InputXml { get; set; }
10+
11+
public ControlRequest()
12+
{
13+
Headers = new Dictionary<string, string>();
14+
}
15+
}
16+
17+
public class ControlResponse
18+
{
19+
public IDictionary<string, string> Headers { get; set; }
20+
21+
public string Xml { get; set; }
22+
23+
public ControlResponse()
24+
{
25+
Headers = new Dictionary<string, string>();
26+
}
27+
}
28+
}

MediaBrowser.Controller/Dlna/IDlnaManager.cs

+22
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,27 @@ public interface IDlnaManager
5555
/// <param name="deviceInfo">The device information.</param>
5656
/// <returns>DeviceProfile.</returns>
5757
DeviceProfile GetProfile(DeviceIdentification deviceInfo);
58+
59+
/// <summary>
60+
/// Gets the server description XML.
61+
/// </summary>
62+
/// <param name="headers">The headers.</param>
63+
/// <param name="serverUuId">The server uu identifier.</param>
64+
/// <returns>System.String.</returns>
65+
string GetServerDescriptionXml(IDictionary<string, string> headers, string serverUuId);
66+
67+
/// <summary>
68+
/// Gets the content directory XML.
69+
/// </summary>
70+
/// <param name="headers">The headers.</param>
71+
/// <returns>System.String.</returns>
72+
string GetContentDirectoryXml(IDictionary<string, string> headers);
73+
74+
/// <summary>
75+
/// Processes the control request.
76+
/// </summary>
77+
/// <param name="request">The request.</param>
78+
/// <returns>ControlResponse.</returns>
79+
ControlResponse ProcessControlRequest(ControlRequest request);
5880
}
5981
}

MediaBrowser.Controller/LiveTv/LiveStreamInfo.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-

1+
using MediaBrowser.Model.Entities;
2+
using System.Collections.Generic;
3+
24
namespace MediaBrowser.Controller.LiveTv
35
{
46
public class LiveStreamInfo
@@ -20,5 +22,22 @@ public class LiveStreamInfo
2022
/// </summary>
2123
/// <value>The identifier.</value>
2224
public string Id { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the media container.
28+
/// </summary>
29+
/// <value>The media container.</value>
30+
public string MediaContainer { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the media streams.
34+
/// </summary>
35+
/// <value>The media streams.</value>
36+
public List<MediaStream> MediaStreams { get; set; }
37+
38+
public LiveStreamInfo()
39+
{
40+
MediaStreams = new List<MediaStream>();
41+
}
2342
}
2443
}

MediaBrowser.Controller/MediaBrowser.Controller.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="Channels\Channel.cs" />
7979
<Compile Include="Collections\CollectionCreationOptions.cs" />
8080
<Compile Include="Collections\ICollectionManager.cs" />
81+
<Compile Include="Dlna\ControlRequest.cs" />
8182
<Compile Include="Dlna\IDlnaManager.cs" />
8283
<Compile Include="Drawing\IImageProcessor.cs" />
8384
<Compile Include="Drawing\ImageFormat.cs" />

MediaBrowser.Dlna/Common/Argument.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+

2+
namespace MediaBrowser.Dlna.Common
3+
{
4+
public class Argument
5+
{
6+
public string Name { get; set; }
7+
8+
public string Direction { get; set; }
9+
10+
public string RelatedStateVariable { get; set; }
11+
}
12+
}

MediaBrowser.Dlna/PlayTo/DeviceIcon.cs MediaBrowser.Dlna/Common/DeviceIcon.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace MediaBrowser.Dlna.PlayTo
2+
namespace MediaBrowser.Dlna.Common
33
{
44
public class DeviceIcon
55
{

MediaBrowser.Dlna/PlayTo/DeviceService.cs MediaBrowser.Dlna/Common/DeviceService.cs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace MediaBrowser.Dlna.PlayTo
2+
namespace MediaBrowser.Dlna.Common
33
{
44
public class DeviceService
55
{
@@ -13,15 +13,6 @@ public class DeviceService
1313

1414
public string EventSubUrl { get; set; }
1515

16-
public DeviceService(string serviceType, string serviceId, string scpdUrl, string controlUrl, string eventSubUrl)
17-
{
18-
ServiceType = serviceType;
19-
ServiceId = serviceId;
20-
ScpdUrl = scpdUrl;
21-
ControlUrl = controlUrl;
22-
EventSubUrl = eventSubUrl;
23-
}
24-
2516
public override string ToString()
2617
{
2718
return string.Format("{0}", ServiceId);
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
3+
namespace MediaBrowser.Dlna.Common
4+
{
5+
public class ServiceAction
6+
{
7+
public string Name { get; set; }
8+
9+
public List<Argument> ArgumentList { get; set; }
10+
11+
public override string ToString()
12+
{
13+
return Name;
14+
}
15+
16+
public ServiceAction()
17+
{
18+
ArgumentList = new List<Argument>();
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)