Data writing method and device, computer device, and storage medium

CN123020730APending Publication Date: 2026-10-30SHENZHEN XIAOPAI TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610929025.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-06-25
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

在实际应用中,监控设备通常7x24小时持续录像,在长期高强度写入场景下,监控设备的存储介质通常会出行写入故障,导致录像失败

Benefits of technology

[0014]本发明提供一种数据写入方案,接收来自录像应用的数据帧,并对数据帧进行切片判断,根据判断结果确定是否需要新建录像文件;若确定需要新建录像文件或复用器为空,则在存储介质中创建新的录像文件作为目标录像文件;若确定不需要新建录像文件,则将存储介质中当前的录像文件作为目标录像文件;将数据帧写入目标录像文件,若写入失败,则循环重试至多N次,若仍写入失败,则转入异常处理流程;异常处理流程包括:若状态变量为第一变量值,则同步关闭目标录像文件、将复用器置空以及将状态变量更新为第二变量值,并指示录像应用继续发送数据帧后返回接收来自录像应用的数据帧的步骤;若状态变量为第二变量值,则同步关闭目标录像文件、将复用器置空以及将状态变量更新为第三变量值,收缩存储介质的可写入容量至已写入容量,并指示录像应用继续发送数据帧后返回接收来自录像应用的数据帧的步骤;若状态变量为第三变量值,则指示录像应用停止发送数据帧。以此,实现了的录像数据写入的智能容错与动态降级。其中,对于帧级异常,能够切换新的录像文件继续写入,使得损失控制在单帧以内,而对于介质级异常,则主动收缩容量边界,在良区内继续工作,最大化录像存续时间,从而综合提升录像文件写入的可靠性。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123020730A_ABST
    Figure CN123020730A_ABST
Patent Text Reader

Abstract

The application relates to the field of video recording technology, in particular to a data writing method and device, a computer device and a storage medium, wherein data frames from a video recording application are received, and it is judged whether a new video recording file needs to be created; if the new video recording file needs to be created or a multiplexer is empty, a new video recording file is created in a storage medium as a target video recording file; if the new video recording file does not need to be created, a current video recording file in the storage medium is taken as the target video recording file; the data frames are written into the target video recording file, if the writing fails, the cycle retry is performed at most N times, if the writing still fails, an abnormal processing procedure is entered, intelligent fault tolerance and dynamic degradation of video recording data writing are realized. For frame-level abnormality, a new video recording file can be switched to continue writing, so that the loss is controlled within a single frame; for medium-level abnormality, the capacity boundary is actively contracted, the work is continued in a good area, and the video recording survival time is maximized, so that the reliability of video recording file writing is comprehensively improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of video recording technology, and in particular to a data writing method, a data writing device, a computer device, and a computer-readable storage medium. Background Technology

[0002] Currently, in surveillance equipment, including network cameras, video files are written to the device's storage medium in a continuous, time-series manner. This storage medium can be built into the device or be a removable external storage medium. In practical applications, surveillance equipment typically records continuously 24 / 7. Under prolonged, high-intensity write operations, the storage medium often experiences write failures, leading to recording failures. Summary of the Invention

[0003] This invention provides a data writing method, a data writing device, a computer device, and a computer-readable storage medium, which can improve the reliability of video file writing.

[0004] In a first aspect, the data writing method provided by the present invention includes: Receive data frames from the recording application, slice the data frames, and determine whether a new recording file needs to be created based on the judgment result; If it is determined that a new recording file needs to be created or the multiplexer is empty, a new recording file is created in the storage medium as the target recording file; if it is determined that a new recording file does not need to be created, the current recording file in the storage medium is used as the target recording file. Write the data frame to the target recording file. If the writing fails, retry up to N times. If the writing still fails, proceed to the exception handling process. The exception handling process includes: If the status variable is the first variable value, then the target recording file is closed synchronously, the multiplexer is cleared, the status variable is updated to the second variable value, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the second variable value, then the target recording file is closed synchronously, the multiplexer is emptied, and the status variable is updated to the third variable value. The writable capacity of the storage medium is reduced to the already written capacity, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the value of the third variable, it instructs the recording application to stop sending data frames.

