Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What does "engine is closed" mean? #437

Open
kristian1108 opened this issue Sep 20, 2024 · 0 comments
Open

What does "engine is closed" mean? #437

kristian1108 opened this issue Sep 20, 2024 · 0 comments

Comments

@kristian1108
Copy link

This code is crashing with this error:

async fn publish_loop() -> Result<(JoinHandle<()>, NativeAudioSource)> {
    const FRAME_DURATION: Duration = Duration::from_millis(1000);
    let file_data = read_samples_from_file(FRAME_DURATION).await?;

    let source = NativeAudioSource::new(
        AudioSourceOptions::default(),
        file_data.sample_rate,
        file_data.num_channels as u32,
        None,
    );

    let source_clone = source.clone();
    let mut frame_index = 0;
    let max_frame_index = file_data.data_frames.len() - 1;
    let handle = tokio::spawn(async move {
        loop {
            if frame_index > max_frame_index {
                frame_index = 0;
            }
            let frame = &file_data.data_frames[frame_index];
            let frame_size = frame.len();
            println!("Writing frame size: {}", frame_size);
            let audio_frame = AudioFrame {
                data: frame.into(),
                num_channels: file_data.num_channels as u32,
                sample_rate: file_data.sample_rate,
                samples_per_channel: (frame_size / file_data.num_channels as usize) as u32,
            };
            source_clone.capture_frame(&audio_frame).await.unwrap();
            frame_index += 1;
        }
    });

    Ok((handle, source))
}

async fn receive_loop() -> Result<JoinHandle<()>>
{
    let url = String::from("<LIVEKIT_URL>");
    let token = create_token("some-room".to_string()).unwrap();

    let (_, mut rx) = Room::connect(&url, &token, RoomOptions::default())
        .await
        .unwrap();

    let event_loop = tokio::spawn(async move {
        while let Some(msg) = rx.recv().await {
            println!("Event: {:?}", msg);
        }
    });

    Ok(event_loop)
}


#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let url = String::from("<LIVEKIT_URL>");
    let token = create_token("some-room".to_string()).unwrap();

    let (room, mut rx) = Room::connect(&url, &token, RoomOptions::default())
        .await
        .unwrap();
    let room = Arc::new(room);

    let (handle, source) = publish_loop().await?;
    let receive_handle = receive_loop().await?;

    let track = LocalAudioTrack::create_audio_track("file", RtcAudioSource::Native(source.clone()));

    room.local_participant()
        .publish_track(
            LocalTrack::Audio(track),
            TrackPublishOptions {
                source: TrackSource::Microphone,
                ..Default::default()
            },
        )
        .await.unwrap();

    handle.await.unwrap();
    receive_handle.await.unwrap();

    room.close().await.expect("failed to close room");

    Ok(())
}
Writing frame size: 48000
Event: Connected { participants_with_tracks: [] }
Event: ConnectionStateChanged(Connected)
thread 'main' panicked at src/main.rs:244:16:
called `Result::unwrap()` on an `Err` value: Engine(Connection("engine is closed"))
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I'm not clear what the engine is or how to reset it. Any help would be much appreciated 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant