Skip to content

Commit

Permalink
fix: Timeout unfulfilled request to decodingInfo and requestMediaKeyS…
Browse files Browse the repository at this point in the history
…ystemAccess

On some (Android) WebView environments,
decodingInfo and requestMediaKeySystemAccess will
not resolve or reject, at least if RESOURCE_PROTECTED_MEDIA_ID
is not set.  This is a workaround for that issue.
  • Loading branch information
CHaNGeTe committed Nov 27, 2024
1 parent 7caa4a1 commit d9b9bae
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions lib/media/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1870,8 +1870,27 @@ shaka.media.DrmEngine = class {
offlineConfig.sessionTypes = ['persistent-license'];

const configs = [offlineConfig, basicConfig];

const access = await navigator.requestMediaKeySystemAccess(
// On some (Android) WebView environments,
// requestMediaKeySystemAccess will
// not resolve or reject, at least if RESOURCE_PROTECTED_MEDIA_ID
// is not set. This is a workaround for that issue.
const TIMEOUT_FOR_CHECKACCESS_IN_SECONDS = 1;
const checkAccessOrTimeout = (keySystem, configs) => {
return new Promise((resolve, reject) => {
let resolved = false;
navigator.requestMediaKeySystemAccess(keySystem, configs)
.then((access) => {
resolved = true;
resolve(access);
});
new shaka.util.Timer(() => {
if (!resolved) {
reject(new Error('Timeout requestMediaKeySystemAccess'));
}
}).tickAfter(TIMEOUT_FOR_CHECKACCESS_IN_SECONDS);
});
};
const access = await checkAccessOrTimeout(
keySystem, configs);
await processMediaKeySystemAccess(keySystem, access);
} catch (error) {} // Ignore errors.
Expand Down Expand Up @@ -1907,13 +1926,34 @@ shaka.media.DrmEngine = class {
},
},
};

// On some (Android) WebView environments, decodingInfo will
// not resolve or reject, at least if RESOURCE_PROTECTED_MEDIA_ID
// is not set. This is a workaround for that issue.
const TIMEOUT_FOR_DECODING_INFO_IN_SECONDS = 1;
const decodeOrTimeout = (decodingConfig) => {
return new Promise((resolve, reject) => {
let resolved = false;
navigator.mediaCapabilities.decodingInfo(decodingConfig)
.then((result) => {
resolved = true;
resolve(result);
});
new shaka.util.Timer(() => {
if (!resolved) {
reject(new Error('Timeout decodingInfo'));
}
}).tickAfter(TIMEOUT_FOR_DECODING_INFO_IN_SECONDS);
});
};
const decodingInfo =
await navigator.mediaCapabilities.decodingInfo(decodingConfig);
await decodeOrTimeout(decodingConfig);

const access = decodingInfo.keySystemAccess;
await processMediaKeySystemAccess(keySystem, access);
} catch (error) {} // Ignore errors.
} catch (error) {
// Ignore errors.
shaka.log.v2('Failed to probe support for', keySystem, error);
}
};

// Initialize the support structure for each key system.
Expand Down

0 comments on commit d9b9bae

Please sign in to comment.