1 /* vim:set ts=2 sw=2 sts=2 et cindent: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef BaseMediaResource_h
7 #define BaseMediaResource_h
9 #include "MediaResource.h"
10 #include "MediaResourceCallback.h"
11 #include "MediaCache.h"
12 #include "nsIChannel.h"
14 #include "nsIStreamListener.h"
15 #include "mozilla/dom/MediaDebugInfoBinding.h"
21 DDLoggedTypeDeclNameAndBase(BaseMediaResource
, MediaResource
);
23 class BaseMediaResource
: public MediaResource
,
24 public DecoderDoctorLifeLogger
<BaseMediaResource
> {
27 * Create a resource, reading data from the channel. Call on main thread only.
28 * The caller must follow up by calling resource->Open().
30 static already_AddRefed
<BaseMediaResource
> Create(
31 MediaResourceCallback
* aCallback
, nsIChannel
* aChannel
,
32 bool aIsPrivateBrowsing
);
34 // Pass true to limit the amount of readahead data (specified by
35 // "media.cache_readahead_limit") or false to read as much as the
37 virtual void ThrottleReadahead(bool bThrottle
) {}
39 // This is the client's estimate of the playback rate assuming
40 // the media plays continuously. The cache can't guess this itself
41 // because it doesn't know when the decoder was paused, buffering, etc.
42 virtual void SetPlaybackRate(uint32_t aBytesPerSecond
) = 0;
44 // Get the estimated download rate in bytes per second (assuming no
45 // pausing of the channel is requested by Gecko).
46 // *aIsReliable is set to true if we think the estimate is useful.
47 virtual double GetDownloadRate(bool* aIsReliable
) = 0;
49 // Moves any existing channel loads into or out of background. Background
50 // loads don't block the load event. This also determines whether or not any
51 // new loads initiated (for example to seek) will be in the background.
52 void SetLoadInBackground(bool aLoadInBackground
);
54 // Suspend any downloads that are in progress.
55 // If aCloseImmediately is set, resources should be released immediately
56 // since we don't expect to resume again any time soon. Otherwise we
57 // may resume again soon so resources should be held for a little
59 virtual void Suspend(bool aCloseImmediately
) = 0;
61 // Resume any downloads that have been suspended.
62 virtual void Resume() = 0;
64 // The mode is initially MODE_METADATA.
65 virtual void SetReadMode(MediaCacheStream::ReadMode aMode
) = 0;
67 // Returns true if the resource can be seeked to unbuffered ranges, i.e.
68 // for an HTTP network stream this returns true if HTTP1.1 Byte Range
69 // requests are supported by the connection/server.
70 virtual bool IsTransportSeekable() = 0;
72 // Get the current principal for the channel
73 virtual already_AddRefed
<nsIPrincipal
> GetCurrentPrincipal() = 0;
75 // Return true if the loading of this resource required cross-origin
77 virtual bool HadCrossOriginRedirects() = 0;
80 * Open the stream. This creates a stream listener and returns it in
81 * aStreamListener; this listener needs to be notified of incoming data.
83 virtual nsresult
Open(nsIStreamListener
** aStreamListener
) = 0;
85 // If this returns false, then we shouldn't try to clone this MediaResource
86 // because its underlying resources are not suitable for reuse (e.g.
87 // because the underlying connection has been lost, or this resource
88 // just can't be safely cloned). If this returns true, CloneData could
89 // still fail. If this returns false, CloneData should not be called.
90 virtual bool CanClone() { return false; }
92 // Create a new stream of the same type that refers to the same URI
93 // with a new channel. Any cached data associated with the original
94 // stream should be accessible in the new stream too.
95 virtual already_AddRefed
<BaseMediaResource
> CloneData(
96 MediaResourceCallback
* aCallback
) {
100 // Returns true if the resource is a live stream.
101 virtual bool IsLiveStream() const { return false; }
103 virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const {
104 // Might be useful to track in the future:
106 // - mURI (possibly owned, looks like just a ref from mChannel)
112 virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const {
113 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf
);
116 virtual void GetDebugInfo(dom::MediaResourceDebugInfo
& aInfo
) {}
119 BaseMediaResource(MediaResourceCallback
* aCallback
, nsIChannel
* aChannel
,
121 : mCallback(aCallback
),
124 mLoadInBackground(false) {}
125 virtual ~BaseMediaResource() {}
127 // Set the request's load flags to aFlags. If the request is part of a
128 // load group, the request is removed from the group, the flags are set, and
129 // then the request is added back to the load group.
130 void ModifyLoadFlags(nsLoadFlags aFlags
);
132 RefPtr
<MediaResourceCallback
> mCallback
;
134 // Channel used to download the media data. Must be accessed
135 // from the main thread only.
136 nsCOMPtr
<nsIChannel
> mChannel
;
138 // URI in case the stream needs to be re-opened. Access from
140 nsCOMPtr
<nsIURI
> mURI
;
142 // True if SetLoadInBackground() has been called with
143 // aLoadInBackground = true, i.e. when the document load event is not
144 // blocked by this resource, and all channel loads will be in the
146 bool mLoadInBackground
;
149 } // namespace mozilla
151 #endif // BaseMediaResource_h