Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/MediaDataDemuxer.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
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
#if !defined(MediaDataDemuxer_h)
8
#define MediaDataDemuxer_h
9
10
#include "DecoderDoctorLogger.h"
11
#include "mozilla/MozPromise.h"
12
#include "mozilla/UniquePtr.h"
13
14
#include "MediaData.h"
15
#include "MediaInfo.h"
16
#include "MediaResult.h"
17
#include "TimeUnits.h"
18
#include "nsISupportsImpl.h"
19
#include "mozilla/RefPtr.h"
20
#include "nsTArray.h"
21
22
namespace mozilla {
23
24
class MediaTrackDemuxer;
25
class TrackMetadataHolder;
26
27
DDLoggedTypeDeclName(MediaDataDemuxer);
28
DDLoggedTypeName(MediaTrackDemuxer);
29
30
// Allows reading the media data: to retrieve the metadata and demux samples.
31
// MediaDataDemuxer isn't designed to be thread safe.
32
// When used by the MediaFormatDecoder, care is taken to ensure that the demuxer
33
// will never be called from more than one thread at once.
34
class MediaDataDemuxer : public DecoderDoctorLifeLogger<MediaDataDemuxer>
35
{
36
public:
37
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDataDemuxer)
38
39
  typedef
40
    MozPromise<MediaResult, MediaResult, /* IsExclusive = */ true> InitPromise;
41
42
  // Initializes the demuxer. Other methods cannot be called unless
43
  // initialization has completed and succeeded.
44
  // Typically a demuxer will wait to parse the metadata before resolving the
45
  // promise. The promise must not be resolved until sufficient data is
46
  // supplied. For example, an incomplete metadata would cause the promise to be
47
  // rejected should no more data be coming, while the demuxer would wait
48
  // otherwise.
49
  virtual RefPtr<InitPromise> Init() = 0;
50
51
  // Returns the number of tracks of aType type available. A value of
52
  // 0 indicates that no such type is available.
53
  virtual uint32_t GetNumberTracks(TrackInfo::TrackType aType) const = 0;
54
55
  // Returns the MediaTrackDemuxer associated with aTrackNumber aType track.
56
  // aTrackNumber is not to be confused with the Track ID.
57
  // aTrackNumber must be constrained between  0 and  GetNumberTracks(aType) - 1
58
  // The actual Track ID is to be retrieved by calling
59
  // MediaTrackDemuxer::TrackInfo.
60
  virtual already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer(
61
    TrackInfo::TrackType aType, uint32_t aTrackNumber) = 0;
62
63
  // Returns true if the underlying resource allows seeking.
64
  virtual bool IsSeekable() const = 0;
65
66
  // Returns true if the underlying resource can only seek within buffered
67
  // ranges.
68
0
  virtual bool IsSeekableOnlyInBufferedRanges() const { return false; }
69
70
  // Returns the media's crypto information, or nullptr if media isn't
71
  // encrypted.
72
  virtual UniquePtr<EncryptionInfo> GetCrypto()
73
0
  {
74
0
    return nullptr;
75
0
  }
76
77
  // Notifies the demuxer that the underlying resource has received more data
78
  // since the demuxer was initialized.
79
  // The demuxer can use this mechanism to inform all track demuxers that new
80
  // data is available and to refresh its buffered range.
81
0
  virtual void NotifyDataArrived() { }
82
83
  // Notifies the demuxer that the underlying resource has had data removed
84
  // since the demuxer was initialized.
85
  // The demuxer can use this mechanism to inform all track demuxers to update
86
  // its buffered range.
87
  // This will be called should the demuxer be used with MediaSource.
88
0
  virtual void NotifyDataRemoved() { }
89
90
  // Indicate to MediaFormatReader if it should compute the start time
91
  // of the demuxed data. If true (default) the first sample returned will be
92
  // used as reference time base.
93
0
  virtual bool ShouldComputeStartTime() const { return true; }
94
95
protected:
96
  virtual ~MediaDataDemuxer()
97
  {
98
  }
99
};
100
101
class MediaTrackDemuxer : public DecoderDoctorLifeLogger<MediaTrackDemuxer>
102
{
103
public:
104
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaTrackDemuxer)
105
106
  class SamplesHolder
107
  {
108
  public:
109
    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SamplesHolder)
110
    nsTArray<RefPtr<MediaRawData>> mSamples;
111
  private:
112
0
    ~SamplesHolder() { }
113
  };
114
115
  class SkipFailureHolder
