1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 #include "GMPVideoHost.h"
7 #include "mozilla/Assertions.h"
8 #include "GMPVideoi420FrameImpl.h"
9 #include "GMPVideoEncodedFrameImpl.h"
10 
11 namespace mozilla::gmp {
12 
GMPVideoHostImpl(GMPSharedMemManager * aSharedMemMgr)13 GMPVideoHostImpl::GMPVideoHostImpl(GMPSharedMemManager* aSharedMemMgr)
14     : mSharedMemMgr(aSharedMemMgr) {}
15 
16 GMPVideoHostImpl::~GMPVideoHostImpl() = default;
17 
CreateFrame(GMPVideoFrameFormat aFormat,GMPVideoFrame ** aFrame)18 GMPErr GMPVideoHostImpl::CreateFrame(GMPVideoFrameFormat aFormat,
19                                      GMPVideoFrame** aFrame) {
20   if (!mSharedMemMgr) {
21     return GMPGenericErr;
22   }
23 
24   if (!aFrame) {
25     return GMPGenericErr;
26   }
27   *aFrame = nullptr;
28 
29   switch (aFormat) {
30     case kGMPI420VideoFrame:
31       *aFrame = new GMPVideoi420FrameImpl(this);
32       return GMPNoErr;
33     case kGMPEncodedVideoFrame:
34       *aFrame = new GMPVideoEncodedFrameImpl(this);
35       return GMPNoErr;
36     default:
37       MOZ_ASSERT_UNREACHABLE("Unknown frame format!");
38   }
39 
40   return GMPGenericErr;
41 }
42 
CreatePlane(GMPPlane ** aPlane)43 GMPErr GMPVideoHostImpl::CreatePlane(GMPPlane** aPlane) {
44   if (!mSharedMemMgr) {
45     return GMPGenericErr;
46   }
47 
48   if (!aPlane) {
49     return GMPGenericErr;
50   }
51   *aPlane = nullptr;
52 
53   auto p = new GMPPlaneImpl(this);
54 
55   *aPlane = p;
56 
57   return GMPNoErr;
58 }
59 
SharedMemMgr()60 GMPSharedMemManager* GMPVideoHostImpl::SharedMemMgr() { return mSharedMemMgr; }
61 
62 // XXX This should merge with ActorDestroyed
DoneWithAPI()63 void GMPVideoHostImpl::DoneWithAPI() { ActorDestroyed(); }
64 
ActorDestroyed()65 void GMPVideoHostImpl::ActorDestroyed() {
66   for (uint32_t i = mPlanes.Length(); i > 0; i--) {
67     mPlanes[i - 1]->DoneWithAPI();
68     mPlanes.RemoveElementAt(i - 1);
69   }
70   for (uint32_t i = mEncodedFrames.Length(); i > 0; i--) {
71     mEncodedFrames[i - 1]->DoneWithAPI();
72     mEncodedFrames.RemoveElementAt(i - 1);
73   }
74   mSharedMemMgr = nullptr;
75 }
76 
PlaneCreated(GMPPlaneImpl * aPlane)77 void GMPVideoHostImpl::PlaneCreated(GMPPlaneImpl* aPlane) {
78   mPlanes.AppendElement(aPlane);
79 }
80 
PlaneDestroyed(GMPPlaneImpl * aPlane)81 void GMPVideoHostImpl::PlaneDestroyed(GMPPlaneImpl* aPlane) {
82   MOZ_ALWAYS_TRUE(mPlanes.RemoveElement(aPlane));
83 }
84 
EncodedFrameCreated(GMPVideoEncodedFrameImpl * aEncodedFrame)85 void GMPVideoHostImpl::EncodedFrameCreated(
86     GMPVideoEncodedFrameImpl* aEncodedFrame) {
87   mEncodedFrames.AppendElement(aEncodedFrame);
88 }
89 
EncodedFrameDestroyed(GMPVideoEncodedFrameImpl * aFrame)90 void GMPVideoHostImpl::EncodedFrameDestroyed(GMPVideoEncodedFrameImpl* aFrame) {
91   MOZ_ALWAYS_TRUE(mEncodedFrames.RemoveElement(aFrame));
92 }
93 
94 }  // namespace mozilla::gmp
95