Virtual Livestreaming Solution

Updated at:

This topic describes an automated livestreaming solution that uses Cloud Phone for virtual livestreaming.

Scenario description

Cloud Phone and virtual livestreaming technology provide an efficient livestreaming solution. The solution uses cloud computing power for real-time avatar rendering, speech synthesis, and interactive responses. With Cloud Phone, you can call a pre-trained virtual streamer model. When a user inputs text, the model automatically generates corresponding voice and video content. This process enables 24/7 automated livestreaming when used with a script. This solution is suitable for classic digital human livestreaming and other virtual livestreaming scenarios.

Cloud Phone supports elastic resource scheduling, which ensures system stability during high-concurrency access. The solution is ideal for small and medium-sized businesses with limited staff because it supports unified content management and batch updates for multi-account matrices. Daily maintenance requires only one or two operators, which greatly reduces labor costs. Cloud Phone has a built-in stream ingest feature that supports AI semantic parsing of real-time audience questions and provides dynamic feedback from the virtual streamer. Preset security group policies and bandwidth control mechanisms ensure a smooth and secure livestream.

Scenario features

  • High computing power requirements

    • Livestreaming mobile applications demand significant computing resources, which leads to high CPU usage. The computing power of mainstream personal devices is insufficient to support these applications stably.

  • High rendering requirements

    • Livestreaming platforms require high video definition and smoothness. This requirement sets a higher standard for the video encoding, decoding efficiency, and graphics rendering capabilities of Cloud Phone providers.

  • Frequent updates to livestreaming platform risk control policies

    • Cloud Phone providers must offer real device impersonation. This feature allows virtual livestreaming vendors to customize device configurations.

    • Some livestreaming applications have strict IP address requirements. For example, some E-commerce applications might apply extra risk control policies to Border Gateway Protocol (BGP) lines.

Solution

image.png

Solution advantages

  • The Arm server architecture meets the growing computing power demands of livestreaming applications. It supports custom matrix sizes to flexibly allocate computing resources. You can configure resources as needed to improve utilization and deployment flexibility.

  • The solution uses a storage-compute separation architecture. You can choose shared or independent storage modes. It supports dynamic, on-demand scaling to improve storage flexibility and resource utilization.

Procedure

Create a Cloud Phone matrix

For more information, see Create and manage a Cloud Phone matrix. Purchase a cpm.gx8.16xlarge Cloud Phone.

You can customize the number of instances. You can adjust the number later by reconfiguring the matrix.

Important

Reconfiguring the matrix erases all data in the matrix.

Network configuration

Standard Cloud Phone network

  • Cloud Phone provides standard network access. It supports two network types: BGP multi-line and static single-line.

  • The standard network uses a BGP line by default, and the IP egress belongs to Alibaba Cloud. For stable application performance, you must prepare a home broadband proxy.

Proxy

If you need a home broadband proxy, submit a ticket to contact the technical team for configuration.

Preparing the physical device and the app runtime environment

Note

You only need to perform these steps once after you create the Cloud Phone.

Step 1: Impersonate a real device

Some livestreaming platforms verify device model parameters. These platforms have livestreaming parameter limits, such as resolution and frame rate, for different device configurations. In these cases, you must impersonate a real device on the Cloud Phone.

If you encounter problems during the physical machine impersonation process, please submit a ticket for our technical team to complete the required configuration.

Step 2: One-click device switch

You can use the Cloud Phone Assistant to perform a one-click device switch.

image (2)image (3)

Step 3: Clear the application cache

On the Cloud Phone, find the livestreaming app and clear its cache.

Step 4: Switch to real camera mode

Remotely execute the following commands, then restart the Cloud Phone.

wy_virt_streaming stop
setprop persist.wy.camera.max.resolution 720p

If application risk control is triggered and requires facial recognition:

  • Use the Android client for facial recognition.

  • A low-light environment is required.

  • Position your face as close to the screen as possible.

Deploy the stream ingest service

Create an ECS instance and deploy an RTMP stream ingest server. If the stream ingest server and the Cloud Phone must communicate over an internal network, make sure the ECS instance and the CPS instance are in the same region. To enable service interconnection, contact technical support.