[0005] Secondly, the data writing device provided by the present invention includes: The slice recognition module is used to receive data frames from the recording application, perform slice judgment on the data frames, and determine whether a new recording file needs to be created based on the judgment result; The file determination module is used to create a new video file in the storage medium as the target video file when it is determined that a new video file needs to be created or the multiplexer is empty; or to use the current video file in the storage medium as the target video file when it is determined that a new video file does not need to be created. The data writing module is used to write data frames to the target video file. If the writing fails, it will retry up to N times. The exception handling module is used to execute the exception handling process when the data writing module fails to write after N retries: If the status variable is the first variable value, then the target recording file is closed synchronously, the multiplexer is cleared, the status variable is updated to the second variable value, and the recording application is instructed to continue sending data frames. If the status variable is the second variable value, the target recording file is closed synchronously, the multiplexer is emptied, the status variable is updated to the third variable value, the writable capacity of the storage medium is shrunk to the already written capacity, and the recording application is instructed to continue sending data frames. If the status variable is the value of the third variable, it instructs the recording application to stop sending data frames.

[0006] Optionally, in one embodiment, the data writing module is configured to retry up to N times in the current frame context.

[0007] Optionally, in one embodiment, the data writing device provided in this application further includes a first status notification module, which is used to return first status information to the recording application, the first status information being used to inform the recording application that the writable capacity of the storage medium has been reduced.

[0008] Optionally, in one embodiment, the data writing device provided in this application further includes a second status notification module, used to return second status information to the recording application, the second status information being used to inform the recording application that the storage medium is damaged.

[0009] Optionally, in one embodiment, the file determination module is further configured to retry up to M times when creating the target video file fails; The exception handling module is also used to execute the exception handling process when the creation of the anchor box fails after M retries.

[0010] Optionally, in one embodiment, the exception handling module is used to obtain the ratio of the written capacity of the storage medium to the writable capacity; if the ratio reaches a preset threshold, the writable capacity of the storage medium is reduced to the written capacity.

[0011] Optionally, in one embodiment, the exception handling module is further configured to instruct the recording application to stop sending data frames when the percentage does not reach a preset threshold.

[0012] Thirdly, the computer device provided by the present invention includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the data writing method provided by the present invention.

[0013] Fourthly, the computer-readable storage medium provided by the present invention stores a computer program, which, when executed by a processor, implements the data writing method provided by the present invention.

[0014] This invention provides a data writing scheme that receives data frames from a recording application, segments the data frames for judgment, and determines whether a new recording file needs to be created based on the judgment result. If it is determined that a new recording file needs to be created or the multiplexer is empty, a new recording file is created in the storage medium as the target recording file. If it is determined that a new recording file does not need to be created, the current recording file in the storage medium is used as the target recording file. The data frame is written to the target recording file. If the writing fails, it is retried up to N times. If the writing still fails, an exception handling process is initiated. The exception handling process includes: if the status variable is a first variable value, the target recording file is closed synchronously, the multiplexer is set to empty, and the status variable is updated to a second variable value. The recording application is then instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is a second variable value, the target recording file is closed synchronously, the multiplexer is set to empty, and the status variable is updated to a third variable value. The writable capacity of the storage medium is reduced to the already written capacity, and the recording application is then instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is a third variable value, the recording application is instructed to stop sending data frames. This enables intelligent fault tolerance and dynamic degradation in video data writing. Specifically, for frame-level anomalies, it can switch to a new video file to continue writing, keeping the loss within a single frame. For media-level anomalies, it actively shrinks the capacity boundary, continuing to work within the good range to maximize the recording duration, thereby comprehensively improving the reliability of video file writing. Attached Figure Description

[0015] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0016] Figure 1 This is a schematic diagram of an application scenario of the data writing method provided in an embodiment of the present invention; Figure 2 This is a flowchart illustrating the data writing method provided in an embodiment of the present invention; Figure 3This is a schematic diagram of the data writing device provided in an embodiment of the present invention; Figure 4 This is a schematic diagram of the structure of a computer device provided in an embodiment of the present invention. Detailed Implementation

[0017] To make the technical problems solved, the technical solutions, and the beneficial effects of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.

