Bug 1846796 - Read a Span of data via BufferReader r=media-playback-reviewers,padenot

Differential Revision: https://phabricator.services.mozilla.com/D185963
This commit is contained in:
Chun-Min Chang 2023-08-11 17:22:40 +00:00
parent 743c3306b6
commit e5394ca958
2 changed files with 19 additions and 13 deletions

View File

@ -309,6 +309,17 @@ class MOZ_RAII BufferReader {
return true;
}
template <typename T>
mozilla::Result<Span<const T>, nsresult> ReadSpan(size_t aLength) {
auto ptr = Read(aLength * sizeof(T));
if (!ptr) {
MOZ_LOG(gMP4MetadataLog, mozilla::LogLevel::Error,
("%s: failure", __func__));
return mozilla::Err(NS_ERROR_FAILURE);
}
return Span(reinterpret_cast<const T*>(ptr), aLength);
}
private:
const uint8_t* mPtr;
size_t mRemaining;

View File

@ -265,25 +265,21 @@ static Result<already_AddRefed<MediaByteBuffer>, nsresult> RetrieveExtraData(
const nsTArray<uint8_t>& aAvccBytes) {
MOZ_ASSERT(!aAvccBytes.IsEmpty());
// TODO: Add a function to return read bytes in Span so we don't need to copy
// the data temporarily.
BufferReader reader(aAvccBytes);
// The first part is sps.
uint32_t spsSize;
MOZ_TRY_VAR(spsSize, reader.ReadU32());
nsTArray<uint8_t> spsData;
if (!reader.ReadArray(spsData, static_cast<size_t>(spsSize))) {
return Err(NS_ERROR_UNEXPECTED);
}
Span<const uint8_t> spsData;
MOZ_TRY_VAR(spsData,
reader.ReadSpan<const uint8_t>(static_cast<size_t>(spsSize)));
// The second part is pps.
uint32_t ppsSize;
MOZ_TRY_VAR(ppsSize, reader.ReadU32());
nsTArray<uint8_t> ppsData;
if (!reader.ReadArray(ppsData, static_cast<size_t>(ppsSize))) {
return Err(NS_ERROR_UNEXPECTED);
}
Span<const uint8_t> ppsData;
MOZ_TRY_VAR(ppsData,
reader.ReadSpan<const uint8_t>(static_cast<size_t>(ppsSize)));
// Ensure we have profile, constraints and level needed to create the extra
// data.
@ -293,9 +289,8 @@ static Result<already_AddRefed<MediaByteBuffer>, nsresult> RetrieveExtraData(
// Create an extra data
auto extraData = MakeRefPtr<MediaByteBuffer>();
H264::WriteExtraData(extraData, spsData[1], spsData[2], spsData[3],
Span<const uint8_t>(spsData),
Span<const uint8_t>(ppsData));
H264::WriteExtraData(extraData, spsData[1], spsData[2], spsData[3], spsData,
ppsData);
MOZ_ASSERT(extraData);
return extraData.forget();
}