Skip to content

Commit dd6d592

Browse files
committed
perfect: 提高 Emby 匹配资源的兼容性
1 parent 0a55954 commit dd6d592

2 files changed

Lines changed: 6 additions & 79 deletions

File tree

internal/handler/emby.go

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -219,45 +219,9 @@ func (handler *EmbyHandler) VideosHandler(ctx *gin.Context) {
219219
// EmbyServer >= 4.9 ====> mediaSourceID = mediasource_31
220220
mediaSourceID := ctx.Query("mediasourceid")
221221

222-
// // 从 URL 中提取 item ID(例如:/emby/videos/43609/stream 中的 43609)
223-
// var itemID string
224-
// if matches := constants.EmbyRegexp.Router.VideosHandler.FindStringSubmatch(orginalPath); len(matches) > 0 {
225-
// parts := strings.Split(orginalPath, "/")
226-
// for i, part := range parts {
227-
// if part == "videos" && i+1 < len(parts) {
228-
// itemID = parts[i+1]
229-
// break
230-
// }
231-
// }
232-
// }
233-
234-
// // 并发控制:确保同一个 item ID 只有一个任务在运行
235-
// // 将整个处理流程放在锁内,避免重复查询和重复获取重定向 URL
236-
// var muWrapper *mutexWithRefCount
237-
// if itemID != "" {
238-
// // 加载或创建 mutex wrapper
239-
// value, _ := embyServerHandler.playbackInfoMutex.LoadOrStore(itemID, &mutexWithRefCount{})
240-
// muWrapper = value.(*mutexWithRefCount)
241-
242-
// // 增加引用计数
243-
// atomic.AddInt32(&muWrapper.refCount, 1)
244-
245-
// // 锁定并处理
246-
// muWrapper.mu.Lock()
247-
// defer func() {
248-
// muWrapper.mu.Unlock()
249-
// // 减少引用计数
250-
// refCount := atomic.AddInt32(&muWrapper.refCount, -1)
251-
// // 如果没有其他 goroutine 在使用,删除这个 mutex 以避免内存泄漏
252-
// if refCount == 0 {
253-
// embyServerHandler.playbackInfoMutex.Delete(itemID)
254-
// }
255-
// }()
256-
// logging.Debugf("开始处理 item %s 的 VideosHandler 请求", itemID)
257-
// }
258-
259222
logging.Debugf("请求 ItemsServiceQueryItem:%s", mediaSourceID)
260-
itemResponse, err := handler.client.ItemsServiceQueryItem(strings.Replace(mediaSourceID, "mediasource_", "", 1), 1, "Path,MediaSources") // 查询 item 需要去除前缀仅保留数字部分
223+
mediaSourceID_without_prefix := strings.Replace(mediaSourceID, "mediasource_", "", 1)
224+
itemResponse, err := handler.client.ItemsServiceQueryItem(mediaSourceID_without_prefix, 1, "Path,MediaSources") // 查询 item 需要去除前缀仅保留数字部分
261225
if err != nil {
262226
logging.Warning("请求 ItemsServiceQueryItem 失败:", err)
263227
handler.ReverseProxy(ctx.Writer, ctx.Request)
@@ -273,12 +237,14 @@ func (handler *EmbyHandler) VideosHandler(ctx *gin.Context) {
273237
}
274238

275239
strmFileType, opt := recgonizeStrmFileType(*item.Path)
240+
276241
for _, mediasource := range item.MediaSources {
277-
if *mediasource.ID == mediaSourceID { // EmbyServer >= 4.9 返回的ID带有前缀mediasource_
242+
logging.Debugf("mediasource.ID: %s ; mediaSourceID: %s ; mediaSourceID_without_prefix: %s", *mediasource.ID, mediaSourceID, mediaSourceID_without_prefix)
243+
// EmbyServer >= 4.9 返回的ID带有前缀mediasource_
244+
if strings.Replace(*mediasource.ID, "mediasource_", "", 1) == mediaSourceID_without_prefix {
278245
switch strmFileType {
279246
case constants.HTTPStrm:
280247
if *mediasource.Protocol == emby.HTTP {
281-
// httpStrmHandler 内部有缓存机制,锁确保串行化访问
282248
ctx.Redirect(http.StatusFound, handler.httpStrmHandler(*mediasource.Path, ctx.Request.UserAgent()))
283249
return
284250
}

internal/handler/jellyfin.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -180,44 +180,6 @@ func (handler *JellyfinHandler) VideosHandler(ctx *gin.Context) {
180180
return
181181
}
182182

183-
// // 从 URL 中提取 item ID(例如:/Videos/813a630bcf9c3f693a2ec8c498f868d2/stream 中的 813a630bcf9c3f693a2ec8c498f868d2)
184-
// var itemID string
185-
// path := ctx.Request.URL.Path
186-
// if matches := constants.JellyfinRegexp.Router.VideosHandler.FindStringSubmatch(path); len(matches) > 0 {
187-
// parts := strings.Split(path, "/")
188-
// for i, part := range parts {
189-
// if part == "Videos" && i+1 < len(parts) {
190-
// itemID = parts[i+1]
191-
// break
192-
// }
193-
// }
194-
// }
195-
196-
// // 并发控制:确保同一个 item ID 只有一个任务在运行
197-
// // 将整个处理流程放在锁内,避免重复查询和重复获取重定向 URL
198-
// var muWrapper *mutexWithRefCount
199-
// if itemID != "" {
200-
// // 加载或创建 mutex wrapper
201-
// value, _ := handler.playbackInfoMutex.LoadOrStore(itemID, &mutexWithRefCount{})
202-
// muWrapper = value.(*mutexWithRefCount)
203-
204-
// // 增加引用计数
205-
// atomic.AddInt32(&muWrapper.refCount, 1)
206-
207-
// // 锁定并处理
208-
// muWrapper.mu.Lock()
209-
// defer func() {
210-
// muWrapper.mu.Unlock()
211-
// // 减少引用计数
212-
// refCount := atomic.AddInt32(&muWrapper.refCount, -1)
213-
// // 如果没有其他 goroutine 在使用,删除这个 mutex 以避免内存泄漏
214-
// if refCount == 0 {
215-
// handler.playbackInfoMutex.Delete(itemID)
216-
// }
217-
// }()
218-
// logging.Debugf("开始处理 item %s 的 VideosHandler 请求", itemID)
219-
// }
220-
221183
mediaSourceID := ctx.Query("mediasourceid")
222184
logging.Debugf("请求 ItemsServiceQueryItem:%s", mediaSourceID)
223185
itemResponse, err := handler.client.ItemsServiceQueryItem(mediaSourceID, 1, "Path,MediaSources") // 查询 item 需要去除前缀仅保留数字部分
@@ -241,7 +203,6 @@ func (handler *JellyfinHandler) VideosHandler(ctx *gin.Context) {
241203
switch strmFileType {
242204
case constants.HTTPStrm:
243205
if *mediasource.Protocol == jellyfin.HTTP {
244-
// httpStrmHandler 内部有缓存机制,锁确保串行化访问
245206
ctx.Redirect(http.StatusFound, handler.httpStrmHandler(*mediasource.Path, ctx.Request.UserAgent()))
246207
return
247208
}

0 commit comments

Comments
 (0)