summaryrefslogtreecommitdiffstats
path: root/dom/media/gtest/TestGMPCrossOrigin.cpp
blob: b9e058c4b3bda01b4f02d2f790adcb097e3c2abc (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
/* -*- 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 "gtest/gtest.h"
#include "mozilla/gtest/MozAssertions.h"
#include "mozilla/StaticPtr.h"
#include "GMPTestMonitor.h"
#include "GMPVideoDecoderProxy.h"
#include "GMPVideoEncoderProxy.h"
#include "GMPServiceParent.h"
#include "nsAppDirectoryServiceDefs.h"
#include "mozilla/Atomics.h"
#include "mozilla/DebugOnly.h"
#include "nsThreadUtils.h"

using namespace mozilla;
using namespace mozilla::gmp;

struct GMPTestRunner {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GMPTestRunner)

  GMPTestRunner() = default;
  void DoTest(void (GMPTestRunner::*aTestMethod)(GMPTestMonitor&));
  void RunTestGMPTestCodec1(GMPTestMonitor& aMonitor);
  void RunTestGMPTestCodec2(GMPTestMonitor& aMonitor);
  void RunTestGMPTestCodec3(GMPTestMonitor& aMonitor);
  void RunTestGMPCrossOrigin1(GMPTestMonitor& aMonitor);
  void RunTestGMPCrossOrigin2(GMPTestMonitor& aMonitor);
  void RunTestGMPCrossOrigin3(GMPTestMonitor& aMonitor);
  void RunTestGMPCrossOrigin4(GMPTestMonitor& aMonitor);

 private:
  ~GMPTestRunner() = default;
};

template <class T, class Base,
          nsresult (NS_STDCALL GeckoMediaPluginService::*Getter)(
              GMPCrashHelper*, nsTArray<nsCString>*, const nsACString&,
              UniquePtr<Base>&&)>
class RunTestGMPVideoCodec : public Base {
 public:
  void Done(T* aGMP, GMPVideoHost* aHost) override {
    EXPECT_TRUE(aGMP);
    EXPECT_TRUE(aHost);
    if (aGMP) {
      aGMP->Close();
    }
    mMonitor.SetFinished();
  }

  static void Run(GMPTestMonitor& aMonitor, const nsCString& aOrigin) {
    UniquePtr<GMPCallbackType> callback(new RunTestGMPVideoCodec(aMonitor));
    Get(aOrigin, std::move(callback));
  }

 protected:
  typedef T GMPCodecType;
  typedef Base GMPCallbackType;

  explicit RunTestGMPVideoCodec(GMPTestMonitor& aMonitor)
      : mMonitor(aMonitor) {}

  static nsresult Get(const nsACString& aNodeId, UniquePtr<Base>&& aCallback) {
    nsTArray<nsCString> tags;
    tags.AppendElement("h264"_ns);
    tags.AppendElement("fake"_ns);

    RefPtr<GeckoMediaPluginService> service =
        GeckoMediaPluginService::GetGeckoMediaPluginService();
    return ((*service).*Getter)(nullptr, &tags, aNodeId, std::move(aCallback));
  }

  GMPTestMonitor& mMonitor;
};

typedef RunTestGMPVideoCodec<GMPVideoDecoderProxy, GetGMPVideoDecoderCallback,
                             &GeckoMediaPluginService::GetGMPVideoDecoder>
    RunTestGMPVideoDecoder;
typedef RunTestGMPVideoCodec<GMPVideoEncoderProxy, GetGMPVideoEncoderCallback,
                             &GeckoMediaPluginService::GetGMPVideoEncoder>
    RunTestGMPVideoEncoder;

void GMPTestRunner::RunTestGMPTestCodec1(GMPTestMonitor& aMonitor) {
  RunTestGMPVideoDecoder::Run(aMonitor, "o"_ns);
}

void GMPTestRunner::RunTestGMPTestCodec2(GMPTestMonitor& aMonitor) {
  RunTestGMPVideoDecoder::Run(aMonitor, ""_ns);
}

void GMPTestRunner::RunTestGMPTestCodec3(GMPTestMonitor& aMonitor) {
  RunTestGMPVideoEncoder::Run(aMonitor, ""_ns);
}

template <class Base>
class RunTestGMPCrossOrigin : public Base {
 public:
  void Done(typename Base::GMPCodecType* aGMP, GMPVideoHost* aHost) override {
    EXPECT_TRUE(aGMP);

    UniquePtr<typename Base::GMPCallbackType> callback(
        new Step2(Base::mMonitor, aGMP, mShouldBeEqual));
    nsresult rv = Base::Get(mOrigin2, std::move(callback));
    EXPECT_NS_SUCCEEDED(rv);
    if (NS_FAILED(rv)) {
      Base::mMonitor.SetFinished();
    }
  }

  static void Run(GMPTestMonitor& aMonitor, const nsCString& aOrigin1,
                  const nsCString& aOrigin2) {
    UniquePtr<typename Base::GMPCallbackType> callback(
        new RunTestGMPCrossOrigin<Base>(aMonitor, aOrigin1, aOrigin2));
    nsresult rv = Base::Get(aOrigin1, std::move(callback));
    EXPECT_NS_SUCCEEDED(rv);
    if (NS_FAILED(rv)) {
      aMonitor.SetFinished();
    }
  }

 private:
  RunTestGMPCrossOrigin(GMPTestMonitor& aMonitor, const nsCString& aOrigin1,
                        const nsCString& aOrigin2)
      : Base(aMonitor),
        mGMP(nullptr),
        mOrigin2(aOrigin2),
        mShouldBeEqual(aOrigin1.Equals(aOrigin2)) {}

  class Step2 : public Base {
   public:
    Step2(GMPTestMonitor& aMonitor, typename Base::GMPCodecType* aGMP,
          bool aShouldBeEqual)
        : Base(aMonitor), mGMP(aGMP), mShouldBeEqual(aShouldBeEqual) {}
    void Done(typename Base::GMPCodecType* aGMP, GMPVideoHost* aHost) override {
      EXPECT_TRUE(aGMP);
      if (aGMP) {
        EXPECT_TRUE(mGMP && (mGMP->GetPluginId() == aGMP->GetPluginId()) ==
                                mShouldBeEqual);
      }
      if (mGMP) {
        mGMP->Close();
      }
      Base::Done(aGMP, aHost);
    }

   private:
    typename Base::GMPCodecType* mGMP;
    bool mShouldBeEqual;
  };

  typename Base::GMPCodecType* mGMP;
  nsCString mOrigin2;
  bool mShouldBeEqual;
};

typedef RunTestGMPCrossOrigin<RunTestGMPVideoDecoder>
    RunTestGMPVideoDecoderCrossOrigin;
typedef RunTestGMPCrossOrigin<RunTestGMPVideoEncoder>
    RunTestGMPVideoEncoderCrossOrigin;

void GMPTestRunner::RunTestGMPCrossOrigin1(GMPTestMonitor& aMonitor) {
  RunTestGMPVideoDecoderCrossOrigin::Run(aMonitor, "origin1"_ns, "origin2"_ns);
}

void GMPTestRunner::RunTestGMPCrossOrigin2(GMPTestMonitor& aMonitor) {
  RunTestGMPVideoEncoderCrossOrigin::Run(aMonitor, "origin1"_ns, "origin2"_ns);
}

void GMPTestRunner::RunTestGMPCrossOrigin3(GMPTestMonitor& aMonitor) {
  RunTestGMPVideoDecoderCrossOrigin::Run(aMonitor, "origin1"_ns, "origin1"_ns);
}

void GMPTestRunner::RunTestGMPCrossOrigin4(GMPTestMonitor& aMonitor) {
  RunTestGMPVideoEncoderCrossOrigin::Run(aMonitor, "origin1"_ns, "origin1"_ns);
}

void GMPTestRunner::DoTest(
    void (GMPTestRunner::*aTestMethod)(GMPTestMonitor&)) {
  RefPtr<GeckoMediaPluginService> service =
      GeckoMediaPluginService::GetGeckoMediaPluginService();
  nsCOMPtr<nsIThread> thread;
  EXPECT_NS_SUCCEEDED(service->GetThread(getter_AddRefs(thread)));

  GMPTestMonitor monitor;
  thread->Dispatch(NewRunnableMethod<GMPTestMonitor&>(
                       "GMPTestRunner::DoTest", this, aTestMethod, monitor),
                   NS_DISPATCH_NORMAL);
  monitor.AwaitFinished();
}

TEST(GeckoMediaPlugins, GMPTestCodec)
{
  RefPtr<GMPTestRunner> runner = new GMPTestRunner();
  runner->DoTest(&GMPTestRunner::RunTestGMPTestCodec1);
  runner->DoTest(&GMPTestRunner::RunTestGMPTestCodec2);
  runner->DoTest(&GMPTestRunner::RunTestGMPTestCodec3);
}

TEST(GeckoMediaPlugins, GMPCrossOrigin)
{
  RefPtr<GMPTestRunner> runner = new GMPTestRunner();
  runner->DoTest(&GMPTestRunner::RunTestGMPCrossOrigin1);
  runner->DoTest(&GMPTestRunner::RunTestGMPCrossOrigin2);
  runner->DoTest(&GMPTestRunner::RunTestGMPCrossOrigin3);
  runner->DoTest(&GMPTestRunner::RunTestGMPCrossOrigin4);
}