IOS IJKPlayer播放RTSP

官网:https://github.com/Bilibili/ijkplayer

本文的意义

今天闲下来,把以前做RTSP的过程简单整理下。
详细的图文步骤网络上有很多,但大多是RTMP等的,这里简单记录一下RTSP的部分,由于IJKPlayer默认不支持RTSP,需要改些配置。
转载请注明出处 ethan_xue博客

1.clone

git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-ios
cd ijkplayer-ios
git checkout -B latest k0.8.0

2.修改config

修改module-lite.sh文件:

export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-protocol=rtp"  
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-demuxer=rtsp" 

保存后

cd /Users/xue/Downloads/RTSP/ijkplayer-ios/config 
rm module.sh //删除原来的
ln -s module-lite.sh module.sh //用修改过的替换

3.修改源文件

ijkplayer-ios/ijkmedia/ijkplayer/ff_ffplay.c找到下面的代码

static int packet_queue_get_or_buffering(FFPlayer *ffp, PacketQueue *q, AVPacket *pkt, int *serial, int *finished)
{
    assert(finished);
    if (!ffp->packet_buffering)
        return packet_queue_get(q, pkt, 1, serial);

    while (1) {
        int new_packet = packet_queue_get(q, pkt, 0, serial);
        if (new_packet < 0)
            return -1;
        else if (new_packet == 0) {
            if (q->is_buffer_indicator && !*finished)
                ffp_toggle_buffering(ffp, 1);
            new_packet = packet_queue_get(q, pkt, 1, serial);
            if (new_packet < 0)
                return -1;
        }

        if (*finished == *serial) {
            av_packet_unref(pkt);
            continue;
        }
        else
            break;
    }

    return 1;
}

修改

static int packet_queue_get_or_buffering(FFPlayer *ffp, PacketQueue *q, AVPacket *pkt, int *serial, int finished){
    while (1) {
        int new_packet = packet_queue_get(q, pkt, 1, serial);
        if (new_packet < 0){
            new_packet = packet_queue_get(q, pkt, 0, serial);
            if(new_packet < 0)
                return -1;
        }else if (new_packet == 0) {
            if (!finished)
                ffp_toggle_buffering(ffp, 1);
            new_packet = packet_queue_get(q, pkt, 1, serial);
            if (new_packet < 0)
                return -1;
        }
        if (finished == *serial) {

            av_free_packet(pkt);
            continue;
        }
        else
            break;
    }
    return 1;
}

4.编译

./init-ios.sh
cd ios
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all

打开xcode编译模拟器和真机,分别编译一遍。

将编译出来的库进行合并
xuedeiMac:Products xue$ lipo -create Release-iphoneos/IJKMediaFramework.framework/IJKMediaFramework Release-iphonesimulator/IJKMediaFramework.framework/IJKMediaFramework -output IJKMediaFramework
得到想要的framework

5.使用

IJKDemoInputURLViewController.m里

url = [NSURL URLWithString:@"rtsp://39.xx.x3.x:556/realplay://bca32cf8bb2e4a1a95aeb294bf536328:SUB:TCP?cnid=1&pnid=0&token=&auth=30&redirect=0&transcode=0&resolution=2&bitrate=100&framerate=10&videotype=2&systemformat=2"];

//    if ([scheme isEqualToString:@"http"]
//        || [scheme isEqualToString:@"https"]
//        || [scheme isEqualToString:@"rtmp"]) {

IJKVideoViewController.m里

// 使用 TCP
    [options setOptionValue:@"tcp" forKey:@"rtsp_transport" ofCategory:kIJKFFOptionCategoryFormat];
    //    [options setPlayerOptionIntValue:1 forKey:@"videotoolbox"];

ffmpeg的配置只需要在

[options setFormatOptionValue:@"tcp" forKey:@"rtsp_transport"];
//[options setOptionValue:@"tcp" forKey:@"rtsp_transport" ofCategory:kIJKFFOptionCategoryFormat];
// 使用硬解
[options setPlayerOptionIntValue:1 forKey:@"videotoolbox"];

参考

ijkplayer ios项目集成(图文详细版) http://www.jianshu.com/p/4f21af680c19

你可能感兴趣的:(IOS)