[0018] It should be understood that, when used in this specification and the appended claims, the term "comprising" indicates the presence of the described features, integrals, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.

[0019] It should also be understood that the term “and / or” as used in this specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, and includes such combinations.

[0020] As used in this specification and the appended claims, the term "if" may be interpreted, depending on the context, as "when," "once," "in response to determination," or "in response to detection." Similarly, the phrase "if determined" or "if [described condition or event] is detected" may be interpreted, depending on the context, as meaning "once determined," "in response to determination," "once [described condition or event] is detected," or "in response to detection of [described condition or event]."

[0021] Furthermore, in the description of this invention and the appended claims, the terms "first," "second," "third," etc., are used only for distinguishing descriptions and should not be construed as indicating or implying relative importance.

[0022] References to "one embodiment" or "some embodiments" as described in this specification mean that one or more embodiments of the invention include a specific feature, structure, or characteristic described in connection with that embodiment. Therefore, phrases such as "in one embodiment," "in some embodiments," "in other embodiments," "in still other embodiments," etc., appearing in different parts of this specification do not necessarily refer to the same embodiment, but rather mean "one or more, but not all, embodiments," unless otherwise specifically emphasized. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized.

[0023] Please refer to Figure 1 , Figure 1 This is a schematic diagram illustrating an application environment for the data writing method provided in this application. As one implementation, the data writing method provided in this application can be applied to a monitoring device, where the monitoring device and a terminal device are connected via a network. The monitoring device can capture audio and video in real time and continuously write the captured audio and video frames into a recording file on a storage medium. The terminal device can access the recording file in the monitoring device via the network to play back the recording. The network, used as a medium to provide a communication link between the monitoring device and the terminal device, can include various connection types, such as wired communication links, wireless communication links, etc., and this application embodiment does not limit this.

[0024] It should be noted that, Figure 1 The monitoring equipment, network, and terminal devices shown are merely illustrative. Depending on actual needs, any number of terminal devices can be included. For example, monitoring equipment can be embedded monitoring devices such as network cameras, while terminal devices can be smart terminal devices with video processing capabilities, such as smartphones, tablets, desktop computers, or laptops.

[0025] In some embodiments, the monitoring device can receive data frames from a recording application, segment the data frames for judgment, and determine whether a new recording file needs to be created based on the judgment result. If it is determined that a new recording file needs to be created or the multiplexer is empty, a new recording file is created in the storage medium as the target recording file. If it is determined that a new recording file does not need to be created, the current recording file in the storage medium is used as the target recording file. The data frame is written to the target recording file. If the writing fails, it is retried up to N times. If the writing still fails, it is transferred to the exception handling process. The exception handling process includes: if the status variable is the first variable value, the target recording file is closed synchronously, the multiplexer is empty, and the status variable is updated to the second variable value. The recording application is then instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the second variable value, the target recording file is closed synchronously, the multiplexer is empty, and the status variable is updated to the third variable value. The writable capacity of the storage medium is reduced to the already written capacity. The recording application is then instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the third variable value, the recording application is instructed to stop sending data frames.

[0026] Please refer to Figure 2 This is a flowchart illustrating a data writing method disclosed in an embodiment of the present invention, as shown below. Figure 2 As shown, the advantages of this data writing method are as follows: In S110, data frames from the recording application are received, and the data frames are sliced ​​for judgment. Based on the judgment result, it is determined whether a new recording file needs to be created.

[0027] It should be noted that the data writing method provided in this application can be executed on a monitoring device. Those skilled in the art should understand that a monitoring device can be any computer device capable of collecting audio and video data and executing recording file writing logic. For example, a monitoring device can be a network camera.

[0028] The video recording application is a program module in surveillance equipment responsible for business logic such as camera control, recording start / stop, and user interaction.

[0029] This application provides a recording engine that runs independently of the recording application and has program modules capable of real-time data frame parsing, intelligent slice analysis, dynamic file management, and self-recovery from abnormal states. The monitoring device implements the data writing method provided in this application based on the recording engine. The data writing method will be described in detail below.

[0030] The recording application continuously pushes data frames to the recording engine, which is responsible for writing the data frames into the recording file. These data frames can be either video or audio frames.

