一、编译lib库
1、编译openssl
编译librtmp需要用到openssl,所以先编译openssl,下载脚本OpenSSL。
编译目录下的build-libssl.sh
文件,整个编译过程需要一段时间
新建文件夹librtmp
,将ssl编译后的两个文件夹lib
与include
拷入
2、编译
在librtmp目录下,
touch build.sh
将下面的脚本复制进去,注意要修改的地方,SDKVERSION
为当前mac上最新的iOS SDK的版本,可以通过得到
ls /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs
#!/bin/sh
###########################################################################
# Change values here
SDKVERSION="11.2"
###########################################################################
# #
# Don't change anything under this line! #
# #
###########################################################################
CURRENTPATH=`pwd`
ARCHS="i386 x86_64 armv7 armv7s arm64"
LIBRTMPREPO="git://git.ffmpeg.org/rtmpdump"
BUILDPATH="${CURRENTPATH}/build"
LIBPATH="${CURRENTPATH}/lib"
INCLUDEPATH="${CURRENTPATH}/include"
SRCPATH="${CURRENTPATH}/src"
LIBRTMP="librtmp.a"
DEVELOPER=`xcode-select -print-path`
if [ ! -d "$DEVELOPER" ]; then
echo "xcode path is not set correctly $DEVELOPER does not exist (most likely because of xcode > 4.3)"
echo "run"
echo "sudo xcode-select -switch <xcode path>"
echo "for default installation:"
echo "sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer"
exit 1
fi
# Check whether openssl has already installed on the machine or not.
# libcrypt.a / libssl.a
set -e
echo 'Check openssl installation'
if [ -f "${LIBPATH}/libcrypto.a" ] && [ -f "${LIBPATH}/libssl.a" ] && [ -d "${INCLUDEPATH}/openssl" ]; then
echo 'Openssl for iOS has already installed, no need to install openssl'
else
echo 'Openssl for iOS not found, will install openssl for iOS'
./build-libssl.sh
echo 'Succeeded to install openssl'
fi
# Download librtmp source code from git repository
# We assuem the user already installed git client.
echo 'Clone librtmp git repository'
# Remove the directory if already exist
rm -rf "${SRCPATH}/rtmpdump"
git clone ${LIBRTMPREPO} src/rtmpdump
cd "${SRCPATH}/rtmpdump/librtmp"
LIBRTMP_REPO=""
for ARCH in ${ARCHS}
do
if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ];
then
PLATFORM="iPhoneSimulator"
else
PLATFORM="iPhoneOS"
fi
export CROSS_TOP="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer"
export CROSS_SDK="${PLATFORM}${SDKVERSION}.sdk"
export BUILD_TOOLS="${DEVELOPER}"
echo "Building librtmp for ${PLATFORM} ${SDKVERSION} ${ARCH}"
echo "Please wait..."
# add arch to CC=
sed -ie "s!AR=\$(CROSS_COMPILE)ar!AR=/usr/bin/ar!" "Makefile"
sed -ie "/CC=\$(CROSS_COMPILE)gcc/d" "Makefile"
echo "CC=\$(CROSS_COMPILE)gcc -arch ${ARCH}" >> "Makefile"
export CROSS_COMPILE="${DEVELOPER}/usr/bin/"
export XCFLAGS="-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -miphoneos-version-min=7.0 -I${INCLUDEPATH} -arch ${ARCH}"
if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ];
then
export XLDFLAGS="-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -miphoneos-version-min=7.0 -L${LIBPATH} -arch ${ARCH}"
else
export XLDFLAGS="-isysroot ${CROSS_TOP}/SDKs/${CROSS_SDK} -miphoneos-version-min=7.0 -L${LIBPATH} -arch ${ARCH}"
fi
OUTPATH="${BUILDPATH}/librtmp-${PLATFORM}${SDKVERSION}-${ARCH}.sdk"
mkdir -p "${OUTPATH}"
LOG="${OUTPATH}/build-librtmp.log"
make SYS=darwin >> "${LOG}" 2>&1
make SYS=darwin prefix="${OUTPATH}" install >> "${LOG}" 2>&1
make clean >> "${LOG}" 2>&1
LIBRTMP_REPO+="${OUTPATH}/lib/${LIBRTMP} "
done
echo "Build universal library..."
lipo -create ${LIBRTMP_REPO}-output ${LIBPATH}/${LIBRTMP}
mkdir -p ${INCLUDEPATH}
cp -R ${BUILDPATH}/librtmp-iPhoneSimulator${SDKVERSION}-i386.sdk/include/ ${INCLUDEPATH}/
echo "Building done."
echo "Cleaning up..."
rm -rf ${SRCPATH}/rtmpdump
echo "Done."
保存并且执行
./build.sh
如果报权限错误,则先赋予权限
chmod +x build.sh
最终可以得到librtmp.a以及include文件
二、导入库
1、将include文件夹里的文件就像正常文件一样导入,如下图
2、lib文件夹里的静态库文件则要进行链接
注意除了三个生成的静态库文件,还要导入一个iOS的动态库,libz.tbd,否则运行会报错,导入之后错误消失。
三、推流
因为librtmp用的是C语言,为了方便调用,我用OC封装了一下一个类RTMPPuser。用来推流的代码是
RTMPPusher *pusher = [[RTMPPusher alloc]init];
BOOL success = [pusher connectWithURL:@"rtmp://192.168.0.16:1935/zbcs/room"];
if (success) {
NSString *htmlFile = [[NSBundle mainBundle]pathForResource:@"demo" ofType:@"flv"];
NSData *video = [NSData dataWithContentsOfFile:htmlFile];
[pusher pushFullVideoData:video chunkSize:10 * 5120];
}
[pusher closeRTMP];
1、建立连接
- (BOOL)connectWithURL:(NSString *)url{
_url = url;
@synchronized (self) {
_rtmp = [self newRTMP];
char *strUrl = (char *)[url cStringUsingEncoding:NSASCIIStringEncoding];
if (!RTMP_SetupURL(_rtmp, strUrl)) {
return NO;
}
RTMP_EnableWrite(_rtmp);
if (!RTMP_Connect(_rtmp, NULL) || !RTMP_ConnectStream(_rtmp, 0)) {
return NO;
}
_connected = RTMP_IsConnected(_rtmp);
return YES;
}
}
2、切片
- (void)pushFullVideoData:(NSData *)fullVideo chunkSize:(NSUInteger)size{
NSUInteger length = [fullVideo length];
NSUInteger chunkSize = 10 * 5120;
NSUInteger offset = 0;
// 将整视频切片后推送
do {
NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[fullVideo bytes] + offset
length:thisChunkSize
freeWhenDone:NO];
offset += thisChunkSize;
[self write:chunk];
sleep(1);
} while (offset < length);
}
3、写入推流
- (NSUInteger)write:(NSData *)data{
@synchronized (self) {
int sent = -1;
if (_connected) {
sent = RTMP_Write(_rtmp, [data bytes], (int)[data length]);
}
return sent;
}
}
以上代码可以在推流Demo下载