FFMPEG Tips (4) How to interrupt a blocked network thread

Source: Internet
Author: User

This article is my "FFMPEG Tips" series of the fourth article, the previous article has mentioned how to extract the code stream information, how to read each frame of data, these are inseparable from the network operation, for example: using FFMPEG read a stream, the general code flow example is as follows:


Avformatcontext *ic = Avformat_alloc_context (); if (Avformat_open_input (&ic, URL, null, NULL) < 0) {return-1;} if (Avformat_find_stream_info (IC, NULL) < 0) {return-1;}    Avpacket Avpkt;av_init_packet (&AVPKT); while (!abort_request) {int ret = av_read_frame (IC, &AVPKT);    if (Ret < 0) {break; }//Processing}av_free_packet (&AVPKT);


which


-Avformat_open_input is primarily responsible for connecting to the media server and reading the header information of the bitstream

-Av_read_frame is primarily responsible for reading one frame of data at a time, including the Solution protocol and encapsulation


Both of these functions can have long-time or blocked situations, such as:


-The network sucks or it's not stable.

-server response is relatively slow

-Live stream does not exist or no data


Therefore, we need an interrupt mechanism that can easily interrupt the blocking process when it waits for a timeout or exits playback.


FFmpeg provides a very simple callback mechanism that registers a custom callback function for external interrupt blocking network operations, as follows:


static int custom_interrupt_callback (void *arg) {if (timeout | | abort_request) {return 1; } return 0;} Avformatcontext *ic = Avformat_alloc_context (); ic->interrupt_callback.callback = custom_interrupt_callback;ic- >interrupt_callback.opaque = Custom_arg;


When a custom callback function returns 1, an interrupt is generated. As a result, we can set timeout or abort_request as a way to interrupt the current network blocking process while waiting for a timeout or exiting the player.


The knowledge point of this article is so much, about how to interrupt the blocking of the network thread is introduced here, the article is not clear of the place welcome message or letter [email protected] exchange, follow my Sina Weibo @ Lu _ June or the public number @Jhuster get the latest articles and information.


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/8B/61/wKiom1hLaBOy4TYxAAAfD7Y8yQU171.jpg "title=" Weixin _jhuster.jpg "alt=" Wkiom1hlaboy4tyxaaafd7y8yqu171.jpg "/>

This article is from the "jhuster column" blog, be sure to keep this source http://ticktick.blog.51cto.com/823160/1881438

FFMPEG Tips (4) How to interrupt a blocked network thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.