When the ECS stream ingest server starts stream ingest, do not add the crf parameter to the FFmpeg command. For example:

ffmpeg -stream_loop -1 -re -i ./output_hw_1080p.mp4 -c copy -f flv rtmp://localhost/live/livestream
  • Make sure the MP4 file is in landscape orientation. You can use FFmpeg to check and convert the resolution to the landscape format with the following command: ffmpeg -i mp4.

  • MP4 files support 720p and 1080p resolutions.

  • MP4 files support a 30 fps frame rate.

If the video does not meet these requirements, you can use FFmpeg to convert the video format before ingest.

Use the Cloud Phone one-click livestreaming tool

  1. Install the VLC application on the Cloud Phone. Verify that the video stream loads successfully.

  2. On the Cloud Phone, execute the wy_virt_streaming command.

    wy_virt_streaming start native -av rtmp://url
  3. After you open the camera application, the device displays the real-time video. If the video does not display correctly, close the camera application, wait 5 seconds, and then restart it.

  4. After the virtual livestream ends, execute the following command to switch back to the real camera.

    wy_virt_streaming stop

Cloud Phone one-click livestreaming tool command reference

wy_virt_streaming start mode [-a path] -v[path] 
-a: Writes audio. Followed by the audio file path or RTMP server path.
-v: Writes video. Followed by the video file path or RTMP server path.
-m: Mirroring feature. -1 to enable, -0 to disable. Optional, default is usually sufficient.
-r: Rotation feature. 90/180/270. Optional, default is usually sufficient.

#Inject audio and video using the Java SDK
wy_virt_streaming start java(Requires integrating the Java SDK or installing the demo app)

#Inject audio and video using the Cloud Phone universal livestreaming solution
wy_virt_streaming start native -av rtmp://1111 (Writes audio and video from an RTMP stream)

wy_virt_streaming start native -v rtmp://1111 (Writes video only)

wy_virt_streaming start native -av /data/local/tmp/test.mp4 (From a local MP4 file)

wy_virt_streaming start native -v /data/local/tmp/test.mp4 (Writes video only from a local file)

wy_virt_streaming stop (Stops the virtual livestream and restores camera redirection mode)

Advanced tutorial: Custom audio and video injection

Get the SDK and Javadoc

Download the virtual camera Java software development kit (SDK): libvhal_sdk-release.aar.

Switch virtual livestreaming mode (for testing only)

To switch from camera redirection to the virtual camera (virtual livestreaming mode), set the following properties:

setprop config.wy.virtualLive 1
setprop persist.config.wy.camera.socket 2

After the switch, restart the related processes:

ps -elf | grep cameraserv | awk '{print $2}' | xargs kill -9
stop camera-client&&start camera-client

Reference the Java SDK

Reference the SDK

The virtual camera Java SDK is a pre-packaged AAR library. To call the virtual camera, you can reference the library as an Implementation in your Android application project.

Import Javadoc (optional)

To obtain documentation hints in a compatible IDE, such as Android Studio, you can import the Javadoc package provided with the SDK. In Android Studio, go to External Libraries -> libvhal_sdk-release.aar -> Library Properties -> Add Javadoc attachments, and then select the extracted Javadoc package.

Class diagram (SDK includes only the Java part)

image.png

