Custom video source
By default, PlanetKit automatically detects and controls the camera device when a video call starts, and uses the video captured from the camera for the video call. However, depending on the requirements of your application, you may need to change this. One example is to display video from an external source instead of the user's video.
To meet these needs, PlanetKit provides a custom video source feature that allows users to directly supply the video frames they want. This feature allows users to render the video frames they want to the PlanetKit module and send the video frames to the peer.
PlanetKit does not control the camera device while using a custom video source.
| Supported call type | Minimum SDK version |
|---|---|
| 1-to-1 call, group call (conference) | PlanetKit 7.0 |
Use cases
You can use the custom video source feature for the following application requirements:
- Using a video file as a video source
- Receiving live streaming or web streaming and use it as a video source
- Directly controlling the camera device from the application, rather than through PlanetKit
Implementation steps
To implement the custom video source feature, follow these steps.
Implement a custom video source class
Create a class that inherits from CustomCamera and implement the methods.
class CustomVideoSource : public PlanetKit::CustomCamera {
public:
void OnStart() override {
// Start custom video source thread
m_videoThread = std::thread(&CustomVideoSource::VideoThreadProc, this);
}
void OnStop() override {
// Stop custom video source thread
m_bRunning = false;
if (m_videoThread.joinable()) {
m_videoThread.join();
}
}
void VideoThreadProc() {
m_bRunning = true;
const auto frameInterval = std::chrono::milliseconds(66); // ~15fps (1000ms / 15 = 66.67ms)
while (m_bRunning) {
// Read from file
PlanetKit::SVideoFrame sVideoFrame;
// Tick count as microseconds
sVideoFrame.llTick =
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
this->WriteFrameData(sVideoFrame);
// Maintain 15fps timing
std::this_thread::sleep_for(frameInterval);
}
}
private:
std::thread m_videoThread;
std::atomic<bool> m_bRunning = false;
};
Use the custom video source
To use a custom video source, call ChangeCamera() of CameraController with an object of the implemented custom video source.
class YourCameraManager {
// Change to custom video source
void SetCustomVideoSource() {
PlanetKit::CameraControllerPtr pCameraController =
PlanetKit::PlanetKitManager()::GetInstance()->GetCameraController();
pCameraController->ChangeCamera(m_pYourCustomVideoSource);
}
...
private:
PlanetKit::CustomCameraPtr m_pYourCustomVideoSource =
PlanetKit::MakeAutoPtr<CustomVideoSource>();
};
Switch back to the camera from a custom video source
To switch back to the camera from a custom video source, call ChangeCamera() of CameraController with the camera object.
class YourCameraManager {
...
// Change to camera device
void ChangeCamera() {
PlanetKit::CameraControllerPtr pCameraController =
PlanetKit::PlanetKitManager()::GetInstance()->GetCameraController();
PlanetKit::CameraInfoArray aCameraInfos;
pCameraController->GetCapturerInfo(aCameraInfos);
pCameraController->ChangeCamera(aCameraInfos.GetAt(0));
}
// Deselect camera
void DeselectCamera() {
PlanetKit::CameraControllerPtr pCameraController =
PlanetKit::PlanetKitManager()::GetInstance()->GetCameraController();
pCameraController->ChangeCamera(nullptr);
}
};
Related API
APIs related to the custom video source feature are as follows: