|
1 | 1 | package com.github.serezhka.jap2server.internal.handler.audio; |
2 | 2 |
|
3 | 3 | import com.github.serezhka.jap2lib.AirPlay; |
| 4 | +import com.github.serezhka.jap2server.AirplayDataConsumer; |
4 | 5 | import io.netty.buffer.ByteBuf; |
5 | 6 | import io.netty.channel.ChannelHandlerContext; |
6 | 7 | import io.netty.channel.SimpleChannelInboundHandler; |
7 | 8 | import io.netty.channel.socket.DatagramPacket; |
8 | 9 | import org.slf4j.Logger; |
9 | 10 | import org.slf4j.LoggerFactory; |
10 | 11 |
|
11 | | -import java.io.IOException; |
12 | | -import java.nio.ByteBuffer; |
13 | | -import java.nio.channels.FileChannel; |
14 | | -import java.nio.file.Paths; |
15 | | -import java.nio.file.StandardOpenOption; |
16 | | -import java.util.Map; |
17 | | -import java.util.TreeMap; |
| 12 | +import java.util.Arrays; |
18 | 13 |
|
19 | 14 | public class AudioHandler extends SimpleChannelInboundHandler<DatagramPacket> { |
20 | 15 |
|
21 | 16 | private static final Logger log = LoggerFactory.getLogger(AudioHandler.class); |
22 | 17 |
|
23 | | - private static boolean aacDecoderInitialized = false; |
24 | | - |
25 | | - static { |
26 | | - try { |
27 | | - System.loadLibrary("libfdk-aac"); |
28 | | - aacDecoderInitialized = true; |
29 | | - } catch (UnsatisfiedLinkError e) { |
30 | | - log.warn("libfdk-aac.dll not found!"); |
31 | | - } |
32 | | - } |
33 | | - |
34 | 18 | private final AirPlay airPlay; |
| 19 | + private final AirplayDataConsumer dataConsumer; |
35 | 20 |
|
36 | | - private final Map<Integer, byte[]> buffer = new TreeMap<>(); |
| 21 | + private final AudioPacket[] buffer = new AudioPacket[512]; |
37 | 22 |
|
38 | | - private FileChannel fc; |
39 | 23 | private int prevSeqNum; |
40 | | - private boolean synced; |
| 24 | + private int packetsInBuffer; |
41 | 25 |
|
42 | | - public AudioHandler(AirPlay airPlay) throws IOException { |
| 26 | + public AudioHandler(AirPlay airPlay, AirplayDataConsumer dataConsumer) { |
43 | 27 | this.airPlay = airPlay; |
44 | | - if (aacDecoderInitialized) { |
45 | | - fc = FileChannel.open(Paths.get("audio.pcm"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); |
46 | | - init(); |
| 28 | + this.dataConsumer = dataConsumer; |
| 29 | + for (int i = 0; i < buffer.length; i++) { |
| 30 | + buffer[i] = new AudioPacket(); |
47 | 31 | } |
48 | 32 | } |
49 | 33 |
|
50 | | - public native void init(); |
51 | | - |
52 | | - public native void decodeFrame(byte[] input, byte[] output); |
53 | | - |
54 | 34 | @Override |
55 | 35 | protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { |
56 | 36 | ByteBuf content = msg.content(); |
57 | | - int contentLength = content.readableBytes(); |
58 | | - byte[] contentBytes = new byte[contentLength]; |
59 | | - content.readBytes(contentBytes); |
60 | | - |
61 | | - int flag = contentBytes[0] & 0xFF; |
62 | | - int type = contentBytes[1] & 0x7F; |
63 | 37 |
|
64 | | - // Version (V) is 2 and the payload type (PT) is 96 (DynamicRTP-Type-96). |
65 | | - |
66 | | - log.debug("Got audio packet. flag: {}, type: {}, length: {}", flag, type, contentLength); |
67 | | - |
68 | | - if (!aacDecoderInitialized) { |
69 | | - return; |
70 | | - } |
71 | | - |
72 | | - if (type == 96 || type == 86) { |
73 | | - int off = 0; |
74 | | - if (type == 86) { |
75 | | - off = 4; |
76 | | - } |
| 38 | + byte[] headerBytes = new byte[12]; |
| 39 | + content.readBytes(headerBytes); |
77 | 40 |
|
78 | | - int curSeqNo = ((contentBytes[off + 2] & 0xFF) << 8) | (contentBytes[off + 3] & 0xFF); |
79 | | - //log.debug("Current sequence number: " + curSeqNo); |
| 41 | + int flag = headerBytes[0] & 0xFF; |
| 42 | + int type = headerBytes[1] & 0x7F; |
80 | 43 |
|
81 | | - long timestamp = (contentBytes[off + 7] & 0xFF) | ((contentBytes[off + 6] & 0xFF) << 8) | ((contentBytes[off + 5] & 0xFF) << 16) | ((contentBytes[off + 4] & 0xFF) << 24); |
82 | | - //log.debug("Timestamp: " + timestamp); |
| 44 | + int curSeqNo = ((headerBytes[2] & 0xFF) << 8) | (headerBytes[3] & 0xFF); |
83 | 45 |
|
84 | | - long ssrc = (contentBytes[off + 11] & 0xFF) | ((contentBytes[off + 6] & 0xFF) << 8) | ((contentBytes[off + 9] & 0xFF) << 16) | ((contentBytes[off + 8] & 0xFF) << 24); |
85 | | - //log.debug("SSRC: " + ssrc); |
| 46 | + long timestamp = (headerBytes[7] & 0xFF) | ((headerBytes[6] & 0xFF) << 8) | |
| 47 | + ((headerBytes[5] & 0xFF) << 16) | ((headerBytes[4] & 0xFF) << 24); |
86 | 48 |
|
87 | | - if (contentLength > 16) { |
88 | | - byte[] audio = new byte[contentLength - 12]; |
89 | | - System.arraycopy(contentBytes, 12, audio, 0, audio.length); |
90 | | - airPlay.fairPlayDecryptAudioData(audio); |
| 49 | + long ssrc = (headerBytes[11] & 0xFF) | ((headerBytes[6] & 0xFF) << 8) | |
| 50 | + ((headerBytes[9] & 0xFF) << 16) | ((headerBytes[8] & 0xFF) << 24); |
91 | 51 |
|
92 | | - if (prevSeqNum == 0) { |
93 | | - prevSeqNum = curSeqNo; |
94 | | - synced = true; |
95 | | - |
96 | | - byte[] audioDecoded = new byte[480 * 4]; |
97 | | - decodeFrame(audio, audioDecoded); |
98 | | - |
99 | | - fc.write(ByteBuffer.wrap(audioDecoded)); |
100 | | - } else { |
101 | | - |
102 | | - if (curSeqNo - prevSeqNum == 1) { |
103 | | - prevSeqNum = curSeqNo; |
104 | | - synced = true; |
105 | | - |
106 | | - byte[] audioDecoded = new byte[480 * 4]; |
107 | | - decodeFrame(audio, audioDecoded); |
108 | | - |
109 | | - fc.write(ByteBuffer.wrap(audioDecoded)); |
110 | | - } else if (curSeqNo > prevSeqNum) { |
111 | | - |
112 | | - synced = false; |
113 | | - |
114 | | - byte[] audioDecoded = new byte[480 * 4]; |
115 | | - decodeFrame(audio, audioDecoded); |
| 52 | + // TODO handle bad cases (missing packets, curSeqNum - prevSeqNum > buffer.length, ...) |
| 53 | + if (curSeqNo <= prevSeqNum) { |
| 54 | + return; |
| 55 | + } |
116 | 56 |
|
117 | | - buffer.put(curSeqNo, audioDecoded); |
118 | | - } |
119 | | - } |
120 | | - } |
| 57 | + log.debug("Got audio packet. flag: {}, type: {}, prevSeqNum: {}, curSecNum: {}, audio packets in buffer: {}", |
| 58 | + flag, type, prevSeqNum, curSeqNo, packetsInBuffer); |
| 59 | + |
| 60 | + AudioPacket audioPacket = buffer[curSeqNo % buffer.length]; |
| 61 | + audioPacket |
| 62 | + .flag(flag) |
| 63 | + .type(type) |
| 64 | + .sequenceNumber(curSeqNo) |
| 65 | + .timestamp(timestamp) |
| 66 | + .ssrc(ssrc) |
| 67 | + .available(true) |
| 68 | + .encodedAudioSize(content.readableBytes()) |
| 69 | + .encodedAudio(packet -> content.readBytes(packet, 0, content.readableBytes())); |
| 70 | + packetsInBuffer++; |
| 71 | + |
| 72 | + while (dequeue(curSeqNo)) { |
| 73 | + curSeqNo++; |
| 74 | + } |
| 75 | + } |
121 | 76 |
|
122 | | - if (!synced) { |
123 | | - while (buffer.containsKey(prevSeqNum + 1)) { |
124 | | - fc.write(ByteBuffer.wrap(buffer.remove(prevSeqNum + 1))); |
125 | | - prevSeqNum++; |
126 | | - } |
127 | | - synced = buffer.isEmpty(); |
| 77 | + private boolean dequeue(int curSeqNo) throws Exception { |
| 78 | + if (curSeqNo - prevSeqNum == 1 || prevSeqNum == 0) { |
| 79 | + AudioPacket audioPacket = buffer[curSeqNo % buffer.length]; |
| 80 | + if (audioPacket.isAvailable()) { |
| 81 | + airPlay.decryptAudio(audioPacket.getEncodedAudio(), audioPacket.getEncodedAudioSize()); |
| 82 | + dataConsumer.onAudio(Arrays.copyOfRange(audioPacket.getEncodedAudio(), 0, audioPacket.getEncodedAudioSize())); |
| 83 | + audioPacket.available(false); |
| 84 | + prevSeqNum = curSeqNo; |
| 85 | + packetsInBuffer--; |
| 86 | + return true; |
128 | 87 | } |
129 | 88 | } |
| 89 | + return false; |
130 | 90 | } |
131 | 91 | } |
0 commit comments