{"id":19614758,"url":"https://github.com/leancloud/leancloudlivekit-ios","last_synced_at":"2025-04-09T22:15:49.618Z","repository":{"id":66228588,"uuid":"62779653","full_name":"leancloud/LeanCloudLiveKit-iOS","owner":"leancloud","description":"Show how to add IM module to Live app","archived":false,"fork":false,"pushed_at":"2017-09-22T04:15:13.000Z","size":17606,"stargazers_count":286,"open_issues_count":2,"forks_count":66,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-04-09T22:15:43.739Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leancloud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-07-07T06:10:04.000Z","updated_at":"2025-01-25T02:25:56.000Z","dependencies_parsed_at":"2023-07-12T12:45:10.888Z","dependency_job_id":null,"html_url":"https://github.com/leancloud/LeanCloudLiveKit-iOS","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FLeanCloudLiveKit-iOS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FLeanCloudLiveKit-iOS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FLeanCloudLiveKit-iOS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leancloud%2FLeanCloudLiveKit-iOS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leancloud","download_url":"https://codeload.github.com/leancloud/LeanCloudLiveKit-iOS/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119288,"owners_count":21050755,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T10:53:36.285Z","updated_at":"2025-04-09T22:15:49.523Z","avatar_url":"https://github.com/leancloud.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LiveKit-iOS\n\n\u003cp align=\"center\"\u003e\n![enter image description here](https://img.shields.io/badge/platform-iOS%208.0%2B-ff69b5618733984.svg) \n\u003ca href=\"https://github.com/leancloud/ChatKit-OC/blob/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/badge/license-MIT-green.svg?style=flat\"\u003e\u003c/a\u003e\n\u003c/a\u003e\n\n [LiveKit](https://github.com/leancloud/LeanCloudLiveKit-iOS) 是一个专门为视频直播业务提供的一个 UI 组件。主要特点是：\n \n   1. 将直播模块与 IM 模块结合，提供了推流端和播放端，以及聊天室等的实现。\n   2. 支持无人数限制的聊天室 \n   3. 支持多种自定义消息拓展并提供了默认实现，比如：弹幕、点赞出心、送飞机游轮或保时捷等礼物、成员加入聊天室自动打招呼等。   \n   4. 针对直播服务，LeanCloud 只按照 [聊天的费用标准](/pricing.html) 收费，而直播相关的内容，LeanCloud 并不参与收费，其产生的费用请在七牛账户中查询。\n   \n## 获取项目\n\n ```Objective-C\ngit clone --depth=1 https://github.com/leancloud/LeanCloudLiveKit-iOS.git\n ```\n\n## 集成效果\n\n![](http://ww2.sinaimg.cn/large/72f96cbajw1f7q9sn89lzg20nl0l9b2a.gif)\n\n![](http://ww2.sinaimg.cn/large/72f96cbajw1f7q9sdezf9g20nl0l9kjn.gif)\n\n![](http://ww1.sinaimg.cn/large/72f96cbajw1f7q8zdrdpgg20nl0km7wk.gif)\n\n## 项目结构\n\n ```Objective-C\n└── LiveKit-iOS\n    ├── LiveKit-iOS\n    │   ├── Assets.xcassets\n    │   ├── Class\n    │   │   ├── Camera\n    │   │   │   ├── Controller\n    │   │   │   └── View\n    │   │   ├── Category\n    │   │   ├── Live\n    │   │   │   ├── ChildViewController\n    │   │   │   ├── Controller\n    │   │   │   ├── Model\n    │   │   │   └── View\n    │   │   ├── Main\n    │   │   │   ├── Controller\n    │   │   │   └── Libs\n    │   │   │       ├── IJKMediaFramework.framework #缺少的库\n    │   │   │       └── ... ...\n    │   │   └── Mine\n    │   │       ├── Category\n    │   │       ├── Controller\n    │   │       └── View\n    │   ├── LiveChat                               #推流端和播放端\n    │       ├── Model\n    │       ├── Resources\n    │       │   ├── Gift\n    │       │   └── HeartImage\n    │       ├── Streaming\n    │       │   └── PLCameraStreamingKit\n    │       ├── Tool\n    │       │   └── MBProgressHUD\n    │       └── View\n    └── Pods\n    ├── ChatKit                                    #IM系统\n        └── Class\n        ├── Model\n        ├── Module\n        ├── Resources\n        ├── Tool\n        │   ├── Categories\n        │   ├── Service\n        │   └── Vendor\n        └── View\n ```\n\n## 使用方法\n\n本库通过 CocoaPods 管理依赖。\n\n\u003e CocoaPods 是目前最流行的 Cocoa 项目库依赖管理工具之一，考虑到便捷与项目的可维护性，我们更推荐您使用 CocoaPods 导入并管理 SDK。\n\n### CocoaPods 导入\n\n  1. CocoaPods 安装\n\n  如果您的机器上已经安装了 CocoaPods，直接进入下一步即可。\n\n  如果您的网络能够访问 CocoaPods 服务器，在终端中运行如下命令直接安装：\n\n ```shell\n     sudo gem install cocoapods\n ```\n\n   如果您的网络不能访问 CocoaPods 服务器，可以通过淘宝的 RubyGems 镜像 进行安装。\n\n   在终端依次运行以下命令：\n\n ```shell\n     gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/\n     sudo gem install cocoapods\n ```\n\n  2. 使用 CocoaPods 导入\n\n   打开终端，然后使用 CocoaPods 进行安装。在终端中运行以下命令：\n\n ```shell\n    pod install\n ```\n\n  或者这个命令：\n\n ```shell\n    # 禁止升级 CocoaPods 的 spec 仓库，否则会卡在 Analyzing dependencies，非常慢\n    pod update --verbose --no-repo-update\n ```\n\n  如果提示找不到库，则可去掉 `--no-repo-update`。\n\n  完成后，CocoaPods 会在您的工程根目录下生成一个 `.xcworkspace` 文件。您需要通过此文件打开您的工程，而不是之前的 `.xcodeproj`。\n\nPod安装后，会发现，还是少一个framework：`IJKMediaFramework.framework` ,在文档上文中的项目结构部分有标注。可以到[这里](https://pan.baidu.com/s/1eSBLDpK)下载，编译好的版本 ：\n\n## 推流端与播放端\n\n### 播放端配置\n\n在 Demo 中，播放端的配置演示主要集中 `PlayerViewController` 这个类。\n\n播放端的配置步骤如下：\n\n  1. APP 层面需要提供URL，然后将 URL 传给 LiveKit， LiveKit 根据 URL 地址查询聊天室信息，如果在发现不存在，LiveKit会创建一个聊天室聊天室。\n \n 具体代码如下：\n\n ```Objective-C\n    \n    [[LCLKLiveService sharedInstance] fetchConversationIfNeededForLiveId:self.liveUrl callback:^(AVIMConversation *conversation, NSError *error) {\n        if (conversation) {\n           //...\n        }\n    }];\n   \n ```\n\n\n  2. 根据 URL 获得聊天室ID后，根据 ID 获取聊天室视图，具体可参考下面这个方法： \n\n  `-[PlayerViewController exampleOpenConversationViewControllerWithConversaionId:fromNavigationController:]`\n  \n   聊天视图位置的设置需要借助 LiveKit 提供的几个生命周期函数`-setViewDidLoadBlock:`、`-setViewWillAppearBlock:`、`-setViewWillDisappearBlock:`，同时将自动加入聊天室属性 `enableAutoJoin` 设置为 YES。\n\n### 推流端配置\n\n在 Demo 中，推流端的配置演示主要集中 `PLViewController` 这个类。\n\nDemo 中使用了七牛 SDK 的推流端 PLStreamingKit：\n\n`PLStreamingKit` 是一个适用于 iOS 的 RTMP 直播推流 SDK，可高度定制化和二次开发。特色是支持 H.264 硬编码，以及支持 AAC 硬编码；同时，还根据移动网络环境的多变性，实现了一套可供开发者灵活选择的编码参数集合。\n\n`PLStreamingKit` 不包括摄像头、麦克风等设备相关的资源获取，只包括音视频数据的编码处理和发送。\n\n具体步骤如下：\n\n在 `AppDelegate.m` 中进行 SDK 初始化，如果未进行 SDK 初始化，在核心类 `PLStreamingSession` 初始化阶段将抛出异常\n\n```Objective-C\n#import \u003cPLStreamingKit/PLStreamingEnv.h\u003e\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n    [PLStreamingEnv initEnv];\n    // Override point for customization after application launch.\n    return YES;\n}\n```\n\n在需要的地方添加\n\n```Objective-C\n#import \u003cPLStreamingKit/PLStreamingKit.h\u003e\n```\n\n`PLStreamingSession` 是核心类，你只需要关注并使用这个类就可以完成推流工作。\n\n`PLStreamingSession ` 的创建\n\n```Objective-C\n// streamJSON 是从服务端拿回的\n//\n// 从服务端拿回的 streamJSON 结构如下：\n//    @{@\"id\": @\"stream_id\",\n//      @\"title\": @\"stream_title\",\n//      @\"hub\": @\"hub_name\",\n//      @\"publishKey\": @\"publish_key\",\n//      @\"publishSecurity\": @\"dynamic\", // or static\n//      @\"disabled\": @(NO),\n//      @\"profiles\": @[@\"480p\", @\"720p\"],    // or empty Array []\n//      @\"hosts\": @{\n//            ...\n//      }\nNSDictionary *streamJSON;\nPLVideoStreamingConfiguration *videoStreamingConfiguration = [PLVideoStreamingConfiguration configurationWithVideoSize:CGSizeMake(320, 576) videoQuality:kPLVideoStreamingQualityLow2];\nPLAudioStreamingConfiguration *audioStreamingConfiguration = [PLAudioStreamingConfiguration defaultConfiguration];\nPLStream *stream = [PLStream streamWithJSON:streamJSON];\n\nself.session = [[PLStreamingSession alloc] initWithVideoStreamingConfiguration:videoStreamingConfiguration audioStreamingConfiguration:audioStreamingConfiguration stream:stream];\nself.session.delegate = self;\n```\n\n推流操作\n\n```Objective-C\n// 开始推流，无论 security policy 是 static 还是 dynamic，都无需再单独计算推流地址\n[self.session startWithCompleted:^(BOOL success) {\n    // 这里的代码在主线程运行，所以可以放心对 UI 控件做操作\n    if (success) {\n        // 连接成功后的处理\n        // 成功后，在这里才可以读取 self.session.pushURL，start 失败和之前不能确保读取到正确的 URL\n    } else {\n        // 连接失败后的处理\n    }\n}];\n\n// 停止推流\n[self.session stop];\n```\n\n销毁推流 session\n\n```Objective-C\n[self.session destroy];\n```\n\n推流端需要获取的 `streamJSON` 如果有不清楚的可以参考：\n\n  1. [《七牛开发者中心-API文档》](http://developer.qiniu.com/article/index.html#pili-api-handbook) \n  2. [《2小时搞定移动直播 App 开发》](http://www.imooc.com/learn/707?sukey=f740b693ad416b27703fbe1bfb6cc97b973f0a33f4b940c57d8ba98cf76ac97363149884f0b55604da9f6135c6942f40) 视频教程\n  \n播放端是采用的通用的直播组件，Demo 中实时播放的直播地址，是从网上抓包抓取的直播地址。如果想观看推流端的直播视频，直接替换 URL 地址就可以达到效果。\n\n播放端和推流端的代码位置，在上文的项目结构部分已经标注出。\n\n## IM 系统配置\n\nIM 部分的配置需要参考：[ChatKit-OC](https://github.com/leancloud/ChatKit-OC) 。\n\nIM 系统的的代码位置，在上文的项目项目结构部分已经标注出。\n\n**参考到的开源项目**\n\n主要是两个部分：\n\n 1. 直播演示部分，主要参考：[520Linkee](https://github.com/GrayJIAXU/520Linkee)   \n 2. IM部分，主要参考：[ChatKit-OC](https://github.com/leancloud/ChatKit-OC) \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fleancloudlivekit-ios","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleancloud%2Fleancloudlivekit-ios","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleancloud%2Fleancloudlivekit-ios/lists"}