美文网首页
iOS直播,视屏推流的那些事

iOS直播,视屏推流的那些事

作者: 莫寂岚 | 来源:发表于2016-10-12 11:24 被阅读192次

需求:

在主播处于特殊情况时,视屏推流推送固定的黑屏视屏流。

要点:

1、rgba888格式数据的生成。
2、fps为20情况下,视屏的推送。
3、在保证频道畅通的前提下,尽可能的节约网络资源。


实现:

1、首先,生成RGBA8888格式的数据
为了推送黑屏流,我使用的是腾讯直播的sdk,在sdk中,支持用户自己采集视屏数据,sdk只负责推流。
那么,第一步,是生成纯黑色的uiimage图像。

//  UIImage+Color.m
//  
//
//  Created by Bear on 16/10/12.
//  
+ (UIImage *)imageWithColor:(UIColor *)color
{
    //VIDEO_RESOLUTION_TYPE_360_640
    CGRect rect = CGRectMake(0, 0, 360, 640);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

在这里需要注意的是,图像大小(像素点)应该和上传视屏流要求的大小是一致的。
第二步,将或的的图像数据转化为RGBA8888数据;

- (unsigned char *) convertUIImageToBitmapRGBA8:(UIImage *) image {

    CGImageRef imageRef = image.CGImage;

    // Create a bitmap context to draw the uiimage into
    CGContextRef context = [self newBitmapRGBA8ContextFromImage:imageRef];

    if(!context) {
        return NULL;
    }

    size_t width = CGImageGetWidth(imageRef);
    size_t height = CGImageGetHeight(imageRef);

    CGRect rect = CGRectMake(0, 0, width, height);

    // Draw image into the context to get the raw image data
    CGContextDrawImage(context, rect, imageRef);

    // Get a pointer to the data
    unsigned char *bitmapData = (unsigned char*)CGBitmapContextGetData(context);

    // Copy the data and release the memory (return memory allocated with new)
    size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context);
    size_t bufferLength = bytesPerRow * height;

    unsigned char *newBitmap = NULL;

    if(bitmapData) {
        newBitmap = (unsigned char *)malloc(sizeof(unsigned char) * bytesPerRow * height);

        if(newBitmap) {    // Copy the data
            for(int i = 0; i < bufferLength; ++i) {
                newBitmap[i] = bitmapData[i];
            }
        }

        free(bitmapData);

    } else {
        NSLog(@"Error getting bitmap pixel data\n");
    }

    CGContextRelease(context);

    return newBitmap;
}

- (CGContextRef) newBitmapRGBA8ContextFromImage:(CGImageRef) image {
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    uint32_t *bitmapData;

    size_t bitsPerPixel = 32;
    size_t bitsPerComponent = 8;
    size_t bytesPerPixel = bitsPerPixel / bitsPerComponent;

    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);

    size_t bytesPerRow = width * bytesPerPixel;
    size_t bufferLength = bytesPerRow * height;

    colorSpace = CGColorSpaceCreateDeviceRGB();

    if(!colorSpace) {
        NSLog(@"Error allocating color space RGB\n");
        return NULL;
    }

    // Allocate memory for image data
    bitmapData = (uint32_t *)malloc(bufferLength);

    if(!bitmapData) {
        NSLog(@"Error allocating memory for bitmap\n");
        CGColorSpaceRelease(colorSpace);
        return NULL;
    }

    //Create bitmap context

    context = CGBitmapContextCreate(bitmapData,
                                    width,
                                    height,
                                    bitsPerComponent,
                                    bytesPerRow,
                                    colorSpace,
                                    kCGImageAlphaPremultipliedLast);    // RGBA
    if(!context) {
        free(bitmapData);
        NSLog(@"Bitmap context not created");
    }

    CGColorSpaceRelease(colorSpace);

    return context;
}

方法的原理大致是用Graphics Context来画出来,他是是图形上下文,也可以理解为一块画布,我们可以在上面进行绘画操作,绘制完成后,将画布放到我们的view中显示即可,view看作是一个画框.CGContextRef功能强大,我们借助它可以画各种图形。

就是说我们拿到了image的cgimage的信息,然后利用画图去获取其信息,将其画出来,返回成一组unsigned char的字符串流。

相关文章

  • iOS直播,视屏推流的那些事

    需求: 在主播处于特殊情况时,视屏推流推送固定的黑屏视屏流。 要点: 1、rgba888格式数据的生成。2、fps...

  • Demo

    IOS视频直播 + 推流实现 采用开源框架ijkplayer 以及LFLiveKit实现推流,主要完善关注,分享和登录

  • iOS 直播 —— 推流

    推流,就是将采集到的音频,视频数据通过流媒体协议发送到流媒体服务器。 推流前的工作:采集,处理,编码压缩 推流中做...

  • iOS 直播 —— 推流

    *推流,就是将采集到的音频,视频数据通过流媒体协议发送到流媒体服务器。*推流前的工作:采集,处理,编码压缩*推流中...

  • iOS 直播 —— 推流

    推流,就是将采集到的音频,视频数据通过流媒体协议发送到流媒体服务器。 推流前的工作:采集,处理,编码压缩 推流中做...

  • iOS直播推流实现-推流

    将最近学习的直播推流技术做个笔记。iOS推流的主要流程如下: 视频音频采集[https://www.jianshu...

  • iOS直播推流实现-采集

    将最近学习的直播推流技术做个笔记。iOS推流的主要流程如下: 视频音频采集[https://www.jianshu...

  • iOS直播推流实现-音视频编码

    将最近学习的直播推流技术做个笔记。iOS推流的主要流程如下: 视频音频采集[https://www.jianshu...

  • iOS直播推流实现-滤镜

    将最近学习的直播推流技术做个笔记。iOS推流的主要流程如下: 视频音频采集[https://www.jianshu...

  • 直播---iOS直播推流/拉流

    工作空闲之余, 闲来无事, 弄了个直播的项目, 基本上主流的功能都有了... 直播推流(主播端直播/美颜/切换摄...

网友评论

      本文标题:iOS直播,视屏推流的那些事

      本文链接:https://www.haomeiwen.com/subject/hohiyttx.html