summaryrefslogtreecommitdiff
path: root/dom/media/ipc/RemoteVideoDecoder.cpp
blob: 8aed525518994547620a6dccf84b105ee8eb1824 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "RemoteVideoDecoder.h"
#include "VideoDecoderChild.h"
#include "VideoDecoderManagerChild.h"
#include "mozilla/layers/TextureClient.h"
#include "base/thread.h"
#include "MediaInfo.h"
#include "MediaPrefs.h"
#include "ImageContainer.h"
#include "mozilla/layers/SynchronousTask.h"

namespace mozilla {
namespace dom {

using base::Thread;
using namespace ipc;
using namespace layers;
using namespace gfx;

RemoteVideoDecoder::RemoteVideoDecoder(MediaDataDecoderCallback* aCallback)
  : mActor(new VideoDecoderChild())
{
#ifdef DEBUG
  mCallback = aCallback;
#endif
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
}

RemoteVideoDecoder::~RemoteVideoDecoder()
{
  // We're about to be destroyed and drop our ref to
  // VideoDecoderChild. Make sure we put a ref into the
  // task queue for the VideoDecoderChild thread to keep
  // it alive until we send the delete message.
  RefPtr<VideoDecoderChild> actor = mActor;
  VideoDecoderManagerChild::GetManagerThread()->Dispatch(NS_NewRunnableFunction([actor]() {
    MOZ_ASSERT(actor);
    actor->DestroyIPDL();
  }), NS_DISPATCH_NORMAL);
}

RefPtr<MediaDataDecoder::InitPromise>
RemoteVideoDecoder::Init()
{
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
  return InvokeAsync(VideoDecoderManagerChild::GetManagerAbstractThread(),
                     this, __func__, &RemoteVideoDecoder::InitInternal);
}

RefPtr<MediaDataDecoder::InitPromise>
RemoteVideoDecoder::InitInternal()
{
  MOZ_ASSERT(mActor);
  MOZ_ASSERT(NS_GetCurrentThread() == VideoDecoderManagerChild::GetManagerThread());
  return mActor->Init();
}

void
RemoteVideoDecoder::Input(MediaRawData* aSample)
{
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
  RefPtr<RemoteVideoDecoder> self = this;
  RefPtr<MediaRawData> sample = aSample;
  VideoDecoderManagerChild::GetManagerThread()->Dispatch(NS_NewRunnableFunction([self, sample]() {
    MOZ_ASSERT(self->mActor);
    self->mActor->Input(sample);
  }), NS_DISPATCH_NORMAL);
}

void
RemoteVideoDecoder::Flush()
{
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
  RefPtr<RemoteVideoDecoder> self = this;
  VideoDecoderManagerChild::GetManagerThread()->Dispatch(NS_NewRunnableFunction([self]() {
    MOZ_ASSERT(self->mActor);
    self->mActor->Flush();
  }), NS_DISPATCH_NORMAL);
}

void
RemoteVideoDecoder::Drain()
{
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
  RefPtr<RemoteVideoDecoder> self = this;
  VideoDecoderManagerChild::GetManagerThread()->Dispatch(NS_NewRunnableFunction([self]() {
    MOZ_ASSERT(self->mActor);
    self->mActor->Drain();
  }), NS_DISPATCH_NORMAL);
}

void
RemoteVideoDecoder::Shutdown()
{
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
  SynchronousTask task("Shutdown");
  RefPtr<RemoteVideoDecoder> self = this;
  VideoDecoderManagerChild::GetManagerThread()->Dispatch(NS_NewRunnableFunction([&]() {
    AutoCompleteTask complete(&task);
    MOZ_ASSERT(self->mActor);
    self->mActor->Shutdown();
  }), NS_DISPATCH_NORMAL);
  task.Wait();
}

bool
RemoteVideoDecoder::IsHardwareAccelerated(nsACString& aFailureReason) const
{
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
  return mActor->IsHardwareAccelerated(aFailureReason);
}

void
RemoteVideoDecoder::SetSeekThreshold(const media::TimeUnit& aTime)
{
  MOZ_ASSERT(mCallback->OnReaderTaskQueue());
  RefPtr<RemoteVideoDecoder> self = this;
  media::TimeUnit time = aTime;
  VideoDecoderManagerChild::GetManagerThread()->Dispatch(NS_NewRunnableFunction([=]() {
    MOZ_ASSERT(self->mActor);
    self->mActor->SetSeekThreshold(time);
  }), NS_DISPATCH_NORMAL);

}

nsresult
RemoteDecoderModule::Startup()
{
  if (!VideoDecoderManagerChild::GetManagerThread()) {
    return NS_ERROR_FAILURE;
  }
  return mWrapped->Startup();
}

bool
RemoteDecoderModule::SupportsMimeType(const nsACString& aMimeType,
                                      DecoderDoctorDiagnostics* aDiagnostics) const
{
  return mWrapped->SupportsMimeType(aMimeType, aDiagnostics);
}

PlatformDecoderModule::ConversionRequired
RemoteDecoderModule::DecoderNeedsConversion(const TrackInfo& aConfig) const
{
  return mWrapped->DecoderNeedsConversion(aConfig);
}

already_AddRefed<MediaDataDecoder>
RemoteDecoderModule::CreateVideoDecoder(const CreateDecoderParams& aParams)
{
  if (!MediaPrefs::PDMUseGPUDecoder() ||
      !aParams.mKnowsCompositor ||
      aParams.mKnowsCompositor->GetTextureFactoryIdentifier().mParentProcessType != GeckoProcessType_GPU) {
    return mWrapped->CreateVideoDecoder(aParams);
  }

  MediaDataDecoderCallback* callback = aParams.mCallback;
  MOZ_ASSERT(callback->OnReaderTaskQueue());
  RefPtr<RemoteVideoDecoder> object = new RemoteVideoDecoder(callback);

  VideoInfo info = aParams.VideoConfig();

  TextureFactoryIdentifier ident = aParams.mKnowsCompositor->GetTextureFactoryIdentifier();
  VideoDecoderManagerChild::GetManagerThread()->Dispatch(NS_NewRunnableFunction([=]() {
    object->mActor->InitIPDL(callback, info, ident);
  }), NS_DISPATCH_NORMAL);

  return object.forget();
}

} // namespace dom
} // namespace mozilla