[关闭]
@qvbicfhdx 2015-07-03T14:46:32.000000Z 字数 14954 阅读 14900

IOS手机直播Demo技术简介

1.测试服务器的搭建

测试服务器基于(nginx+rtmp-modual)

1.下载nginx
2.下载nginx-rtmp-module
3.编译安装nginx:
  1. /configure --add-module=/对应的目录/nginx-rtmp-module
  2. make
  3. make install

nginx 默认安装在/usr/local/nginx 目录下,
可执行程序: /usr/local/nginx/sbin/nginx
nginx服务器配置文件在:/usr/local/nginx/conf/下

4.配置nginx
  1. $ cd /usr/local/nginx/conf
  2. $ sudo vim nginx.conf

在文件后面加上rtmp协议的支持如下:

  1. rtmp {
  2. server {
  3. listen 1935;
  4. application myapp {
  5. live on;
  6. #record keyframes;
  7. #record_path /tmp;
  8. #record_max_size 128K;
  9. #record_interval 30s;
  10. #record_suffix .this.is.flv;
  11. #on_publish http://localhost:8080/publish;
  12. #on_play http://localhost:8080/play;
  13. #on_record_done http://localhost:8080/record_done;
  14. }
  15. application hls {
  16. live on;
  17. hls on;
  18. hls_path /tmp/app;
  19. hls_fragment 5s;
  20. }
  21. }
  22. }
  1. rtmp {
  2. server {
  3. listen 1935;
  4. application myapp {
  5. live on;
  6. #record keyframes;
  7. #record_path /tmp;
  8. #record_max_size 128K;
  9. #record_interval 30s;
  10. #record_suffix .this.is.flv;
  11. #on_publish http://localhost:8080/publish;
  12. #on_play http://localhost:8080/play;
  13. #on_record_done http://localhost:8080/record_done;
  14. }
  15. application hls {
  16. live on;
  17. hls on;
  18. hls_path /tmp/app;
  19. hls_fragment 5s;
  20. }## 标题 ##
  21. }
  22. }
5.启动nginx :

sudo /usr/local/nginx/sbin/nginx
打开 localhost 测试一下,有没有启动成功
成功的界面如下:
![屏幕快照 2015-07-02 下午4.12.05.png](./屏幕快照 2015-07-02 下午4.12.05.png)

sudo /usr/local/nginx/sbin/nginx
打开 localhost 测试一下,有没有启动成功
成功的界面会显示 welecome nginx 等信息

2.编译ffmpeg库

1.下载并编译libx264

