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

Audio file is cropped from the beginning #435

Open
vkuptcov opened this issue Apr 2, 2024 · 3 comments
Open

Audio file is cropped from the beginning #435

vkuptcov opened this issue Apr 2, 2024 · 3 comments

Comments

@vkuptcov
Copy link

vkuptcov commented Apr 2, 2024

I created a very simple server that sends ogg OPUS audio files to web clients.

	room, err := lksdk.ConnectToRoom(os.Getenv("LIVEKIT_URL"), lksdk.ConnectInfo{
		APIKey:              os.Getenv("LIVEKIT_API_KEY"),
		APISecret:           os.Getenv("LIVEKIT_API_SECRET"),
		RoomName:            roomName,
		ParticipantIdentity: "local",
	}, &lksdk.RoomCallback{})
	if err != nil {
		log.Fatal("cannot connect to room:", err)
		return err
	}

	track, err := lksdk.NewLocalFileTrack(file,
		lksdk.ReaderTrackWithMime(webrtc.MimeTypeOpus),
		lksdk.ReaderTrackWithOnWriteComplete(func() { fmt.Println("track finished") }),
	)
	if err != nil {
		log.Fatal("cannot create track:", err)
		return err
	}

	res, err := room.LocalParticipant.PublishTrack(track, &lksdk.TrackPublicationOptions{
		Name: file,
	})

	if err != nil {
		log.Fatal("cannot publish track:", err)
		return err
	}

Basically, I did everything as described here https://github.com/livekit/server-sdk-go?tab=readme-ov-file#publishing-tracks-to-room
adapting only to audio files.
In about 90% of the cases, the first second or even several seconds are missed and I don't hear them on the client side.
It should be possible to somehow make sure that all clients subscribed to the track before publishing it.

@davidzhao
Copy link
Member

This is a bit tricky due to the subscription lifecycle. the current logic in NewLocalReaderTrack is to start writing the file as soon as the track is considered published:

	track.OnBind(func() {
		if err := track.StartWrite(provider, provider.OnWriteComplete); err != nil {
			logger.Errorw("Could not start writing", err)
		}
	})

There isn't really a way to know when all of the the subscribers have subscribed to the track.. but I think you can modify this function to do the following:

  1. construct an empty opus packet: see here for an example
  2. write it with track.WriteRTP, this lets server complete the publication process
  3. sleep for a few seconds to give subscribers time to subscribe
  4. then call track.StartWrite

@vkuptcov
Copy link
Author

vkuptcov commented Apr 3, 2024

Thank you, @davidzhao ! I've tested the idea and it looks like it's the way to go

@denis-obukhov
Copy link

denis-obukhov commented Jun 28, 2024

Is there a ready-to-use method to publish an audio file without missing the beginning parts? It's quite strange that this is an issue in such a large streaming ecosystem. Specifically, my case is 1-to-1 prerecorded audio file streaming

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

3 participants