Skip to content

Commit

Permalink
URLSessionTask과 관계 없이 ResourceLoader가 원하는 데이터를 전달할 수 있도록 수정 (#1099)
Browse files Browse the repository at this point in the history
## Description
- URLSessionTask가 끝나면 바로 downloadedAudioData를 삭제하고, ResourceLoader로 데이터를
전달하는 로직을 중단하고있어 Player가 Resource가 아직 준비되지 않았다고 판단하고 있음
- 네트워크와 상관 없이 ResourceLoader가 원하는 정보를 음악이 중단될 때까지 제공하도록 수정
  • Loading branch information
childc authored Oct 27, 2023
1 parent ea7f267 commit 06e3742
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions NuguCore/Sources/MediaPlayer/MediaPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ extension MediaPlayer {
mediaPlayer.replaceCurrentItem(with: nil)
removePlayerItemObserver() // CHECK-ME: 타이밍 이슈 없을지 확인

downloadAudioData = nil
playerItem = nil
player = nil

Expand Down Expand Up @@ -114,13 +115,12 @@ extension MediaPlayer {
}

public func seek(to offset: TimeIntervallic, completion: ((EndedUp<Error>) -> Void)?) {
guard
let mediaPlayer = player,
mediaPlayer.currentItem != nil else {
completion?(.failure(MediaPlayableError.notPrepareSource))
return
guard let mediaPlayer = player,
mediaPlayer.currentItem != nil else {
completion?(.failure(MediaPlayableError.notPrepareSource))
return
}

mediaPlayer.seek(to: offset.cmTime)
completion?(.success)
}
Expand Down Expand Up @@ -204,13 +204,13 @@ extension MediaPlayer: MediaUrlDataSource {
playerItem.cacheKey = cacheKey
addPlayerItemObserver(object: playerItem)
self.playerItem = playerItem

player = AVQueuePlayer(playerItem: playerItem)

if offset.seconds > 0 {
player?.seek(to: offset.cmTime)
}

}
}

Expand Down Expand Up @@ -244,28 +244,29 @@ extension MediaPlayer {

extension MediaPlayer: AVAssetResourceLoaderDelegate {
public func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
guard let originalScheme = originalScheme else {
log.error("originalScheme should not be nil")
return false
}

pendingRequestQueue.sync {
_ = pendingRequests.insert(loadingRequest)
}

if sessionHasFinishedLoading == true {
guard (sessionHasFinishedLoading ?? false) == false else {
processPendingRequests()
return true
}

if downloadSession == nil {
guard let url = loadingRequest.request.url,
var urlToConvert = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
var urlToConvert = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
log.error("intercepted url is invalid")
return false
}

sessionHasFinishedLoading = false
guard let originalScheme = originalScheme else {
log.error("originalScheme should not be nil")
return false
}

urlToConvert.scheme = originalScheme
sessionHasFinishedLoading = false

guard let convertedUrl = urlToConvert.url else {
log.error("intercepted url is invalid")
Expand Down Expand Up @@ -314,7 +315,7 @@ private extension MediaPlayer {

func fillInContentInformation(contentInformationRequest: AVAssetResourceLoadingContentInformationRequest?) {
guard let downloadResponse = downloadResponse,
let mimeType = downloadResponse.mimeType else { return }
let mimeType = downloadResponse.mimeType else { return }

let unmanagedFileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)
guard let contentType = unmanagedFileUTI?.takeRetainedValue() else {
Expand Down Expand Up @@ -361,11 +362,10 @@ private extension MediaPlayer {
downloadDataTask?.cancel()
downloadDataTask = nil
downloadSession = nil
downloadAudioData = nil
downloadResponse = nil
originalScheme = nil

if pendingRequests.count > 0 {
if pendingRequests.isEmpty == false {
for request in pendingRequests {
request.finishLoading()
}
Expand Down Expand Up @@ -406,7 +406,7 @@ extension MediaPlayer: URLSessionDataDelegate {
self.downloadAudioData?.append(data)

if let downloadAudioData = downloadAudioData,
let expectedDataLength = expectedDataLength {
let expectedDataLength = expectedDataLength {
log.debug("\(Float(downloadAudioData.count) / Float(expectedDataLength))")
} else {
log.debug("expectedDataLength should not be nil!")
Expand All @@ -418,10 +418,9 @@ extension MediaPlayer: URLSessionDataDelegate {
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
defer {
releaseCacheData()
sessionHasFinishedLoading = true
}

sessionHasFinishedLoading = true

if error != nil {
log.error("\(error!)")
return
Expand All @@ -430,14 +429,14 @@ extension MediaPlayer: URLSessionDataDelegate {
processPendingRequests()

guard let audioDataToWrite = downloadAudioData,
let itemKeyForCache = playerItem?.cacheKey else {
let itemKeyForCache = playerItem?.cacheKey else {
return
}

MediaCacheManager.saveMediaData(
mediaData: audioDataToWrite,
cacheKey: itemKeyForCache
) ? log.debug("SaveComplete: \(itemKeyForCache)") : log.error("SaveFailed")
) ? log.debug("SaveComplete: \(itemKeyForCache)") : log.error("SaveFailed")
}
}

Expand All @@ -453,11 +452,11 @@ private extension MediaPlayer {
if let cacheKey = object.cacheKey {
MediaCacheManager.setModifiedDateForCacheFile(key: cacheKey)
}

case .failed:
log.debug("playback failed reason: \(object.error.debugDescription)")
self.delegate?.mediaPlayerStateDidChange(.error(error: object.error ?? MediaPlayableError.unknown), mediaPlayer: self)

default:
break
}
Expand Down

0 comments on commit 06e3742

Please sign in to comment.