summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/libwebrtcglue/WebrtcVideoCodecFactory.h
blob: ef5765043f34f0bffca72a280b8f7ed3887c86c5 (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
/* -*- 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/. */

#ifndef DOM_MEDIA_WEBRTC_LIBWEBRTCGLUE_WEBRTCVIDEOCODECFACTORY_H_
#define DOM_MEDIA_WEBRTC_LIBWEBRTCGLUE_WEBRTCVIDEOCODECFACTORY_H_

#include "api/video_codecs/video_decoder_factory.h"
#include "api/video_codecs/video_encoder_factory.h"
#include "MediaEventSource.h"
#include "PerformanceRecorder.h"

namespace mozilla {
class GmpPluginNotifierInterface {
  virtual void DisconnectAll() = 0;
  virtual MediaEventSource<uint64_t>& CreatedGmpPluginEvent() = 0;
  virtual MediaEventSource<uint64_t>& ReleasedGmpPluginEvent() = 0;
};

class GmpPluginNotifier : public GmpPluginNotifierInterface {
 public:
  explicit GmpPluginNotifier(nsCOMPtr<nsISerialEventTarget> aOwningThread)
      : mOwningThread(std::move(aOwningThread)),
        mCreatedGmpPluginEvent(mOwningThread),
        mReleasedGmpPluginEvent(mOwningThread) {}

  ~GmpPluginNotifier() = default;

  void DisconnectAll() override {
    MOZ_ASSERT(mOwningThread->IsOnCurrentThread());
    mCreatedGmpPluginEvent.DisconnectAll();
    mReleasedGmpPluginEvent.DisconnectAll();
  }

  MediaEventSource<uint64_t>& CreatedGmpPluginEvent() override {
    return mCreatedGmpPluginEvent;
  }

  MediaEventSource<uint64_t>& ReleasedGmpPluginEvent() override {
    return mReleasedGmpPluginEvent;
  }

 protected:
  const nsCOMPtr<nsISerialEventTarget> mOwningThread;
  MediaEventForwarder<uint64_t> mCreatedGmpPluginEvent;
  MediaEventForwarder<uint64_t> mReleasedGmpPluginEvent;
};

class WebrtcVideoDecoderFactory : public GmpPluginNotifier,
                                  public webrtc::VideoDecoderFactory {
 public:
  WebrtcVideoDecoderFactory(nsCOMPtr<nsISerialEventTarget> aOwningThread,
                            std::string aPCHandle, TrackingId aTrackingId)
      : GmpPluginNotifier(std::move(aOwningThread)),
        mPCHandle(std::move(aPCHandle)),
        mTrackingId(std::move(aTrackingId)) {}

  std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override {
    MOZ_CRASH("Unexpected call");
    return std::vector<webrtc::SdpVideoFormat>();
  }

  std::unique_ptr<webrtc::VideoDecoder> CreateVideoDecoder(
      const webrtc::SdpVideoFormat& aFormat) override;

 private:
  const std::string mPCHandle;
  const TrackingId mTrackingId;
};

class WebrtcVideoEncoderFactory : public GmpPluginNotifierInterface,
                                  public webrtc::VideoEncoderFactory {
  class InternalFactory : public GmpPluginNotifier,
                          public webrtc::VideoEncoderFactory {
   public:
    InternalFactory(nsCOMPtr<nsISerialEventTarget> aOwningThread,
                    std::string aPCHandle)
        : GmpPluginNotifier(std::move(aOwningThread)),
          mPCHandle(std::move(aPCHandle)) {}

    std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override {
      MOZ_CRASH("Unexpected call");
      return std::vector<webrtc::SdpVideoFormat>();
    }

    std::unique_ptr<webrtc::VideoEncoder> CreateVideoEncoder(
        const webrtc::SdpVideoFormat& aFormat) override;

    bool Supports(const webrtc::SdpVideoFormat& aFormat);

   private:
    const std::string mPCHandle;
  };

 public:
  explicit WebrtcVideoEncoderFactory(
      nsCOMPtr<nsISerialEventTarget> aOwningThread, std::string aPCHandle)
      : mInternalFactory(MakeUnique<InternalFactory>(std::move(aOwningThread),
                                                     std::move(aPCHandle))) {}

  std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override {
    MOZ_CRASH("Unexpected call");
    return std::vector<webrtc::SdpVideoFormat>();
  }

  std::unique_ptr<webrtc::VideoEncoder> CreateVideoEncoder(
      const webrtc::SdpVideoFormat& aFormat) override;

  void DisconnectAll() override { mInternalFactory->DisconnectAll(); }

  MediaEventSource<uint64_t>& CreatedGmpPluginEvent() override {
    return mInternalFactory->CreatedGmpPluginEvent();
  }
  MediaEventSource<uint64_t>& ReleasedGmpPluginEvent() override {
    return mInternalFactory->ReleasedGmpPluginEvent();
  }

 private:
  const UniquePtr<InternalFactory> mInternalFactory;
};
}  // namespace mozilla

#endif