|
1 | | -# Introduction |
| 1 | +# LiveKit C++ Client SDK |
2 | 2 |
|
3 | | -This site contains API reference for the LiveKit C++ SDK. |
| 3 | +Build real-time audio/video applications in C++ with LiveKit. |
4 | 4 |
|
5 | | -> [!WARNING] |
6 | | -> This SDK is currently in Developer Preview mode and not ready for production use. |
7 | | -> There will be bugs and APIs may change during this period. |
| 5 | +> **Note:** This SDK is currently in Developer Preview. APIs may change before the stable release. |
8 | 6 |
|
9 | | -**Additional Resources** |
| 7 | +## Quick Start |
| 8 | + |
| 9 | +```cpp |
| 10 | +#include "livekit/livekit.h" |
| 11 | + |
| 12 | +bool initializeLivekit(const std::string& url, const std::string& token) { |
| 13 | + // Init LiveKit |
| 14 | + livekit::initialize(livekit::LogSink::kConsole); |
| 15 | + |
| 16 | + room_ = std::make_unique<livekit::Room>(); |
| 17 | + livekit::RoomOptions options; |
| 18 | + options.auto_subscribe = true; |
| 19 | + options.dynacast = false; |
| 20 | + if (!room_->Connect(url, token, options)) { |
| 21 | + std::cerr << "Failed to connect\n"; |
| 22 | + livekit::shutdown(); |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + std::cout << "Connected.\n"; |
| 27 | + |
| 28 | + // ---- Create & publish AUDIO ---- |
| 29 | + // Note: Hook up your own audio capture flow to |audioSource_| |
| 30 | + audioSource_ = std::make_shared<livekit::AudioSource>(48000, 1, 10); |
| 31 | + auto audioTrack = livekit::LocalAudioTrack::createLocalAudioTrack("noise", audioSource_); |
| 32 | + |
| 33 | + livekit::TrackPublishOptions audioOpts; |
| 34 | + audioOpts.source = livekit::TrackSource::SOURCE_MICROPHONE; |
| 35 | + |
| 36 | + try { |
| 37 | + audioPub_ = room_->localParticipant()->publishTrack(audioTrack, audioOpts); |
| 38 | + std::cout << "Published audio: sid=" << audioPub_->sid() << "\n"; |
| 39 | + } catch (const std::exception& e) { |
| 40 | + std::cerr << "Failed to publish audio: " << e.what() << "\n"; |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + // ---- Create & publish VIDEO ---- |
| 45 | + // Note: Hook up your own video capture flow to |videoSource_| |
| 46 | + videoSource_ = std::make_shared<livekit::VideoSource>(1280, 720); |
| 47 | + auto videoTrack = livekit::LocalVideoTrack::createLocalVideoTrack("rgb", videoSource_); |
| 48 | + |
| 49 | + livekit::TrackPublishOptions videoOpts; |
| 50 | + videoOpts.source = livekit::TrackSource::SOURCE_CAMERA; |
| 51 | + |
| 52 | + try { |
| 53 | + videoPub_ = room_->localParticipant()->publishTrack(videoTrack, videoOpts); |
| 54 | + std::cout << "Published video: sid=" << videoPub_->sid() << "\n"; |
| 55 | + } catch (const std::exception& e) { |
| 56 | + std::cerr << "Failed to publish video: " << e.what() << "\n"; |
| 57 | + return false; |
| 58 | + } |
| 59 | + return true; |
| 60 | +} |
| 61 | + |
| 62 | +void shutdownLivekit() { |
| 63 | + // Best-effort unpublish |
| 64 | + try { |
| 65 | + if (room_ && audioPub_) |
| 66 | + room_->localParticipant()->unpublishTrack(audioPub_->sid()); |
| 67 | + if (room_ && videoPub_) |
| 68 | + room_->localParticipant()->unpublishTrack(videoPub_->sid()); |
| 69 | + } catch (...) { |
| 70 | + } |
| 71 | + |
| 72 | + audioPub_.reset(); |
| 73 | + videoPub_.reset(); |
| 74 | + audioSource_.reset(); |
| 75 | + videoSource_.reset(); |
| 76 | + room_.reset(); |
| 77 | + |
| 78 | + livekit::shutdown(); |
| 79 | +} |
| 80 | +``` |
| 81 | +
|
| 82 | +## Key Classes |
| 83 | +
|
| 84 | +| Class | Description | |
| 85 | +|-------|-------------| |
| 86 | +| @ref livekit::Room | Main entry point - connect to a LiveKit room | |
| 87 | +| @ref livekit::RoomOptions | Configuration for room connection (auto_subscribe, dynacast, etc.) | |
| 88 | +| @ref livekit::LocalParticipant | The local user - publish tracks and send data | |
| 89 | +| @ref livekit::RemoteParticipant | Other participants in the room | |
| 90 | +| @ref livekit::AudioSource | Audio input source for publishing (sample rate, channels) | |
| 91 | +| @ref livekit::VideoSource | Video input source for publishing (width, height) | |
| 92 | +| @ref livekit::LocalAudioTrack | Local audio track created from AudioSource | |
| 93 | +| @ref livekit::LocalVideoTrack | Local video track created from VideoSource | |
| 94 | +| @ref livekit::LocalTrackPublication | Handle to a published local track | |
| 95 | +| @ref livekit::TrackPublishOptions | Options for publishing (source type, codec, etc.) | |
| 96 | +| @ref livekit::AudioStream | Receive audio from remote participants | |
| 97 | +| @ref livekit::VideoStream | Receive video from remote participants | |
| 98 | +| @ref livekit::RoomDelegate | Callbacks for room events | |
| 99 | +
|
| 100 | +## Installation |
| 101 | +
|
| 102 | +See the [GitHub README](https://github.com/livekit/client-sdk-cpp#readme) for build instructions. |
| 103 | +
|
| 104 | +**Requirements:** |
| 105 | +- CMake ≥ 3.20 |
| 106 | +- Rust/Cargo (latest stable) |
| 107 | +- Platform: Windows, macOS, or Linux |
| 108 | +
|
| 109 | +## Examples |
| 110 | +
|
| 111 | +- [SimpleRoom](https://github.com/livekit/client-sdk-cpp/tree/main/examples/simple_room) - Basic room connection with audio/video |
| 112 | +- [SimpleRpc](https://github.com/livekit/client-sdk-cpp/tree/main/examples/simple_rpc) - Remote procedure calls between participants |
| 113 | +- [SimpleDataStream](https://github.com/livekit/client-sdk-cpp/tree/main/examples/simple_data_stream) - Send text and binary data streams |
| 114 | +
|
| 115 | +## Resources |
10 | 116 |
|
11 | 117 | - [GitHub Repository](https://github.com/livekit/client-sdk-cpp) |
12 | | -- [Examples](https://github.com/livekit/client-sdk-cpp/tree/main/examples) |
13 | | -- [Introduction to LiveKit](https://docs.livekit.io/home/get-started/intro-to-livekit/) |
| 118 | +- [LiveKit Documentation](https://docs.livekit.io/) |
14 | 119 | - [Community Slack](https://livekit.io/join-slack) |
0 commit comments