[0031] Upon receiving a data frame from the recording application, the system first triggers a segmentation decision logic. This logic determines whether the current data frame should be written to a new recording file or continue writing to the current recording file. The criteria for segmentation include, but are not limited to, whether the data frame is a keyframe and whether the data frame's timestamp crosses a preset segment duration. For example, a segment duration can be set to 30 seconds. If the difference between the timestamp of a new data frame and the start timestamp of the current recording file is 30 seconds or more, it is determined that a new recording file needs to be created.

[0032] In S120, if it is determined that a new video file needs to be created or the multiplexer is empty, a new video file is created in the storage medium as the target video file; if it is determined that a new video file does not need to be created, the current video file in the storage medium is used as the target video file.

[0033] The multiplexer is an intermediate component used to align audio and video frames by timestamp and encapsulate them into a specific container format (such as MP4 or AVI). After receiving video and audio frames, the multiplexer synchronously writes them to the corresponding track in the recording file. An empty multiplexer means that it has not been initialized or associated with any recording file. In this case, a new recording file must be created to ensure that audio and video frames can be correctly encapsulated.

[0034] Correspondingly, if the judgment result based on the slice determines that a new recording file needs to be created, or if the multiplexer is empty, a new recording file is created in the storage medium of the monitoring device as the target recording file for writing the above data frames. For example, the createFile() interface can be called to generate a recording file with a timestamp and device ID as the file name, initialize the multiplexer, and bind the recording file.

[0035] If it is determined from the slice judgment result that there is no need to create a new recording file, then the current recording file bound to the multiplexer is directly used as the target recording file for writing the above data frames.

[0036] In S130, the data frame is written to the target video file. If the writing fails, it is retried up to N times. If the writing still fails, the process proceeds to the exception handling procedure S140, which includes: If the status variable is the first variable value, then the target recording file is closed synchronously, the multiplexer is cleared, the status variable is updated to the second variable value, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the second variable value, then the target recording file is closed synchronously, the multiplexer is emptied, and the status variable is updated to the third variable value. The writable capacity of the storage medium is reduced to the already written capacity, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the value of the third variable, it instructs the recording application to stop sending data frames.

[0037] As described above, after determining the target file to be written, the system attempts to call the multiplexer's write interface (such as inputFrame()) to write the data frame to the target recording file. If successful, the internal state of the multiplexer is updated and timestamp synchronization is advanced. If the write fails, the system repeatedly calls the multiplexer's write interface, attempting to absorb the transient failure through intra-frame retries. If the system still fails after N retries, it is determined that the target recording file is corrupted, and an exception handling process is triggered. The value of N is not limited here; for example, in this embodiment, N can be set to 3 to balance write reliability and system response latency.

[0038] It should be noted that the exception handling process includes a three-level state progression, with each level corresponding to a different fault tolerance strategy: the first level focuses on rapid recovery, the second level emphasizes resource reclamation, and the third level executes forced termination; the three levels of states are progressively switched through atomic operations to ensure strong consistency and real-time performance of state transitions.

[0039] The system maintains a progressive three-level exception state through a state variable. The state variable defaults to the first variable value, which corresponds to a file-level exception. At this time, an atomic ternary operation is performed: synchronously closing the target recording file, setting the multiplexer to null, and incrementing the state variable. After the operation is completed, an instruction message is sent to the recording application to instruct it to continue sending data frames. Then, the system returns to the step of receiving data frames from the recording application.

[0040] Synchronously closing the target recording file refers to performing a blocking close through the underlying file system interface, ensuring that all cached data is written to disk and the handle is completely released; setting the multiplexer to null means clearing its internal buffer, resetting the timestamp synchronizer, and disconnecting the pointer associated with the target recording file; the incrementing status variable will be updated from the first variable value to the second variable value. In practical implementation, the atomicity of the operation can be ensured by using a mutex lock.

