Пример #1
0
// InsertTrack inserts track `t` at position `i` in the queue.
func (q *Queue) InsertTrack(i int, t interfaces.Track) error {
	q.mutex.Lock()
	beforeLen := len(q.Queue)

	// An error should never occur here since maxTrackDuration is restricted to
	// ints. Any error in the configuration will be caught during yaml load.
	maxTrackDuration, _ := time.ParseDuration(fmt.Sprintf("%ds",
		viper.GetInt("queue.max_track_duration")))

	if viper.GetInt("queue.max_track_duration") == 0 ||
		t.GetDuration() <= maxTrackDuration {
		q.Queue = append(q.Queue, Track{})
		copy(q.Queue[i+1:], q.Queue[i:])
		q.Queue[i] = t
	} else {
		q.mutex.Unlock()
		return errors.New("The track is too long to add to the queue")
	}
	if len(q.Queue) == beforeLen+1 {
		q.mutex.Unlock()
		q.playIfNeeded()
		return nil
	}
	q.mutex.Unlock()
	return errors.New("Could not add track to queue")
}