Example #1
0
        public static bool TryRegisterExtension(string extenstion, MediaFileStream implementation)
        {
            if (string.IsNullOrWhiteSpace(extenstion))
            {
                return(false);
            }

            if (extenstion[0] == (char)ASCII.Period)
            {
                extenstion = extenstion.Substring(1);
            }

            try
            {
                m_ExtensionMap.Add(extenstion, implementation);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #2
0
        public static MediaFileStream GetCompatbileImplementation(string fileName, AppDomain domain = null, System.IO.FileMode mode = System.IO.FileMode.Open, System.IO.FileAccess access = System.IO.FileAccess.ReadWrite)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            //Determine if to use Extension first?

            //object[] args = new object[] { fileName };

            System.IO.FileStream fs = new System.IO.FileStream(fileName, mode, access);

            object[] args = null;

            bool final = false;

            Action sharedFinalizer = null;

            sharedFinalizer = () =>
            {
                if (false == final)
                {
                    return;
                }

                if (fs != null)
                {
                    fs.Dispose();

                    fs = null;
                }

                if (args != null)
                {
                    args = null;
                }

                if (sharedFinalizer != null)
                {
                    sharedFinalizer = null;
                }
            };

            args = new object[] { fs, new Uri(fileName), access, false, sharedFinalizer };

            //Get all loaded assemblies in the current application domain
            foreach (var assembly in (domain ?? AppDomain.CurrentDomain).GetAssemblies())
            {
                //Iterate each derived type which is a SubClassOf RtcpPacket.
                foreach (var derivedType in assembly.GetTypes().Where(t => false == t.IsAbstract && t.IsSubclassOf(MediaFileStreamType)))
                {
                    //OrderBy where the Type.Extensions has an element which corresponds to the extension of the fileName.

                    //If the derivedType is an abstraction then add to the AbstractionBag and continue
                    //if (derivedType.IsAbstract) continue;

                    MediaFileStream created = (MediaFileStream)derivedType.GetConstructor(ConstructorTypes).Invoke(args);

                    using (var rootNode = created.Root)
                        if (rootNode != null && rootNode.IsComplete)
                        {
                            args = null;

                            //Ensure the finalizer can now run.
                            final = true;

                            return(created);
                        }

                    //The created MediaFileStream is useless for this type of file.
                    created.Dispose();

                    //Seek back
                    fs.Seek(0, System.IO.SeekOrigin.Begin);
                }
            }

            fs.Dispose();

            fs = null;

            args = null;

            return(null);
        }