[0041] When the status variable is the second variable value, corresponding to a zone-level anomaly, the atomic ternary operation is also performed: synchronously closing the target recording file, emptying the multiplexer, and incrementing the status variable (at this time, the status variable will be updated from the second variable value to the third variable value). Unlike file corruption anomalies, the writable capacity of the storage medium is also shrunk to the already written capacity to avoid physical bad sectors. Furthermore, after completing the shrinking operation, an instruction message is sent to the recording application instructing it to continue sending data frames, and then the process returns to the step of receiving data frames from the recording application.

[0042] It should be noted that the core assumption of the circular overwrite model is that all locations within the interval [0, written capacity) have been successfully written to, proving that these locations are physically writable. However, the interval [written capacity, writable capacity) has never been successfully written to (the write pointer fails as soon as it reaches this interval), so bad blocks are highly likely to be within this interval. Shrinking the writable capacity of the storage medium to the written capacity is equivalent to cutting off bad areas from the writable interval, allowing the write pointer to only circulate within the verified writable good areas.

[0043] When the status variable is the third variable value, it corresponds to a media-level anomaly, which triggers a forced termination process: sending an instruction message to the recording application to stop sending data frames.

[0044] As can be seen from the above, the data writing scheme provided by this invention receives data frames from a recording application, performs segmentation judgment on the data frames, and determines whether a new recording file needs to be created based on the judgment result; if it is determined that a new recording file needs to be created or the multiplexer is empty, a new recording file is created in the storage medium as the target recording file; if it is determined that a new recording file does not need to be created, the current recording file in the storage medium is used as the target recording file; the data frame is written to the target recording file. If the writing fails, it is retried up to N times. If the writing still fails, it is transferred to the exception handling process; the exception handling process includes: if the state changes If the quantity is the first variable value, the target recording file is simultaneously closed, the multiplexer is reset, and the status variable is updated to the second variable value. The recording application is then instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the second variable value, the target recording file is simultaneously closed, the multiplexer is reset, and the status variable is updated to the third variable value. The writable capacity of the storage medium is reduced to the already written capacity, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the third variable value, the recording application is instructed to stop sending data frames. This achieves intelligent fault tolerance and dynamic degradation for recording data writing. Specifically, for file-level anomalies, a new recording file can be switched to continue writing, keeping the loss within a single frame. For zone-level anomalies, the capacity boundary is actively shrunk, continuing to work within good zones to maximize recording duration, thereby comprehensively improving the reliability of recording file writing.

[0045] Optionally, in one embodiment, retrying the loop up to N times includes: Retry up to N times within the current frame context.

[0046] To avoid excessive interference with the recording application due to retry failures, in this embodiment of the application, the recording application retryes up to N times in the current frame context. That is, the context is not switched and the data stream is not interrupted during the retry process, so as to ensure that the recording application is unaware of the write anomaly.

[0047] Optionally, in one embodiment, after shrinking the writable capacity of the storage medium to the already written capacity, the data writing method provided in this application further includes: Return first status information to the recording application, which informs the recording application that the writable capacity of the storage medium has been reduced.

[0048] In this embodiment, after shrinking the writable capacity of the storage medium to the already writable capacity, a first status information is returned to the recording application. This first status information is used to inform the recording application that the writable capacity of the storage medium has been shrunk. The method of returning the first status information is not limited here; it can be implemented through callback functions, event notifications, or status register updates. For example, the first status information can be passed to the recording application through a callback function. In specific implementations, the callback function can carry the current writable capacity and a status value representing the first status information, or it can carry only the status value.

[0049] On the other hand, after receiving the initial status information, the recording application can adjust its subsequent data transmission strategy, such as reducing the frame rate or switching encoding parameters, to adapt to the reduced writable capacity. Furthermore, the recording application can output a notification message indicating a decrease in the writable capacity of the storage medium, allowing users to promptly grasp the storage status and take appropriate actions, such as manually replacing the storage medium or backing up recording files. The presentation format of this notification message is not limited; it can be a screen pop-up, a flashing alarm light, a voice announcement, or an application push notification, all flexibly adapted to the actual device configuration. For example, a screen pop-up could display "Storage space is critically low, currently available capacity is only 'fill writable capacity here,' it is recommended to back up immediately or replace the memory card."

[0050] Optionally, in one embodiment, after instructing the recording application to stop sending data frames, the data writing method provided in this application further includes: Return a second status message to the recording application, which informs the recording application that the storage medium is damaged.

