Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/BaseMediaResource.h
Line
Count
Source (jump to first uncovered line)
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/. */
5
6
#ifndef BaseMediaResource_h
7
#define BaseMediaResource_h
8
9
#include "MediaResource.h"
10
#include "MediaResourceCallback.h"
11
#include "MediaCache.h"
12
#include "nsIChannel.h"
13
#include "nsIURI.h"
14
#include "nsIStreamListener.h"
15
16
class nsIPrincipal;
17
18
namespace mozilla {
19
20
DDLoggedTypeDeclNameAndBase(BaseMediaResource, MediaResource);
21
22
class BaseMediaResource
23
  : public MediaResource
24
  , public DecoderDoctorLifeLogger<BaseMediaResource>
25
{
26
public:
27
  /**
28
   * Create a resource, reading data from the channel. Call on main thread only.
29
   * The caller must follow up by calling resource->Open().
30
   */
31
  static already_AddRefed<BaseMediaResource> Create(
32
    MediaResourceCallback* aCallback,
33
    nsIChannel* aChannel,
34
    bool aIsPrivateBrowsing);
35
36
  // Pass true to limit the amount of readahead data (specified by
37
  // "media.cache_readahead_limit") or false to read as much as the
38
  // cache size allows.
39
0
  virtual void ThrottleReadahead(bool bThrottle) {}
40
41
  // This is the client's estimate of the playback rate assuming
42
  // the media plays continuously. The cache can't guess this itself
43
  // because it doesn't know when the decoder was paused, buffering, etc.
44
  virtual void SetPlaybackRate(uint32_t aBytesPerSecond) = 0;
45
46
  // Get the estimated download rate in bytes per second (assuming no
47
  // pausing of the channel is requested by Gecko).
48
  // *aIsReliable is set to true if we think the estimate is useful.
49
  virtual double GetDownloadRate(bool* aIsReliable) = 0;
50
51
  // Moves any existing channel loads into or out of background. Background
52
  // loads don't block the load event. This also determines whether or not any
53
  // new loads initiated (for example to seek) will be in the background.
54
  void SetLoadInBackground(bool aLoadInBackground);
55
56
  // Suspend any downloads that are in progress.
57
  // If aCloseImmediately is set, resources should be released immediately
58
  // since we don't expect to resume again any time soon. Otherwise we
59
  // may resume again soon so resources should be held for a little
60
  // while.
61
  virtual void Suspend(bool aCloseImmediately) = 0;
62
63
  // Resume any downloads that have been suspended.
64
  virtual void Resume() = 0;
65
66
  // The mode is initially MODE_METADATA.
67
  virtual void SetReadMode(MediaCacheStream::ReadMode aMode) = 0;
68
69
  // Returns true if the resource can be seeked to unbuffered ranges, i.e.
70
  // for an HTTP network stream this returns true if HTTP1.1 Byte Range
71
  // requests are supported by the connection/server.
72
  virtual bool IsTransportSeekable() = 0;
73
74
  // Get the current principal for the channel
75
  virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal() = 0;
76
77
  /**
78
   * Open the stream. This creates a stream listener and returns it in
79
   * aStreamListener; this listener needs to be notified of incoming data.
80
   */
81
  virtual nsresult Open(nsIStreamListener** aStreamListener) = 0;
82
83
  // If this returns false, then we shouldn't try to clone this MediaResource
84
  // because its underlying resources are not suitable for reuse (e.g.
85
  // because the underlying connection has been lost, or this resource
86
  // just can't be safely cloned). If this returns true, CloneData could
87
  // still fail. If this returns false, CloneData should not be called.
88
0
  virtual bool CanClone() { return false; }
89
90
  // Create a new stream of the same type that refers to the same URI
91
  // with a new channel. Any cached data associated with the original
92
  // stream should be accessible in the new stream too.
93
  virtual already_AddRefed<BaseMediaResource> CloneData(
94
    MediaResourceCallback* aCallback)
95
0
  {
96
0
    return nullptr;
97
0
  }
98
99
  // Returns true if the resource is a live stream.
100
0
  virtual bool IsLiveStream() const { return false; }
101
102
  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
103
0
  {
104
0
    // Might be useful to track in the future:
105
0
    // - mChannel
106
0
    // - mURI (possibly owned, looks like just a ref from mChannel)
107
0
    // Not owned:
108
0
    // - mCallback
109
0
    return 0;
110
0
  }
111
112
  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
113
0
  {
114
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
115
0
  }
116
117
0
  virtual nsCString GetDebugInfo() { return nsCString(); }
118
119
protected:
120
  BaseMediaResource(MediaResourceCallback* aCallback,
121
                    nsIChannel* aChannel,
122
                    nsIURI* aURI)
123
    : mCallback(aCallback)
124
    , mChannel(aChannel)
125
    , mURI(aURI)
126
    , mLoadInBackground(false)
127
0
  {
128
0
  }
129
0
  virtual ~BaseMediaResource() {}
130
131
  // Set the request's load flags to aFlags.  If the request is part of a
132
  // load group, the request is removed from the group, the flags are set, and
133
  // then the request is added back to the load group.
134
  void ModifyLoadFlags(nsLoadFlags aFlags);
135
136
  RefPtr<MediaResourceCallback> mCallback;
137
138
  // Channel used to download the media data. Must be accessed
139
  // from the main thread only.
140
  nsCOMPtr<nsIChannel> mChannel;
141
142
  // URI in case the stream needs to be re-opened. Access from
143
  // main thread only.
144
  nsCOMPtr<nsIURI> mURI;
145
146
  // True if SetLoadInBackground() has been called with
147
  // aLoadInBackground = true, i.e. when the document load event is not
148
  // blocked by this resource, and all channel loads will be in the
149
  // background.
150
  bool mLoadInBackground;
151
};
152
153
} // namespace mozilla
154
155
#endif // BaseMediaResource_h