116
  {
117
  public:
118
    SkipFailureHolder(const MediaResult& aFailure, uint32_t aSkipped)
119
      : mFailure(aFailure)
120
      , mSkipped(aSkipped)
121
0
    {}
122
    MediaResult mFailure;
123
    uint32_t mSkipped;
124
  };
125
126
  typedef MozPromise<media::TimeUnit, MediaResult, /* IsExclusive = */ true>
127
    SeekPromise;
128
  typedef MozPromise<RefPtr<SamplesHolder>, MediaResult,
129
                     /* IsExclusive = */ true>
130
    SamplesPromise;
131
  typedef MozPromise<uint32_t, SkipFailureHolder, /* IsExclusive = */ true>
132
    SkipAccessPointPromise;
133
134
  // Returns the TrackInfo (a.k.a Track Description) for this track.
135
  // The TrackInfo returned will be:
136
  // TrackInfo::kVideoTrack -> VideoInfo.
137
  // TrackInfo::kAudioTrack -> AudioInfo.
138
  // respectively.
139
  virtual UniquePtr<TrackInfo> GetInfo() const = 0;
140
141
  // Seeks to aTime. Upon success, SeekPromise will be resolved with the
142
  // actual time seeked to. Typically the random access point time
143
  virtual RefPtr<SeekPromise> Seek(const media::TimeUnit& aTime) = 0;
144
145
  // Returns the next aNumSamples sample(s) available.
146
  // If only a lesser amount of samples is available, only those will be
147
  // returned.
148
  // A aNumSamples value of -1 indicates to return all remaining samples.
149
  // A video sample is typically made of a single video frame while an audio
150
  // sample will contains multiple audio frames.
151
  virtual RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) = 0;
152
153
  // Returns true if a call to GetSamples() may block while waiting on the
154
  // underlying resource to return the data.
155
  // This is used by the MediaFormatReader to determine if buffering heuristics
156
  // should be used.
157
  virtual bool GetSamplesMayBlock() const
158
0
  {
159
0
    return true;
160
0
  }
161
162
  // Cancel all pending actions (Seek, GetSamples) and reset current state
163
  // All pending promises are to be rejected with CANCEL.
164
  // The next call to GetSamples would return the first sample available in the
165
  // track.
166
  virtual void Reset() = 0;
167
168
  // Returns timestamp of next random access point or an error if the demuxer
169
  // can't report this.
170
  virtual nsresult GetNextRandomAccessPoint(media::TimeUnit* aTime)
171
0
  {
172
0
    return NS_ERROR_NOT_IMPLEMENTED;
173
0
  }
174
175
  // Returns timestamp of previous random access point or an error if the
176
  // demuxer can't report this.
177
  virtual nsresult GetPreviousRandomAccessPoint(media::TimeUnit* aTime)
178
0
  {
179
0
    return NS_ERROR_NOT_IMPLEMENTED;
180
0
  }
181
182
  // Skip frames until the next Random Access Point located after
183
  // aTimeThreshold.
184
  // The first frame returned by the next call to GetSamples() will be the
185
  // first random access point found after aTimeThreshold.
186
  // Upon success, returns the number of frames skipped.
187
  virtual RefPtr<SkipAccessPointPromise>
188
  SkipToNextRandomAccessPoint(const media::TimeUnit& aTimeThreshold) = 0;
189
190
  // Gets the resource's offset used for the last Seek() or GetSample().
191
  // A negative value indicates that this functionality isn't supported.
192
  virtual int64_t GetResourceOffset() const
193
0
  {
194
0
    return -1;
195
0
  }
196
197
  virtual TrackInfo::TrackType GetType() const
198
0
  {
199
0
    return GetInfo()->GetType();
200
0
  }
201
202
  virtual media::TimeIntervals GetBuffered() = 0;
203
204
  // By default, it is assumed that the entire resource can be evicted once
205
  // all samples have been demuxed.
206
  virtual int64_t GetEvictionOffset(const media::TimeUnit& aTime)
207
0
  {
208
0
    return INT64_MAX;
209
0
  }
210
211
  // If the MediaTrackDemuxer and MediaDataDemuxer hold cross references.
212
  // BreakCycles must be overridden.
213
  virtual void BreakCycles()
214
0
  {
215
0
  }
216
217
protected:
218
  virtual ~MediaTrackDemuxer() { }
219
};
220
221
} // namespace mozilla
222
223
#endif // MediaDataDemuxer_h