// Package service — direct-play / HLS streaming. // // StreamService exposes two flavours of playback: // // - Direct play: the original file is served with HTTP Range support. // Works for browser-friendly containers (mp4 / webm / m4v), no ffmpeg // involved, zero CPU overhead. // - HLS: when the client opts in (or the source codec / container is // not browser-friendly), the TranscoderService runs ffmpeg in the // background and we serve the resulting .m3u8 + .ts files directly. // // The HTTP layer decides which mode to use based on the request path: // // GET /api/stream/:id → direct play // GET /api/hls/:id/index.m3u8 → HLS playlist // GET /api/hls/:id/seg_NNNNN.ts → HLS segment package service import ( "context" "errors" "io" "net/http" "net/url" "os" "path/filepath" "strings" "time" "go.uber.org/zap" "github.com/ShukeBta/MediaStationGo/internal/config" "github.com/ShukeBta/MediaStationGo/internal/repository" ) // StreamService serves media files with proper Range support so browsers can // seek into the stream. type StreamService struct { cfg *config.Config log *zap.Logger repo *repository.Container transcoder *TranscoderService } // NewStreamService is the constructor. func NewStreamService(cfg *config.Config, log *zap.Logger, repo *repository.Container, transcoder *TranscoderService) *StreamService { return &StreamService{ cfg: cfg, log: log, repo: repo, transcoder: transcoder, } } // ErrMediaNotFound is returned when the media row or its file is missing. var ErrMediaNotFound = errors.New("media not found") // directPlayOnly reports whether the admin enabled「客户端直连解码」mode, // in which the host never transcodes (HLS is refused) and all playback is // handled by the client (direct play / 302 redirect). func (s *StreamService) directPlayOnly(ctx context.Context) bool { if s.repo == nil || s.repo.Setting == nil { return false } v, err := s.repo.Setting.Get(ctx, PlaybackDirectOnlySettingKey) if err != nil { return false } return parseBoolSetting(v, false) } // withAuthToken propagates the caller's auth token onto an internal redirect // target. A browser