Basic call flow
Video stream
  1. Step 1: Build the camera VideoSink

    Before you generate and deliver video content for the virtual camera, you must build a VideoSink to create a connection to the virtual camera hardware abstraction layer (HAL). You can use InstanceManager to obtain a VideoSink instance.

    Only one VideoSink instance can be active at a time during the application lifecycle. Each call to the NewVideoSink method releases the old instance and replaces it with a new one. Be careful not to reference a released VideoSink instance.

    The following is an example:

    VideoSink videoSink = InstanceManager.getInstance().NewVideoSink("/conn", new IMediaCallback() {
        @Override
        public void onOpen() { }
        @Override
        public void onClose() { }
        @Override
        public void onServerUp() { }
        @Override
        public void onNone() { }
    });
  2. Step 2: Obtain and set camera capabilities as needed

    The virtual camera supports four video resolutions (480p, 720p, 1080p, and 1080p_t1) and four encoding formats (H264, H265, I420, and AV1). To make sure the virtual camera HAL understands the incoming packet format, call VideoSink::SetCameraCapability to declare the packet resolution and format before you send the first packet.

    The following is an example:

    CameraInfo[] cameraInfos = new CameraInfo[2];
    CameraInfo cameraInfo = new CameraInfo();
    cameraInfo.cameraId = 0;
    cameraInfo.codecType = VideoCodecType.kI420;
    cameraInfo.facing = CameraFacing.BACK_FACING;
    cameraInfo.resolution = FrameResolution.k720p;
    cameraInfo.sensorOrientation = SensorOrientation.ORIENTATION_90;
    cameraInfos[0] = cameraInfo;
    cameraInfo = new CameraInfo();
    cameraInfo.cameraId = 1;
    cameraInfo.codecType = VideoCodecType.kI420;
    cameraInfo.facing = CameraFacing.FRONT_FACING;
    cameraInfo.resolution = FrameResolution.k720p;
    cameraInfo.sensorOrientation = SensorOrientation.ORIENTATION_270;
    cameraInfos[1] = cameraInfo;
    videoSink.SetCameraCapability(cameraInfos);

    The preceding code defines two virtual cameras. The virtual rear camera supports 720p resolution and the I420 data format. Its sensor orientation is rotated 90 degrees clockwise. The virtual front camera supports 720p resolution and the I420 data format. Its sensor orientation is rotated 270 degrees clockwise.

    If you are unsure which capabilities the current virtual camera HAL supports, you can call VideoSink::GetCameraCapability.

  3. Step 3: Produce and deliver video data

    1. Control the frame transmission rate yourself

      If you decide to control the frame transmission rate, deliver the packet directly to VideoSink. The packet's data format must match the format set in Step 2. The call is shown below.

      // The VideoSink::SendDataPacket method has multiple overloads. Choose the appropriate one as needed.
      videoSink.SendDataPacket(bufferToSend);
      Important

      If you use this method, make sure the data delivery frequency matches the video frame rate. Otherwise, the video playback speed will be abnormal.

    2. Use the predefined Consumer (I420 only)

      If your data format is I420 and you cannot control the delivery frequency, you can use the predefined VideoStreamConsumer. You can pass the video stream, dimensions, and frame rate properties to its constructor. Then, you can call BeginConsuming to stream the data.

Audio stream
  1. Step 1: Build the microphone AudioSink

    Similar to the video stream, before you deliver audio data to the virtual microphone HAL, you must build an AudioSink instance to create a connection. You can use InstanceManager to obtain the AudioSink instance.

    AudioSink audioSink = InstanceManager.getInstance().NewAudioSink("127.0.0.1", new IMediaCallback() {
        @Override
        public void onOpen() { }
        @Override
        public void onClose() { }
        @Override
        public void onServerUp() { }
        @Override
        public void onNone() { }
    });
  2. Step 2: Produce and deliver audio data

    1. Control the transmission rate yourself

      The virtual microphone only supports dual-channel audio at 48 kHz with a 16-bit sampling depth. You do not need to set capabilities as you do for the virtual camera. After the AudioSink is built, you can use it to deliver audio data immediately. For example:

      audioSink.SendDataPacket(audioBuffer);
      Important

      If you use this method, one data packet must contain 10 ms of audio data (1920 bytes). You must send 100 packets evenly per second. An uneven or slow rate causes audio stuttering.

    2. Use the predefined Consumer

      Similar to video data, if you cannot control the audio data transmission rate, you can use the predefined AudioStreamConsumer. You can pass the audio properties to its constructor and then call BeginConsuming to stream the data.

Release underlying resources

The virtual camera and microphone Java SDK is an abstraction layer for the underlying Socket connections (Unix Domain Socket for video, TCP Socket for audio) and the native client library. Each AudioSink or VideoSink Java object corresponds to a native layer object and a Socket connection.

Both AudioSink and VideoSink implement AutoClosable. You can manually call the Close method to release their underlying resources. You can also use the try-with-resource syntax for automatic release. However, AudioSink and VideoSink typically have long lifecycles that match the application's lifecycle. The developer decides when to release the resources.

If you forget to call the Close method, the object's finalizer releases the underlying resources when the Java object is garbage collected.