-
Notifications
You must be signed in to change notification settings - Fork 0
/
TIAirtunesReceiver.m
525 lines (401 loc) · 16.9 KB
/
TIAirtunesReceiver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//
// TIAirtunesReceiver.m
// TIAirtunesReceiver
//
// Created by Tom Irving on 01/03/2012.
// Copyright 2012 Tom Irving. All rights reserved.
//
#import "TIAirtunesReceiver.h"
#import <Security/SecRandom.h>
#import <CommonCrypto/CommonCryptor.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#import "TIAirtunesEncryptor.h"
#import "TIAirtunesAdditions.h"
//==========================================================
// Trying to follow what's described here:
// http://git.zx2c4.com/Airtunes2/about/
//==========================================================
//==========================================================
#pragma mark - TIAirtunesReceiver -
//==========================================================
NSString * const TIAirtunesReceiverErrorDomain = @"TIAirtunesReceiverErrorDomain";
NSString * const TIAirtunesReceiverCommandAnnounce = @"ANNOUNCE";
NSString * const TIAirtunesReceiverCommandSetup = @"SETUP";
NSString * const TIAirtunesReceiverCommandRecord = @"RECORD";
NSString * const TIAirtunesReceiverCommandFlush = @"FLUSH";
NSString * const TIAirtunesReceiverCommandSetParameter = @"SET_PARAMETER";
NSString * const TIAirtunesReceiverCommandTeardown = @"TEARDOWN";
NSInteger const kTIAirtunesReceiverFramesPerPacket = 352;
NSInteger const kTIAirtunesReceiverDefaultVolume = 0;
@interface TIAirtunesReceiver ()
@property (nonatomic, readonly) NSString * localAddress;
@end
@interface TIAirtunesReceiver (Private)
- (void)annouce;
- (void)record;
- (void)flush;
- (NSInteger)sendCommand:(NSString *)command headers:(NSDictionary *)headers contentType:(NSString *)contentType body:(NSString *)body;
- (void)sendControlPayload:(NSData *)payload;
- (void)processResponse:(NSString *)response;
- (void)openControlStreams;
- (void)closeControlStreams;
- (void)openAudioStream;
- (void)closeAudioStream;
- (void)bufferOutgoingControlData;
- (NSError *)errorForCode:(NSInteger)code;
- (NSDictionary *)dictionaryFromResponse:(NSString *)response;
@end
@implementation TIAirtunesReceiver
@synthesize associatedService;
@synthesize name;
@synthesize address;
@synthesize passwordProtected;
@synthesize connected;
@synthesize disconnectionCallback;
@synthesize audioJackType;
- (id)initWithNetService:(NSNetService *)netService {
if ((self = [super init])){
encryptor = [[TIAirtunesEncryptor alloc] init];
associatedService = [netService retain];
name = [netService.friendlyName copy];
struct sockaddr_in * socketAddress = (struct sockaddr_in *)[[associatedService.addresses objectAtIndex:0] bytes];
address = [[NSString alloc] initWithFormat:@"%s", inet_ntoa(socketAddress->sin_addr)];
password = nil;
passwordProtected = [[netService.TXTRecordDictionary objectForKey:@"pw"] isEqualToString:@"true"];
RTSPURL = nil;
session = nil;
cSeq = 0;
controlInputStream = nil;
controlOutputStream = nil;
controlInputBuffer = [[NSMutableData alloc] init];
controlOutputBuffer = [[NSMutableData alloc] init];
audioServerPort = 6000; // Default
audioOutputStream = nil;
audioOuputBuffer = [[NSMutableData alloc] init];
aSeq = 0;
aTimestamp = 0;
connectionCallback = nil;
connected = NO;
announcing = NO;
recording = NO;
commandsAwaitingResponse = [[NSMutableDictionary alloc] init];
audioJackType = TIAirtunesReceiverAudioJackTypeUnplugged;
}
return self;
}
#pragma mark - Communication, yo
- (void)connectWithPassword:(NSString *)aPassword callback:(TIAirtunesReceiverConnectionCallback)callback {
if (!connected){
// Need to actually implement authentication.
// For now, make sure your Airport express isn't protected.
[password release];
password = [aPassword copy];
if (callback){
[connectionCallback release];
connectionCallback = [callback copy];
}
if (controlInputStream || controlOutputStream) [self closeControlStreams];
if ([associatedService getInputStream:&controlInputStream outputStream:&controlOutputStream]){
[self openControlStreams];
}
}
}
- (void)announce {
if (!announcing){
announcing = YES;
NSString * announceBody = [[NSString alloc] initWithFormat:@"v=0\r\n"
"o=iTunes %d O IN IP4 %@\r\n"
"s=iTunes\r\n"
"c=IN IP4 %@\r\n"
"t=0 0\r\n"
"m=audio 0 RTP/AVP 96\r\n"
"a=rtpmap:96 AppleLossless\r\n"
"a=fmtp:96 %ld 0 16 40 10 14 2 255 0 0 44100\r\n"
"a=rsaaeskey:%@\r\n"
"a=aesiv:%@\r\n"
"\r\n",
encryptor.sID, self.localAddress, address, kTIAirtunesReceiverFramesPerPacket,
encryptor.encodedAESKey, encryptor.encodedAESIV];
[self sendCommand:TIAirtunesReceiverCommandAnnounce headers:nil contentType:@"application/sdp" body:announceBody];
[announceBody release];
}
}
- (void)record {
if (!recording && session){
recording = YES;
NSDictionary * headers = [[NSDictionary alloc] initWithObjectsAndKeys:@"npt=0-", @"Range", @"seq=0;rtptime=0", @"RTP-Info", nil];
[self sendCommand:TIAirtunesReceiverCommandRecord headers:headers contentType:nil body:nil];
[headers release];
}
}
- (void)flush {
if (recording){
recording = NO;
NSDictionary * headers = [[NSDictionary alloc] initWithObjectsAndKeys:@"seq=0;rtptime=0", @"RTP-Info", nil];
[self sendCommand:TIAirtunesReceiverCommandFlush headers:headers contentType:nil body:nil];
[headers release];
}
}
- (void)setVolume:(NSInteger)volume {
NSString * volumeBody = [[NSString alloc] initWithFormat:@"volume: %ld", volume];
[self sendCommand:TIAirtunesReceiverCommandSetParameter headers:nil contentType:@"text/parameters" body:volumeBody];
[volumeBody release];
}
- (void)playAudioData:(NSData *)data {
if (!connected) [self connectWithPassword:password callback:nil];
// When audio works just playing a small sample, we should be able to get it working with a continuous stream.
// So you can just keep feeding in data to be played.
// if (data) [audioOuputBuffer appendData:data];
if (data) [audioOuputBuffer setData:data];
if (audioOutputStream.hasSpaceAvailable) [self bufferOutgoingAudio];
}
- (void)disconnect {
[self sendCommand:TIAirtunesReceiverCommandTeardown headers:nil contentType:nil body:nil];
[self closeControlStreams];
[RTSPURL release];
RTSPURL = nil;
[session release];
session = nil;
cSeq = 0;
}
- (void)processResponse:(NSString *)response {
NSDictionary * responseDict = [self dictionaryFromResponse:response];
NSInteger responseCSeq = [[responseDict objectForKey:@"CSeq"] integerValue];
NSNumber * numberKey = [NSNumber numberWithInteger:responseCSeq];
NSString * messageType = [commandsAwaitingResponse objectForKey:numberKey];
if ([messageType isEqualToString:TIAirtunesReceiverCommandAnnounce]){
announcing = NO;
if ([response contains:@"RTSP/1.0 200"]){
NSDictionary * headers = [[NSDictionary alloc] initWithObjectsAndKeys:@"RTP/AVP/TCP;unicast;interleaved=0-1;mode=record", @"Transport", nil];
[self sendCommand:TIAirtunesReceiverCommandSetup headers:headers contentType:nil body:nil];
[headers release];
}
else
{
NSInteger code = ([response contains:@"RTSP/1.0 401"] ? TIAirtunesReceiverErrorCodeUnauthorized :TIAirtunesReceiverErrorCodeReceiverInUse);
if (connectionCallback) connectionCallback([self errorForCode:code]);
}
}
else if ([messageType isEqualToString:TIAirtunesReceiverCommandSetup]){
NSString * transportString = [responseDict objectForKey:@"Transport"];
NSRange serverPortRange = [transportString rangeOfString:@"server_port="];
audioServerPort = [[transportString substringFromIndex:NSMaxRange(serverPortRange)] intValue];
NSString * jackStatus = [responseDict objectForKey:@"Audio-Jack-Status"];
BOOL jackConnected = !([jackStatus isEqualToString:@"disconnected"]);
audioJackType = TIAirtunesReceiverAudioJackTypeUnplugged;
if (jackConnected) audioJackType = ([jackStatus contains:@"digital"] ? TIAirtunesReceiverAudioJackTypeDigital :
TIAirtunesReceiverAudioJackTypeAnalog);
if (!connected){
[session release];
session = [[responseDict objectForKey:@"Session"] copy];
[self record];
}
}
else if ([messageType isEqualToString:TIAirtunesReceiverCommandRecord]){
aSeq = 0;
aTimestamp = 0;
if (!connected){
[self setVolume:kTIAirtunesReceiverDefaultVolume];
[self openAudioStream];
connected = YES;
if (connectionCallback) connectionCallback(audioJackType == TIAirtunesReceiverAudioJackTypeUnplugged ?
[self errorForCode:TIAirtunesReceiverErrorCodeAudioJackUnplugged] : nil);
}
}
else if ([messageType isEqualToString:TIAirtunesReceiverCommandSetParameter]){
}
else if ([messageType isEqualToString:TIAirtunesReceiverCommandFlush]){
aSeq = 0;
aTimestamp = 0;
}
[commandsAwaitingResponse removeObjectForKey:numberKey];
}
- (NSInteger)sendCommand:(NSString *)command headers:(NSDictionary *)headers contentType:(NSString *)contentType body:(NSString *)body {
if (!RTSPURL) RTSPURL = [[NSString alloc] initWithFormat:@"rtsp://%@/%d", self.localAddress, encryptor.sID];
NSMutableString * request = [[NSMutableString alloc] init];
[request appendFormat:@"%@ %@ RTSP/1.0\r\n", command, RTSPURL];
[request appendFormat:@"cSeq: %d\r\n", ++cSeq];
if (session) [request appendFormat:@"Session: %@\r\n", session];
if (contentType && body) [request appendFormat:@"Content-Type: %@\r\n"
"Content-Length: %d\r\n", contentType, body.length];
[request appendFormat:@"User-Agent: AirplayServer/1.0\r\n"];
[request appendFormat:@"Client-Instance: %@\r\n", encryptor.sCI];
for (NSString * key in headers){
[request appendFormat:@"%@: %@\r\n", key, [headers objectForKey:key]];
}
[request appendString:@"\r\n"];
if (body)[request appendString:body];
[self sendControlPayload:[request dataUsingEncoding:NSASCIIStringEncoding]];
[request release];
[commandsAwaitingResponse setObject:command forKey:[NSNumber numberWithInteger:cSeq]];
return cSeq;
}
- (void)sendControlPayload:(NSData *)payload {
if (payload) [controlOutputBuffer appendData:payload];
if (controlOutputStream.hasSpaceAvailable) [self bufferOutgoingControlData];
}
#pragma mark - Stream stuff
- (void)openControlStreams {
[controlInputStream setDelegate:self];
[controlOutputStream setDelegate:self];
[controlInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[controlOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[controlInputStream open];
[controlOutputStream open];
}
- (void)closeControlStreams {
[controlInputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[controlOutputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[controlInputStream release];
[controlOutputStream release];
controlInputStream = nil;
controlOutputStream = nil;
[self closeAudioStream];
connected = NO;
recording = NO;
announcing = NO;
disconnectionCallback();
}
- (void)openAudioStream {
[self closeAudioStream];
[NSStream getStreamsToHost:[NSHost hostWithAddress:address] port:audioServerPort inputStream:NULL outputStream:&audioOutputStream];
[audioOutputStream retain];
[audioOutputStream setDelegate:self];
[audioOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[audioOutputStream open];
}
- (void)closeAudioStream {
[audioOutputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[audioOutputStream release];
audioOutputStream = nil;
}
- (void)bufferOutgoingControlData {
if (controlOutputBuffer.length){
NSInteger length = 0;
if ((length = [controlOutputStream write:controlOutputBuffer.bytes maxLength:controlOutputBuffer.length]) > 0){
[controlOutputBuffer replaceBytesInRange:NSMakeRange(0, length) withBytes:NULL length:0];
}
else
{
[controlOutputBuffer setLength:0];
[self disconnect];
}
}
}
- (void)bufferOutgoingAudio {
if (audioOuputBuffer.length){
NSInteger audioLength = MIN(kTIAirtunesReceiverFramesPerPacket * 4, audioOuputBuffer.length);
unsigned char audioBuffer[audioLength];
[audioOuputBuffer getBytes:audioBuffer length:audioLength];
// This should work, right? The sound.m4a is ALAC encoded, so don't need to do that ourselves, or do we?
// The header follows the pattern described in the protocol.
// Maybe the encryptor isn't working, but I'm unsure how to test it.
NSMutableData * headerData = [[NSMutableData alloc] initWithLength:12];
[headerData replaceBytesInRange:NSMakeRange(0, 2) withBytes:(unsigned char[2]){0x80, (aSeq ? 0xe0 : 0x60)}];
[headerData replaceBytesInRange:NSMakeRange(2, 2) withBytes:(unsigned char[2]){((aSeq >> 8) & 0xFF), (aSeq & 0xFF)}];
[headerData replaceBytesInRange:NSMakeRange(4, 4) withBytes:(unsigned char[4]){((aTimestamp >> 24) & 0xFF), ((aTimestamp >> 16) & 0xFF),
((aTimestamp >> 8) & 0xFF), (aTimestamp & 0xFF)}];
[headerData replaceBytesInRange:NSMakeRange(8, 4) withBytes:(unsigned char[4]){0x30, 0x9f, 0xdc, 0x88}];
NSData * encryptedBytes = [encryptor encryptData:[NSData dataWithBytes:audioBuffer length:audioLength]];
NSMutableData * encodedData = [[NSMutableData alloc] initWithLength:(headerData.length + encryptedBytes.length)];
[encodedData setLength:(headerData.length + encryptedBytes.length)];
[encodedData replaceBytesInRange:NSMakeRange(0, headerData.length) withBytes:headerData.bytes];
[encodedData replaceBytesInRange:NSMakeRange(headerData.length, encryptedBytes.length) withBytes:encryptedBytes.bytes];
NSInteger length = 0;
if ((length = [audioOutputStream write:encodedData.bytes maxLength:encodedData.length]) > 0){
[audioOuputBuffer replaceBytesInRange:NSMakeRange(0, audioLength) withBytes:NULL length:0];
aSeq++;
aTimestamp += kTIAirtunesReceiverFramesPerPacket;
}
else
{
[audioOuputBuffer setLength:0];
[self disconnect];
}
[encodedData release];
}
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
if (eventCode == NSStreamEventOpenCompleted){
if (aStream != audioOutputStream){
if (!connected) [self announce];
}
}
if (eventCode == NSStreamEventHasBytesAvailable){
if (aStream == controlInputStream){
uint8_t buffer[1024];
while (controlInputStream.hasBytesAvailable){
NSInteger length = 0;
if ((length = [controlInputStream read:buffer maxLength:sizeof(buffer)])){
[controlInputBuffer appendData:[NSData dataWithBytes:buffer length:length]];
}
else
{
[controlInputBuffer setLength:0];
[self closeControlStreams];
}
}
NSString * response = [[NSString alloc] initWithData:controlInputBuffer encoding:NSUTF8StringEncoding];
[controlInputBuffer setLength:0];
[self processResponse:response];
[response release];
}
}
if (eventCode == NSStreamEventHasSpaceAvailable){
if (aStream == controlOutputStream) [self bufferOutgoingControlData];
if (aStream == audioOutputStream) [self bufferOutgoingAudio];
}
if (eventCode == NSStreamEventEndEncountered){
[self closeControlStreams]; // Easier to close everything
}
}
#pragma mark - Getters and that
- (NSString *)localAddress {
// Could make this return the real address, but it's not really used.
return @"192.168.1.2";
}
- (NSError *)errorForCode:(NSInteger)code {
NSString * message = @"An unknown error occured";
if (code == TIAirtunesReceiverErrorCodeAudioJackUnplugged) message = @"The Audio Jack is unplugged.";
if (code == TIAirtunesReceiverErrorCodeUnauthorized) message = @"The password was rejected.";
if (code == TIAirtunesReceiverErrorCodeReceiverInUse) message = @"This receiver is in use by another application.";
NSDictionary * userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:message, NSLocalizedDescriptionKey, nil];
NSError * error = [NSError errorWithDomain:TIAirtunesReceiverErrorDomain code:TIAirtunesReceiverErrorCodeAudioJackUnplugged userInfo:userInfo];
[userInfo release];
return error;
}
- (NSDictionary *)dictionaryFromResponse:(NSString *)response {
NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] init];
NSArray * lines = [response componentsSeparatedByString:@"\r\n"];
for (int i = 0; i < lines.count; i++){
NSArray * keyObjectPair = [[lines objectAtIndex:i] componentsSeparatedByString:@":"];
if (keyObjectPair.count > 1){
NSString * trimmedObject = [[keyObjectPair objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[dictionary setObject:trimmedObject forKey:[keyObjectPair objectAtIndex:0]];
}
}
return [dictionary autorelease];
}
#pragma mark - Other Shit
- (NSString *)description {
return [NSString stringWithFormat:@"<TIAirtunesReceiver %p; name: %@; passwordProtected: %@>",
self, name, (passwordProtected ? @"YES" : @"NO")];
}
- (void)dealloc {
[self disconnect];
[associatedService release];
[name release];
[address release];
[password release];
[controlInputBuffer release];
[controlOutputBuffer release];
[audioOuputBuffer release];
[connectionCallback release];
[disconnectionCallback release];
[encryptor release];
[super dealloc];
}
@end