编译脚本:

  1. #!/bin/sh
  2. CONFIGURE_FLAGS="--enable-static --enable-pic --disable-cli"
  3. ARCHS="arm64 armv7s x86_64 i386 armv7"
  4. # directories
  5. SOURCE="x264"
  6. FAT="x264-iOS"
  7. SCRATCH="scratch-x264"
  8. # must be an absolute path
  9. THIN=`pwd`/"thin-x264"
  10. # the one included in x264 does not work; specify full path to working one
  11. GAS_PREPROCESSOR=/usr/local/bin/gas-preprocessor.pl
  12. COMPILE="y"
  13. LIPO="y"
  14. if [ "$*" ]
  15. then
  16. if [ "$*" = "lipo" ]
  17. then
  18. # skip compile
  19. COMPILE=
  20. else
  21. ARCHS="$*"
  22. if [ $# -eq 1 ]
  23. then
  24. # skip lipo
  25. LIPO=
  26. fi
  27. fi
  28. fi
  29. if [ "$COMPILE" ]
  30. then
  31. CWD=`pwd`
  32. for ARCH in $ARCHS
  33. do
  34. echo "building $ARCH..."
  35. mkdir -p "$SCRATCH/$ARCH"
  36. cd "$SCRATCH/$ARCH"
  37. if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
  38. then
  39. PLATFORM="iPhoneSimulator"
  40. CPU=
  41. if [ "$ARCH" = "x86_64" ]
  42. then
  43. SIMULATOR="-mios-simulator-version-min=7.0"
  44. HOST=
  45. else
  46. SIMULATOR="-mios-simulator-version-min=5.0"
  47. HOST="--host=i386-apple-darwin"
  48. fi
  49. else
  50. PLATFORM="iPhoneOS"
  51. if [ $ARCH = "armv7s" ]
  52. then
  53. CPU="--cpu=swift"
  54. else
  55. CPU=
  56. fi
  57. SIMULATOR=
  58. if [ $ARCH = "arm64" ]
  59. then
  60. HOST="--host=aarch64-apple-darwin"
  61. else
  62. HOST="--host=arm-apple-darwin"
  63. fi
  64. fi
  65. XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
  66. CC="xcrun -sdk $XCRUN_SDK clang -Wno-error=unused-command-line-argument-hard-error-in-future -arch $ARCH"
  67. CFLAGS="-arch $ARCH $SIMULATOR"
  68. CXXFLAGS="$CFLAGS"
  69. LDFLAGS="$CFLAGS"
  70. CC=$CC $CWD/$SOURCE/configure \
  71. $CONFIGURE_FLAGS \
  72. $HOST \
  73. $CPU \
  74. --extra-cflags="$CFLAGS" \
  75. --extra-ldflags="$LDFLAGS" \
  76. --prefix="$THIN/$ARCH"
  77. mkdir extras
  78. ln -s $GAS_PREPROCESSOR extras
  79. make -j3 install
  80. cd $CWD
  81. done
  82. fi
  83. if [ "$LIPO" ]
  84. then
  85. echo "building fat binaries..."
  86. mkdir -p $FAT/lib
  87. set - $ARCHS
  88. CWD=`pwd`
  89. cd $THIN/$1/lib
  90. for LIB in *.a
  91. do
  92. cd $CWD
  93. lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
  94. done
  95. cd $CWD
  96. cp -rf $THIN/$1/include $FAT
  97. fi
2.下载并编译libfdk-aac

版本:fdk-aac-0.1.4
Fdk-aac编译脚本:

  1. #!/bin/bash
  2. SDKVERSION="8.3"
  3. LIB_PATH="fdk-aac-0.1.4"
  4. #ARCHS="armv7 armv7s i386"
  5. ARCHS="i386 armv7s armv7"
  6. #OUTPUTDIR="dependencies/lib_fdkaac"
  7. OUTPUTDIR="$LIB_PATH/build-ios/master"
  8. OLD_DEVELOPER_PATH="/Developer"
  9. NEW_DEVELOPER_PATH="/Applications/Xcode.app/Contents/Developer"
  10. # Get the install path
  11. if [ -d "$NEW_DEVELOPER_PATH" ]
  12. then
  13. DEVELOPER="$NEW_DEVELOPER_PATH"
  14. else
  15. DEVELOPER="$OLD_DEVELOPER_PATH"
  16. fi
  17. CurrentPath=$(cd "$(dirname "$0")"; pwd)
  18. LIB_PATH="$CurrentPath/$LIB_PATH"
  19. OUTPUTDIR="$CurrentPath/$OUTPUTDIR"
  20. cd $LIB_PATH
  21. for ARCH in ${ARCHS}
  22. do
  23. if [ "${ARCH}" == "i386" ];
  24. then
  25. PLATFORM="iPhoneSimulator"
  26. else
  27. PLATFORM="iPhoneOS"
  28. fi
  29. PLATFORM_SDK="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk"
  30. MIN_VERSION_FLAG="-miphoneos-version-min=${SDKVERSION}"
  31. HOST="${ARCH}-apple-darwin"
  32. export CC="${DEVELOPER}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
  33. export CFLAGS="${MIN_VERSION_FLAG} -arch ${ARCH}"
  34. export LDFLAGS="${MIN_VERSION_FLAG} -arch ${ARCH} -isysroot ${PLATFORM_SDK}"
  35. export LIBS="-L${PLATFORM_SDK}/usr/lib"
  36. export CXXFLAGS="${MIN_VERSION_FLAG} -arch ${ARCH} -I${PLATFORM_SDK}/usr/include"
  37. mkdir -p "${OUTPUTDIR}/${ARCH}"
  38. make clean
  39. echo "** CC=${CC}"
  40. echo "** CFLAGS=${CFLAGS}"
  41. echo "** LDFLAGS=${LDFLAGS}"
  42. echo "** LIBS=${LIBS}"
  43. echo "** CXXFLAGS=${CXXFLAGS}"
  44. ./configure --prefix="${OUTPUTDIR}/${ARCH}" --host="${HOST}" --with-sysroot="${PLATFORM_SDK}"
  45. make && make install && make clean
  46. done
  47. mkdir -p "${OUTPUTDIR}/universal/lib"
  48. cd "${OUTPUTDIR}/armv7/lib"
  49. for file in *.a
  50. do
  51. cd "${OUTPUTDIR}"
  52. xcrun -sdk iphoneos lipo -output universal/lib/$file -create -arch armv7 armv7/lib/$file -arch armv7s armv7s/lib/$file -arch i386 i386/lib/$file
  53. echo "Universal $file created."
  54. done
  55. cp -r ${OUTPUTDIR}/armv7/include ${OUTPUTDIR}/universal/
  56. echo "Done."
3.下载并编译librtmp

下载好,直接运行

  1. cd librtmp-iOS目录
  2. 运行 ./build-librtmp.sh
4.下载并编译ffmpeg

ffmpeg版本:2.0.2
编译shell脚本:

  1. #!/bin/bash
  2. ###########################################################################
  3. # Choose your ffmpeg version and your currently-installed iOS SDK version:
  4. #
  5. VERSION="2.0.2" #指定对应ffmpeg的版本号
  6. SDKVERSION=""
  7. #
  8. #
  9. ###########################################################################
  10. #
  11. # Don't change anything under this line!
  12. #
  13. ###########################################################################
  14. # No need to change this since xcode build will only compile in the
  15. # necessary bits from the libraries we create
  16. ARCHS="armv7 armv7s i386"
  17. DEVELOPER=`xcode-select -print-path`
  18. cd "`dirname \"$0\"`"
  19. REPOROOT=$(pwd)
  20. # Where we'll end up storing things in the end
  21. OUTPUTDIR="${REPOROOT}/dependencies"
  22. mkdir -p ${OUTPUTDIR}/include
  23. mkdir -p ${OUTPUTDIR}/lib
  24. mkdir -p ${OUTPUTDIR}/bin
  25. BUILDDIR="${REPOROOT}/build"
  26. mkdir -p $BUILDDIR
  27. # where we will keep our sources and build from.
  28. SRCDIR="${BUILDDIR}/src"
  29. mkdir -p $SRCDIR
  30. # where we will store intermediary builds
  31. INTERDIR="${BUILDDIR}/built"
  32. mkdir -p $INTERDIR
  33. ########################################
  34. cd $SRCDIR
  35. # Exit the script if an error happens
  36. set -e
  37. if [ ! -e "${SRCDIR}/ffmpeg-${VERSION}.tar.bz2" ]; then
  38. echo "Downloading ffmpeg-${VERSION}.tar.bz2"
  39. curl -LO http://ffmpeg.org/releases/ffmpeg-${VERSION}.tar.bz2
  40. else
  41. echo "Using ffmpeg-${VERSION}.tar.bz2"
  42. fi
  43. tar jxf ffmpeg-${VERSION}.tar.bz2 -C $SRCDIR
  44. cd "${SRCDIR}/ffmpeg-${VERSION}"
  45. set +e # don't bail out of bash script if ccache doesn't exist
  46. CCACHE=`which ccache`
  47. if [ $? == "0" ]; then
  48. echo "Building with ccache: $CCACHE"
  49. CCACHE="${CCACHE} "
  50. else
  51. echo "Building without ccache"
  52. CCACHE=""
  53. fi
  54. set -e # back to regular "bail out on error" mode
  55. for ARCH in ${ARCHS}
  56. do
  57. if [ "${ARCH}" == "i386" ];
  58. then
  59. PLATFORM="iPhoneSimulator"
  60. ETRA_CONFIG="--arch=i386 --disable-asm --enable-cross-compile --target-os=darwin --cpu=i386"
  61. EXTRA_CFLAGS="-arch i386"
  62. EXTRA_LDFLAGS="-I/Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk/usr/lib -mfpu=neon"
  63. else
  64. PLATFORM="iPhoneOS"
  65. EXTRA_CONFIG="--arch=arm --target-os=darwin --enable-cross-compile --cpu=cortex-a9 --disable-armv5te"
  66. EXTRA_CFLAGS="-w -arch ${ARCH} -mfpu=neon"
  67. EXTRA_LDFLAGS="-mfpu=neon"
  68. fi
  69. mkdir -p "${INTERDIR}/${ARCH}"
  70. echo "platform is ${PLATFORM}"
  71. ./configure --prefix="${INTERDIR}/${ARCH}" --sysroot="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk" --cc="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" --as='/usr/local/bin/gas-preprocessor.pl' --extra-cflags="${EXTRA_CFLAGS} -miphoneos-version-min=${SDKVERSION} -I${OUTPUTDIR}/include" --extra-ldflags="-arch ${ARCH} ${EXTRA_LDFLAGS} -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk -miphoneos-version-min=${SDKVERSION} -L${OUTPUTDIR}/lib" ${EXTRA_CONFIG} --extra-cxxflags="$CPPFLAGS -I${OUTPUTDIR}/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk" \
  72. --enable-cross-compile \
  73. --disable-doc \
  74. --disable-ffmpeg \
  75. --disable-ffplay \
  76. --disable-ffprobe \
  77. --disable-ffserver \
  78. --disable-asm \
  79. --disable-debug \
  80. --disable-symver \
  81. --disable-avdevice \
  82. --disable-avfilter \
  83. --disable-encoders \
  84. --disable-muxers \
  85. --disable-filters \
  86. --disable-devices \
  87. --disable-swscale \
  88. --disable-everything \
  89. --enable-protocol=librtmp \
  90. --enable-protocol=file \
  91. --enable-decoder=h264 \
  92. --enable-decoder=aac \
  93. --enable-decoder=nellymoser \
  94. --enable-decoder=mp3 \
  95. --enable-encoder=aac \
  96. --enable-encoder=libfdk_aac \
  97. --enable-encoder=libx264 \
  98. --enable-demuxer=h264 \
  99. --enable-demuxer=aac \
  100. --enable-demuxer=flv \
  101. --enable-demuxer=mp3 \
  102. --enable-muxer=flv \
  103. --enable-filter=aresample \
  104. --enable-hwaccel=h264_vaapi \
  105. --enable-hwaccel=h264_vda \
  106. --enable-hwaccel=h264_vdpau \
  107. --enable-version3 \
  108. --enable-librtmp \
  109. --enable-nonfree \
  110. --enable-gpl \
  111. --enable-libfdk-aac \
  112. extra-cflags=-I$(pwd)/librtmp/include \
  113. extra-ldflags=-L$(pwd)/librtmp/lib \
  114. extra-cflags=-I$(pwd)/fdk-aac-0.1.4/build-ios/master/universal/include \
  115. extra-ldflags=-L$(pwd)/fdk-aac-0.1.4/build-ios/master/universal/lib\
  116. extra-cflags=-I$(pwd)/include \
  117. extra-ldflags=-L$(pwd)/lib \
  118. echo "prepare to make!!!"
  119. make && make install && make clean
  120. done
  121. mkdir -p "${INTERDIR}/universal/lib"
  122. cd "${INTERDIR}/armv7/lib"
  123. for file in *.a
  124. do
  125. cd ${INTERDIR}
  126. xcrun -sdk iphoneos lipo -output universal/lib/$file -create -arch armv7 armv7/lib/$file -arch armv7s armv7s/lib/$file -arch i386 i386/lib/$file
  127. echo "Universal $file created."
  128. done
  129. cp -r ${INTERDIR}/armv7/include ${INTERDIR}/universal/
  130. echo "Done."

3.采集音视频

1.ios音频采集:

使用AudioQueue进行音频的录制,引用苹果官方文档的一张图说明一下:

使用AudioQueue进行音频的录制,引用苹果官方文档的一张图说明一下:
recording_callback_function_2x.png

  1. #define kNumberRecordBuffers 3
  2. @protocol AudioRecordDelegate
  3. // 音频回调委托,返回录制的PCM数据
  4. -(void)AudioDataOutputBuffer:(uint8_t *)audioBuffer bufferSize:(int)size;
  5. @end
  6. @interface AudioRecoder : NSObject
  7. {
  8. AudioQueueRef mQueue;
  9. AudioQueueBufferRef mBuffers[kNumberRecordBuffers];
  10. AudioStreamBasicDescription mRecordFormat;
  11. }
  12. @property id<AudioRecordDelegate> outDelegate;
  13. -(id)initWIthSampleRate:(int)sampleRate; //通过采样率生成一个实例
  14. -(void)setAudioRecordDelegate:(id<AudioRecordDelegate>)delegate;
  15. -(void)startRecord; // 开启录音
  16. -(void)stopRecord; // 暂停录音
  17. @end
  18. 核心的方法,只要在音频回调里:
  19. void MyInputBufferHandler(void * inUserData,
  20. AudioQueueRef inAQ,
  21. AudioQueueBufferRef inBuffer,
  22. const AudioTimeStamp * inStartTime,
  23. UInt32 inNumPackets,
  24. const AudioStreamPacketDescription* inPacketDesc)
  25. {
  26. AudioRecoder* audioRecord = (__bridge AudioRecoder *)(inUserData);
  27. if(audioRecord.outDelegate) {
  28. // 通过委托将音频回调给代理使用
  29. [audioRecord.outDelegate AudioDataOutputBuffer:(uint8_t *)inBuffer->mAudioData bufferSize:inBuffer->mAudioDataByteSize];
  30. }
  31. AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
  32. }
2.ios视频录制

因为我们需要获取视频的原始数据,所以视频录制使用AVCaptureVideoDataOutput这个类

  1. @class CamerRecordViewController;
  2. @protocol CamerRecordViewControllerDelegate <NSObject>
  3. // 录制的视频原始数据,kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange 格式
  4. - (void)videoOutPut:(uint8_t *)rawData dataSize:(size_t)bufferSize;
  5. @end
  6. @interface CamerRecordViewController : UIViewController
  7. @property (nonatomic,assign) id<CamerRecordViewControllerDelegate> delegate;
  8. @end

核心的方法,视频回调:

  1. #pragma mark - 视频输出代理
  2. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  3. {
  4. // 为媒体数据设置一个CMSampleBuffer的Core Video图像缓存对象
  5. CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  6. // 锁定pixel buffer的基地址
  7. if (kCVReturnSuccess == CVPixelBufferLockBaseAddress(imageBuffer, 0)) {
  8. // 得到pixel buffer的基地址
  9. uint8_t *bufferPtr = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer,0);
  10. uint8_t *uvPtr = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1);
  11. size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);
  12. NSLog(@"=== buffsize : %zu",bufferSize);
  13. bool isPlanar = CVPixelBufferIsPlanar(imageBuffer);
  14. if (isPlanar) {
  15. int planeCount = CVPixelBufferGetPlaneCount(imageBuffer);
  16. NSLog(@"=== planeCount : %d \n ",planeCount);
  17. }
  18. size_t ysize = 640*480;
  19. uint8_t *newbuffer = (uint8_t *)malloc(ysize*1.5);
  20. memcpy(newbuffer, bufferPtr, ysize*1.5);
  21. // if (self.delegate && [self.delegate respondsToSelector:@selector(videoOutPut:dataSize:)]) {
  22. //
  23. // [self.delegate videoOutPut:(uint8_t *)newbuffer dataSize:bufferSize];
  24. //
  25. // }
  26. [self videoOutPut:newbuffer dataSize:bufferSize];
  27. free(newbuffer);
  28. // 解锁pixel buffer
  29. CVPixelBufferUnlockBaseAddress(imageBuffer,0);
  30. }
  31. }