[0051] In this embodiment, after instructing the recording application to stop sending data frames, a second status information is also returned to the recording application. This second status information is used to inform the recording application that the storage medium is damaged. The method of returning the second status information is not limited here; it can be implemented through callback functions, event notifications, or status register updates. For example, the second status information can be passed to the recording application through a callback function. In specific implementations, the callback function can carry a status value representing the second status information.

[0052] On the other hand, upon receiving the second status information, the recording application can immediately terminate the data writing process. Furthermore, the recording application can save the currently cached data frames to a secure area, such as backing them up to a remote server via a cloud backup channel, ensuring that important data is not lost. Additionally, the recording application can trigger an alarm mechanism, outputting a message indicating that the storage media is damaged, facilitating timely replacement by the user. The presentation format of this message is not limited; it can be a screen pop-up, flashing alarm light, voice announcement, or application push notification, all flexibly adaptable to the actual device configuration. For example, a screen pop-up could simultaneously display "Storage media abnormal, please replace immediately."

[0053] Optionally, in one embodiment, after creating a new video file in the storage medium as the target video file, the data writing method provided in this application further includes: If creation fails, the process will retry up to M times. If creation still fails, the process will proceed to the step of returning a second status information to the recording application.

[0054] It is understandable that in practical applications, there are situations where the storage medium fails to create a new video file due to physical damage or file system abnormalities. Accordingly, this application embodiment provides an optional retry mechanism: when attempting to create a new video file in the storage medium as the target video file fails, a retry mechanism is initiated, retries up to M times in the current frame context. The retry interval can increase with the number of retries to avoid momentary interference. If all M attempts fail, the storage medium is determined to be damaged, and the process proceeds to returning a second status information to the recording application. The value of M is not specifically limited here and can be set according to device performance and storage medium type. For example, in this application embodiment, M can be set to 3 times to balance response efficiency and fault tolerance. The initial retry interval is 100 milliseconds, increasing by 50 milliseconds with each retrieval to avoid continuous retries increasing the burden on the media. Furthermore, how the second status information is returned to the recording application is not elaborated here; it can be executed through callback functions, event notifications, or status register updates, as described in the previous embodiments.

[0055] It should be noted that the creation of video files involves operations such as directory creation, inode allocation, and box header writing, which have a high probability of momentary failure. The above retry mechanism can effectively absorb most momentary anomalies and significantly improve the robustness of the system.

[0056] Optionally, in one embodiment, before shrinking the writable capacity of the storage medium to the already written capacity, the data writing method provided in this application further includes: Obtain the percentage of the written capacity to the writable capacity of the storage medium; If the percentage reaches a preset threshold, the writable capacity of the storage medium will be reduced to the already written capacity.

[0057] To prevent excessive shrinkage of the writable capacity of the storage medium due to a single failure in the early stages of recording, embodiments of this application further provide a protection mechanism for capacity shrinkage.

[0058] Specifically, when the shrinking of the writable capacity of the storage medium is triggered to the written capacity, the capacity shrinking operation is not performed immediately. Instead, the ratio of the current written capacity to the writable capacity of the storage medium is obtained first, and then it is determined whether the ratio has reached a preset threshold. If it has, the capacity shrinking operation is performed to shrink the writable capacity of the storage medium to the written capacity.

[0059] It should be noted that the value of the preset threshold is not limited in this application embodiment. It can be flexibly set according to the actual use scenario of the device and the characteristics of the storage medium. For example, in the security monitoring scenario, the threshold can be set to 30%, while in the vehicle recorder scenario, it can be set to 25% to adapt to the different needs for storage margin in different scenarios.

[0060] Optionally, in one embodiment, after obtaining the percentage of the written capacity of the storage medium relative to the writable capacity, the data writing method provided in this application further includes: If the percentage does not reach the preset threshold, the process will proceed to instruct the recording application to stop sending data frames.

[0061] In this embodiment, if the percentage does not reach the threshold, the contraction is temporarily suspended, and the recording application is instructed to stop sending data frames. On the other hand, the recording application ends the writing of the recording file and saves the currently cached data frames to a secure area, such as backing up the currently cached data frames to a remote server via a cloud backup channel to ensure that important data is not lost. Furthermore, the recording application can also trigger local log recording of abnormal events. In addition, the recording application can output prompts indicating abnormal storage media status, facilitating timely troubleshooting of hardware faults or the performance of formatting operations, etc.

[0062] To facilitate better implementation of the above data writing method, this embodiment of the invention also provides a corresponding data writing device. The meanings of the terms used are the same as in the above data writing method; for specific implementation details, please refer to the descriptions in the above method embodiments.

[0063] Please refer to Figure 3 The data writing device may include a slice recognition module 210, a file determination module 220, a data writing module 230, and an exception handling module 240. Detailed descriptions of each functional module are as follows: The slice recognition module 210 is used to receive data frames from the recording application, perform slice judgment on the data frames, and determine whether a new recording file needs to be created based on the judgment result; The file determination module 220 is used to create a new video file in the storage medium as the target video file when it is determined that a new video file needs to be created or the multiplexer is empty; or to use the current video file in the storage medium as the target video file when it is determined that a new video file does not need to be created. The data writing module 230 is used to write data frames to the target video file. If the writing fails, it will retry up to N times. The exception handling module 240 is used to execute the exception handling process when the data writing module fails to write after N retries: If the status variable is the first variable value, then the target recording file is closed synchronously, the multiplexer is cleared, the status variable is updated to the second variable value, and the recording application is instructed to continue sending data frames. If the status variable is the second variable value, the target recording file is closed synchronously, the multiplexer is emptied, the status variable is updated to the third variable value, the writable capacity of the storage medium is shrunk to the already written capacity, and the recording application is instructed to continue sending data frames. If the status variable is the value of the third variable, it instructs the recording application to stop sending data frames.

[0064] Optionally, in one embodiment, the data writing module 230 is configured to retry up to N times in the current frame context.

[0065] Optionally, in one embodiment, the data writing device provided in this application further includes a first status notification module, which is used to return first status information to the recording application, the first status information being used to inform the recording application that the writable capacity of the storage medium has been reduced.

[0066] Optionally, in one embodiment, the data writing device provided in this application further includes a second status notification module, used to return second status information to the recording application, the second status information being used to inform the recording application that the storage medium is damaged.

[0067] Optionally, in one embodiment, the file determination module 220 is further configured to retry up to M times when creating the target video file fails; The exception handling module is also used to execute the exception handling process when the creation of the anchor box fails after M retries.

[0068] Optionally, in one embodiment, the exception handling module 240 is used to obtain the ratio of the written capacity of the storage medium to the writable capacity; if the ratio reaches a preset threshold, the writable capacity of the storage medium is reduced to the written capacity.

[0069] Optionally, in one embodiment, the exception handling module 240 is further configured to instruct the recording application to stop sending data frames when the percentage does not reach a preset threshold.

[0070] For specific limitations regarding the data writing device, please refer to the limitations on the data writing method above, which will not be repeated here. Each module in the aforementioned data writing device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware or independently of the processor in the computer device, or stored in software in the memory of the computer device, so that the processor can call and execute the operations corresponding to each module.

[0071] In one embodiment, a computer device is provided, which may be a monitoring device, and its internal structure diagram may be as follows: Figure 4 As shown. The computer device includes a processor, memory, network interface, and database connected via a system bus. The processor provides computing and control capabilities. The memory includes a non-volatile storage medium and internal memory. The non-volatile storage medium stores an operating system, computer programs, and a database. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The network interface connects to external terminal devices, providing video file playback services for the connected terminal devices. When the computer program is executed by the processor, it implements the data writing method provided by this invention, for example: Receive data frames from the recording application, slice the data frames, and determine whether a new recording file needs to be created based on the judgment result; If it is determined that a new recording file needs to be created or the multiplexer is empty, a new recording file is created in the storage medium as the target recording file; if it is determined that a new recording file does not need to be created, the current recording file in the storage medium is used as the target recording file. Write the data frame to the target recording file. If the writing fails, retry up to N times. If the writing still fails, proceed to the exception handling process. The exception handling process includes: If the status variable is the first variable value, then the target recording file is closed synchronously, the multiplexer is cleared, the status variable is updated to the second variable value, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the second variable value, then the target recording file is closed synchronously, the multiplexer is emptied, and the status variable is updated to the third variable value. The writable capacity of the storage medium is reduced to the already written capacity, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is the value of the third variable, it instructs the recording application to stop sending data frames.

[0072] In one embodiment, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the data writing method described in the above embodiment.

[0073] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed by a processor, implements the data writing method described in the above embodiment.

[0074] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. This computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided by this invention can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0075] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is used as an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.

[0076] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included within the protection scope of the present invention.

Claims

1. A data writing method, characterized in that, include: Receive data frames from the recording application, perform segmentation judgment on the data frames, and determine whether a new recording file needs to be created based on the judgment result; If it is determined that a new video file needs to be created or the multiplexer is empty, a new video file is created in the storage medium as the target video file. If it is determined that no new video file is needed, then the current video file in the storage medium is used as the target video file; Write the data frame to the target video file. If writing fails, retry up to N times. If writing still fails, proceed to the exception handling process. The exception handling process includes: If the status variable is the first variable value, then the target recording file is closed synchronously, the multiplexer is cleared, and the status variable is updated to the second variable value. After instructing the recording application to continue sending data frames, the process returns to the step of receiving data frames from the recording application. If the status variable is the second variable value, then the target recording file is closed synchronously, the multiplexer is cleared, and the status variable is updated to the third variable value. The writable capacity of the storage medium is reduced to the already written capacity, and the recording application is instructed to continue sending data frames before returning to the step of receiving data frames from the recording application. If the status variable is a third variable value, then the recording application is instructed to stop sending data frames.

2. The data writing method according to claim 1, characterized in that, The loop retries up to N times include: Retry up to N times within the current frame context.

3. The data writing method according to claim 1, characterized in that, After shrinking the writable capacity of the storage medium to the already writable capacity, the data writing method further includes: The recording application is returned a first status information, which informs the recording application that the writable capacity of the storage medium has been reduced.

4. The data writing method according to claim 1, characterized in that, After instructing the recording application to stop sending data frames, the data writing method further includes: The recording application is returned a second status information, which informs the recording application that the storage medium is damaged.

5. The data writing method according to claim 4, characterized in that, After creating a new video file in the storage medium as the target video file, the process further includes: If creation fails, the process will retry up to M times. If creation still fails, the process will proceed to the step of returning the second status information to the recording application.

6. The data writing method according to claim 1, characterized in that, Before shrinking the writable capacity of the storage medium back to the already writable capacity, the method further includes: Obtain the percentage of the written capacity to the writable capacity of the storage medium; If the percentage reaches a preset threshold, the writable capacity of the storage medium is reduced to the already written capacity.

7. The data writing method according to claim 6, characterized in that, After obtaining the percentage of the written capacity to the writable capacity of the storage medium, the method further includes: If the percentage does not reach the preset threshold, then proceed to the step of instructing the recording application to stop sending data frames.

8. A data writing device, characterized in that, include: The slice recognition module is used to receive data frames from the recording application, perform slice judgment on the data frames, and determine whether a new recording file needs to be created based on the judgment result; The file determination module is used to create a new video file in the storage medium as the target video file when it is determined that a new video file needs to be created or the multiplexer is empty; Alternatively, if it is determined that no new video file needs to be created, the current video file in the storage medium can be used as the target video file; The data writing module is used to write the data frame into the target video file. If the writing fails, it will retry up to N times. The exception handling module is used to execute the exception handling procedure when the data writing module fails to write after N retries: If the status variable is the first variable value, then the target recording file is closed synchronously, the multiplexer is cleared, and the status variable is updated to the second variable value, and the recording application is instructed to continue sending data frames; If the status variable is the second variable value, then the target recording file is closed synchronously, the multiplexer is cleared, and the status variable is updated to the third variable value, the writable capacity of the storage medium is reduced to the already written capacity, and the recording application is instructed to continue sending data frames. If the status variable is a third variable value, then the recording application is instructed to stop sending data frames.

9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the data writing method according to any one of claims 1 to 7.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the data writing method according to any one of claims 1 to 7.