summaryrefslogtreecommitdiffstats
path: root/dom/media/gmp/GMPVideoEncodedFrameImpl.cpp
blob: c0ed87df4da6f36d5b8fd82144c48ea52b37ceec (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "GMPVideoEncodedFrameImpl.h"
#include "GMPVideoHost.h"
#include "mozilla/gmp/GMPTypes.h"
#include "GMPSharedMemManager.h"

namespace mozilla::gmp {

GMPVideoEncodedFrameImpl::GMPVideoEncodedFrameImpl(GMPVideoHostImpl* aHost)
    : mEncodedWidth(0),
      mEncodedHeight(0),
      mTimeStamp(0ll),
      mDuration(0ll),
      mFrameType(kGMPDeltaFrame),
      mSize(0),
      mCompleteFrame(false),
      mHost(aHost),
      mBufferType(GMP_BufferSingle) {
  MOZ_ASSERT(aHost);
  aHost->EncodedFrameCreated(this);
}

GMPVideoEncodedFrameImpl::GMPVideoEncodedFrameImpl(
    const GMPVideoEncodedFrameData& aFrameData, GMPVideoHostImpl* aHost)
    : mEncodedWidth(aFrameData.mEncodedWidth()),
      mEncodedHeight(aFrameData.mEncodedHeight()),
      mTimeStamp(aFrameData.mTimestamp()),
      mDuration(aFrameData.mDuration()),
      mFrameType(static_cast<GMPVideoFrameType>(aFrameData.mFrameType())),
      mSize(aFrameData.mSize()),
      mCompleteFrame(aFrameData.mCompleteFrame()),
      mHost(aHost),
      mBuffer(aFrameData.mBuffer()),
      mBufferType(aFrameData.mBufferType()) {
  MOZ_ASSERT(aHost);
  aHost->EncodedFrameCreated(this);
}

GMPVideoEncodedFrameImpl::~GMPVideoEncodedFrameImpl() {
  DestroyBuffer();
  if (mHost) {
    mHost->EncodedFrameDestroyed(this);
  }
}

GMPVideoFrameFormat GMPVideoEncodedFrameImpl::GetFrameFormat() {
  return kGMPEncodedVideoFrame;
}

void GMPVideoEncodedFrameImpl::DoneWithAPI() {
  DestroyBuffer();

  // Do this after destroying the buffer because destruction
  // involves deallocation, which requires a host.
  mHost = nullptr;
}

void GMPVideoEncodedFrameImpl::ActorDestroyed() {
  // Simply clear out Shmem reference, do not attempt to
  // properly free it. It has already been freed.
  mBuffer = ipc::Shmem();
  // No more host.
  mHost = nullptr;
}

bool GMPVideoEncodedFrameImpl::RelinquishFrameData(
    GMPVideoEncodedFrameData& aFrameData) {
  aFrameData.mEncodedWidth() = mEncodedWidth;
  aFrameData.mEncodedHeight() = mEncodedHeight;
  aFrameData.mTimestamp() = mTimeStamp;
  aFrameData.mDuration() = mDuration;
  aFrameData.mFrameType() = mFrameType;
  aFrameData.mSize() = mSize;
  aFrameData.mCompleteFrame() = mCompleteFrame;
  aFrameData.mBuffer() = mBuffer;
  aFrameData.mBufferType() = mBufferType;

  // This method is called right before Shmem is sent to another process.
  // We need to effectively zero out our member copy so that we don't
  // try to delete Shmem we don't own later.
  mBuffer = ipc::Shmem();

  return true;
}

void GMPVideoEncodedFrameImpl::DestroyBuffer() {
  if (mHost && mBuffer.IsWritable()) {
    mHost->SharedMemMgr()->MgrDeallocShmem(GMPSharedMem::kGMPEncodedData,
                                           mBuffer);
  }
  mBuffer = ipc::Shmem();
}

GMPErr GMPVideoEncodedFrameImpl::CreateEmptyFrame(uint32_t aSize) {
  if (aSize == 0) {
    DestroyBuffer();
  } else if (aSize > AllocatedSize()) {
    DestroyBuffer();
    if (!mHost->SharedMemMgr()->MgrAllocShmem(GMPSharedMem::kGMPEncodedData,
                                              aSize, &mBuffer) ||
        !Buffer()) {
      return GMPAllocErr;
    }
  }
  mSize = aSize;

  return GMPNoErr;
}

GMPErr GMPVideoEncodedFrameImpl::CopyFrame(const GMPVideoEncodedFrame& aFrame) {
  auto& f = static_cast<const GMPVideoEncodedFrameImpl&>(aFrame);

  if (f.mSize != 0) {
    GMPErr err = CreateEmptyFrame(f.mSize);
    if (err != GMPNoErr) {
      return err;
    }
    memcpy(Buffer(), f.Buffer(), f.mSize);
  }
  mEncodedWidth = f.mEncodedWidth;
  mEncodedHeight = f.mEncodedHeight;
  mTimeStamp = f.mTimeStamp;
  mDuration = f.mDuration;
  mFrameType = f.mFrameType;
  mSize = f.mSize;  // already set...
  mCompleteFrame = f.mCompleteFrame;
  mBufferType = f.mBufferType;
  // Don't copy host, that should have been set properly on object creation via
  // host.

  return GMPNoErr;
}

void GMPVideoEncodedFrameImpl::SetEncodedWidth(uint32_t aEncodedWidth) {
  mEncodedWidth = aEncodedWidth;
}

uint32_t GMPVideoEncodedFrameImpl::EncodedWidth() { return mEncodedWidth; }

void GMPVideoEncodedFrameImpl::SetEncodedHeight(uint32_t aEncodedHeight) {
  mEncodedHeight = aEncodedHeight;
}

uint32_t GMPVideoEncodedFrameImpl::EncodedHeight() { return mEncodedHeight; }

void GMPVideoEncodedFrameImpl::SetTimeStamp(uint64_t aTimeStamp) {
  mTimeStamp = aTimeStamp;
}

uint64_t GMPVideoEncodedFrameImpl::TimeStamp() { return mTimeStamp; }

void GMPVideoEncodedFrameImpl::SetDuration(uint64_t aDuration) {
  mDuration = aDuration;
}

uint64_t GMPVideoEncodedFrameImpl::Duration() const { return mDuration; }

void GMPVideoEncodedFrameImpl::SetFrameType(GMPVideoFrameType aFrameType) {
  mFrameType = aFrameType;
}

GMPVideoFrameType GMPVideoEncodedFrameImpl::FrameType() { return mFrameType; }

void GMPVideoEncodedFrameImpl::SetAllocatedSize(uint32_t aNewSize) {
  if (aNewSize <= AllocatedSize()) {
    return;
  }

  if (!mHost) {
    return;
  }

  ipc::Shmem new_mem;
  if (!mHost->SharedMemMgr()->MgrAllocShmem(GMPSharedMem::kGMPEncodedData,
                                            aNewSize, &new_mem) ||
      !new_mem.get<uint8_t>()) {
    return;
  }

  if (mBuffer.IsReadable()) {
    memcpy(new_mem.get<uint8_t>(), Buffer(), mSize);
  }

  DestroyBuffer();

  mBuffer = new_mem;
}

uint32_t GMPVideoEncodedFrameImpl::AllocatedSize() {
  if (mBuffer.IsWritable()) {
    return mBuffer.Size<uint8_t>();
  }
  return 0;
}

void GMPVideoEncodedFrameImpl::SetSize(uint32_t aSize) { mSize = aSize; }

uint32_t GMPVideoEncodedFrameImpl::Size() { return mSize; }

void GMPVideoEncodedFrameImpl::SetCompleteFrame(bool aCompleteFrame) {
  mCompleteFrame = aCompleteFrame;
}

bool GMPVideoEncodedFrameImpl::CompleteFrame() { return mCompleteFrame; }

const uint8_t* GMPVideoEncodedFrameImpl::Buffer() const {
  return mBuffer.get<uint8_t>();
}

uint8_t* GMPVideoEncodedFrameImpl::Buffer() { return mBuffer.get<uint8_t>(); }

void GMPVideoEncodedFrameImpl::Destroy() { delete this; }

GMPBufferType GMPVideoEncodedFrameImpl::BufferType() const {
  return mBufferType;
}

void GMPVideoEncodedFrameImpl::SetBufferType(GMPBufferType aBufferType) {
  mBufferType = aBufferType;
}

}  // namespace mozilla::gmp