Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions components/livekit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ idf_component_register(
esp_codec_dev
esp_netif
esp_websocket_client
esp_webrtc
esp_peer
json
mbedtls
media_lib_sal
peer_default
webrtc_utils
nanopb
khash
)
Expand Down
48 changes: 25 additions & 23 deletions components/livekit/core/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "freertos/event_groups.h"
#include "media_lib_os.h"
#include "esp_codec_dev.h"
#include "esp_capture_sink.h"
#include <inttypes.h>
#include <stdlib.h>
#include "esp_log.h"
Expand Down Expand Up @@ -102,7 +103,7 @@ typedef struct {
peer_handle_t sub_peer_handle;

esp_codec_dev_handle_t renderer_handle;
esp_capture_path_handle_t capturer_path;
esp_capture_sink_handle_t capturer_path;
bool is_media_streaming;

char* server_url;
Expand Down Expand Up @@ -196,24 +197,24 @@ static void on_peer_sub_audio_frame(esp_peer_audio_frame_t* frame, void *ctx)

// MARK: - Published media

/// Converts `esp_peer_audio_codec_t` to equivalent `esp_capture_codec_type_t` value.
static inline esp_capture_codec_type_t capture_audio_codec_type(esp_peer_audio_codec_t peer_codec)
/// Converts `esp_peer_audio_codec_t` to equivalent `esp_capture_format_id_t` value.
static inline esp_capture_format_id_t capture_audio_codec_type(esp_peer_audio_codec_t peer_codec)
{
switch (peer_codec) {
case ESP_PEER_AUDIO_CODEC_G711A: return ESP_CAPTURE_CODEC_TYPE_G711A;
case ESP_PEER_AUDIO_CODEC_G711U: return ESP_CAPTURE_CODEC_TYPE_G711U;
case ESP_PEER_AUDIO_CODEC_OPUS: return ESP_CAPTURE_CODEC_TYPE_OPUS;
default: return ESP_CAPTURE_CODEC_TYPE_NONE;
case ESP_PEER_AUDIO_CODEC_G711A: return ESP_CAPTURE_FMT_ID_G711A;
case ESP_PEER_AUDIO_CODEC_G711U: return ESP_CAPTURE_FMT_ID_G711U;
case ESP_PEER_AUDIO_CODEC_OPUS: return ESP_CAPTURE_FMT_ID_OPUS;
default: return ESP_CAPTURE_FMT_ID_NONE;
}
}

/// Converts `esp_peer_video_codec_t` to equivalent `esp_capture_codec_type_t` value.
static inline esp_capture_codec_type_t capture_video_codec_type(esp_peer_video_codec_t peer_codec)
/// Converts `esp_peer_video_codec_t` to equivalent `esp_capture_format_id_t` value.
static inline esp_capture_format_id_t capture_video_codec_type(esp_peer_video_codec_t peer_codec)
{
switch (peer_codec) {
case ESP_PEER_VIDEO_CODEC_H264: return ESP_CAPTURE_CODEC_TYPE_H264;
case ESP_PEER_VIDEO_CODEC_MJPEG: return ESP_CAPTURE_CODEC_TYPE_MJPEG;
default: return ESP_CAPTURE_CODEC_TYPE_NONE;
case ESP_PEER_VIDEO_CODEC_H264: return ESP_CAPTURE_FMT_ID_H264;
case ESP_PEER_VIDEO_CODEC_MJPEG: return ESP_CAPTURE_FMT_ID_MJPEG;
default: return ESP_CAPTURE_FMT_ID_NONE;
}
}

Expand All @@ -224,14 +225,14 @@ static inline void _media_stream_send_audio(engine_t *eng)
esp_capture_stream_frame_t audio_frame = {
.stream_type = ESP_CAPTURE_STREAM_TYPE_AUDIO,
};
while (esp_capture_acquire_path_frame(eng->capturer_path, &audio_frame, true) == ESP_CAPTURE_ERR_OK) {
while (esp_capture_sink_acquire_frame(eng->capturer_path, &audio_frame, true) == ESP_CAPTURE_ERR_OK) {
esp_peer_audio_frame_t audio_send_frame = {
.pts = audio_frame.pts,
.data = audio_frame.data,
.size = audio_frame.size,
};
peer_send_audio(eng->pub_peer_handle, &audio_send_frame);
esp_capture_release_path_frame(eng->capturer_path, &audio_frame);
esp_capture_sink_release_frame(eng->capturer_path, &audio_frame);
}
}

Expand All @@ -242,14 +243,14 @@ static inline void _media_stream_send_video(engine_t *eng)
esp_capture_stream_frame_t video_frame = {
.stream_type = ESP_CAPTURE_STREAM_TYPE_VIDEO,
};
if (esp_capture_acquire_path_frame(eng->capturer_path, &video_frame, true) == ESP_CAPTURE_ERR_OK) {
if (esp_capture_sink_acquire_frame(eng->capturer_path, &video_frame, true) == ESP_CAPTURE_ERR_OK) {
esp_peer_video_frame_t video_send_frame = {
.pts = video_frame.pts,
.data = video_frame.data,
.size = video_frame.size,
};
peer_send_video(eng->pub_peer_handle, &video_send_frame);
esp_capture_release_path_frame(eng->capturer_path, &video_frame);
esp_capture_sink_release_frame(eng->capturer_path, &video_frame);
}
}

Expand All @@ -276,7 +277,7 @@ static engine_err_t media_stream_begin(engine_t *eng)
}
media_lib_thread_handle_t handle = NULL;
eng->is_media_streaming = true;
if (media_lib_thread_create_from_scheduler(&handle, STREAM_THREAD_NAME, media_stream_task, eng) != ESP_OK) {
if (media_lib_thread_create_from_scheduler(&handle, "lk_eng_stream", media_stream_task, eng) != ESP_OK) {
ESP_LOGE(TAG, "Failed to create media stream thread");
eng->is_media_streaming = false;
return ENGINE_ERR_MEDIA;
Expand Down Expand Up @@ -1127,13 +1128,13 @@ engine_handle_t engine_init(const engine_options_t *options)

esp_capture_sink_cfg_t sink_cfg = {
.audio_info = {
.codec = capture_audio_codec_type(eng->options.media.audio_info.codec),
.format_id = capture_audio_codec_type(eng->options.media.audio_info.codec),
.sample_rate = eng->options.media.audio_info.sample_rate,
.channel = eng->options.media.audio_info.channel,
.bits_per_sample = 16,
},
.video_info = {
.codec = capture_video_codec_type(eng->options.media.video_info.codec),
.format_id = capture_video_codec_type(eng->options.media.video_info.codec),
.width = eng->options.media.video_info.width,
.height = eng->options.media.video_info.height,
.fps = eng->options.media.video_info.fps,
Expand All @@ -1143,17 +1144,18 @@ engine_handle_t engine_init(const engine_options_t *options)
// TODO: Can we ensure the renderer is valid? If not, return error.
eng->renderer_handle = options->media.renderer;
}
if (esp_capture_setup_path(

if (esp_capture_sink_setup(
eng->options.media.capturer,
ESP_CAPTURE_PATH_PRIMARY,
0, // Path index
&sink_cfg,
&eng->capturer_path
) != ESP_CAPTURE_ERR_OK) {
goto _init_failed;
}
if (esp_capture_enable_path(
if (esp_capture_sink_enable(
eng->capturer_path,
ESP_CAPTURE_RUN_TYPE_ALWAYS
ESP_CAPTURE_RUN_MODE_ALWAYS
) != ESP_CAPTURE_ERR_OK) {
goto _init_failed;
}
Expand Down
3 changes: 0 additions & 3 deletions components/livekit/core/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
#pragma once

#include "esp_peer.h"
#include "esp_peer_signaling.h"
#include "esp_capture.h"
#include "av_render.h"

#include "livekit_types.h"
#include "common.h"
#include "protocol.h"

#define STREAM_THREAD_NAME "lk_stream"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
10 changes: 6 additions & 4 deletions components/livekit/core/livekit.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ livekit_err_t livekit_room_create(livekit_room_handle_t *handle, const livekit_r
if (handle == NULL || options == NULL) {
return LIVEKIT_ERR_INVALID_ARG;
}
if (!system_is_media_lib_setup()) {
ESP_LOGE(TAG, "Must perform system initialization before creating a room");
if (!system_init_is_done()) {
ESP_LOGE(TAG, "System initialization not performed or failed");
return LIVEKIT_ERR_SYSTEM_INIT;
}

Expand Down Expand Up @@ -412,8 +412,10 @@ livekit_err_t livekit_room_rpc_unregister(livekit_room_handle_t handle, const ch

livekit_err_t livekit_system_init(void)
{
if (!system_setup_media_lib()) {
return LIVEKIT_ERR_SYSTEM_INIT;
esp_err_t ret = system_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "System initialization failed");
return ret;
}
return LIVEKIT_ERR_NONE;
}
7 changes: 1 addition & 6 deletions components/livekit/core/peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include "esp_log.h"
#include "esp_peer.h"
#include "esp_peer_default.h"
#include "esp_peer_signaling.h"
#include "esp_webrtc_defaults.h"
#include "media_lib_os.h"
#include "esp_codec_dev.h"
#include "utils.h"
Expand All @@ -30,9 +28,6 @@ static const char *SUB_TAG = "livekit_peer.sub";
static const char *PUB_TAG = "livekit_peer.pub";
#define TAG(peer) (peer->options.role == PEER_ROLE_SUBSCRIBER ? SUB_TAG : PUB_TAG)

#define SUB_THREAD_NAME (PEER_THREAD_NAME_PREFIX "sub")
#define PUB_THREAD_NAME (PEER_THREAD_NAME_PREFIX "pub")

#define RELIABLE_CHANNEL_LABEL "_reliable"
#define LOSSY_CHANNEL_LABEL "_lossy"
#define STREAM_ID_INVALID 0xFFFF
Expand Down Expand Up @@ -364,7 +359,7 @@ peer_err_t peer_connect(peer_handle_t handle)
peer->running = true;
media_lib_thread_handle_t thread;
const char* thread_name = peer->options.role == PEER_ROLE_SUBSCRIBER ?
SUB_THREAD_NAME : PUB_THREAD_NAME;
"lk_peer_sub" : "lk_peer_pub";
if (media_lib_thread_create_from_scheduler(&thread, thread_name, peer_task, peer) != ESP_PEER_ERR_NONE) {
ESP_LOGE(TAG(peer), "Failed to create thread");
return PEER_ERR_RTC;
Expand Down
2 changes: 0 additions & 2 deletions components/livekit/core/peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "common.h"
#include "protocol.h"

#define PEER_THREAD_NAME_PREFIX "lk_peer_"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
Loading