2013-09-19 08:26:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "WebVTTListener.h"
|
|
|
|
#include "mozilla/dom/TextTrackCue.h"
|
2013-09-18 09:46:51 -07:00
|
|
|
#include "mozilla/dom/TextTrackRegion.h"
|
|
|
|
#include "mozilla/dom/VTTRegionBinding.h"
|
2013-09-19 08:26:00 -07:00
|
|
|
#include "mozilla/dom/HTMLTrackElement.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIWebVTTParserWrapper.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(WebVTTListener, mElement, mParserWrapper)
|
2013-09-19 08:26:00 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebVTTListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWebVTTListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIChannelEventSink)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebVTTListener)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(WebVTTListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(WebVTTListener)
|
|
|
|
|
|
|
|
PRLogModuleInfo* gTextTrackLog;
|
2015-06-01 17:57:58 -07:00
|
|
|
# define VTT_LOG(...) MOZ_LOG(gTextTrackLog, PR_LOG_DEBUG, (__VA_ARGS__))
|
2013-09-19 08:26:00 -07:00
|
|
|
|
|
|
|
WebVTTListener::WebVTTListener(HTMLTrackElement* aElement)
|
|
|
|
: mElement(aElement)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mElement, "Must pass an element to the callback");
|
|
|
|
if (!gTextTrackLog) {
|
|
|
|
gTextTrackLog = PR_NewLogModule("TextTrack");
|
|
|
|
}
|
2013-11-20 19:02:42 -08:00
|
|
|
VTT_LOG("WebVTTListener created.");
|
2013-09-19 08:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
WebVTTListener::~WebVTTListener()
|
|
|
|
{
|
2013-11-20 19:02:42 -08:00
|
|
|
VTT_LOG("WebVTTListener destroyed.");
|
2013-09-19 08:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::GetInterface(const nsIID &aIID,
|
|
|
|
void** aResult)
|
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
WebVTTListener::LoadResource()
|
|
|
|
{
|
|
|
|
if (!HTMLTrackElement::IsWebVTTEnabled()) {
|
|
|
|
NS_WARNING("WebVTT support disabled."
|
|
|
|
" See media.webvtt.enabled in about:config. ");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsresult rv;
|
|
|
|
mParserWrapper = do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsPIDOMWindow* window = mElement->OwnerDoc()->GetWindow();
|
|
|
|
rv = mParserWrapper->LoadParser(window);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mParserWrapper->Watch(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-03-14 09:13:41 -07:00
|
|
|
mElement->SetReadyState(TextTrackReadyState::Loading);
|
2013-09-19 08:26:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
|
|
|
|
nsIChannel* aNewChannel,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* cb)
|
|
|
|
{
|
|
|
|
if (mElement) {
|
|
|
|
mElement->OnChannelRedirect(aOldChannel, aNewChannel, aFlags);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::OnStartRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2014-03-17 14:49:52 -07:00
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
mElement->SetReadyState(TextTrackReadyState::FailedToLoad);
|
|
|
|
}
|
2014-03-17 13:52:11 -07:00
|
|
|
// Attempt to parse any final data the parser might still have.
|
|
|
|
mParserWrapper->Flush();
|
2014-03-13 11:29:32 -07:00
|
|
|
if (mElement->ReadyState() != TextTrackReadyState::FailedToLoad) {
|
2014-03-14 09:13:41 -07:00
|
|
|
mElement->SetReadyState(TextTrackReadyState::Loaded);
|
2013-09-19 08:26:00 -07:00
|
|
|
}
|
2014-06-18 13:10:24 -07:00
|
|
|
|
|
|
|
mElement->DropChannel();
|
|
|
|
|
2014-03-17 14:49:52 -07:00
|
|
|
return aStatus;
|
2013-09-19 08:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
WebVTTListener::ParseChunk(nsIInputStream* aInStream, void* aClosure,
|
|
|
|
const char* aFromSegment, uint32_t aToOffset,
|
|
|
|
uint32_t aCount, uint32_t* aWriteCount)
|
|
|
|
{
|
|
|
|
nsCString buffer(aFromSegment, aCount);
|
|
|
|
WebVTTListener* listener = static_cast<WebVTTListener*>(aClosure);
|
|
|
|
|
|
|
|
if (NS_FAILED(listener->mParserWrapper->Parse(buffer))) {
|
2013-11-20 19:02:42 -08:00
|
|
|
VTT_LOG("Unable to parse chunk of WEBVTT text. Aborting.");
|
2013-09-19 08:26:00 -07:00
|
|
|
*aWriteCount = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::OnDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
uint64_t aOffset,
|
|
|
|
uint32_t aCount)
|
|
|
|
{
|
|
|
|
uint32_t count = aCount;
|
|
|
|
while (count > 0) {
|
|
|
|
uint32_t read;
|
|
|
|
nsresult rv = aStream->ReadSegments(ParseChunk, this, count, &read);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!read) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
count -= read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
WebVTTListener::OnCue(JS::Handle<JS::Value> aCue, JSContext* aCx)
|
2013-09-19 08:26:00 -07:00
|
|
|
{
|
|
|
|
if (!aCue.isObject()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextTrackCue* cue;
|
2013-11-21 04:51:16 -08:00
|
|
|
nsresult rv = UNWRAP_OBJECT(VTTCue, &aCue.toObject(), cue);
|
2013-09-19 08:26:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
cue->SetTrackElement(mElement);
|
|
|
|
mElement->mTrack->AddCue(*cue);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
WebVTTListener::OnRegion(JS::Handle<JS::Value> aRegion, JSContext* aCx)
|
2013-09-19 08:26:00 -07:00
|
|
|
{
|
2014-03-10 08:33:06 -07:00
|
|
|
// Nothing for this callback to do.
|
2013-09-19 08:26:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-17 12:11:41 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::OnParsingError(int32_t errorCode, JSContext* cx)
|
|
|
|
{
|
|
|
|
// We only care about files that have a bad WebVTT file signature right now
|
|
|
|
// as that means the file failed to load.
|
|
|
|
if (errorCode == ErrorCodes::BadSignature) {
|
|
|
|
mElement->SetReadyState(TextTrackReadyState::FailedToLoad);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-19 08:26:00 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|