Disclosure of Invention
In order to overcome the problems in the related art, the present disclosure provides the following technical solutions:
according to a first aspect of the embodiments of the present disclosure, there is provided a method for playing a video in a webpage, the method including:
receiving encoded video data, and determining an encoding and decoding mode corresponding to the encoded video data;
if the coding and decoding mode is a coding and decoding mode which is not supported by a preset video module in the browser, decoding the coded video data through a decoder which is preset in other application programs except the browser to obtain a plurality of decoded video frames;
and drawing the video frame obtained by the decoding processing on a canvas of a webpage in the browser frame by frame.
Optionally, the browser supports an HTML5 technical framework, and the codec mode that is not supported by the preset video module in the browser is an h.265 codec mode.
Optionally, the drawing, frame by frame, the video frame obtained through the decoding process on a canvas of a webpage in the browser includes:
and calling a webGL interface, and drawing the video frames obtained through decoding processing on a canvas of a webpage in the browser frame by frame.
Optionally, the method further comprises:
acquiring a video frame rate;
determining the target number of video frames needing to be drawn in unit time length according to the video frame rate;
the drawing, frame by frame, a video frame obtained by a decoding process on a canvas of a webpage in the browser, including:
and drawing the target number of video frames obtained by decoding processing on a canvas of a webpage in the browser within the unit time length frame by frame.
Optionally, the decoding, by a decoder preset in another application program other than the browser, the encoded video data to obtain a plurality of decoded video frames includes:
decoding the encoded video data through a decoder preset in other application programs except the browser to obtain a plurality of decoded video frames and acquisition time points corresponding to each video frame;
the drawing, frame by frame, a video frame obtained by a decoding process on a canvas of a webpage in the browser, including:
acquiring video frames acquired through decoding processing one by one, determining the acquisition time point of the currently acquired video frame as the drawing starting time point of the currently acquired video frame, determining the acquisition time point of the first video frame acquired after the currently acquired video frame as the drawing ending time point of the currently acquired video frame, and drawing the currently acquired video frame on a canvas of a webpage in the browser based on the drawing starting time point and the drawing ending time point of the currently acquired video frame.
Optionally, the drawing the currently acquired video frame on a canvas of a webpage in the browser based on the starting drawing time point and the ending drawing time point of the currently acquired video frame includes:
when the drawing starting time point of the currently acquired video frame is reached, drawing the currently acquired video frame on a canvas of a webpage in the browser;
and when the end drawing time point of the currently acquired video frame is reached, removing the currently acquired video frame drawn on the canvas of the webpage in the browser.
According to a second aspect of the embodiments of the present disclosure, there is provided an apparatus for playing a video in a webpage, the apparatus including:
the determining module is used for receiving the coded video data and determining a coding and decoding mode corresponding to the coded video data;
the decoding module is used for decoding the coded video data through a decoder preset in other application programs except the browser to obtain a plurality of decoded video frames when the coding and decoding mode is a coding and decoding mode which is not supported by a video module preset in the browser;
and the drawing module is used for drawing the video frames obtained through the decoding processing on the canvas of the webpage in the browser frame by frame.
Optionally, the browser supports an HTML5 technical framework, and the codec mode that is not supported by the preset video module in the browser is an h.265 codec mode.
Optionally, the rendering module is configured to:
and calling a webGL interface, and drawing the video frames obtained through decoding processing on a canvas of a webpage in the browser frame by frame.
Optionally, the apparatus further comprises:
the acquisition module is used for acquiring a video frame rate;
the determining module is further configured to determine a target number of video frames that need to be drawn in a unit duration according to the video frame rate;
and the drawing module is used for drawing the video frames of the target number obtained by decoding processing on the canvas of the webpage in the browser within the unit time length frame by frame.
Optionally, the decoding module is configured to decode the encoded video data through a decoder preset in another application program other than the browser, so as to obtain a plurality of decoded video frames and a corresponding acquisition time point of each video frame;
the rendering module is configured to acquire video frames acquired through decoding one by one, determine a collection time point of a currently acquired video frame as a drawing start time point of the currently acquired video frame, determine a collection time point of a first video frame collected after the currently acquired video frame as a drawing end time point of the currently acquired video frame, and render the currently acquired video frame on a canvas of a webpage in the browser based on the drawing start time point and the drawing end time point of the currently acquired video frame.
Optionally, the rendering module is configured to:
when the drawing starting time point of the currently acquired video frame is reached, drawing the currently acquired video frame on a canvas of a webpage in the browser;
and when the end drawing time point of the currently acquired video frame is reached, removing the currently acquired video frame drawn on the canvas of the webpage in the browser.
According to a third aspect of the embodiments of the present disclosure, there is provided a terminal comprising a processor, a communication interface, a memory, and a communication bus, wherein:
the processor, the communication interface and the memory complete mutual communication through the communication bus;
the memory is used for storing a computer program;
the processor is used for executing the program stored in the memory so as to realize the method for playing the video in the webpage.
According to a fourth aspect of the embodiments of the present disclosure, a computer-readable storage medium is provided, in which a computer program is stored, and the computer program, when executed by a processor, implements the above method for playing a video in a webpage.
The technical scheme provided by the embodiment of the disclosure can have the following beneficial effects:
through the embodiment of the disclosure, if the live video data requested by the terminal from the server is not the video data coded in the h.264 coding and decoding mode, the coded video data is decoded by a decoder preset in other application programs except the browser, and after the decoding process is performed, the video frames obtained through the decoding process can be drawn on the canvas of the webpage in the browser frame by frame, so as to realize the effect of normally playing the live video data.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure.
Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, like numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the exemplary embodiments below are not intended to represent all implementations consistent with the present disclosure. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present disclosure, as detailed in the appended claims.
The embodiment of the disclosure provides a method for playing a video in a webpage, which can be realized by a terminal and can also be realized by the cooperation of a server. The terminal may be a personal computer or the like, among others. Fig. 1 is a schematic structural diagram of a system for playing a video in a webpage according to an embodiment of the present disclosure.
In the application, some users watch live broadcast through a mobile phone, and some users watch live broadcast through a personal computer. For the mode of watching the live broadcast through the personal computer, the user can select to download and install the live broadcast application program, and the live broadcast is watched on the personal computer through the live broadcast application program. However, some users consider the process of downloading and installing the live application to be cumbersome, so that the part of users choose to directly view the live application in the webpage through the browser. A video module (also referred to as a video tag) may be preset in the browser, and the video module may be configured to decode video data encoded in the h.264 codec mode, and may also be configured to play decoded live video data. However, because the installation space of the browser is limited, it is difficult to preset a plurality of video modules in the browser, so that different types of video modules decode video data encoded by different types of coding and decoding modes. Therefore, when the live video data is encoded in the h.265 encoding and decoding mode, the video module preset in the browser cannot normally decode the type of live video data, and thus cannot directly play the live video data in the web page in the browser. The method provided by the embodiment of the present disclosure solves the problem, and when the live video data is data encoded in an h.265 encoding/decoding mode, the live video data may also be directly played in a web page in a browser.
An exemplary embodiment of the present disclosure provides a method for playing a video in a webpage, as shown in fig. 2, a processing flow of the method may include the following steps:
step S201, receiving the encoded video data, and determining an encoding/decoding mode corresponding to the encoded video data.
In implementation, the encoded video data may be live video data or other types of data, which is described in this embodiment of the disclosure, and the other types of data are similar to the live video data, and are not described herein again. The server can receive live video data to be coded uploaded by the anchor terminal, can perform coding processing on the live video data to be coded, and can perform coding processing on the live video data to be coded by adopting different types of coding and decoding modes. The codec modes may include h.264, h.265, etc. modes. When a terminal requests live broadcast of video data, the server can send the encoded video data to the terminal requesting the live broadcast of the video data. Accordingly, the terminal can receive the encoded video data.
The encoded video data may also carry corresponding encoding and decoding mode information, and the terminal may determine the encoding and decoding mode corresponding to the encoded video data based on the encoding and decoding mode information. If the encoding and decoding mode corresponding to the encoded video data is determined to be the H.264 encoding and decoding mode, the terminal can decode the encoded video data through the video module, and can play the decoded live video data in a webpage of the browser.
Step S202, if the coding and decoding mode is a coding and decoding mode which is not supported by a video module preset in the browser, decoding the coded video data through a decoder preset in other application programs except the browser to obtain a plurality of decoded video frames.
In implementation, the codec mode that is not supported by the video module preset in the browser may be predetermined, the codec mode that is not supported by the video module preset in the browser is stored in the unsupported codec mode list, and if the codec mode corresponding to the encoded video data matches any codec mode in the unsupported codec mode list, the codec mode corresponding to the encoded video data is determined to be the codec mode that is not supported by the video module preset in the browser, and the encoded video data may be decoded by a decoder preset in another application program other than the browser. If the browser supports the HTML5 technical framework, the codec mode which is not supported by the preset video module in the browser is H.265 codec mode.
For example, a plurality of applications including a browser and an X video playback application are installed in a personal computer, and a plurality of decoders may be preset in the X video playback application, and the plurality of decoders may be used to perform decoding processing on video data encoded in different types of codec modes. The browser can call the X video playing application program to decode the video data coded by the coding and decoding mode which is not supported by the video module. For example, the browser may call a video playing application program X capable of decoding video data encoded in the h.265 codec mode. After the encoded video data is decoded by a decoder preset in an application program other than the browser, a plurality of decoded video frames can be obtained. The decoded video frames may be YUV (a color coding method, "Y" represents brightness (Luma) or gray scale value, and "U" and "V" represent Chroma (Chroma) and saturation, which are used to specify the color of the pixels) data.
Optionally, if the codec mode is a codec mode that is not supported by a video module preset in the browser, the decoding processing on the encoded video data by a decoder preset in another application program other than the browser to obtain a plurality of decoded video frames may include: if the coding and decoding mode is a coding and decoding mode which is not supported by a preset video module in the browser, and the total number of threads of a Central Processing Unit (CPU) of a terminal where the browser is located is greater than or equal to a preset threshold, decoding the encoded video data by using a decoder which is preset in other application programs except the browser, so as to obtain a plurality of decoded video frames. And if the total number of the CPU threads of the terminal where the browser is located is less than a preset threshold value, requesting the server to issue video data coded by a coding and decoding mode supported by a video module preset in the browser.
In implementation, the server may use different types of codec modes to encode the live video data to be encoded. In order to save data transmission bandwidth, the server can preferentially select to send video data coded by the H.265 coding and decoding mode to the terminal. However, video data encoded in the h.264 codec mode also exists in the server, so different types of video streams exist in the server, and when the terminal cannot normally receive or play the video data encoded in the h.265 codec mode, the server can switch to transmitting the video data encoded in the h.264 codec mode to the terminal.
For a terminal, in the process of decoding encoded video data, CPU resources of the terminal may be used, which may have a certain requirement on the performance of the CPU. If the performance of the CPU is too low, a video playing stuck phenomenon may occur during the decoding process. Therefore, before actually performing the decoding processing, it may be determined in advance whether the total number of the CPU threads is greater than or equal to a preset threshold, and if the total number of the CPU threads is greater than or equal to the preset threshold, it indicates that the performance of the CPU is high, and the process of performing the decoding processing may be adapted. For the browser, the operation parameters of the terminal which can be taken by the browser are limited, for example, the number of cores of a CPU (central processing unit) of the terminal, the model of the CPU, the number of idle threads of the CPU and the like which are difficult to be taken by the browser, but the total number of CPU threads of the terminal can be taken by the browser, so that the performance of the CPU can be judged according to the total number of the CPU threads.
In a possible implementation manner, if the codec mode is a codec mode that is not supported by a video module preset in the browser, and the total number of CPU threads of the terminal where the browser is located is greater than or equal to 4, decoding the encoded video data by using a decoder preset in an application program other than the browser, so as to obtain a plurality of decoded video frames. And if the total number of the CPU threads of the terminal where the browser is located is less than 4, requesting the server to issue video data coded by a coding and decoding mode supported by a video module preset in the browser.
In step S203, the video frame obtained by the decoding process is drawn on the canvas of the webpage in the browser frame by frame.
In implementation, the canvas may also be referred to as a canvas. If the encoded video data is the video data encoded by adopting the H.265 encoding and decoding mode, the video module cannot decode the encoded video data, so that an application program outside the browser is required to assist in decoding, and then a plurality of decoded video frames are returned to the browser. The browser can draw the video frame obtained by the decoding process in the in-browser webpage frame by HTML5 element such as canvas. The canvas has a width and height attribute for describing the size of the display area occupied in the webpage. When the video frame obtained through decoding processing is drawn on the canvas of the webpage in the browser frame by frame, the effect of playing the video frame can be realized, and thus the live video data can be normally watched in the webpage.
Optionally, the method provided by the embodiment of the present disclosure may further include: acquiring a video frame rate; determining the target number of video frames needing to be drawn in unit time length according to the video frame rate; step S203 may include: and drawing the target number of video frames obtained by decoding processing on the canvas of the webpage in the browser within a unit time length frame by frame.
In an implementation, the terminal may obtain a video frame rate, such as 60 frames per second. The target number of video frames that need to be rendered per unit time length may be determined from the video frame rate, for example, if the unit time length is second and the video frame rate is 60 frames per second, the target number of video frames that need to be rendered per unit time length is 60 frames.
Theoretically, the time intervals between every two adjacent video frames are all consistent, but due to network jitter and other reasons, if a packet loss phenomenon occurs, several video frames may be lost between some two adjacent video frames, and the time interval between the two adjacent video frames is relatively large. Furthermore, if a situation that several video frames may be lost between two adjacent video frames is ignored, and the video frames are played at a fixed time interval, a situation that sound and pictures are inconsistent may occur. For example, when 3 video frames in the middle of the first frame and the fifth frame are lost, and the time interval between every two normal adjacent video frames is 16ms, if the playing duration of the first frame is not extended, the fifth frame starts to be played at an interval of 16ms after the first frame is played, when the fifth frame is played, the sent audio may correspond to the picture of the second frame, and further, the sound and the picture may be inconsistent.
And decoding the encoded video data through a decoder preset in other application programs except the browser to obtain a plurality of decoded video frames and acquisition time points corresponding to each video frame. If the target number of the video frames needing to be drawn in unit time length is 60 frames, the terminal can firstly acquire 60 video frames which are not played currently and collected firstly, each video frame corresponds to a collection time point, the collection time point t1 of the first collected video frame in the 60 video frames can be determined, the collection time point t60 of the last collected video frame in the 60 video frames is determined, the time difference obtained by subtracting the collection time point t1 from the collection time point t60 is calculated, and if the time difference is equal to the unit time length, the 60 video frames are drawn on a canvas of a webpage in a browser in the unit time length frame by frame; if the time difference is larger than the unit duration, the number of the video frames is reduced from the last collected video frame in the 60 video frames one by one, the time difference between the new last collected video frame and the first collected video frame is recalculated, and when the recalculated time difference is smaller than or equal to the unit duration, the current remaining video frames are drawn on a canvas of a webpage in the browser within the unit duration frame by frame. The subtracted video frame may be processed at the next unit duration.
Optionally, step S202 may include: decoding the encoded video data through a decoder preset in other application programs except the browser to obtain a plurality of decoded video frames and acquisition time points corresponding to each video frame; step S203 may include: the method comprises the steps of acquiring video frames acquired through decoding processing one by one, determining the acquisition time point of the currently acquired video frame as the drawing starting time point of the currently acquired video frame, determining the acquisition time point of the first video frame acquired after the currently acquired video frame as the drawing ending time point of the currently acquired video frame, and drawing the currently acquired video frame on a canvas of a webpage in a browser based on the drawing starting time point and the drawing ending time point of the currently acquired video frame.
In implementation, after the encoded video data is decoded, in addition to obtaining a plurality of decoded video frames, a corresponding capture time point of each video frame may also be obtained. When the video frames obtained by the decoding processing are drawn, each video frame has a drawing start time point and a drawing end time point, and the corresponding video frame can be drawn at the drawing start time point and cleared at the drawing end time point. Before the video frame is drawn, the video frames obtained by the decoding process may be acquired one by one, the capture time point of the currently acquired video frame may be determined as a start drawing time point of the currently acquired video frame, and the capture time point of the first video frame captured after the currently acquired video frame may be determined as an end drawing time point of the currently acquired video frame.
For example, taking the first video frame as an example, if the capture time point of the first video frame is 0ms, and the capture time point of the second video frame is 16ms, then 0ms may be used as the start rendering time point of the first video frame, and 16ms may be used as the end rendering time point of the first video frame. The first video frame may be drawn on a canvas of a webpage in the browser based on a start drawing time point and an end drawing time point of the first video frame.
Optionally, the step of drawing the currently acquired video frame on the canvas of the webpage in the browser based on the start drawing time point and the end drawing time point of the currently acquired video frame may include: when the starting drawing time point of the currently acquired video frame is reached, drawing the currently acquired video frame on a canvas of a webpage in a browser; and when the end drawing time point of the currently acquired video frame is reached, clearing the currently acquired video frame drawn on the canvas of the webpage in the browser.
In an implementation, when video frames obtained by the decoding process are drawn, each video frame has a drawing start time point and a drawing end time point, and the corresponding video frame may be drawn at the drawing start time point and cleared at the drawing end time point.
For example, the drawing start time point and the drawing end time point of the first video frame are 0ms and 16ms in sequence, when the 0ms is reached, the first video frame may be drawn on the canvas of the webpage page in the browser, and after 16ms is passed and the 16ms is reached, the interface for clearing the canvas in the browser may be called to clear the first video frame drawn on the canvas of the webpage page in the browser. After the first video frame is cleared, the drawing of the second video frame may continue.
Alternatively, step S203 may include: and calling a webGL interface, and drawing the video frames obtained through the decoding processing on a canvas of a webpage page in the browser frame by frame.
In an implementation, the video frame may be drawn on the canvas of the webpage by software or hardware means. The drawing speed of drawing the video frame on the canvas of the webpage page in a hardware mode is higher. The terminal can call a webGL interface, the webGL drawing technology allows JavaScript and OpenGL ES 2.0 to be combined together, and the webGL interface can provide hardware accelerated rendering for HTML5 canvas by adding one JavaScript binding of OpenGL ES 2.0.
When the video frame is drawn, the encoded video data can be continuously received from the server, and the encoded video data can be decoded. Therefore, after the video frame obtained by decoding before is drawn, a new video frame obtained by decoding can be obtained, and the newly obtained video frame obtained by decoding can be continuously drawn by the method provided above, so that the effect of playing live video data is realized.
As shown in fig. 3, the web player may also be referred to as a video module, and the web player may process the encoded video data and audio data, respectively. For the encoded video data, the web player may determine the total number of CPU threads, and if the total number of CPU threads is greater than or equal to a preset threshold and the encoding and decoding mode of the encoded video data is the h.265 encoding and decoding mode, an external decoder is invoked to perform decoding processing on the encoded video data, where the encoded video data may be a file in an flv format, and YUV data may be obtained after the decoding processing. If the codec mode of the encoded video data is the h.264 encoding mode and the encoded video data can be a flv format file, the flv format file can be converted into an mp4 format file, and the mp4 format file can be played through a video module. Then, the webGL interface may be called, and finally the YUV data is rendered and drawn on a canvas. For the audio data, the audio data may be a flv format file, the flv (Flash video, streaming media) format file may be converted into an mp4(Moving Picture Experts Group 4) format file, and the mp4 format file may be played through a video module. A video frame drawn on a canvas by the above method may be as shown in fig. 4.
Through the embodiment of the disclosure, if the live video data requested by the terminal from the server is not the video data coded in the h.264 coding and decoding mode, the coded video data is decoded by a decoder preset in other application programs except the browser, and after the decoding process is performed, the video frames obtained through the decoding process can be drawn on the canvas of the webpage in the browser frame by frame, so as to realize the effect of normally playing the live video data.
Yet another exemplary embodiment of the present disclosure provides an apparatus for playing a video in a webpage, as shown in fig. 5, the apparatus including:
a determining module 501, configured to receive encoded video data and determine an encoding/decoding mode corresponding to the encoded video data;
a decoding module 502, configured to, when the coding and decoding mode is a coding and decoding mode that is not supported by a video module preset in a browser, perform decoding processing on the encoded video data through a decoder preset in another application program other than the browser, to obtain a plurality of decoded video frames;
a drawing module 503, configured to draw the video frame obtained through the decoding process on the canvas of the webpage in the browser frame by frame.
Optionally, the browser supports an HTML5 technical framework, and the codec mode that is not supported by the preset video module in the browser is an h.265 codec mode.
Optionally, the drawing module 503 is configured to:
and calling a webGL interface, and drawing the video frames obtained through decoding processing on a canvas of a webpage in the browser frame by frame.
Optionally, the apparatus further comprises:
the acquisition module is used for acquiring a video frame rate;
the determining module 501 is further configured to determine, according to the video frame rate, a target number of video frames that need to be drawn in a unit duration;
the drawing module 503 is configured to draw the target number of video frames obtained through the decoding processing on the canvas of the webpage in the browser frame by frame within the unit time length.
Optionally, the decoding module 502 is configured to decode the encoded video data through a decoder preset in another application program other than the browser, so as to obtain a plurality of decoded video frames and a corresponding acquisition time point of each video frame;
the drawing module 503 is configured to acquire video frames acquired through decoding one by one, determine a collection time point of a currently acquired video frame as a drawing start time point of the currently acquired video frame, determine a collection time point of a first video frame collected after the currently acquired video frame as a drawing end time point of the currently acquired video frame, and draw the currently acquired video frame on a canvas of a webpage in the browser based on the drawing start time point and the drawing end time point of the currently acquired video frame.
Optionally, the drawing module 503 is configured to:
when the drawing starting time point of the currently acquired video frame is reached, drawing the currently acquired video frame on a canvas of a webpage in the browser;
and when the end drawing time point of the currently acquired video frame is reached, removing the currently acquired video frame drawn on the canvas of the webpage in the browser.
With regard to the apparatus in the above-described embodiment, the specific manner in which each module performs the operation has been described in detail in the embodiment related to the method, and will not be elaborated here.
Through the embodiment of the disclosure, if the live video data requested by the terminal from the server is not the video data coded in the h.264 coding and decoding mode, the coded video data is decoded by a decoder preset in other application programs except the browser, and after the decoding process is performed, the video frames obtained through the decoding process can be drawn on the canvas of the webpage in the browser frame by frame, so as to realize the effect of normally playing the live video data.
It should be noted that: in the above embodiment, when the apparatus for playing a video in a web page plays a video in the web page, only the division of the functional modules is used for illustration, in practical applications, the function distribution may be completed by different functional modules according to needs, that is, the internal structure of the terminal is divided into different functional modules, so as to complete all or part of the above described functions. In addition, the apparatus for playing a video in a web page provided by the above embodiment and the method embodiment for playing a video in a web page belong to the same concept, and specific implementation processes thereof are detailed in the method embodiment and are not described herein again.
Fig. 6 shows a schematic structural diagram of a terminal 1900 provided in an exemplary embodiment of the present disclosure. The terminal 1900, which may vary significantly depending on configuration or performance, may include one or more processors (CPUs) 1910 and one or more memories 1920. The memory 1920 stores at least one instruction, which is loaded and executed by the processor 1910 to implement the method for playing video in a webpage according to the above embodiments.
Other embodiments of the disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the disclosure disclosed herein. This application is intended to cover any variations, uses, or adaptations of the disclosure following, in general, the principles of the disclosure and including such departures from the present disclosure as come within known or customary practice within the art to which the disclosure pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the disclosure being indicated by the following claims.
It will be understood that the present disclosure is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.