1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "framegrabber.hxx"
23 #include <tools/stream.hxx>
24 #include <vcl/graph.hxx>
25 #include <vcl/cvtgrf.hxx>
26 #include <unotools/localfilehelper.hxx>
28 using namespace ::com::sun::star;
30 namespace avmedia { namespace macavf {
32 FrameGrabber::FrameGrabber( const uno::Reference< lang::XMultiServiceFactory >& /*rxMgr*/ )
33 : mpImageGen( nullptr )
37 FrameGrabber::~FrameGrabber()
40 CFRelease( mpImageGen );
44 bool FrameGrabber::create( const ::rtl::OUString& rURL )
46 NSString* pNSStr = [NSString stringWithCharacters:reinterpret_cast<unichar const *>(rURL.getStr()) length:rURL.getLength()];
47 SAL_WNODEPRECATED_DECLARATIONS_PUSH
48 //TODO: 10.11 stringByAddingPercentEscapesUsingEncoding
49 NSURL* pNSURL = [NSURL URLWithString: [pNSStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
50 SAL_WNODEPRECATED_DECLARATIONS_POP
51 AVAsset* pMovie = [AVURLAsset URLAssetWithURL:pNSURL options:nil];
54 SAL_WARN("avmedia", "AVGrabber::create() cannot load url=" << [pNSStr UTF8String] );
58 return create( pMovie );
62 bool FrameGrabber::create( AVAsset* pMovie )
64 if( [[pMovie tracksWithMediaType:AVMediaTypeVideo] count] == 0)
66 SAL_WARN("avmedia", "AVGrabber::create() found no video content!" );
70 mpImageGen = [AVAssetImageGenerator assetImageGeneratorWithAsset:pMovie];
71 CFRetain( mpImageGen );
76 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
78 uno::Reference< graphic::XGraphic > xRet;
82 // get the requested image from the movie
83 CGImage* pCGImage = [mpImageGen copyCGImageAtTime:CMTimeMakeWithSeconds(fMediaTime,1000) actualTime:nullptr error:nullptr];
85 // convert the image to a TIFF-formatted byte-array
86 CFMutableDataRef pCFData = CFDataCreateMutable( kCFAllocatorDefault, 0 );
87 CGImageDestination* pCGImgDest = CGImageDestinationCreateWithData( pCFData, kUTTypeTIFF, 1, nullptr );
88 CGImageDestinationAddImage( pCGImgDest, pCGImage, nullptr );
89 CGImageDestinationFinalize( pCGImgDest );
90 CFRelease( pCGImgDest );
91 const long nBitmapLen = CFDataGetLength( pCFData );
92 UInt8 * pBitmapBytes = const_cast<UInt8 *>(CFDataGetBytePtr( pCFData ));
94 // convert the image into the return-value type which is a graphic::XGraphic
95 SvMemoryStream aMemStm( pBitmapBytes, nBitmapLen, StreamMode::READ | StreamMode::WRITE );
97 if( GraphicConverter::Import( aMemStm, aGraphic, ConvertDataFormat::TIF ) == ERRCODE_NONE )
98 xRet = aGraphic.GetXGraphic();
100 // clean up resources
101 CFRelease( pCFData );
106 ::rtl::OUString SAL_CALL FrameGrabber::getImplementationName( )
108 return ::rtl::OUString( AVMEDIA_MACAVF_FRAMEGRABBER_IMPLEMENTATIONNAME );
111 sal_Bool SAL_CALL FrameGrabber::supportsService( const ::rtl::OUString& ServiceName )
113 return ServiceName == AVMEDIA_MACAVF_FRAMEGRABBER_SERVICENAME;
116 uno::Sequence< ::rtl::OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
118 return { AVMEDIA_MACAVF_FRAMEGRABBER_SERVICENAME };
121 } // namespace macavf
122 } // namespace avmedia
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */