1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/MediaBufferDecoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef MediaBufferDecoder_h_ 1.11 +#define MediaBufferDecoder_h_ 1.12 + 1.13 +#include "nsWrapperCache.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsAutoPtr.h" 1.16 +#include "nsIThreadPool.h" 1.17 +#include "nsString.h" 1.18 +#include "nsTArray.h" 1.19 +#include "mozilla/dom/TypedArray.h" 1.20 +#include "mozilla/MemoryReporting.h" 1.21 + 1.22 +namespace mozilla { 1.23 + 1.24 +namespace dom { 1.25 +class AudioBuffer; 1.26 +class AudioContext; 1.27 +class DecodeErrorCallback; 1.28 +class DecodeSuccessCallback; 1.29 +} 1.30 + 1.31 +struct WebAudioDecodeJob MOZ_FINAL 1.32 +{ 1.33 + // You may omit both the success and failure callback, or you must pass both. 1.34 + // The callbacks are only necessary for asynchronous operation. 1.35 + WebAudioDecodeJob(const nsACString& aContentType, 1.36 + dom::AudioContext* aContext, 1.37 + dom::DecodeSuccessCallback* aSuccessCallback = nullptr, 1.38 + dom::DecodeErrorCallback* aFailureCallback = nullptr); 1.39 + ~WebAudioDecodeJob(); 1.40 + 1.41 + NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebAudioDecodeJob) 1.42 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebAudioDecodeJob) 1.43 + 1.44 + enum ErrorCode { 1.45 + NoError, 1.46 + UnknownContent, 1.47 + UnknownError, 1.48 + InvalidContent, 1.49 + NoAudio 1.50 + }; 1.51 + 1.52 + typedef void (WebAudioDecodeJob::*ResultFn)(ErrorCode); 1.53 + typedef nsAutoArrayPtr<float> ChannelBuffer; 1.54 + 1.55 + void OnSuccess(ErrorCode /* ignored */); 1.56 + void OnFailure(ErrorCode aErrorCode); 1.57 + 1.58 + bool AllocateBuffer(); 1.59 + 1.60 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.61 + 1.62 + nsCString mContentType; 1.63 + uint32_t mWriteIndex; 1.64 + nsRefPtr<dom::AudioContext> mContext; 1.65 + nsRefPtr<dom::DecodeSuccessCallback> mSuccessCallback; 1.66 + nsRefPtr<dom::DecodeErrorCallback> mFailureCallback; // can be null 1.67 + nsRefPtr<dom::AudioBuffer> mOutput; 1.68 + FallibleTArray<ChannelBuffer> mChannelBuffers; 1.69 +}; 1.70 + 1.71 +/** 1.72 + * This class is used to decode media buffers on a dedicated threadpool. 1.73 + * 1.74 + * This class manages the resources that it uses internally (such as the 1.75 + * thread-pool) and provides a clean external interface. 1.76 + */ 1.77 +class MediaBufferDecoder 1.78 +{ 1.79 +public: 1.80 + void AsyncDecodeMedia(const char* aContentType, uint8_t* aBuffer, 1.81 + uint32_t aLength, WebAudioDecodeJob& aDecodeJob); 1.82 + 1.83 + ~MediaBufferDecoder() { Shutdown(); } 1.84 + void Shutdown(); 1.85 + 1.86 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const 1.87 + { 1.88 + return 0; 1.89 + } 1.90 + 1.91 +private: 1.92 + bool EnsureThreadPoolInitialized(); 1.93 + 1.94 +private: 1.95 + nsCOMPtr<nsIThreadPool> mThreadPool; 1.96 +}; 1.97 + 1.98 +} 1.99 + 1.100 +#endif 1.101 +