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
|
/*
* Copyright (C) 2018 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RemoteVideoSample.h"
#if ENABLE(MEDIA_STREAM) && PLATFORM(COCOA)
#include "GraphicsContextCG.h"
#include "IOSurface.h"
#include "Logging.h"
#include "MediaSampleAVFObjC.h"
#include "VideoFrameCV.h"
#if USE(ACCELERATE)
#include <Accelerate/Accelerate.h>
#endif
#include <pal/cf/CoreMediaSoftLink.h>
#include "CoreVideoSoftLink.h"
namespace WebCore {
static inline std::unique_ptr<IOSurface> transferBGRAPixelBufferToIOSurface(CVPixelBufferRef pixelBuffer)
{
#if USE(ACCELERATE)
ASSERT(CVPixelBufferGetPixelFormatType(pixelBuffer) == kCVPixelFormatType_32BGRA);
auto result = CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
ASSERT(result == kCVReturnSuccess);
if (result != kCVReturnSuccess) {
RELEASE_LOG_ERROR(Media, "transferBGRAPixelBufferToIOSurface CVPixelBufferLockBaseAddress() returned error code %d", result);
return nullptr;
}
IntSize size { static_cast<int>(CVPixelBufferGetWidth(pixelBuffer)), static_cast<int>(CVPixelBufferGetHeight(pixelBuffer)) };
auto ioSurface = IOSurface::create(size, DestinationColorSpace::SRGB(), IOSurface::Format::BGRA);
IOSurface::Locker lock(*ioSurface);
vImage_Buffer src;
src.width = size.width();
src.height = size.height();
src.rowBytes = CVPixelBufferGetBytesPerRow(pixelBuffer);
src.data = CVPixelBufferGetBaseAddress(pixelBuffer);
vImage_Buffer dest;
dest.width = size.width();
dest.height = size.height();
dest.rowBytes = ioSurface->bytesPerRow();
dest.data = lock.surfaceBaseAddress();
vImageUnpremultiplyData_BGRA8888(&src, &dest, kvImageNoFlags);
result = CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
ASSERT(result == kCVReturnSuccess);
if (result != kCVReturnSuccess) {
RELEASE_LOG_ERROR(Media, "transferBGRAPixelBufferToIOSurface CVPixelBufferUnlockBaseAddress() returned error code %d", result);
return nullptr;
}
return ioSurface;
#else
RELEASE_LOG_ERROR(Media, "transferBGRAPixelBufferToIOSurface cannot convert to IOSurface");
return nullptr;
#endif
}
std::unique_ptr<RemoteVideoSample> RemoteVideoSample::create(MediaSample& sample, ShouldCheckForIOSurface shouldCheckForIOSurface)
{
RetainPtr<CVPixelBufferRef> imageBuffer;
if (is<MediaSampleAVFObjC>(sample))
imageBuffer = downcast<MediaSampleAVFObjC>(sample).pixelBuffer();
else if (is<VideoFrameCV>(sample))
imageBuffer = downcast<VideoFrameCV>(sample).pixelBuffer();
else {
RELEASE_LOG_ERROR(Media, "RemoteVideoSample::create: cannot obtain CVPixelBuffer from sample. Unknown sample type.");
return nullptr;
}
if (!imageBuffer) {
RELEASE_LOG_ERROR(Media, "RemoteVideoSample::create: cannot obtain CVPixelBuffer from sample. No buffer.");
return nullptr;
}
std::unique_ptr<IOSurface> ioSurface;
auto surface = CVPixelBufferGetIOSurface(imageBuffer.get());
if (!surface && shouldCheckForIOSurface == ShouldCheckForIOSurface::Yes) {
// Special case for canvas data that is RGBA, not IOSurface backed.
auto pixelFormatType = CVPixelBufferGetPixelFormatType(imageBuffer.get());
if (pixelFormatType != kCVPixelFormatType_32BGRA) {
RELEASE_LOG_ERROR(Media, "RemoteVideoSample::create does not support non IOSurface backed samples that are not BGRA");
return nullptr;
}
ioSurface = transferBGRAPixelBufferToIOSurface(imageBuffer.get());
if (!ioSurface)
return nullptr;
surface = ioSurface->surface();
}
return std::unique_ptr<RemoteVideoSample>(new RemoteVideoSample(surface, WTFMove(imageBuffer), DestinationColorSpace::SRGB(), sample.presentationTime(), sample.videoRotation(), sample.videoMirrored()));
}
std::unique_ptr<RemoteVideoSample> RemoteVideoSample::create(RetainPtr<CVPixelBufferRef>&& imageBuffer, MediaTime&& presentationTime, MediaSample::VideoRotation rotation, ShouldCheckForIOSurface shouldCheckForIOSurface)
{
auto surface = CVPixelBufferGetIOSurface(imageBuffer.get());
if (!surface && shouldCheckForIOSurface == ShouldCheckForIOSurface::Yes) {
RELEASE_LOG_ERROR(Media, "RemoteVideoSample::create: CVPixelBufferGetIOSurface returned nullptr");
return nullptr;
}
return std::unique_ptr<RemoteVideoSample>(new RemoteVideoSample(surface, WTFMove(imageBuffer), DestinationColorSpace::SRGB(), WTFMove(presentationTime), rotation, false));
}
RemoteVideoSample::RemoteVideoSample(IOSurfaceRef surface, RetainPtr<CVPixelBufferRef>&& imageBuffer, const DestinationColorSpace& colorSpace, MediaTime&& time, MediaSample::VideoRotation rotation, bool mirrored)
: m_ioSurface(WebCore::IOSurface::createFromSurface(surface, colorSpace))
, m_imageBuffer(WTFMove(imageBuffer))
, m_rotation(rotation)
, m_time(WTFMove(time))
, m_videoFormat(IOSurfaceGetPixelFormat(surface))
, m_size(IntSize(IOSurfaceGetWidth(surface), IOSurfaceGetHeight(surface)))
, m_mirrored(mirrored)
{
}
IOSurfaceRef RemoteVideoSample::surface() const
{
if (!m_ioSurface && m_sendRight)
const_cast<RemoteVideoSample*>(this)->m_ioSurface = WebCore::IOSurface::createFromSendRight(WTFMove(const_cast<RemoteVideoSample*>(this)->m_sendRight), DestinationColorSpace::SRGB());
return m_ioSurface ? m_ioSurface->surface() : nullptr;
}
}
#endif // ENABLE(MEDIA_STREAM) && PLATFORM(COCOA)
|