gusucode.com > audiovideo工具箱matlab源码程序 > audiovideo/+audiovideo/+internal/+writer/+plugin/MP4MediaFoundationPlugin.m

    classdef (Hidden) MP4MediaFoundationPlugin < audiovideo.internal.writer.plugin.IPlugin
    %MP4MediaFoundationPlugin Extension of the IPlugin class to write MPEG-4 H.264 Files
    
    % Copyright 2011-2016 The MathWorks, Inc.
    
    properties
        ColorFormat = 'RGB24';
        ColorChannels = 3;
        BitsPerPixel = 24;
    end
    
    properties(Access=protected)
        FileName;
        CustomEventListener;
    end
    
    methods
        function obj = MP4MediaFoundationPlugin(fileName)
            %MP4MediaFoundationPlugin Construct a MP4MediaFoundationPlugin object.
            %
            %   OBJ = MP4MediaFoundationPlugin(FILENAME) constructs a
            %   MP4MediaFoundationPlugin object pointing to the file
            %   specified by FILENAME.  The file is not created until
            %   MP4MediaFoundationPlugin.open() is called. 
            %
            %   See also MP4MediaFoundationPlugin/open, MP4MediaFoundationPlugin/close.
            
            obj = obj@audiovideo.internal.writer.plugin.IPlugin();
            
            % Handle the zero argument constructor.  This is needed, for
            % example, when constructing empty profile objects.
            if isempty(fileName)
                obj.Channel = [];
                return;
            end
            
            obj.FileName = fileName;
        end
        
        function set.FileName(obj, value)
            obj.FileName = value;
            
            % After setting the value, create the asyncio Channel object.
            % This is done here instead of in the constructor
            % so that the channel is initialized properly during load and
            % save
            obj.createChannel();
        end
        
        function open(obj, options)
            %OPEN Opens the channel for writing.
            %   MP4MediaFoundationPlugin objects must be open prior to calling
            %   writeVideoFrame.
                        
            assert(~isempty(obj.Channel), 'Channel must be set before opening the plugin');
            obj.open@audiovideo.internal.writer.plugin.IPlugin();
                     
            try
                obj.Channel.open(options);
            catch devErr
                obj.handleDeviceError(devErr.identifier, devErr.message);
            end
        end
        
        function close(obj)
            obj.close@audiovideo.internal.writer.plugin.IPlugin();
        end
        
        function writeVideoFrame(obj, data)
            %writeVideoFrame Write a single video frame to the channel.
            %   obj.writeVideoFrame(data) will write a single video frame
            %   to the channel. 
            
            assert(~isempty(obj.Channel), 'Channel must be set before writing to the plugin');
            assert(isnumeric(data),'Data to write must be numeric');
            
            try
                obj.Channel.OutputStream.write(data);
            catch devErr
                obj.handleDeviceError(devErr.identifier, devErr.message);
            end
        end
        
                
        function [pluginName, mlConverterName, slConverterName, options] = ...
                                                getChannelInitOptions(obj)
            %GETCHANNELINITOPTIONS options for asyncio channel creation
            %   Setup options used in createChannel function.
            %   Subclasses can override this function to provide custom
            %   plugins and options
            pluginName = 'videomfwriterplugin';
            [mlConverterName, slConverterName] = obj.getConverterName;
            options.OutputFileName = obj.FileName;
        end
       
        function [filterName, options] = getFilterInitOptions(obj)
            filterName = 'videotransformfilter';
            options.InputFrameType = 'RawPlanarColumn';
            if ~isempty(obj.Channel)
                options.OutputFrameType = obj.Channel.ExpectedFrameType;
            else
                options.OutputFrameType = '';
            end
        end

    end
    
   
    methods(Access=protected)
       
        function [mlErrorID, msgHoles] = deviceErrorToErrorID(obj, deviceErr, deviceMsg)
            %DEVICEERRORTOERRORID conversion from Device Error ID to MATLAB
            %Error ID
            %   Convert the Error ID generated by the Device Plugin to a
            %   MATLAB Error ID. Each plugin has knowledge about the
            %   specific errors that are thrown by the corresponding Device
            %   Plugin
            msgHoles = {};
            mlPrefix =  audiovideo.internal.writer.plugin.IPlugin.ErrorPrefix;
            switch(deviceErr)
                case 'mediafoundationplugin:invalidInputs'
                    mlErrorID = sprintf('%s:%s', mlPrefix, 'invalidInputs');
                case 'mediafoundationplugin:unexpectedError'
                    mlErrorID = sprintf('%s:%s', mlPrefix, 'unexpectedErrorWithReason');
                    msgHoles = {deviceMsg};
                case 'mediafoundationplugin:apiInitFailed'
                    mlErrorID = sprintf('%s:%s', mlPrefix, 'mediafoundationInitFailed');
                otherwise % unexpected error
                    mlErrorID = sprintf('%s:%s', mlPrefix, 'unexpectedError');
            end
        end
    end
end