summaryrefslogtreecommitdiffstats
path: root/dom/media/gmp/GMPMessageUtils.h
blob: 33a18f7ad82d45a619b0c7a0c58e98f614fe353b (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
/* -*- 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 GMPMessageUtils_h_
#define GMPMessageUtils_h_

#include "gmp-video-codec.h"
#include "gmp-video-frame-encoded.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "GMPSanitizedExports.h"

namespace IPC {

template <>
struct ParamTraits<GMPErr>
    : public ContiguousEnumSerializer<GMPErr, GMPNoErr, GMPLastErr> {};

template <>
struct ParamTraits<GMPVideoFrameType>
    : public ContiguousEnumSerializer<GMPVideoFrameType, kGMPKeyFrame,
                                      kGMPVideoFrameInvalid> {};

template <>
struct ParamTraits<GMPVideoCodecComplexity>
    : public ContiguousEnumSerializer<GMPVideoCodecComplexity,
                                      kGMPComplexityNormal,
                                      kGMPComplexityInvalid> {};

template <>
struct ParamTraits<GMPVP8ResilienceMode>
    : public ContiguousEnumSerializer<GMPVP8ResilienceMode, kResilienceOff,
                                      kResilienceInvalid> {};

template <>
struct ParamTraits<GMPVideoCodecType>
    : public ContiguousEnumSerializer<GMPVideoCodecType, kGMPVideoCodecVP8,
                                      kGMPVideoCodecInvalid> {};

template <>
struct ParamTraits<GMPVideoCodecMode>
    : public ContiguousEnumSerializer<GMPVideoCodecMode, kGMPRealtimeVideo,
                                      kGMPCodecModeInvalid> {};

template <>
struct ParamTraits<GMPBufferType>
    : public ContiguousEnumSerializer<GMPBufferType, GMP_BufferSingle,
                                      GMP_BufferInvalid> {};

template <>
struct ParamTraits<cdm::EncryptionScheme>
    : public ContiguousEnumSerializerInclusive<
          cdm::EncryptionScheme, cdm::EncryptionScheme::kUnencrypted,
          cdm::EncryptionScheme::kCbcs> {};

template <>
struct ParamTraits<cdm::HdcpVersion>
    : public ContiguousEnumSerializerInclusive<
          cdm::HdcpVersion, cdm::HdcpVersion::kHdcpVersionNone,
          cdm::HdcpVersion::kHdcpVersion2_2> {};

template <>
struct ParamTraits<GMPSimulcastStream> {
  typedef GMPSimulcastStream paramType;

  static void Write(MessageWriter* aWriter, const paramType& aParam) {
    WriteParam(aWriter, aParam.mWidth);
    WriteParam(aWriter, aParam.mHeight);
    WriteParam(aWriter, aParam.mNumberOfTemporalLayers);
    WriteParam(aWriter, aParam.mMaxBitrate);
    WriteParam(aWriter, aParam.mTargetBitrate);
    WriteParam(aWriter, aParam.mMinBitrate);
    WriteParam(aWriter, aParam.mQPMax);
  }

  static bool Read(MessageReader* aReader, paramType* aResult) {
    if (ReadParam(aReader, &(aResult->mWidth)) &&
        ReadParam(aReader, &(aResult->mHeight)) &&
        ReadParam(aReader, &(aResult->mNumberOfTemporalLayers)) &&
        ReadParam(aReader, &(aResult->mMaxBitrate)) &&
        ReadParam(aReader, &(aResult->mTargetBitrate)) &&
        ReadParam(aReader, &(aResult->mMinBitrate)) &&
        ReadParam(aReader, &(aResult->mQPMax))) {
      return true;
    }
    return false;
  }
};

template <>
struct ParamTraits<GMPVideoCodec> {
  typedef GMPVideoCodec paramType;

  static void Write(MessageWriter* aWriter, const paramType& aParam) {
    WriteParam(aWriter, aParam.mGMPApiVersion);
    WriteParam(aWriter, aParam.mCodecType);
    WriteParam(aWriter, static_cast<const nsCString&>(
                            nsDependentCString(aParam.mPLName)));
    WriteParam(aWriter, aParam.mPLType);
    WriteParam(aWriter, aParam.mWidth);
    WriteParam(aWriter, aParam.mHeight);
    WriteParam(aWriter, aParam.mStartBitrate);
    WriteParam(aWriter, aParam.mMaxBitrate);
    WriteParam(aWriter, aParam.mMinBitrate);
    WriteParam(aWriter, aParam.mMaxFramerate);
    WriteParam(aWriter, aParam.mFrameDroppingOn);
    WriteParam(aWriter, aParam.mKeyFrameInterval);
    WriteParam(aWriter, aParam.mQPMax);
    WriteParam(aWriter, aParam.mNumberOfSimulcastStreams);
    for (uint32_t i = 0; i < aParam.mNumberOfSimulcastStreams; i++) {
      WriteParam(aWriter, aParam.mSimulcastStream[i]);
    }
    WriteParam(aWriter, aParam.mMode);
  }

  static bool Read(MessageReader* aReader, paramType* aResult) {
    // NOTE: make sure this matches any versions supported
    if (!ReadParam(aReader, &(aResult->mGMPApiVersion)) ||
        aResult->mGMPApiVersion != kGMPVersion33) {
      return false;
    }
    if (!ReadParam(aReader, &(aResult->mCodecType))) {
      return false;
    }

    nsAutoCString plName;
    if (!ReadParam(aReader, &plName) ||
        plName.Length() > kGMPPayloadNameSize - 1) {
      return false;
    }
    memcpy(aResult->mPLName, plName.get(), plName.Length());
    memset(aResult->mPLName + plName.Length(), 0,
           kGMPPayloadNameSize - plName.Length());

    if (!ReadParam(aReader, &(aResult->mPLType)) ||
        !ReadParam(aReader, &(aResult->mWidth)) ||
        !ReadParam(aReader, &(aResult->mHeight)) ||
        !ReadParam(aReader, &(aResult->mStartBitrate)) ||
        !ReadParam(aReader, &(aResult->mMaxBitrate)) ||
        !ReadParam(aReader, &(aResult->mMinBitrate)) ||
        !ReadParam(aReader, &(aResult->mMaxFramerate)) ||
        !ReadParam(aReader, &(aResult->mFrameDroppingOn)) ||
        !ReadParam(aReader, &(aResult->mKeyFrameInterval))) {
      return false;
    }

    if (!ReadParam(aReader, &(aResult->mQPMax)) ||
        !ReadParam(aReader, &(aResult->mNumberOfSimulcastStreams))) {
      return false;
    }

    if (aResult->mNumberOfSimulcastStreams > kGMPMaxSimulcastStreams) {
      return false;
    }

    for (uint32_t i = 0; i < aResult->mNumberOfSimulcastStreams; i++) {
      if (!ReadParam(aReader, &(aResult->mSimulcastStream[i]))) {
        return false;
      }
    }

    if (!ReadParam(aReader, &(aResult->mMode))) {
      return false;
    }

    return true;
  }
};

}  // namespace IPC

#endif  // GMPMessageUtils_h_