{"record":{"id":"9b5b022e7be11c21","repo":"stride3d/stride","slug":"media-is-already-open","errorCode":null,"errorMessage":"Media is already open.","messagePattern":"Media is already open\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Video/FFmpeg/FFmpegMedia.cs","lineNumber":126,"sourceCode":"        }\n\n        [CanBeNull]\n        public StreamInfo GetStreamInfo(VideoStream stream) => currentStreams.TryGetValue(stream, out var streamInfo) ? streamInfo : null;\n\n        /// <summary>\n        /// Opens this media.\n        /// </summary>\n        /// <remarks>\n        /// Once the media is open, the collection of <see cref=\"Streams\"/> is populated.\n        /// </remarks>\n        public void Open(string url, long startPosition = 0, long length = -1)\n        {\n            FFmpegUtils.EnsurePlatformSupport();\n            if (isDisposed)\n                throw new ObjectDisposedException(nameof(FFmpegMedia));\n            if (IsOpen)\n                // TODO: log?\n                throw new InvalidOperationException(@\"Media is already open.\");\n\n            if (startPosition != 0 && length != -1)\n                url = $@\"subfile,,start,{startPosition},end,{startPosition + length},,:{url}\";\n\n            var pFormatContext = ffmpeg.avformat_alloc_context();\n            var ret = ffmpeg.avformat_open_input(&pFormatContext, url, null, null);\n            if (ret < 0)\n            {\n                Logger.Error($\"Could not open file. Error code={ret.ToString(\"X8\")}\");\n                Logger.Error(GetErrorMessage(ret));\n                throw new ApplicationException(@\"Could not open file.\");\n            }\n\n            ret = ffmpeg.avformat_find_stream_info(pFormatContext, null);\n            if (ret < 0)\n            {\n                Logger.Error($\"Could not find stream info. Error code={ret.ToString(\"X8\")}\");\n                Logger.Error(GetErrorMessage(ret));","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Video/FFmpeg/FFmpegMedia.cs#L108-L144","documentation":"FFmpegMedia.Open throws this InvalidOperationException when Open is called on a media instance that is already open (IsOpen is true). An FFmpegMedia wraps one AVFormatContext, so it cannot open a second URL/stream without being closed first. The message is logged via the TODO-logged path and thrown to the caller.","triggerScenarios":"Calling Open (directly, or via CreateAssets / DoCommandOverride) twice on the same FFmpegMedia instance without calling Close/Dispose in between.","commonSituations":"Reusing a cached FFmpegMedia for a second playback; a load/asset-creation pipeline invoking CreateAssets on a media that was opened earlier; event handlers triggering Open again while the first open is still active.","solutions":["Call Close (or Dispose) on the FFmpegMedia before opening it again.","Check the IsOpen property before calling Open and skip or recreate the instance.","Create a new FFmpegMedia instance for each asset/source instead of reusing one."],"exampleFix":"// before\nif (media.IsOpen)\n    media.Open(url); // throws\n// after\nif (media.IsOpen)\n    media.Close();\nmedia.Open(url);","handlingStrategy":"validation","validationCode":"if (media.IsOpen) media.Close(); // or skip the Open call","typeGuard":"bool canOpen(FFmpegMedia m) => !m.IsOpen && !m.IsDisposed;","tryCatchPattern":"try\n{\n    media.Open(url);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already open\"))\n{\n    // reuse existing open media or close and retry\n}","preventionTips":["Check IsOpen before every Open call.","Keep one FFmpegMedia per source; create new instances for new assets.","Always Close/Dispose media in finally blocks.","Avoid calling Open re-entrantly from event handlers."],"tags":["ffmpeg","invalid-state","media-playback"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}