4.编码(AAC,H264)

编码分两步,一是AAC编码,二是H264的编码,两者都是基于FFMPEG库函数进行编码

AAC编码

引用网上的一张图进行说明编码的一个过程:


demo对应循环编码的函数如下:

simplest_ffmpeg_audio_encoder.jpg
demo对应循环编码的函数如下:

  1. int encoderAAC(BOBOAACEncoder *encoder,uint8_t *inputBuffer,int inputSize,char *outputBuffer,int *outSize)
  2. {
  3. /* AVFrame表示一祯原始的音频数据,因为编码的时候需要一个AVFrame,,
  4. 在这里创建一个AVFrame,用来填充录音回调传过来的inputBuffer.里面是数据格式为PCM
  5. 调用FFMPEG的编码函数 avcodec_encode_audio2()后,将PCM-->AAC
  6. 编码压缩为AAC格式,并保存在AVPacket中。
  7. 再调用 av_interleaved_write_frame
  8. 我们将它写入到指定的推流地址上,FFMPEG将发包集成在它内部实现里
  9. */
  10. // pthread_mutex_lock(&encoder->mutex);
  11. AVFrame *frame = avcodec_alloc_frame();
  12. frame->nb_samples = encoder->pCodeCtx->frame_size;
  13. frame->format = encoder->pCodeCtx->sample_fmt;
  14. frame->sample_rate = encoder->pCodeCtx->sample_rate;
  15. frame->channels = encoder->pCodeCtx->channels;
  16. frame->pts = frame->nb_samples*frameIndex;
  17. av_init_packet(&encoder->packet);
  18. int gotFrame = 0;
  19. int ret = avcodec_fill_audio_frame(frame, encoder->pCodeCtx->channels, AV_SAMPLE_FMT_S16, (uint8_t*)inputBuffer, encoder->buffer_size, 0);
  20. if (ret<0) {
  21. printf("avcodec_fill_audio_frame error !\n");
  22. av_frame_free(&frame);
  23. av_free_packet(&encoder->packet);
  24. return -1;
  25. }
  26. encoder->packet.data = NULL;
  27. encoder->packet.size = 0;
  28. ret = avcodec_encode_audio2(encoder->pCodeCtx, &encoder->packet, frame, &gotFrame);
  29. frameIndex++;
  30. if (ret < 0) {
  31. printf("encoder error! \n");
  32. av_frame_free(&frame);
  33. av_free_packet(&encoder->packet);
  34. // pthread_mutex_unlock(&encoder->mutex);
  35. return -1;
  36. }
  37. if (gotFrame == 1) {
  38. encoder->packet.stream_index = encoder->pStream->index;
  39. encoder->packet.pts = frame->pts;
  40. ret = av_interleaved_write_frame(encoder->pFormatCtx, &encoder->packet);
  41. printf("[AAC]: audio encoder %d frame success \n",frameIndex);
  42. }
  43. av_frame_free(&frame);
  44. av_free_packet(&encoder->packet);
  45. // pthread_mutex_unlock(&encoder->mutex);
  46. return 1;
  47. }
