content/media/webaudio/MediaBufferDecoder.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:69d30c6f8201
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef MediaBufferDecoder_h_
8 #define MediaBufferDecoder_h_
9
10 #include "nsWrapperCache.h"
11 #include "nsCOMPtr.h"
12 #include "nsAutoPtr.h"
13 #include "nsIThreadPool.h"
14 #include "nsString.h"
15 #include "nsTArray.h"
16 #include "mozilla/dom/TypedArray.h"
17 #include "mozilla/MemoryReporting.h"
18
19 namespace mozilla {
20
21 namespace dom {
22 class AudioBuffer;
23 class AudioContext;
24 class DecodeErrorCallback;
25 class DecodeSuccessCallback;
26 }
27
28 struct WebAudioDecodeJob MOZ_FINAL
29 {
30 // You may omit both the success and failure callback, or you must pass both.
31 // The callbacks are only necessary for asynchronous operation.
32 WebAudioDecodeJob(const nsACString& aContentType,
33 dom::AudioContext* aContext,
34 dom::DecodeSuccessCallback* aSuccessCallback = nullptr,
35 dom::DecodeErrorCallback* aFailureCallback = nullptr);
36 ~WebAudioDecodeJob();
37
38 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebAudioDecodeJob)
39 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebAudioDecodeJob)
40
41 enum ErrorCode {
42 NoError,
43 UnknownContent,
44 UnknownError,
45 InvalidContent,
46 NoAudio
47 };
48
49 typedef void (WebAudioDecodeJob::*ResultFn)(ErrorCode);
50 typedef nsAutoArrayPtr<float> ChannelBuffer;
51
52 void OnSuccess(ErrorCode /* ignored */);
53 void OnFailure(ErrorCode aErrorCode);
54
55 bool AllocateBuffer();
56
57 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
58
59 nsCString mContentType;
60 uint32_t mWriteIndex;
61 nsRefPtr<dom::AudioContext> mContext;
62 nsRefPtr<dom::DecodeSuccessCallback> mSuccessCallback;
63 nsRefPtr<dom::DecodeErrorCallback> mFailureCallback; // can be null
64 nsRefPtr<dom::AudioBuffer> mOutput;
65 FallibleTArray<ChannelBuffer> mChannelBuffers;
66 };
67
68 /**
69 * This class is used to decode media buffers on a dedicated threadpool.
70 *
71 * This class manages the resources that it uses internally (such as the
72 * thread-pool) and provides a clean external interface.
73 */
74 class MediaBufferDecoder
75 {
76 public:
77 void AsyncDecodeMedia(const char* aContentType, uint8_t* aBuffer,
78 uint32_t aLength, WebAudioDecodeJob& aDecodeJob);
79
80 ~MediaBufferDecoder() { Shutdown(); }
81 void Shutdown();
82
83 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
84 {
85 return 0;
86 }
87
88 private:
89 bool EnsureThreadPoolInitialized();
90
91 private:
92 nsCOMPtr<nsIThreadPool> mThreadPool;
93 };
94
95 }
96
97 #endif
98

mercurial