Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/FileMediaResource.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 mozilla_dom_media_FileMediaResource_h
7
#define mozilla_dom_media_FileMediaResource_h
8
9
#include "BaseMediaResource.h"
10
#include "mozilla/Mutex.h"
11
12
namespace mozilla {
13
14
class FileMediaResource : public BaseMediaResource
15
{
16
public:
17
  FileMediaResource(MediaResourceCallback* aCallback,
18
                    nsIChannel* aChannel,
19
                    nsIURI* aURI,
20
                    int64_t aSize = -1 /* unknown size */)
21
    : BaseMediaResource(aCallback, aChannel, aURI)
22
    , mSize(aSize)
23
    , mLock("FileMediaResource.mLock")
24
    , mSizeInitialized(aSize != -1)
25
0
  {
26
0
  }
27
  ~FileMediaResource()
28
0
  {
29
0
  }
30
31
  // Main thread
32
  nsresult Open(nsIStreamListener** aStreamListener) override;
33
  nsresult Close() override;
34
0
  void     Suspend(bool aCloseImmediately) override {}
35
0
  void     Resume() override {}
36
  already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override;
37
  nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) override;
38
39
  // These methods are called off the main thread.
40
41
  // Other thread
42
0
  void     SetReadMode(MediaCacheStream::ReadMode aMode) override {}
43
0
  void     SetPlaybackRate(uint32_t aBytesPerSecond) override {}
44
  nsresult ReadAt(int64_t aOffset, char* aBuffer,
45
                  uint32_t aCount, uint32_t* aBytes) override;
46
  // (Probably) file-based, caching recommended.
47
0
  bool ShouldCacheReads() override { return true; }
48
49
  // Any thread
50
0
  void    Pin() override {}
51
0
  void    Unpin() override {}
52
  double  GetDownloadRate(bool* aIsReliable) override
53
0
  {
54
0
    // The data's all already here
55
0
    *aIsReliable = true;
56
0
    return 100*1024*1024; // arbitray, use 100MB/s
57
0
  }
58
59
  int64_t GetLength() override
60
0
  {
61
0
    MutexAutoLock lock(mLock);
62
0
63
0
    EnsureSizeInitialized();
64
0
    return mSizeInitialized ? mSize : 0;
65
0
  }
66
67
  int64_t GetNextCachedData(int64_t aOffset) override
68
0
  {
69
0
    MutexAutoLock lock(mLock);
70
0
71
0
    EnsureSizeInitialized();
72
0
    return (aOffset < mSize) ? aOffset : -1;
73
0
  }
74
75
  int64_t GetCachedDataEnd(int64_t aOffset) override
76
0
  {
77
0
    MutexAutoLock lock(mLock);
78
0
79
0
    EnsureSizeInitialized();
80
0
    return std::max(aOffset, mSize);
81
0
  }
82
0
  bool    IsDataCachedToEndOfResource(int64_t aOffset) override { return true; }
83
0
  bool    IsTransportSeekable() override { return true; }
84
85
  nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override;
86
87
  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
88
0
  {
89
0
    // Might be useful to track in the future:
90
0
    // - mInput
91
0
    return BaseMediaResource::SizeOfExcludingThis(aMallocSizeOf);
92
0
  }
93
94
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
95
0
  {
96
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
97
0
  }
98
99
protected:
100
  // These Unsafe variants of Read and Seek perform their operations
101
  // without acquiring mLock. The caller must obtain the lock before
102
  // calling. The implmentation of Read, Seek and ReadAt obtains the
103
  // lock before calling these Unsafe variants to read or seek.
104
  nsresult UnsafeRead(char* aBuffer, uint32_t aCount, uint32_t* aBytes);
105
  nsresult UnsafeSeek(int32_t aWhence, int64_t aOffset);
106
private:
107
  // Ensures mSize is initialized, if it can be.
108
  // mLock must be held when this is called, and mInput must be non-null.
109
  void EnsureSizeInitialized();
110
  already_AddRefed<MediaByteBuffer> UnsafeMediaReadAt(
111
                        int64_t aOffset, uint32_t aCount);
112
113
  // The file size, or -1 if not known. Immutable after Open().
114
  // Can be used from any thread.
115
  int64_t mSize;
116
117
  // This lock handles synchronisation between calls to Close() and
118
  // the Read, Seek, etc calls. Close must not be called while a
119
  // Read or Seek is in progress since it resets various internal
120
  // values to null.
121
  // This lock protects mSeekable, mInput, mSize, and mSizeInitialized.
122
  Mutex mLock;
123
124
  // Seekable stream interface to file. This can be used from any
125
  // thread.
126
  nsCOMPtr<nsISeekableStream> mSeekable;
127
128
  // Input stream for the media data. This can be used from any
129
  // thread.
130
  nsCOMPtr<nsIInputStream>  mInput;
131
132
  // Whether we've attempted to initialize mSize. Note that mSize can be -1
133
  // when mSizeInitialized is true if we tried and failed to get the size
134
  // of the file.
135
  bool mSizeInitialized;
136
  // Set to true if NotifyDataEnded callback has been processed (which only
137
  // occurs if resource size is known)
138
  bool mNotifyDataEndedProcessed = false;
139
};
140
141
} // namespace mozilla
142
143
#endif // mozilla_dom_media_FileMediaResource_h