H264编码

编码过程与AAC一样,注意H264编码时,需要将源数据转换为YUV420P的格式,编码函数如下:

  1. int encoderH264(BOBOAACEncoder *encoder,uint8_t *inputBuffer,size_t bufferSize)
  2. {
  3. AVFrame *frame = avcodec_alloc_frame();
  4. frame->pts = picFrameIndex;
  5. // int ysize = encoder->pVideoCodecCtx->width *encoder->pVideoCodecCtx->height;
  6. av_init_packet(&encoder->videoPacket);
  7. int gotPicture = 0;
  8. //将原数据填充到AVFrame结构里,便于进行编码
  9. avpicture_fill((AVPicture *)frame, inputBuffer, encoder->pVideoCodecCtx->pix_fmt, encoder->pVideoCodecCtx->width, encoder->pVideoCodecCtx->height);
  10. encoder->videoPacket.data = NULL;
  11. encoder->videoPacket.size = 0;
  12. // 进行H264编码
  13. int ret = avcodec_encode_video2(encoder->pVideoCodecCtx, &encoder->videoPacket, frame, &gotPicture);
  14. picFrameIndex++;
  15. if (ret < 0) {
  16. printf("avcodec_fill_video_frame error !\n");
  17. av_frame_free(&frame);
  18. av_free_packet(&encoder->videoPacket);
  19. return -1;
  20. }
  21. // 成功编码一祯数据
  22. if (gotPicture == 1) {
  23. encoder->videoPacket.stream_index = encoder->pVideoStrem->index;
  24. encoder->videoPacket.pts = frame->pts;
  25. //写入到指定的地方 由encoder->pFormatCtx 保存着输出的路径
  26. ret = av_interleaved_write_frame(encoder->pFormatCtx, &encoder->videoPacket);
  27. }
  28. av_frame_free(&frame);
  29. av_free_packet(&encoder->videoPacket);
  30. return 1;
  31. }

5.RTMP推送

目前基于FFMPEG做为推流器,RTMP连接及推送音视频包都在其内部实现了,主要在函数

  1. //指定输出格式为FLV,Path这里传个RTMP://XXXX/XXX
  2. ret = avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", path);
  3. // 这个函数进行了URL的映射 ,比如我们PATH 为RTMP协议的,FFMPEG内部将使用librtmp里面的握手连接等函数。
  4. if (avio_open(&ofmt_ctx->pb, path,AVIO_FLAG_WRITE) < 0 ) {
  5. printf("failed to open output file \n");
  6. return NULL;
  7. }

目前这个Demo还有一些问题没有处理好,H264编码出来的数据播放后呈黑白(估计在IOS录制时,UV数据没有转换好)后续处理会弃用FFMPEG进行推流,直接使用Librtmp进行推送数据包。这样程序便于控制流传输的各个状态,及错误捕捉。


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注