=====================================================
The simplest example of a ffmpeg-based mobile Sample series article List:
The simplest mobile-based example of FFmpeg: Android HelloWorld
The simplest ffmpeg-based mobile sample: Android Video Decoder
Simplest FFmpeg-based mobile sample: Android Video Decoder-Single library version
Simplest FFmpeg-based mobile sample: Android push-to-stream
The simplest ffmpeg-based mobile sample: Android video transcoding
Simplest FFmpeg-based mobile sample attachment: Android comes with player
Simplest FFmpeg-based mobile sample attachment: SDL Android HelloWorld
The simplest ffmpeg-based mobile sample: IOS HelloWorld
The simplest ffmpeg-based mobile sample: IOS Video Decoder
Simplest FFmpeg-based mobile sample: IOS push-to-stream
The simplest ffmpeg-based mobile sample: IOS video transcoding
Simplest FFmpeg-based mobile sample attachment: iOS comes with player
Simplest FFmpeg-based mobile sample: Windows Phone HelloWorld
=====================================================
This document records the FFmpeg-based streaming device under the iOS platform.
The source code for the demo C language is from the simplest ffmpeg-based push-flow device.
The related concepts are no longer recorded repeatedly.
Source
The folder structure of the project is seen.
The C code is located in the Viewcontroller.m file, as seen in the following sections.
/** * Simplest FFmpeg based-ios * simplest FFmpeg IOS streamer * * Lei Huawei Lei Xiaohua * [email protected] * Communication University/Digital TV Technology * C Ommunication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program is a push-stream device under the iOS platform. It is able to push local files out in the form of streaming media. * * This software are the simplest streamer in IOS. * It can stream local media files to streaming media server. */#import "ViewController.h" #include <libavformat/avformat.h> #include <libavutil/mathematics.h># Include <libavutil/time.h> @interface Viewcontroller () @end @implementation viewcontroller-(void) Viewdidload {[ Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(Ibaction) Clickstreambutton: (ID) sender {char input_str_full[500]={0}; Char output_str_full[500]={0}; NSString *input_str= [NSString stringwithformat:@] Resource.bundle/%@ ", Self.input.text]; NSString *input_nsstr=[[[nsbundle Mainbundle]resourcepath] stringbyappendingpathcomponent:input_str]; sprintf (Input_str_full, "%s", [Input_nsstr utf8string]); sprintf (Output_str_full, "%s", [Self.output.text utf8string]); printf ("Input path:%s\n", input_str_full); printf ("Output path:%s\n", output_str_full); Avoutputformat *ofmt = NULL; Input Avformatcontext and Output avformatcontext avformatcontext *ifmt_ctx = null, *OFMT_CTX = NULL; Avpacket PKT; Char in_filename[500]={0}; Char out_filename[500]={0}; int ret, I; int videoindex=-1; int frame_index=0; int64_t start_time=0; In_filename = "Cuc_ieschool.mov"; In_filename = "cuc_ieschool.h264"; In_filename = "cuc_ieschool.flv";//input file URL//out_filename = "Rtmp://localhost/publishlive/livestream";//Outp UT url[rtmp]//out_filename = "rtp://233.233.233.233:6666";//output url[udp] strcpy (in_filename,input_str_full); strcpy (ouT_filename,output_str_full); Av_register_all (); Network Avformat_network_init (); Input if (ret = Avformat_open_input (&ifmt_ctx, in_filename, 0, 0)) < 0) {printf ("Could not open Inpu T file. "); Goto end; } if (ret = avformat_find_stream_info (ifmt_ctx, 0)) < 0) {printf ("Failed to retrieve input stream Informat Ion "); Goto end; } for (i=0; i<ifmt_ctx->nb_streams; i++) if (ifmt_ctx->streams[i]->codec->codec_type==avmedia_t Ype_video) {videoindex=i; Break } av_dump_format (Ifmt_ctx, 0, in_filename, 0); Output avformat_alloc_output_context2 (&ofmt_ctx, NULL, "flv", out_filename); RTMP//avformat_alloc_output_context2 (&ofmt_ctx, NULL, "Mpegts", out_filename);//udp if (!ofmt_ctx) { printf ("Could not create output context\n"); ret = Averror_unknown; Goto end; } ofmt = ofmt_ctx->oformat; for (i = 0; I < ifmt_ctx->nb_streams; i++) {Avstream *in_stream = ifmt_ctx->streams[i]; Avstream *out_stream = Avformat_new_stream (Ofmt_ctx, In_stream->codec->codec); if (!out_stream) {printf ("Failed Allocating output stream\n"); ret = Averror_unknown; Goto end; } ret = Avcodec_copy_context (Out_stream->codec, In_stream->codec); if (Ret < 0) {printf ("Failed to copy context from input to output stream codec context\n"); Goto end; } out_stream->codec->codec_tag = 0; if (Ofmt_ctx->oformat->flags & Avfmt_globalheader) out_stream->codec->flags |= CODEC_FLAG_GLOBA L_header; }//dump Format------------------av_dump_format (ofmt_ctx, 0, Out_filename, 1); Open output URL if (! ( Ofmt->flags & avfmt_nofile) {ret = Avio_open (&OFMT_CTX->PB, Out_filename, avio_flag_write);if (Ret < 0) {printf ("Could not open output URL '%s '", out_filename); Goto end; }} ret = Avformat_write_header (Ofmt_ctx, NULL); if (Ret < 0) {printf ("Error occurred when opening output url\n"); Goto end; } start_time=av_gettime (); while (1) {Avstream *in_stream, *out_stream; Get an avpacket ret = Av_read_frame (Ifmt_ctx, &PKT); if (Ret < 0) break; Fix:no pts (Example:raw)//simple Write pts if (pkt.pts==av_nopts_value) {//write pts Avrational time_base1=ifmt_ctx->streams[videoindex]->time_base; Duration between 2 frames (US) int64_t calc_duration= (double) av_time_base/av_q2d (ifmt_ctx->streams[videoin Dex]->r_frame_rate); Parameters pkt.pts= (Double) (frame_index*calc_duration)/(double) (av_q2d (TIME_BASE1) *av_time_base); pkt.dts=pkt.pts; Pkt.duration= (Double) calc_duration/(double) (av_q2d (TIME_BASE1) *av_time_base); }//important:delay if (pkt.stream_index==videoindex) {avrational time_base=ifmt_ctx->streams[v ideoindex]->time_base; Avrational Time_base_q={1,av_time_base}; int64_t pts_time = Av_rescale_q (Pkt.dts, Time_base, time_base_q); int64_t now_time = Av_gettime ()-start_time; if (Pts_time > Now_time) av_usleep (pts_time-now_time); } In_stream = ifmt_ctx->streams[pkt.stream_index]; Out_stream = ofmt_ctx->streams[pkt.stream_index]; /* Copy packet *///convert Pts/dts pkt.pts = Av_rescale_q_rnd (pkt.pts, In_stream->time_base, out_stream- >time_base, (av_round_near_inf| Av_round_pass_minmax)); Pkt.dts = Av_rescale_q_rnd (Pkt.dts, In_stream->time_base, Out_stream->time_base, (AV_ROUND_NEAR_INF| Av_round_pass_minmax)); Pkt.duration = AV_rescale_q (Pkt.duration, In_stream->time_base, out_stream->time_base); Pkt.pos =-1; Print to screens if (Pkt.stream_index==videoindex) {printf ("Send%8d video frames to output url\n", frame _index); frame_index++; }//ret = Av_write_frame (Ofmt_ctx, &PKT); ret = Av_interleaved_write_frame (Ofmt_ctx, &PKT); if (Ret < 0) {printf ("Error muxing packet\n"); Break } av_free_packet (&PKT); }//write the end of the document (write file trailer) Av_write_trailer (OFMT_CTX); End:avformat_close_input (&IFMT_CTX); /* Close Output */if (Ofmt_ctx &&!) Ofmt->flags & Avfmt_nofile)) Avio_close (OFMT_CTX->PB); Avformat_free_context (OFMT_CTX); if (Ret < 0 && ret! = averror_eof) {printf ("Error occurred.\n"); Return } return; } @end
Execution results
The results of the app's execution on the phone, for example, are seen. Clicking "Stream" pushes the "War3end.mp4" file located in Resource.bundle to the URL of "Rtmp://www.velab.com.cn/live/test".
Use the video player (where you use Ffplay) to view the live stream of the push, for example, as seen.
Download
simplest ffmpeg mobile
Project Home
Github:https://github.com/leixiaohua1020/simplest_ffmpeg_mobile
Open source China: https://git.oschina.net/leixiaohua1020/simplest_ffmpeg_mobile
sourceforge:https://sourceforge.net/projects/simplestffmpegmobile/
CSDN Project: http://download.csdn.net/detail/leixiaohua1020/8924391
This solution includes various examples of using FFMPEG to process multimedia on the mobile side:
[Android]
Simplest_android_player: Android interface-based video player
Simplest_ffmpeg_android_helloworld: FFmpeg-based HelloWorld program under Android platform
Simplest_ffmpeg_android_decoder: The simplest ffmpeg-based video decoder on the Android platform
Simplest_ffmpeg_android_decoder_onelib: The simplest ffmpeg-based video decoder on the Android Platform-Library edition
Simplest_ffmpeg_android_streamer: The simplest ffmpeg-based push-to-flow device under the Android platform
Simplest_ffmpeg_android_transcoder: FFmpeg command-line tool ported under Android platform
Simplest_sdl_android_helloworld: The simplest program for porting SDL to the Android platform
[IOS]
Simplest_ios_player: Video player based on iOS interface
HelloWorld program based on FFmpeg under the Simplest_ffmpeg_ios_helloworld:ios platform
The simplest ffmpeg-based video decoder under the Simplest_ffmpeg_ios_decoder:ios platform
The simplest ffmpeg based on the Simplest_ffmpeg_ios_streamer:ios platform
FFMPEG.C command-line tool ported under Simplest_ffmpeg_ios_transcoder:ios Platform
Simplest_sdl_ios_helloworld: The simplest program to migrate SDL to the iOS platform
Simplest FFmpeg-based mobile sample: IOS push-to-flow device