1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(MEDIA_STREAM) && PLATFORM(COCOA)
29
30#include "MediaSample.h"
31#include "RemoteVideoSample.h"
32#include <wtf/MachSendRight.h>
33#include <wtf/MediaTime.h>
34
35#if HAVE(IOSURFACE)
36#include "IOSurface.h"
37#endif
38
39namespace WebCore {
40
41class RemoteVideoSample {
42public:
43 RemoteVideoSample() = default;
44 RemoteVideoSample(RemoteVideoSample&&) = default;
45 RemoteVideoSample& operator=(RemoteVideoSample&&) = default;
46 ~RemoteVideoSample() = default;
47
48#if HAVE(IOSURFACE)
49 WEBCORE_EXPORT static std::unique_ptr<RemoteVideoSample> create(MediaSample&&);
50 WEBCORE_EXPORT IOSurfaceRef surface();
51#endif
52
53 const MediaTime& time() const { return m_time; }
54 uint32_t videoFormat() const { return m_videoFormat; }
55 IntSize size() const { return m_size; }
56
57 template<class Encoder> void encode(Encoder& encoder) const
58 {
59#if HAVE(IOSURFACE)
60 if (m_ioSurface)
61 encoder << m_ioSurface->createSendRight();
62 else
63 encoder << WTF::MachSendRight();
64#endif
65 encoder << m_rotation;
66 encoder << m_time;
67 encoder << m_videoFormat;
68 encoder << m_size;
69 encoder << m_mirrored;
70 }
71
72 template<class Decoder> static bool decode(Decoder& decoder, RemoteVideoSample& sample)
73 {
74#if HAVE(IOSURFACE)
75 MachSendRight sendRight;
76 if (!decoder.decode(sendRight))
77 return false;
78 sample.m_sendRight = WTFMove(sendRight);
79#endif
80 MediaSample::VideoRotation rotation;
81 if (!decoder.decode(rotation))
82 return false;
83 sample.m_rotation = rotation;
84
85 MediaTime time;
86 if (!decoder.decode(time))
87 return false;
88 sample.m_time = WTFMove(time);
89
90 uint32_t format;
91 if (!decoder.decode(format))
92 return false;
93 sample.m_videoFormat = format;
94
95 IntSize size;
96 if (!decoder.decode(size))
97 return false;
98 sample.m_size = WTFMove(size);
99
100 bool mirrored;
101 if (!decoder.decode(mirrored))
102 return false;
103 sample.m_mirrored = mirrored;
104
105 return true;
106 }
107
108private:
109
110#if HAVE(IOSURFACE)
111 RemoteVideoSample(IOSurfaceRef, CGColorSpaceRef, MediaTime&&, MediaSample::VideoRotation, bool);
112
113 std::unique_ptr<WebCore::IOSurface> m_ioSurface;
114 WTF::MachSendRight m_sendRight;
115#endif
116 MediaSample::VideoRotation m_rotation { MediaSample::VideoRotation::None };
117 MediaTime m_time;
118 uint32_t m_videoFormat { 0 };
119 IntSize m_size;
120 bool m_mirrored { false };
121};
122
123}
124
125#endif // ENABLE(MEDIA_STREAM)
126