summaryrefslogtreecommitdiffstats
path: root/dom/media/mediasource/SourceBufferResource.cpp
blob: 49447be01532e4e1f1d0d27975c257b47829d647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SourceBufferResource.h"

#include "mozilla/Logging.h"
#include "mozilla/TaskQueue.h"
#include "MediaData.h"

mozilla::LogModule* GetSourceBufferResourceLog() {
  static mozilla::LazyLogModule sLogModule("SourceBufferResource");
  return sLogModule;
}

#define SBR_DEBUG(arg, ...)                                         \
  DDMOZ_LOG(GetSourceBufferResourceLog(), mozilla::LogLevel::Debug, \
            "::%s: " arg, __func__, ##__VA_ARGS__)
#define SBR_DEBUGV(arg, ...)                                          \
  DDMOZ_LOG(GetSourceBufferResourceLog(), mozilla::LogLevel::Verbose, \
            "::%s: " arg, __func__, ##__VA_ARGS__)

namespace mozilla {

RefPtr<GenericPromise> SourceBufferResource::Close() {
  MOZ_ASSERT(OnThread());
  SBR_DEBUG("Close");
  mClosed = true;
  return GenericPromise::CreateAndResolve(true, __func__);
}

nsresult SourceBufferResource::ReadAt(int64_t aOffset, char* aBuffer,
                                      uint32_t aCount, uint32_t* aBytes) {
  SBR_DEBUG("ReadAt(aOffset=%" PRId64 ", aBuffer=%p, aCount=%u, aBytes=%p)",
            aOffset, aBytes, aCount, aBytes);
  return ReadAtInternal(aOffset, aBuffer, aCount, aBytes);
}

nsresult SourceBufferResource::ReadAtInternal(int64_t aOffset, char* aBuffer,
                                              uint32_t aCount,
                                              uint32_t* aBytes) {
  MOZ_ASSERT(OnThread());

  if (mClosed || aOffset < 0 || uint64_t(aOffset) < mInputBuffer.GetOffset() ||
      aOffset > GetLength()) {
    return NS_ERROR_FAILURE;
  }

  uint32_t available = GetLength() - aOffset;
  uint32_t count = std::min(aCount, available);

  SBR_DEBUGV("offset=%" PRId64 " GetLength()=%" PRId64
             " available=%u count=%u mEnded=%d",
             aOffset, GetLength(), available, count, mEnded);
  if (available == 0) {
    SBR_DEBUGV("reached EOF");
    *aBytes = 0;
    return NS_OK;
  }

  mInputBuffer.CopyData(aOffset, count, aBuffer);
  *aBytes = count;

  return NS_OK;
}

nsresult SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset,
                                             uint32_t aCount) {
  SBR_DEBUG("ReadFromCache(aBuffer=%p, aOffset=%" PRId64 ", aCount=%u)",
            aBuffer, aOffset, aCount);
  uint32_t bytesRead;
  nsresult rv = ReadAtInternal(aOffset, aBuffer, aCount, &bytesRead);
  NS_ENSURE_SUCCESS(rv, rv);

  // ReadFromCache return failure if not all the data is cached.
  return bytesRead == aCount ? NS_OK : NS_ERROR_FAILURE;
}

uint32_t SourceBufferResource::EvictData(uint64_t aPlaybackOffset,
                                         int64_t aThreshold) {
  MOZ_ASSERT(OnThread());
  SBR_DEBUG("EvictData(aPlaybackOffset=%" PRIu64
            ","
            "aThreshold=%" PRId64 ")",
            aPlaybackOffset, aThreshold);
  uint32_t result = mInputBuffer.Evict(aPlaybackOffset, aThreshold);
  return result;
}

void SourceBufferResource::EvictBefore(uint64_t aOffset) {
  MOZ_ASSERT(OnThread());
  SBR_DEBUG("EvictBefore(aOffset=%" PRIu64 ")", aOffset);

  mInputBuffer.EvictBefore(aOffset);
}

uint32_t SourceBufferResource::EvictAll() {
  MOZ_ASSERT(OnThread());
  SBR_DEBUG("EvictAll()");
  return mInputBuffer.EvictAll();
}

void SourceBufferResource::AppendData(MediaByteBuffer* aData) {
  AppendData(MediaSpan(aData));
}

void SourceBufferResource::AppendData(const MediaSpan& aData) {
  MOZ_ASSERT(OnThread());
  SBR_DEBUG("AppendData(aData=%p, aLength=%zu)", aData.Elements(),
            aData.Length());
  mInputBuffer.AppendItem(aData);
  mEnded = false;
}

void SourceBufferResource::Ended() {
  MOZ_ASSERT(OnThread());
  SBR_DEBUG("");
  mEnded = true;
}

SourceBufferResource::~SourceBufferResource() { SBR_DEBUG(""); }

SourceBufferResource::SourceBufferResource()
#if defined(DEBUG)
    : mThread(AbstractThread::GetCurrent())
#endif
{
  SBR_DEBUG("");
}

#if defined(DEBUG)
const AbstractThread* SourceBufferResource::GetThread() const {
  return mThread;
}
bool SourceBufferResource::OnThread() const {
  return !GetThread() || GetThread()->IsCurrentThreadIn();
}
#endif

#undef SBR_DEBUG
#undef SBR_DEBUGV
}  // namespace mozilla