简体   繁体   中英

How to connect to a media server using Real Time Streaming Protocol (RTSP) from an iOS application?

I am new to iOS programming and working on an iPhone application to capture video and stream it live through a media server. I got a sample code from this link . But this code makes the iPhone as an RTSP server and the media server has to pull the stream from iPhone. Even though the present setup is working fine, I want iPhone to push the live stream to the media server as I believe in real time the server won't be trying to get the stream from all the iPhone users. The connection should be other way round to make it work (Correct me if I am wrong). Please tell me what's the proper way to do this. Please also tell me which framework or inbuilt functions are provided by Apple which can be used to send the Live stream to a server making iPhone work as a client and initiating the connection.

Please check the code:

Header file

#import <Foundation/Foundation.h>
#import "RTSPServer.h"

@interface RTSPClientConnection : NSObject


+ (RTSPClientConnection*) createWithSocket:(CFSocketNativeHandle) s server:(RTSPServer*) server;

- (void) onVideoData:(NSArray*) data time:(double) pts;
- (void) shutdown;

@end

Implementation File

#import "RTSPClientConnection.h"
#import "RTSPMessage.h"
#import "NALUnit.h"
#import "arpa/inet.h"

void tonet_short(uint8_t* p, unsigned short s)
{
//Code
}
void tonet_long(uint8_t* p, unsigned long l)
{
//Code
}

static const char* Base64Mapping = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const int max_packet_size = 1200;

NSString* encodeLong(unsigned long val, int nPad)
{
//Code
}

NSString* encodeToBase64(NSData* data)
{
//Code
}

enum ServerState
{
ServerIdle,
Setup,
Playing,
};

@interface RTSPClientConnection ()
{
CFSocketRef _s;
RTSPServer* _server;
CFRunLoopSourceRef _rls;

CFDataRef _addrRTP;
CFSocketRef _sRTP;
CFDataRef _addrRTCP;
CFSocketRef _sRTCP;
NSString* _session;
ServerState _state;
long _packets;
long _bytesSent;
long _ssrc;
BOOL _bFirst;

// time mapping using NTP
uint64_t _ntpBase;
uint64_t _rtpBase;
double _ptsBase;

// RTCP stats
long _packetsReported;
long _bytesReported;
NSDate* _sentRTCP;

// reader reports
CFSocketRef _recvRTCP;
CFRunLoopSourceRef _rlsRTCP;
}

- (RTSPClientConnection*) initWithSocket:(CFSocketNativeHandle) s Server:(RTSPServer*) server;
- (void) onSocketData:(CFDataRef)data;
- (void) onRTCP:(CFDataRef) data;

@end

static void onSocket (
           CFSocketRef s,
           CFSocketCallBackType callbackType,
           CFDataRef address,
           const void *data,
           void *info
           )
{
    RTSPClientConnection* conn = (__bridge RTSPClientConnection*)info;
    switch (callbackType)
    {
    case kCFSocketDataCallBack:
        [conn onSocketData:(CFDataRef) data];
        break;

    default:
        NSLog(@"unexpected socket event");
        break;
    }

}

static void onRTCP(CFSocketRef s,
               CFSocketCallBackType callbackType,
               CFDataRef address,
               const void *data,
               void *info
               )
{
    RTSPClientConnection* conn = (__bridge RTSPClientConnection*)info;
    switch (callbackType)
    {
    case kCFSocketDataCallBack:
        [conn onRTCP:(CFDataRef) data];
        break;

    default:
        NSLog(@"unexpected socket event");
        break;
    }
}

@implementation RTSPClientConnection

+ (RTSPClientConnection*) createWithSocket:(CFSocketNativeHandle) s server:(RTSPServer*) server
{
RTSPClientConnection* conn = [RTSPClientConnection alloc];
if ([conn initWithSocket:s Server:server] != nil)
{
    return conn;
}
return nil;
}

- (RTSPClientConnection*) initWithSocket:(CFSocketNativeHandle)s Server:(RTSPServer *)server
{
_state = ServerIdle;
_server = server;
CFSocketContext info;
memset(&info, 0, sizeof(info));
info.info = (void*)CFBridgingRetain(self);

_s = CFSocketCreateWithNative(nil, s, kCFSocketDataCallBack, onSocket, &info);

_rls = CFSocketCreateRunLoopSource(nil, _s, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), _rls, kCFRunLoopCommonModes);

return self;
}

- (void) onSocketData:(CFDataRef)data
{
    //Code
}

- (NSString*) makeSDP
{
    //Code
}

- (void) onVideoData:(NSArray*) data time:(double) pts
{
    //Code
}

- (void) writeHeader:(uint8_t*) packet marker:(BOOL) bMarker time:(double) pts
{
    //Code
}

- (void) sendPacket:(uint8_t*) packet length:(int) cBytes
{
    //Code
}

- (void) onRTCP:(CFDataRef) data
{
// NSLog(@"RTCP recv");
}

- (void) tearDown
{
    // Code
}

- (void) shutdown
{
[self tearDown];
@synchronized(self)
{
    CFSocketInvalidate(_s);
    _s = nil;
}
}
@end

Thanks.

I worked on it the last 4 months, there is no easy way to do it. You have to code your own RTCP protocol.

edit : i heard about RTPDump, but I never tried

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM