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

    classdef (Hidden) IPlugin  < handle
    %IPLUGIN Define the interface for VIDEOWRITER plugins
    %   Plugins are the utility used by VIDEOWRITER to connect the
    %   user-visible VIDEOWRITER object to the code that is responsible for
    %   actually writing data to the disk.
    %
    %   This is an internal class and is not intended for customer use.
    
    % Copyright 2009-2014 The MathWorks, Inc.
    
    properties (Abstract)
        ColorFormat; % The format in which data is written to the disk, e.g. RGB24
        ColorChannels; % The number of channels in the output.  Normally 1 or 3.
        BitsPerPixel; % The number of bits in an output pixel, e.g. 24 for RGB24 or 8 for MONO8.
    end
    
    properties(Dependent, Transient);
        BufferSize; % The size of the buffer for the channel.
    end
    
    properties(Access=protected, Transient)
        Channel; % The low-level API interface.
        FrameWrittenEventListener; % Listener for frame written events.
        IsOpen=false; % Indicate if the plugin is in the open state or not.
    end
    
    properties(Access=protected)
        PrivateBufferSize = 0;
    end
    
    properties(Constant)
        ErrorPrefix = 'MATLAB:audiovideo:VideoWriter';
	% Use toolboxdir() for proper paths in deployed mode.
        PluginPath = toolboxdir(fullfile('shared','multimedia','bin',...
            computer('arch'),'writer'));
    end
    
    methods
        function open(obj)
            obj.IsOpen = true;
        end
        
        function close(obj)
            % CLOSE Closes the channel for writing.
            %   It is an error to call writeVideoFrame after calling
            %   close().
            
            obj.IsOpen = false;

            if isempty(obj.Channel)
                return;
            end
            
            % Finish sending data.
            obj.Channel.OutputStream.drain();
            try
                obj.Channel.close();
            catch devErr
                obj.handleDeviceError(devErr.identifier, devErr.message);
            end
                
        end
        
        
        function set.BufferSize(obj, value)
            if obj.IsOpen
                error(message('MATLAB:audiovideo:VideoWriter:setBufferWhileOpen'));
            end
            
            obj.PrivateBufferSize = value;
            obj.createChannel();
        end
        
        function value = get.BufferSize(obj)
            value = obj.PrivateBufferSize;
        end
    end
    
    methods(Abstract)
        writeVideoFrame(obj);
    end
    
    events
        % Notification every time the plugin writes a frame to disk.
        FrameWrittenEvent;
    end
    
    methods
        function delete(obj)
            % Close the object and destroy the reference to the underlying
            % channel object.
            
            close(obj);
            delete(obj.Channel);
        end
    end
    
    methods (Abstract)
        % Asyncio Channel plugins and options Access
        [pluginName, converterName, options] = getChannelInitOptions(obj);
        [filterName, options] = getFilterInitOptions(obj);
    end
    
    methods (Abstract, Access=protected)
        % Function that translates the error ID generated by the Device
        % plugin to a MATLAB Error ID.
        [mlErrorID, msgHoles] = deviceErrorToErrorID(obj, deviceErr, deviceMsg);
    end
    
    methods (Access=protected)
        function createChannel(obj)
            %createChannel Create an asyncio Channel object
            pluginDir = obj.PluginPath;
            
            % Get plugin names and options
            % Note: getChannelInitOptions is implemented by a subclass
            [pluginName, converterName, ~, options] = getChannelInitOptions(obj);
            
            obj.Channel = asyncio.Channel(fullfile(pluginDir, pluginName),...
                fullfile(pluginDir, converterName),...
                options, [obj.BufferSize, obj.BufferSize]);
            
            % Get filter name and options
            % Note: createFilterOptions is implemented by a subclass
            [filterName, options] = getFilterInitOptions(obj);
            obj.Channel.OutputStream.addFilter(fullfile(pluginDir,filterName),...
                options);
            
            obj.Channel.OutputStream.Timeout = Inf;
            
            obj.FrameWrittenEventListener = event.listener(obj.Channel, 'Custom', ...
                @(source, data) obj.onFrameWritten(data));
        end
        
        function onFrameWritten(obj, data)
            % When the channel writes a frame, this listener handles it.
            % It is responsible for re-broadcasting the event so that the
            % profile can handle it.
            import audiovideo.internal.writer.plugin.FrameWrittenEventData;
            notify(obj, 'FrameWrittenEvent', ...
                FrameWrittenEventData(data.Data.width, ...
                data.Data.height, ...
                data.Data.bitsPerPixel, ...
                data.Data.colorChannels, ...
                data.Data.isSigned) );
        end
        
        function handleDeviceError(obj, deviceErr, deviceMsg)
            % Convert the Error ID thrown by the Device into a MATLAB
            % Error ID. This error ID will be stored in the Message Catalog
            % to allow translation.
                       
            [mlErrorID, msgHoles] = obj.deviceErrorToErrorID(deviceErr, deviceMsg);
            if isempty(msgHoles)
                msgObj = message(mlErrorID);
            else
                msgObj = message(mlErrorID, msgHoles{:});
            end
            error(msgObj);
        end
        
        function [mlConverterName, slConverterName] = getConverterName(obj)
            mlConverterName = 'videomlconverter';
            slConverterName = 'videoslconverter';
        end
    end
end