|
1 | | -using SharpMp4; |
| 1 | +using Microsoft.Extensions.Configuration; |
| 2 | +using SharpMp4; |
2 | 3 | using SharpRTSPServer; |
3 | 4 | using System; |
4 | 5 | using System.Collections.Generic; |
5 | 6 | using System.IO; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Timers; |
8 | 9 |
|
9 | | -// TODO config file |
10 | | -const string hostName = "127.0.0.1"; |
11 | | -const ushort port = 8554; |
12 | | -const string fileName = "frag_bunny.mp4"; |
13 | | -const string userName = "admin"; |
14 | | -const string password = "password"; |
| 10 | +IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); |
| 11 | +string hostName = config["HostName"]; |
| 12 | +ushort port = ushort.Parse(config["Port"]); |
| 13 | +string fileName = config["FilePath"]; |
| 14 | +string userName = config["UserName"]; |
| 15 | +string password = config["Password"]; |
15 | 16 |
|
16 | 17 | using (var server = new RTSPServer(port, userName, password)) |
17 | 18 | { |
|
20 | 21 | uint audioTrackId = 0; |
21 | 22 | TrakBox audioTrackBox = null; |
22 | 23 | TrakBox videoTrackBox = null; |
23 | | - AudioSampleEntryBox audioSampleEntry = null; |
24 | 24 | double videoFrameRate = 0; |
25 | | - int audioSamplingRate = 0; |
26 | 25 |
|
27 | 26 | // frag_bunny.mp4 audio is not playable in VLC on Windows 11 (works on MacOS) |
28 | 27 | using (Stream fs = new BufferedStream(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))) |
|
62 | 61 | if (audioTrackBox != null) |
63 | 62 | { |
64 | 63 | audioTrackId = fmp4.FindAudioTrackID().First(); |
65 | | - audioSampleEntry = audioTrackBox.GetAudioSampleEntryBox(); |
66 | 64 |
|
67 | | - var aac = audioTrackBox.GetMdia().GetMinf().GetStbl().GetStsd().Children.FirstOrDefault(x => x.Type == AudioSampleEntryBox.TYPE3) as AudioSampleEntryBox; |
68 | | - if (aac != null) |
| 65 | + var audioSampleEntry = audioTrackBox.GetAudioSampleEntryBox(); |
| 66 | + if (audioSampleEntry.Type == AudioSampleEntryBox.TYPE3) // AAC |
69 | 67 | { |
70 | 68 | var audioConfigDescriptor = audioSampleEntry.GetAudioSpecificConfigDescriptor(); |
71 | | - audioSamplingRate = audioConfigDescriptor.GetSamplingFrequency(); |
| 69 | + int audioSamplingRate = audioConfigDescriptor.GetSamplingFrequency(); |
72 | 70 | server.AudioTrack = new SharpRTSPServer.AACTrack(await audioConfigDescriptor.ToBytes(), audioSamplingRate, audioConfigDescriptor.ChannelConfiguration); |
73 | 71 | } |
| 72 | + else |
| 73 | + { |
| 74 | + // unsupported audio |
| 75 | + } |
74 | 76 | } |
75 | 77 | } |
76 | 78 | } |
|
114 | 116 | { |
115 | 117 | var audioSampleDuration = SharpMp4.AACTrack.AAC_SAMPLE_SIZE; |
116 | 118 | var audioTrack = parsedMDAT[audioTrackId]; |
117 | | - audioTimer = new Timer(audioSampleDuration * 1000 / audioSamplingRate); |
| 119 | + audioTimer = new Timer(audioSampleDuration * 1000 / (server.AudioTrack as SharpRTSPServer.AACTrack).SamplingRate); |
118 | 120 | audioTimer.Elapsed += (s, e) => |
119 | 121 | { |
120 | 122 | server.FeedInRawAudioSamples((uint)(audioIndex * audioSampleDuration), new List<byte[]>() { audioTrack[0][audioIndex++ % audioTrack[0].Count] }); |
|
0 commit comments