Overview
Publishing from the camera refers to the process of collecting video and audio data from the mobile phone's camera and mic, encoding the data, and pushing it to cloud-based live streaming platforms. Tencent Cloud live_flutter_plugin
provides the camera push capabilities via V2TXLivePusher
APIs.
Sample Code
Tencent Cloud offers an easy-to-understand API example project to help you quickly learn how to use different APIs.
Environment Requirements
Android
Screen recording is supported in Android 5.0 and above.
Floating windows need to be enabled manually on some mobile phones and systems.
iOS
Screen recording is a new feature in iOS 10. In addition to using ReplayKit to record video from the screen, which is possible in iOS 9, with iOS 10, users can also stream live video from the screen. For details, see Go Live with ReplayKit. In iOS 11, Apple made ReplayKit more usable and more universally applicable and launched ReplayKit2, going from supporting ReplayKit alone to allowing the recording of the entire screen. Therefore, we recommend using ReplayKit2 in iOS 11 to enable the screen sharing feature. Screen sharing relies on extensions, which operate as independent processes. However, to ensure system smoothness, iOS allocates limited resources to extensions and may kill extensions with high memory usage. Given this, Tencent Cloud has further reduced the memory usage of LiteAVSDK while retaining its high streaming quality and low latency to ensure the stability of extensions. Note:
This document describes how to use ReplayKit 2 on iOS 11 to push streams from the screen. The parts about the use of the SDK also apply to other custom stream push scenarios. For more information, see the code sample in the Live Demo Screen
folder of the demo. 1. Create the live streaming extension
Open your project with Xcode and select New > Target... > Broadcast Upload Extension, as shown below.
Enter a product name and click Finish. A new directory with the product name entered will appear in your project. Under the directory, there is an automatically generated SampleHandler
class, which is responsible for screen recording operations. Note:
Xcode 9 or later is required, and your iPhone must be updated to iOS 11 or later. Screen recording is not supported on emulators.
2. Import TXLiteAVSDK_ReplayKitExt.framework
Import TXLiteAVSDK_ReplayKitExt.framework
into the live streaming extension the same way you import a framework into the host application. The system libraries the SDK depends on are also the same. For more information, see iOS. Getting Started
1. Set dependencies
dependencies:
live_flutter_plugin: latest version number
If you have the required license, get the license URL and key in the CSS console. 2. Before your application calls features of live_flutter_plugin
, complete the following configuration:
import 'package:live_flutter_plugin/v2_tx_live_premier.dart';
/// Tencent Cloud license management page (https://console.tencentcloud.com/live/license)
setupLicense() {
// The license URL of the current application
var LICENSEURL = "";
// The license key of the current application
var LICENSEURLKEY = "";
V2TXLivePremier.setLicence(LICENSEURL, LICENSEURLKEY);
}
Note:
The packageName/BundleId
configured in the license must be the same as that of the application; otherwise, stream push will fail.
3. Create a pusher object
Create a V2TXLivePusher object, which will be responsible for publishing operations.
V2TXLivePusher livePusher = V2TXLivePusher(V2TXLiveMode.v2TXLiveModeRTMP);
4. Start stream push
After completing Step 1, you can use the code below to start publishing streams: String rtmpUrl = "rtmp://2157.livepush.myqcloud.com/live/xxxxxx";
livePusher.startMicrophone();
livePusher.startScreenCapture();
livePusher.startPush(rtmpUrl);
Why is V2TXLIVE_ERROR_INVALID_LICENSE
returned?If the startPush
API returns V2TXLIVE_ERROR_INVALID_LICENSE
, it means your license verification failed. Please check your configuration against Step 2. Configure a license for the SDK. 5. Stop stream push
As there can be only one V2TXLivePusher
object running at a time, make sure that you release all the resources when stopping publishing.
// Stop screen sharing and release the resources
void stopPush() {
livePusher.stopMicrophone();
livePusher.stopScreenCapture();
livePusher.stopPush();
}
Event Handling
Listening for events
Errors
An error indicates that the SDK encountered a serious problem that made it impossible for stream publishing to continue.
|
| | A common unclassified error occurred. |
V2TXLIVE_ERROR_INVALID_PARAMETER | | An invalid parameter was passed in during API calling. |
| | The API call was rejected. |
V2TXLIVE_ERROR_NOT_SUPPORTED | | The API cannot be called. |
V2TXLIVE_ERROR_INVALID_LICENSE | | Failed to call the API due to invalid license. |
V2TXLIVE_ERROR_REQUEST_TIMEOUT | | The server request timed out. |
V2TXLIVE_ERROR_SERVER_PROCESS_FAILED | | The server could not handle your request. |
Warnings
A warning indicates that the SDK encountered a problem whose severity level is warning. Warning events trigger tentative protection or recovery logic and can often be resolved.
|
V2TXLIVE_WARNING_NETWORK_BUSY | | Bad network connection: data upload blocked due to limited upstream bandwidth. |
V2TXLIVE_WARNING_VIDEO_BLOCK | | Stuttering during video playback. |
V2TXLIVE_WARNING_CAMERA_START_FAILED | | Failed to turn the camera on. |
V2TXLIVE_WARNING_CAMERA_OCCUPIED | | The camera is occupied. Try a different camera. |
V2TXLIVE_WARNING_CAMERA_NO_PERMISSION | | No access to the camera. This usually occurs on mobile devices and may be because the user denied the access. |
V2TXLIVE_WARNING_MICROPHONE_START_FAILED | | Failed to turn the mic on. |
V2TXLIVE_WARNING_MICROPHONE_OCCUPIED | | The mic is occupied. This occurs when, for example, the user is having a call on the mobile device. |
V2TXLIVE_WARNING_MICROPHONE_NO_PERMISSION | | No access to the mic. This usually occurs on mobile devices and may be because the user denied the access. |
V2TXLIVE_WARNING_SCREEN_CAPTURE_NOT_SUPPORTED | | The system does not support screen sharing. |
V2TXLIVE_WARNING_SCREEN_CAPTURE_START_FAILED | | Failed to start screen recording. If this occurs on a mobile device, it may be because the user denied the access. |
V2TXLIVE_WARNING_SCREEN_CAPTURE_INTERRUPTED | | Screen recording was stopped by the system. |
FAQs
ReplayKit2 is a new framework introduced by Apple in iOS 11, for which relatively few official documents have been released. The framework is still being improved, and problems have been found. See below for some common questions you may have when using ReplayKit2.
1. When does screen recording stop automatically?
Screen recording stops automatically when the screen locks or there is an incoming call. At such times, the broadcastFinished
function in SampleHandler
will be invoked, and you can send a notification to users about the interruption.
2. Why does screen recording stop sometimes during screen sharing?
The problem usually occurs after landscape/portrait mode switch if the resolution for stream publishing is set high. The broadcast upload extension is allocated a memory of only 50 MB and will be killed if its memory usage exceeds the limit. Given this, we recommend that you set the resolution to 720p or lower.
3. Why are images streamed from the screen of iPhone X distorted?
iPhone X has a notch at the top of the screen, so video captured from the screen is not in the aspect ratio of 16:9. If you set the output resolution for stream publishing to 16:9, for example, to HD (960 × 540), the images published will be slightly distorted because their original aspect ratio is not 16:9. We recommend that you set the resolution according to your screen size. Besides, if you play video streamed from the screen of iPhone X in aspect fit mode, the video may have black bars, and if you play it in aspect fill mode, the video may be cropped.
Was this page helpful?