mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:30:27 +00:00
faafb5fd9d
- As suggested in PR 101, use OpenBSD assembler files. Update the NetBSD on them and use .S instead of .s, to indicate files to process (or preprocessor would fail on comments). (6a17dbacc3) - Bug 1229769 - Expose Promise interface to WorkerDebugger #ifdef SPIDERMONKEY_PROMISE;r=bz (da9e838c23) - Bug 1155969 - Make xpt.py flake8 compliant. r=ted (84f8eab5a3) - Bug 977464 - Always relink XPT files for all changed XPIDL interfaces without requiring the IID to be revved; r=khuey (9b22512c41) - Bug 977464 follow-up: Fix the indentation to use 4 spaces (bd68a8ebc3) - Bug 1240053 - Consider the order of methods, their params, and constant important when comparing XPT interfaces to decide whether to relink XPT files; r=khuey (b9253dd183) - Bug 1264377. Get rid of some unnecessary custom JSClass hook functions in xpconnect sandboxes and DOM simple globals. r=bholley (60950b416b) - Bug 1258496 - Purge message manager cached scripts on 'message-manager-flush-caches' notification. r=smaug (028b229d02) - Bug 1251298 - Null out |*idp| when necessary in DoInterfaceDescriptor. r=khuey. (dbdd15dae8) - Bug 659625 - part1: implement Console::clear in dom/base/Console.cpp;r=baku (17c4b33789) - Bug 659625 - part2: implement console.clear in devtools webconsole;r=bgrins (b72c6173ee) - Bug 1248507 - p5. DecoderDoctorDiagnostics implementation - r=jya,bz (22f68130af) - Bug 1248507 - p6. Minimal notification definition - r=bz (02f3eeb2f9) - Bug 1248507 - p7. Notify decoder-doctor-notification listeners - r=jya,bz (2c2eb33388) - Bug 1248507 - p8. FFMpeg checks: Console message - r=bz (50a993c143) - Bug 1248507 - p9. FFMpeg checks: Notification definition - r=bz (0bcdcc090c) - Bug 1248507 - p10. Detect and report when FFMpeg/Linux fails to load - r=jya (28137efda0) - Bug 1190939: Decode VP9 4:4:4 properly. r=jya (98508bb48b) - Bug 1232911 - [1.2] Allow to test for specific VPX MIME type version. r=cpearce (1b53e02981) - Bug 1251887 - Add break to unintentional switch fallthrough in GfxInfoBase.cpp to fix -Wimplicit-fallthrough warning. r=milan (9969a7bec7) - Bug 1232911 - [2.2] Add VPX decoding blocking support. r=snorp (fa860a9d4d) - Bug 1249777: Added support for 10.11 in the blocklisting code as well. r=mstange (479f629083) - Bug 1242084 - Fix GfxInfoBase nsStringBuffer leak. r=dvander (87b38ee72d) - Bug 1222201: Only use container calculated dimensions. r=cpearce (693ebdf450) - Bug 1190240 - Cannot compile WMFVideoMFTManager.cpp using Windows 10 SDK. r=cpearce (8ee2e315f5) - Bug 1248496 - Enable D3D11 DXVA. r=ajones (a79df0baf2) - Bug 1248496 - Show which DXVA API is being used in about:support. r=jya (1f6b1f0c8e) - Bug 1257028 - Fallback to d3d9 decoding if d3d11 fails. r=cpearce (5ad7c159f1) - Bug 1232045 - WebMDemuxer handles resolution changes. r=jya (18bdc79b1c) - Bug 1243538: P1. Make MediaInfo::mImage an nsIntSize again and introduce a mImageRect member. r=mattwoodrow (a446cca01e) - Bug 1243538: P2. Add convenience VideoInfo::ScaledImageRect. r=mattwoodrow (657e675b72) - Bug 1243538: P3. Adjust libvpx decoder to allow different decoding size from metadata. r=mattwoodrow (50949ce02d) - Bug 1243538: P4. Adjust ffvpx decoder to allow different decoding size from metadata. r=mattwoodrow (392c8939f5) - Bug 1243538: P5. Adjust wmf decoder to allow different decoding size from metadata. r=cpearce (f50940564f) - Bug 1239611 - Remove GonkNativeWindowClient r=nical (2c7ccb54a4) - Bug 1170589 - Force decoder to use all allocated buffers. r=bwu (7e5c02e48a) - Bug 1222923 - Enable MOZ_FMP4 on gonk L r=jolin (c04ad6ff55) - Bug 1178214 - Return INIT_ERROR when video resolution exceeds hw codec capability. r=sotaro (bf3c45cde1) - Bug 1147304 - Send codec specific data for MPEG4 codec type only. r=jya (ca48d110f4) - Bug 1243538: P6. Adjust gonk decoder to allow different decoding size from metadata. r=alfredo (257e017762) - Bug 1243538: [webm] P7. Let the decoder handle picture resizing. r=SingingTree (32dc4a5aac) - Bug 1262727: [webm] Ensure first frame returned after seek is a keyframe. r=kinetik (f16140852a) - Bug 1246536: [webm] Only use discard padding information on last packet. r=kinetik (0bac4f8855) - Bug 1266013: Fix Firefox OS compile errors. r=gerald (f021717287) - cleanup (390cdec6ee) - Bug 1264991: Don't construct invalid channel configuration. r=gerald (661828e8b8) - Bug 1265093: Fix CID 1358648. r=gerald (55468c1261) - Bug 1262659 - Report HTTP Live Streaming playback requests. r=cpearce,bsmedberg (96b8cd2810) - Bug 1265400 - Use unsigned long for AudioBuffer length and numberOfChannels; r=smaug (f74f27ea4e)
1031 lines
29 KiB
C++
1031 lines
29 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "nsError.h"
|
|
#include "MediaDecoderStateMachine.h"
|
|
#include "AbstractMediaDecoder.h"
|
|
#include "MediaResource.h"
|
|
#include "WebMDemuxer.h"
|
|
#include "WebMBufferedParser.h"
|
|
#include "gfx2DGlue.h"
|
|
#include "mozilla/Atomics.h"
|
|
#include "mozilla/Endian.h"
|
|
#include "mozilla/Preferences.h"
|
|
#include "mozilla/SharedThreadPool.h"
|
|
#include "MediaDataDemuxer.h"
|
|
#include "nsAutoRef.h"
|
|
#include "NesteggPacketHolder.h"
|
|
#include "XiphExtradata.h"
|
|
#include "prprf.h" // leaving it for PR_vsnprintf()
|
|
#include "mozilla/Snprintf.h"
|
|
|
|
#include <algorithm>
|
|
#include <stdint.h>
|
|
|
|
#define VPX_DONT_DEFINE_STDINT_TYPES
|
|
#include "vpx/vp8dx.h"
|
|
#include "vpx/vpx_decoder.h"
|
|
|
|
#define WEBM_DEBUG(arg, ...) MOZ_LOG(gWebMDemuxerLog, mozilla::LogLevel::Debug, ("WebMDemuxer(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
|
|
|
|
namespace mozilla {
|
|
|
|
using namespace gfx;
|
|
|
|
LazyLogModule gWebMDemuxerLog("WebMDemuxer");
|
|
LazyLogModule gNesteggLog("Nestegg");
|
|
|
|
// How far ahead will we look when searching future keyframe. In microseconds.
|
|
// This value is based on what appears to be a reasonable value as most webm
|
|
// files encountered appear to have keyframes located < 4s.
|
|
#define MAX_LOOK_AHEAD 10000000
|
|
|
|
static Atomic<uint32_t> sStreamSourceID(0u);
|
|
|
|
// Functions for reading and seeking using WebMDemuxer required for
|
|
// nestegg_io. The 'user data' passed to these functions is the
|
|
// demuxer.
|
|
static int webmdemux_read(void* aBuffer, size_t aLength, void* aUserData)
|
|
{
|
|
MOZ_ASSERT(aUserData);
|
|
MOZ_ASSERT(aLength < UINT32_MAX);
|
|
WebMDemuxer* demuxer = reinterpret_cast<WebMDemuxer*>(aUserData);
|
|
uint32_t count = aLength;
|
|
if (demuxer->IsMediaSource()) {
|
|
int64_t length = demuxer->GetEndDataOffset();
|
|
int64_t position = demuxer->GetResource()->Tell();
|
|
MOZ_ASSERT(position <= demuxer->GetResource()->GetLength());
|
|
MOZ_ASSERT(position <= length);
|
|
if (length >= 0 && count + position > length) {
|
|
count = length - position;
|
|
}
|
|
MOZ_ASSERT(count <= aLength);
|
|
}
|
|
uint32_t bytes = 0;
|
|
nsresult rv =
|
|
demuxer->GetResource()->Read(static_cast<char*>(aBuffer), count, &bytes);
|
|
bool eof = bytes < aLength;
|
|
return NS_FAILED(rv) ? -1 : eof ? 0 : 1;
|
|
}
|
|
|
|
static int webmdemux_seek(int64_t aOffset, int aWhence, void* aUserData)
|
|
{
|
|
MOZ_ASSERT(aUserData);
|
|
WebMDemuxer* demuxer = reinterpret_cast<WebMDemuxer*>(aUserData);
|
|
nsresult rv = demuxer->GetResource()->Seek(aWhence, aOffset);
|
|
return NS_SUCCEEDED(rv) ? 0 : -1;
|
|
}
|
|
|
|
static int64_t webmdemux_tell(void* aUserData)
|
|
{
|
|
MOZ_ASSERT(aUserData);
|
|
WebMDemuxer* demuxer = reinterpret_cast<WebMDemuxer*>(aUserData);
|
|
return demuxer->GetResource()->Tell();
|
|
}
|
|
|
|
static void webmdemux_log(nestegg* aContext,
|
|
unsigned int aSeverity,
|
|
char const* aFormat, ...)
|
|
{
|
|
if (!MOZ_LOG_TEST(gNesteggLog, LogLevel::Debug)) {
|
|
return;
|
|
}
|
|
|
|
va_list args;
|
|
char msg[256];
|
|
const char* sevStr;
|
|
|
|
switch(aSeverity) {
|
|
case NESTEGG_LOG_DEBUG:
|
|
sevStr = "DBG";
|
|
break;
|
|
case NESTEGG_LOG_INFO:
|
|
sevStr = "INF";
|
|
break;
|
|
case NESTEGG_LOG_WARNING:
|
|
sevStr = "WRN";
|
|
break;
|
|
case NESTEGG_LOG_ERROR:
|
|
sevStr = "ERR";
|
|
break;
|
|
case NESTEGG_LOG_CRITICAL:
|
|
sevStr = "CRT";
|
|
break;
|
|
default:
|
|
sevStr = "UNK";
|
|
break;
|
|
}
|
|
|
|
va_start(args, aFormat);
|
|
|
|
snprintf_literal(msg, "%p [Nestegg-%s] ", aContext, sevStr);
|
|
PR_vsnprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), aFormat, args);
|
|
MOZ_LOG(gNesteggLog, LogLevel::Debug, (msg));
|
|
|
|
va_end(args);
|
|
}
|
|
|
|
|
|
WebMDemuxer::WebMDemuxer(MediaResource* aResource)
|
|
: WebMDemuxer(aResource, false)
|
|
{
|
|
}
|
|
|
|
WebMDemuxer::WebMDemuxer(MediaResource* aResource, bool aIsMediaSource)
|
|
: mResource(aResource)
|
|
, mBufferedState(nullptr)
|
|
, mInitData(nullptr)
|
|
, mContext(nullptr)
|
|
, mVideoTrack(0)
|
|
, mAudioTrack(0)
|
|
, mSeekPreroll(0)
|
|
, mAudioCodec(-1)
|
|
, mVideoCodec(-1)
|
|
, mHasVideo(false)
|
|
, mHasAudio(false)
|
|
, mNeedReIndex(true)
|
|
, mLastWebMBlockOffset(-1)
|
|
, mIsMediaSource(aIsMediaSource)
|
|
{
|
|
}
|
|
|
|
WebMDemuxer::~WebMDemuxer()
|
|
{
|
|
Reset();
|
|
Cleanup();
|
|
}
|
|
|
|
RefPtr<WebMDemuxer::InitPromise>
|
|
WebMDemuxer::Init()
|
|
{
|
|
InitBufferedState();
|
|
|
|
if (NS_FAILED(ReadMetadata())) {
|
|
return InitPromise::CreateAndReject(DemuxerFailureReason::DEMUXER_ERROR, __func__);
|
|
}
|
|
|
|
if (!GetNumberTracks(TrackInfo::kAudioTrack) &&
|
|
!GetNumberTracks(TrackInfo::kVideoTrack)) {
|
|
return InitPromise::CreateAndReject(DemuxerFailureReason::DEMUXER_ERROR, __func__);
|
|
}
|
|
|
|
return InitPromise::CreateAndResolve(NS_OK, __func__);
|
|
}
|
|
|
|
void
|
|
WebMDemuxer::InitBufferedState()
|
|
{
|
|
MOZ_ASSERT(!mBufferedState);
|
|
mBufferedState = new WebMBufferedState;
|
|
}
|
|
|
|
bool
|
|
WebMDemuxer::HasTrackType(TrackInfo::TrackType aType) const
|
|
{
|
|
return !!GetNumberTracks(aType);
|
|
}
|
|
|
|
uint32_t
|
|
WebMDemuxer::GetNumberTracks(TrackInfo::TrackType aType) const
|
|
{
|
|
switch(aType) {
|
|
case TrackInfo::kAudioTrack:
|
|
return mHasAudio ? 1 : 0;
|
|
case TrackInfo::kVideoTrack:
|
|
return mHasVideo ? 1 : 0;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
UniquePtr<TrackInfo>
|
|
WebMDemuxer::GetTrackInfo(TrackInfo::TrackType aType,
|
|
size_t aTrackNumber) const
|
|
{
|
|
switch(aType) {
|
|
case TrackInfo::kAudioTrack:
|
|
return mInfo.mAudio.Clone();
|
|
case TrackInfo::kVideoTrack:
|
|
return mInfo.mVideo.Clone();
|
|
default:
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
already_AddRefed<MediaTrackDemuxer>
|
|
WebMDemuxer::GetTrackDemuxer(TrackInfo::TrackType aType, uint32_t aTrackNumber)
|
|
{
|
|
if (GetNumberTracks(aType) <= aTrackNumber) {
|
|
return nullptr;
|
|
}
|
|
RefPtr<WebMTrackDemuxer> e =
|
|
new WebMTrackDemuxer(this, aType, aTrackNumber);
|
|
mDemuxers.AppendElement(e);
|
|
|
|
return e.forget();
|
|
}
|
|
|
|
nsresult
|
|
WebMDemuxer::Reset()
|
|
{
|
|
mVideoPackets.Reset();
|
|
mAudioPackets.Reset();
|
|
return NS_OK;
|
|
}
|
|
|
|
void
|
|
WebMDemuxer::Cleanup()
|
|
{
|
|
if (mContext) {
|
|
nestegg_destroy(mContext);
|
|
mContext = nullptr;
|
|
}
|
|
mBufferedState = nullptr;
|
|
}
|
|
|
|
nsresult
|
|
WebMDemuxer::ReadMetadata()
|
|
{
|
|
nestegg_io io;
|
|
io.read = webmdemux_read;
|
|
io.seek = webmdemux_seek;
|
|
io.tell = webmdemux_tell;
|
|
io.userdata = this;
|
|
int r = nestegg_init(&mContext, io, &webmdemux_log,
|
|
IsMediaSource() ? mResource.GetLength() : -1);
|
|
if (r == -1) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
{
|
|
// Check how much data nestegg read and force feed it to BufferedState.
|
|
RefPtr<MediaByteBuffer> buffer = mResource.MediaReadAt(0, mResource.Tell());
|
|
if (!buffer) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
mBufferedState->NotifyDataArrived(buffer->Elements(), buffer->Length(), 0);
|
|
if (mBufferedState->GetInitEndOffset() < 0) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
MOZ_ASSERT(mBufferedState->GetInitEndOffset() <= mResource.Tell());
|
|
}
|
|
mInitData = mResource.MediaReadAt(0, mBufferedState->GetInitEndOffset());
|
|
if (!mInitData ||
|
|
mInitData->Length() != size_t(mBufferedState->GetInitEndOffset())) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
unsigned int ntracks = 0;
|
|
r = nestegg_track_count(mContext, &ntracks);
|
|
if (r == -1) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
for (unsigned int track = 0; track < ntracks; ++track) {
|
|
int id = nestegg_track_codec_id(mContext, track);
|
|
if (id == -1) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
int type = nestegg_track_type(mContext, track);
|
|
if (type == NESTEGG_TRACK_VIDEO && !mHasVideo) {
|
|
nestegg_video_params params;
|
|
r = nestegg_track_video_params(mContext, track, ¶ms);
|
|
if (r == -1) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
mVideoCodec = nestegg_track_codec_id(mContext, track);
|
|
switch(mVideoCodec) {
|
|
case NESTEGG_CODEC_VP8:
|
|
mInfo.mVideo.mMimeType = "video/webm; codecs=vp8";
|
|
break;
|
|
case NESTEGG_CODEC_VP9:
|
|
mInfo.mVideo.mMimeType = "video/webm; codecs=vp9";
|
|
break;
|
|
default:
|
|
NS_WARNING("Unknown WebM video codec");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
// Picture region, taking into account cropping, before scaling
|
|
// to the display size.
|
|
unsigned int cropH = params.crop_right + params.crop_left;
|
|
unsigned int cropV = params.crop_bottom + params.crop_top;
|
|
nsIntRect pictureRect(params.crop_left,
|
|
params.crop_top,
|
|
params.width - cropH,
|
|
params.height - cropV);
|
|
|
|
// If the cropping data appears invalid then use the frame data
|
|
if (pictureRect.width <= 0 ||
|
|
pictureRect.height <= 0 ||
|
|
pictureRect.x < 0 ||
|
|
pictureRect.y < 0) {
|
|
pictureRect.x = 0;
|
|
pictureRect.y = 0;
|
|
pictureRect.width = params.width;
|
|
pictureRect.height = params.height;
|
|
}
|
|
|
|
// Validate the container-reported frame and pictureRect sizes. This
|
|
// ensures that our video frame creation code doesn't overflow.
|
|
nsIntSize displaySize(params.display_width, params.display_height);
|
|
nsIntSize frameSize(params.width, params.height);
|
|
if (!IsValidVideoRegion(frameSize, pictureRect, displaySize)) {
|
|
// Video track's frame sizes will overflow. Ignore the video track.
|
|
continue;
|
|
}
|
|
|
|
mVideoTrack = track;
|
|
mHasVideo = true;
|
|
|
|
mInfo.mVideo.mDisplay = displaySize;
|
|
mInfo.mVideo.mImage = frameSize;
|
|
mInfo.mVideo.SetImageRect(pictureRect);
|
|
|
|
switch (params.stereo_mode) {
|
|
case NESTEGG_VIDEO_MONO:
|
|
mInfo.mVideo.mStereoMode = StereoMode::MONO;
|
|
break;
|
|
case NESTEGG_VIDEO_STEREO_LEFT_RIGHT:
|
|
mInfo.mVideo.mStereoMode = StereoMode::LEFT_RIGHT;
|
|
break;
|
|
case NESTEGG_VIDEO_STEREO_BOTTOM_TOP:
|
|
mInfo.mVideo.mStereoMode = StereoMode::BOTTOM_TOP;
|
|
break;
|
|
case NESTEGG_VIDEO_STEREO_TOP_BOTTOM:
|
|
mInfo.mVideo.mStereoMode = StereoMode::TOP_BOTTOM;
|
|
break;
|
|
case NESTEGG_VIDEO_STEREO_RIGHT_LEFT:
|
|
mInfo.mVideo.mStereoMode = StereoMode::RIGHT_LEFT;
|
|
break;
|
|
}
|
|
uint64_t duration = 0;
|
|
r = nestegg_duration(mContext, &duration);
|
|
if (!r) {
|
|
mInfo.mVideo.mDuration = media::TimeUnit::FromNanoseconds(duration).ToMicroseconds();
|
|
}
|
|
} else if (type == NESTEGG_TRACK_AUDIO && !mHasAudio) {
|
|
nestegg_audio_params params;
|
|
r = nestegg_track_audio_params(mContext, track, ¶ms);
|
|
if (r == -1) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
mAudioTrack = track;
|
|
mHasAudio = true;
|
|
mCodecDelay = media::TimeUnit::FromNanoseconds(params.codec_delay).ToMicroseconds();
|
|
mAudioCodec = nestegg_track_codec_id(mContext, track);
|
|
if (mAudioCodec == NESTEGG_CODEC_VORBIS) {
|
|
mInfo.mAudio.mMimeType = "audio/webm; codecs=vorbis";
|
|
} else if (mAudioCodec == NESTEGG_CODEC_OPUS) {
|
|
mInfo.mAudio.mMimeType = "audio/webm; codecs=opus";
|
|
uint8_t c[sizeof(uint64_t)];
|
|
BigEndian::writeUint64(&c[0], mCodecDelay);
|
|
mInfo.mAudio.mCodecSpecificConfig->AppendElements(&c[0], sizeof(uint64_t));
|
|
}
|
|
mSeekPreroll = params.seek_preroll;
|
|
mInfo.mAudio.mRate = params.rate;
|
|
mInfo.mAudio.mChannels = params.channels;
|
|
|
|
unsigned int nheaders = 0;
|
|
r = nestegg_track_codec_data_count(mContext, track, &nheaders);
|
|
if (r == -1) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
AutoTArray<const unsigned char*,4> headers;
|
|
AutoTArray<size_t,4> headerLens;
|
|
for (uint32_t header = 0; header < nheaders; ++header) {
|
|
unsigned char* data = 0;
|
|
size_t length = 0;
|
|
r = nestegg_track_codec_data(mContext, track, header, &data, &length);
|
|
if (r == -1) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
headers.AppendElement(data);
|
|
headerLens.AppendElement(length);
|
|
}
|
|
|
|
// Vorbis has 3 headers, convert to Xiph extradata format to send them to
|
|
// the demuxer.
|
|
// TODO: This is already the format WebM stores them in. Would be nice
|
|
// to avoid having libnestegg split them only for us to pack them again,
|
|
// but libnestegg does not give us an API to access this data directly.
|
|
if (nheaders > 1) {
|
|
if (!XiphHeadersToExtradata(mInfo.mAudio.mCodecSpecificConfig,
|
|
headers, headerLens)) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
}
|
|
else {
|
|
mInfo.mAudio.mCodecSpecificConfig->AppendElements(headers[0],
|
|
headerLens[0]);
|
|
}
|
|
uint64_t duration = 0;
|
|
r = nestegg_duration(mContext, &duration);
|
|
if (!r) {
|
|
mInfo.mAudio.mDuration = media::TimeUnit::FromNanoseconds(duration).ToMicroseconds();
|
|
}
|
|
}
|
|
}
|
|
return NS_OK;
|
|
}
|
|
|
|
bool
|
|
WebMDemuxer::IsSeekable() const
|
|
{
|
|
return mContext && nestegg_has_cues(mContext);
|
|
}
|
|
|
|
bool
|
|
WebMDemuxer::IsSeekableOnlyInBufferedRanges() const
|
|
{
|
|
return mContext && !nestegg_has_cues(mContext);
|
|
}
|
|
|
|
void
|
|
WebMDemuxer::EnsureUpToDateIndex()
|
|
{
|
|
if (!mNeedReIndex || !mInitData) {
|
|
return;
|
|
}
|
|
AutoPinned<MediaResource> resource(mResource.GetResource());
|
|
MediaByteRangeSet byteRanges;
|
|
nsresult rv = resource->GetCachedRanges(byteRanges);
|
|
if (NS_FAILED(rv) || !byteRanges.Length()) {
|
|
return;
|
|
}
|
|
mBufferedState->UpdateIndex(byteRanges, resource);
|
|
|
|
mNeedReIndex = false;
|
|
|
|
if (!mIsMediaSource) {
|
|
return;
|
|
}
|
|
mLastWebMBlockOffset = mBufferedState->GetLastBlockOffset();
|
|
MOZ_ASSERT(mLastWebMBlockOffset <= mResource.GetLength());
|
|
}
|
|
|
|
void
|
|
WebMDemuxer::NotifyDataArrived()
|
|
{
|
|
WEBM_DEBUG("");
|
|
mNeedReIndex = true;
|
|
}
|
|
|
|
void
|
|
WebMDemuxer::NotifyDataRemoved()
|
|
{
|
|
mBufferedState->Reset();
|
|
if (mInitData) {
|
|
mBufferedState->NotifyDataArrived(mInitData->Elements(), mInitData->Length(), 0);
|
|
}
|
|
mNeedReIndex = true;
|
|
}
|
|
|
|
UniquePtr<EncryptionInfo>
|
|
WebMDemuxer::GetCrypto()
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
bool
|
|
WebMDemuxer::GetNextPacket(TrackInfo::TrackType aType, MediaRawDataQueue *aSamples)
|
|
{
|
|
if (mIsMediaSource) {
|
|
// To ensure mLastWebMBlockOffset is properly up to date.
|
|
EnsureUpToDateIndex();
|
|
}
|
|
|
|
RefPtr<NesteggPacketHolder> holder(NextPacket(aType));
|
|
|
|
if (!holder) {
|
|
return false;
|
|
}
|
|
|
|
int r = 0;
|
|
unsigned int count = 0;
|
|
r = nestegg_packet_count(holder->Packet(), &count);
|
|
if (r == -1) {
|
|
return false;
|
|
}
|
|
int64_t tstamp = holder->Timestamp();
|
|
|
|
// The end time of this frame is the start time of the next frame. Fetch
|
|
// the timestamp of the next packet for this track. If we've reached the
|
|
// end of the resource, use the file's duration as the end time of this
|
|
// video frame.
|
|
int64_t next_tstamp = INT64_MIN;
|
|
if (aType == TrackInfo::kAudioTrack) {
|
|
RefPtr<NesteggPacketHolder> next_holder(NextPacket(aType));
|
|
if (next_holder) {
|
|
next_tstamp = next_holder->Timestamp();
|
|
PushAudioPacket(next_holder);
|
|
} else if (!mIsMediaSource ||
|
|
(mIsMediaSource && mLastAudioFrameTime.isSome())) {
|
|
next_tstamp = tstamp;
|
|
next_tstamp += tstamp - mLastAudioFrameTime.refOr(0);
|
|
} else {
|
|
PushAudioPacket(holder);
|
|
}
|
|
mLastAudioFrameTime = Some(tstamp);
|
|
} else if (aType == TrackInfo::kVideoTrack) {
|
|
RefPtr<NesteggPacketHolder> next_holder(NextPacket(aType));
|
|
if (next_holder) {
|
|
next_tstamp = next_holder->Timestamp();
|
|
PushVideoPacket(next_holder);
|
|
} else if (!mIsMediaSource ||
|
|
(mIsMediaSource && mLastVideoFrameTime.isSome())) {
|
|
next_tstamp = tstamp;
|
|
next_tstamp += tstamp - mLastVideoFrameTime.refOr(0);
|
|
} else {
|
|
PushVideoPacket(holder);
|
|
}
|
|
mLastVideoFrameTime = Some(tstamp);
|
|
}
|
|
|
|
if (mIsMediaSource && next_tstamp == INT64_MIN) {
|
|
return false;
|
|
}
|
|
|
|
int64_t discardPadding = 0;
|
|
(void) nestegg_packet_discard_padding(holder->Packet(), &discardPadding);
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
|
unsigned char* data;
|
|
size_t length;
|
|
r = nestegg_packet_data(holder->Packet(), i, &data, &length);
|
|
if (r == -1) {
|
|
WEBM_DEBUG("nestegg_packet_data failed r=%d", r);
|
|
return false;
|
|
}
|
|
bool isKeyframe = false;
|
|
if (aType == TrackInfo::kAudioTrack) {
|
|
isKeyframe = true;
|
|
} else if (aType == TrackInfo::kVideoTrack) {
|
|
vpx_codec_stream_info_t si;
|
|
PodZero(&si);
|
|
si.sz = sizeof(si);
|
|
switch (mVideoCodec) {
|
|
case NESTEGG_CODEC_VP8:
|
|
vpx_codec_peek_stream_info(vpx_codec_vp8_dx(), data, length, &si);
|
|
break;
|
|
case NESTEGG_CODEC_VP9:
|
|
vpx_codec_peek_stream_info(vpx_codec_vp9_dx(), data, length, &si);
|
|
break;
|
|
}
|
|
isKeyframe = si.is_kf;
|
|
if (isKeyframe) {
|
|
// We only look for resolution changes on keyframes for both VP8 and
|
|
// VP9. Other resolution changes are invalid.
|
|
if (mLastSeenFrameWidth.isSome() && mLastSeenFrameHeight.isSome() &&
|
|
(si.w != mLastSeenFrameWidth.value() ||
|
|
si.h != mLastSeenFrameHeight.value())) {
|
|
mInfo.mVideo.mDisplay = nsIntSize(si.w, si.h);
|
|
mSharedVideoTrackInfo = new SharedTrackInfo(mInfo.mVideo, ++sStreamSourceID);
|
|
}
|
|
mLastSeenFrameWidth = Some(si.w);
|
|
mLastSeenFrameHeight = Some(si.h);
|
|
}
|
|
}
|
|
|
|
WEBM_DEBUG("push sample tstamp: %ld next_tstamp: %ld length: %ld kf: %d",
|
|
tstamp, next_tstamp, length, isKeyframe);
|
|
RefPtr<MediaRawData> sample = new MediaRawData(data, length);
|
|
sample->mTimecode = tstamp;
|
|
sample->mTime = tstamp;
|
|
sample->mDuration = next_tstamp - tstamp;
|
|
sample->mOffset = holder->Offset();
|
|
sample->mKeyframe = isKeyframe;
|
|
if (discardPadding && i == count - 1) {
|
|
uint8_t c[8];
|
|
BigEndian::writeInt64(&c[0], discardPadding);
|
|
sample->mExtraData = new MediaByteBuffer;
|
|
sample->mExtraData->AppendElements(&c[0], 8);
|
|
}
|
|
if (aType == TrackInfo::kVideoTrack) {
|
|
sample->mTrackInfo = mSharedVideoTrackInfo;
|
|
}
|
|
aSamples->Push(sample);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
RefPtr<NesteggPacketHolder>
|
|
WebMDemuxer::NextPacket(TrackInfo::TrackType aType)
|
|
{
|
|
bool isVideo = aType == TrackInfo::kVideoTrack;
|
|
|
|
// The packet queue that packets will be pushed on if they
|
|
// are not the type we are interested in.
|
|
WebMPacketQueue& otherPackets = isVideo ? mAudioPackets : mVideoPackets;
|
|
|
|
// The packet queue for the type that we are interested in.
|
|
WebMPacketQueue &packets = isVideo ? mVideoPackets : mAudioPackets;
|
|
|
|
// Flag to indicate that we do need to playback these types of
|
|
// packets.
|
|
bool hasType = isVideo ? mHasVideo : mHasAudio;
|
|
|
|
// Flag to indicate that we do need to playback the other type
|
|
// of track.
|
|
bool hasOtherType = isVideo ? mHasAudio : mHasVideo;
|
|
|
|
// Track we are interested in
|
|
uint32_t ourTrack = isVideo ? mVideoTrack : mAudioTrack;
|
|
|
|
// Value of other track
|
|
uint32_t otherTrack = isVideo ? mAudioTrack : mVideoTrack;
|
|
|
|
if (packets.GetSize() > 0) {
|
|
return packets.PopFront();
|
|
}
|
|
|
|
do {
|
|
RefPtr<NesteggPacketHolder> holder = DemuxPacket();
|
|
if (!holder) {
|
|
return nullptr;
|
|
}
|
|
|
|
if (hasOtherType && otherTrack == holder->Track()) {
|
|
// Save the packet for when we want these packets
|
|
otherPackets.Push(holder);
|
|
continue;
|
|
}
|
|
|
|
// The packet is for the track we want to play
|
|
if (hasType && ourTrack == holder->Track()) {
|
|
return holder;
|
|
}
|
|
} while (true);
|
|
}
|
|
|
|
RefPtr<NesteggPacketHolder>
|
|
WebMDemuxer::DemuxPacket()
|
|
{
|
|
nestegg_packet* packet;
|
|
int r = nestegg_read_packet(mContext, &packet);
|
|
if (r <= 0) {
|
|
return nullptr;
|
|
}
|
|
|
|
unsigned int track = 0;
|
|
r = nestegg_packet_track(packet, &track);
|
|
if (r == -1) {
|
|
return nullptr;
|
|
}
|
|
|
|
int64_t offset = mResource.Tell();
|
|
RefPtr<NesteggPacketHolder> holder = new NesteggPacketHolder();
|
|
if (!holder->Init(packet, offset, track, false)) {
|
|
return nullptr;
|
|
}
|
|
|
|
return holder;
|
|
}
|
|
|
|
void
|
|
WebMDemuxer::PushAudioPacket(NesteggPacketHolder* aItem)
|
|
{
|
|
mAudioPackets.PushFront(aItem);
|
|
}
|
|
|
|
void
|
|
WebMDemuxer::PushVideoPacket(NesteggPacketHolder* aItem)
|
|
{
|
|
mVideoPackets.PushFront(aItem);
|
|
}
|
|
|
|
nsresult
|
|
WebMDemuxer::SeekInternal(const media::TimeUnit& aTarget)
|
|
{
|
|
EnsureUpToDateIndex();
|
|
uint32_t trackToSeek = mHasVideo ? mVideoTrack : mAudioTrack;
|
|
uint64_t target = aTarget.ToNanoseconds();
|
|
|
|
if (NS_FAILED(Reset())) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
if (mSeekPreroll) {
|
|
uint64_t startTime = 0;
|
|
if (!mBufferedState->GetStartTime(&startTime)) {
|
|
startTime = 0;
|
|
}
|
|
WEBM_DEBUG("Seek Target: %f",
|
|
media::TimeUnit::FromNanoseconds(target).ToSeconds());
|
|
if (target < mSeekPreroll || target - mSeekPreroll < startTime) {
|
|
target = startTime;
|
|
} else {
|
|
target -= mSeekPreroll;
|
|
}
|
|
WEBM_DEBUG("SeekPreroll: %f StartTime: %f Adjusted Target: %f",
|
|
media::TimeUnit::FromNanoseconds(mSeekPreroll).ToSeconds(),
|
|
media::TimeUnit::FromNanoseconds(startTime).ToSeconds(),
|
|
media::TimeUnit::FromNanoseconds(target).ToSeconds());
|
|
}
|
|
int r = nestegg_track_seek(mContext, trackToSeek, target);
|
|
if (r == -1) {
|
|
WEBM_DEBUG("track_seek for track %u to %f failed, r=%d", trackToSeek,
|
|
media::TimeUnit::FromNanoseconds(target).ToSeconds(), r);
|
|
// Try seeking directly based on cluster information in memory.
|
|
int64_t offset = 0;
|
|
bool rv = mBufferedState->GetOffsetForTime(target, &offset);
|
|
if (!rv) {
|
|
WEBM_DEBUG("mBufferedState->GetOffsetForTime failed too");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
r = nestegg_offset_seek(mContext, offset);
|
|
if (r == -1) {
|
|
WEBM_DEBUG("and nestegg_offset_seek to %" PRIu64 " failed", offset);
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
WEBM_DEBUG("got offset from buffered state: %" PRIu64 "", offset);
|
|
}
|
|
|
|
mLastAudioFrameTime.reset();
|
|
mLastVideoFrameTime.reset();
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
media::TimeIntervals
|
|
WebMDemuxer::GetBuffered()
|
|
{
|
|
EnsureUpToDateIndex();
|
|
AutoPinned<MediaResource> resource(mResource.GetResource());
|
|
|
|
media::TimeIntervals buffered;
|
|
|
|
MediaByteRangeSet ranges;
|
|
nsresult rv = resource->GetCachedRanges(ranges);
|
|
if (NS_FAILED(rv)) {
|
|
return media::TimeIntervals();
|
|
}
|
|
uint64_t duration = 0;
|
|
uint64_t startOffset = 0;
|
|
if (!nestegg_duration(mContext, &duration)) {
|
|
if(mBufferedState->GetStartTime(&startOffset)) {
|
|
duration += startOffset;
|
|
}
|
|
WEBM_DEBUG("Duration: %f StartTime: %f",
|
|
media::TimeUnit::FromNanoseconds(duration).ToSeconds(),
|
|
media::TimeUnit::FromNanoseconds(startOffset).ToSeconds());
|
|
}
|
|
for (uint32_t index = 0; index < ranges.Length(); index++) {
|
|
uint64_t start, end;
|
|
bool rv = mBufferedState->CalculateBufferedForRange(ranges[index].mStart,
|
|
ranges[index].mEnd,
|
|
&start, &end);
|
|
if (rv) {
|
|
NS_ASSERTION(startOffset <= start,
|
|
"startOffset negative or larger than start time");
|
|
|
|
if (duration && end > duration) {
|
|
WEBM_DEBUG("limit range to duration, end: %f duration: %f",
|
|
media::TimeUnit::FromNanoseconds(end).ToSeconds(),
|
|
media::TimeUnit::FromNanoseconds(duration).ToSeconds());
|
|
end = duration;
|
|
}
|
|
media::TimeUnit startTime = media::TimeUnit::FromNanoseconds(start);
|
|
media::TimeUnit endTime = media::TimeUnit::FromNanoseconds(end);
|
|
WEBM_DEBUG("add range %f-%f", startTime.ToSeconds(), endTime.ToSeconds());
|
|
buffered += media::TimeInterval(startTime, endTime);
|
|
}
|
|
}
|
|
return buffered;
|
|
}
|
|
|
|
bool WebMDemuxer::GetOffsetForTime(uint64_t aTime, int64_t* aOffset)
|
|
{
|
|
EnsureUpToDateIndex();
|
|
return mBufferedState && mBufferedState->GetOffsetForTime(aTime, aOffset);
|
|
}
|
|
|
|
|
|
//WebMTrackDemuxer
|
|
WebMTrackDemuxer::WebMTrackDemuxer(WebMDemuxer* aParent,
|
|
TrackInfo::TrackType aType,
|
|
uint32_t aTrackNumber)
|
|
: mParent(aParent)
|
|
, mType(aType)
|
|
, mNeedKeyframe(true)
|
|
{
|
|
mInfo = mParent->GetTrackInfo(aType, aTrackNumber);
|
|
MOZ_ASSERT(mInfo);
|
|
}
|
|
|
|
WebMTrackDemuxer::~WebMTrackDemuxer()
|
|
{
|
|
mSamples.Reset();
|
|
}
|
|
|
|
UniquePtr<TrackInfo>
|
|
WebMTrackDemuxer::GetInfo() const
|
|
{
|
|
return mInfo->Clone();
|
|
}
|
|
|
|
RefPtr<WebMTrackDemuxer::SeekPromise>
|
|
WebMTrackDemuxer::Seek(media::TimeUnit aTime)
|
|
{
|
|
// Seeks to aTime. Upon success, SeekPromise will be resolved with the
|
|
// actual time seeked to. Typically the random access point time
|
|
|
|
media::TimeUnit seekTime = aTime;
|
|
mSamples.Reset();
|
|
mParent->SeekInternal(aTime);
|
|
mParent->GetNextPacket(mType, &mSamples);
|
|
mNeedKeyframe = true;
|
|
|
|
// Check what time we actually seeked to.
|
|
if (mSamples.GetSize() > 0) {
|
|
const RefPtr<MediaRawData>& sample = mSamples.First();
|
|
seekTime = media::TimeUnit::FromMicroseconds(sample->mTime);
|
|
}
|
|
SetNextKeyFrameTime();
|
|
|
|
return SeekPromise::CreateAndResolve(seekTime, __func__);
|
|
}
|
|
|
|
RefPtr<MediaRawData>
|
|
WebMTrackDemuxer::NextSample()
|
|
{
|
|
while (mSamples.GetSize() < 1 && mParent->GetNextPacket(mType, &mSamples)) {
|
|
}
|
|
if (mSamples.GetSize()) {
|
|
return mSamples.PopFront();
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
RefPtr<WebMTrackDemuxer::SamplesPromise>
|
|
WebMTrackDemuxer::GetSamples(int32_t aNumSamples)
|
|
{
|
|
RefPtr<SamplesHolder> samples = new SamplesHolder;
|
|
if (!aNumSamples) {
|
|
return SamplesPromise::CreateAndReject(DemuxerFailureReason::DEMUXER_ERROR, __func__);
|
|
}
|
|
|
|
while (aNumSamples) {
|
|
RefPtr<MediaRawData> sample(NextSample());
|
|
if (!sample) {
|
|
break;
|
|
}
|
|
if (mNeedKeyframe && !sample->mKeyframe) {
|
|
continue;
|
|
}
|
|
mNeedKeyframe = false;
|
|
samples->mSamples.AppendElement(sample);
|
|
aNumSamples--;
|
|
}
|
|
|
|
if (samples->mSamples.IsEmpty()) {
|
|
return SamplesPromise::CreateAndReject(DemuxerFailureReason::END_OF_STREAM, __func__);
|
|
} else {
|
|
UpdateSamples(samples->mSamples);
|
|
return SamplesPromise::CreateAndResolve(samples, __func__);
|
|
}
|
|
}
|
|
|
|
void
|
|
WebMTrackDemuxer::SetNextKeyFrameTime()
|
|
{
|
|
if (mType != TrackInfo::kVideoTrack) {
|
|
return;
|
|
}
|
|
|
|
int64_t frameTime = -1;
|
|
|
|
mNextKeyframeTime.reset();
|
|
|
|
MediaRawDataQueue skipSamplesQueue;
|
|
bool foundKeyframe = false;
|
|
while (!foundKeyframe && mSamples.GetSize()) {
|
|
RefPtr<MediaRawData> sample = mSamples.PopFront();
|
|
if (sample->mKeyframe) {
|
|
frameTime = sample->mTime;
|
|
foundKeyframe = true;
|
|
}
|
|
skipSamplesQueue.Push(sample.forget());
|
|
}
|
|
Maybe<int64_t> startTime;
|
|
if (skipSamplesQueue.GetSize()) {
|
|
const RefPtr<MediaRawData>& sample = skipSamplesQueue.First();
|
|
startTime.emplace(sample->mTimecode);
|
|
}
|
|
// Demux and buffer frames until we find a keyframe.
|
|
RefPtr<MediaRawData> sample;
|
|
while (!foundKeyframe && (sample = NextSample())) {
|
|
if (sample->mKeyframe) {
|
|
frameTime = sample->mTime;
|
|
foundKeyframe = true;
|
|
}
|
|
int64_t sampleTimecode = sample->mTimecode;
|
|
skipSamplesQueue.Push(sample.forget());
|
|
if (!startTime) {
|
|
startTime.emplace(sampleTimecode);
|
|
} else if (!foundKeyframe &&
|
|
sampleTimecode > startTime.ref() + MAX_LOOK_AHEAD) {
|
|
WEBM_DEBUG("Couldn't find keyframe in a reasonable time, aborting");
|
|
break;
|
|
}
|
|
}
|
|
// We may have demuxed more than intended, so ensure that all frames are kept
|
|
// in the right order.
|
|
mSamples.PushFront(Move(skipSamplesQueue));
|
|
|
|
if (frameTime != -1) {
|
|
mNextKeyframeTime.emplace(media::TimeUnit::FromMicroseconds(frameTime));
|
|
WEBM_DEBUG("Next Keyframe %f (%u queued %.02fs)",
|
|
mNextKeyframeTime.value().ToSeconds(),
|
|
uint32_t(mSamples.GetSize()),
|
|
media::TimeUnit::FromMicroseconds(mSamples.Last()->mTimecode - mSamples.First()->mTimecode).ToSeconds());
|
|
} else {
|
|
WEBM_DEBUG("Couldn't determine next keyframe time (%u queued)",
|
|
uint32_t(mSamples.GetSize()));
|
|
}
|
|
}
|
|
|
|
void
|
|
WebMTrackDemuxer::Reset()
|
|
{
|
|
mSamples.Reset();
|
|
media::TimeIntervals buffered = GetBuffered();
|
|
mNeedKeyframe = true;
|
|
if (buffered.Length()) {
|
|
WEBM_DEBUG("Seek to start point: %f", buffered.Start(0).ToSeconds());
|
|
mParent->SeekInternal(buffered.Start(0));
|
|
SetNextKeyFrameTime();
|
|
} else {
|
|
mNextKeyframeTime.reset();
|
|
}
|
|
}
|
|
|
|
void
|
|
WebMTrackDemuxer::UpdateSamples(nsTArray<RefPtr<MediaRawData>>& aSamples)
|
|
{
|
|
if (mNextKeyframeTime.isNothing() ||
|
|
aSamples.LastElement()->mTime >= mNextKeyframeTime.value().ToMicroseconds()) {
|
|
SetNextKeyFrameTime();
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
WebMTrackDemuxer::GetNextRandomAccessPoint(media::TimeUnit* aTime)
|
|
{
|
|
if (mNextKeyframeTime.isNothing()) {
|
|
// There's no next key frame.
|
|
*aTime =
|
|
media::TimeUnit::FromMicroseconds(std::numeric_limits<int64_t>::max());
|
|
} else {
|
|
*aTime = mNextKeyframeTime.ref();
|
|
}
|
|
return NS_OK;
|
|
}
|
|
|
|
RefPtr<WebMTrackDemuxer::SkipAccessPointPromise>
|
|
WebMTrackDemuxer::SkipToNextRandomAccessPoint(media::TimeUnit aTimeThreshold)
|
|
{
|
|
uint32_t parsed = 0;
|
|
bool found = false;
|
|
RefPtr<MediaRawData> sample;
|
|
|
|
WEBM_DEBUG("TimeThreshold: %f", aTimeThreshold.ToSeconds());
|
|
while (!found && (sample = NextSample())) {
|
|
parsed++;
|
|
if (sample->mKeyframe && sample->mTime >= aTimeThreshold.ToMicroseconds()) {
|
|
found = true;
|
|
mSamples.Reset();
|
|
mSamples.PushFront(sample.forget());
|
|
}
|
|
}
|
|
SetNextKeyFrameTime();
|
|
if (found) {
|
|
WEBM_DEBUG("next sample: %f (parsed: %d)",
|
|
media::TimeUnit::FromMicroseconds(sample->mTime).ToSeconds(),
|
|
parsed);
|
|
return SkipAccessPointPromise::CreateAndResolve(parsed, __func__);
|
|
} else {
|
|
SkipFailureHolder failure(DemuxerFailureReason::END_OF_STREAM, parsed);
|
|
return SkipAccessPointPromise::CreateAndReject(Move(failure), __func__);
|
|
}
|
|
}
|
|
|
|
media::TimeIntervals
|
|
WebMTrackDemuxer::GetBuffered()
|
|
{
|
|
return mParent->GetBuffered();
|
|
}
|
|
|
|
void
|
|
WebMTrackDemuxer::BreakCycles()
|
|
{
|
|
mParent = nullptr;
|
|
}
|
|
|
|
#undef WEBM_DEBUG
|
|
} // namespace mozilla
|