forked from chrisballinger/FFmpeg-iOS-Encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTSPServer.m
More file actions
executable file
·180 lines (153 loc) · 4.19 KB
/
Copy pathRTSPServer.m
File metadata and controls
executable file
·180 lines (153 loc) · 4.19 KB
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
//
// RTSPServer.m
// Encoder Demo
//
// Created by Geraint Davies on 17/01/2013.
// Copyright (c) 2013 GDCL http://www.gdcl.co.uk/license.htm
//
#import "RTSPServer.h"
#import "RTSPClientConnection.h"
#import "ifaddrs.h"
#import "arpa/inet.h"
@interface RTSPServer ()
{
CFSocketRef _listener;
NSMutableArray* _connections;
NSData* _configData;
int _bitrate;
}
- (RTSPServer*) init:(NSData*) configData;
- (void) onAccept:(CFSocketNativeHandle) childHandle;
@end
static void onSocket (
CFSocketRef s,
CFSocketCallBackType callbackType,
CFDataRef address,
const void *data,
void *info
)
{
RTSPServer* server = (__bridge RTSPServer*)info;
switch (callbackType)
{
case kCFSocketAcceptCallBack:
{
CFSocketNativeHandle* pH = (CFSocketNativeHandle*) data;
[server onAccept:*pH];
break;
}
default:
NSLog(@"unexpected socket event");
break;
}
}
@implementation RTSPServer
@synthesize bitrate = _bitrate;
+ (RTSPServer*) setupListener:(NSData*) configData
{
RTSPServer* obj = [RTSPServer alloc];
if (![obj init:configData])
{
return nil;
}
return obj;
}
- (RTSPServer*) init:(NSData*) configData
{
_configData = configData;
_connections = [NSMutableArray arrayWithCapacity:10];
CFSocketContext info;
memset(&info, 0, sizeof(info));
info.info = (void*)CFBridgingRetain(self);
_listener = CFSocketCreate(nil, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, onSocket, &info);
// must set SO_REUSEADDR in case a client is still holding this address
int t = 1;
setsockopt(CFSocketGetNative(_listener), SOL_SOCKET, SO_REUSEADDR, &t, sizeof(t));
struct sockaddr_in addr;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons(554);
CFDataRef dataAddr = CFDataCreate(nil, (const uint8_t*)&addr, sizeof(addr));
CFSocketError e = CFSocketSetAddress(_listener, dataAddr);
CFRelease(dataAddr);
if (e)
{
NSLog(@"bind error %d", (int) e);
}
CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(nil, _listener, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), rls, kCFRunLoopCommonModes);
CFRelease(rls);
return self;
}
- (NSData*) getConfigData
{
return _configData;
}
- (void) onAccept:(CFSocketNativeHandle) childHandle
{
RTSPClientConnection* conn = [RTSPClientConnection createWithSocket:childHandle server:self];
if (conn != nil)
{
@synchronized(self)
{
NSLog(@"Client connected");
[_connections addObject:conn];
}
}
}
- (void) onVideoData:(NSArray*) data time:(double) pts
{
@synchronized(self)
{
for (RTSPClientConnection* conn in _connections)
{
[conn onVideoData:data time:pts];
}
}
}
- (void) shutdownConnection:(id)conn
{
@synchronized(self)
{
NSLog(@"Client disconnected");
[_connections removeObject:conn];
}
}
- (void) shutdownServer
{
@synchronized(self)
{
for (RTSPClientConnection* conn in _connections)
{
[conn shutdown];
}
_connections = [NSMutableArray arrayWithCapacity:10];
if (_listener != nil)
{
CFSocketInvalidate(_listener);
_listener = nil;
}
}
}
+ (NSString*) getIPAddress
{
NSString* address;
struct ifaddrs *interfaces = nil;
// get all our interfaces and find the one that corresponds to wifi
if (!getifaddrs(&interfaces))
{
for (struct ifaddrs* addr = interfaces; addr != NULL; addr = addr->ifa_next)
{
if (([[NSString stringWithUTF8String:addr->ifa_name] isEqualToString:@"en0"]) &&
(addr->ifa_addr->sa_family == AF_INET))
{
struct sockaddr_in* sa = (struct sockaddr_in*) addr->ifa_addr;
address = [NSString stringWithUTF8String:inet_ntoa(sa->sin_addr)];
break;
}
}
}
freeifaddrs(interfaces);
return address;
}
@end