rtsp url

Programming 2016. 3. 27. 07:34

//16.3.27

Not working --------------------

rtsp://video2.earthcam.com/fecnetwork/4017timessquare.flv


Working ------------------

rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov  H.264  240*160

rtsp://85.255.175.244:554/h264     H.264 1920*1080

rtsp://q1604.dnsdojo.com:1935/live/sys2.stream    H.264  800*500

Posted by 세모아
,

AVPacket 해제에 대한 글


출처 :  http://stackoverflow.com/questions/26090992/ffmpeg-can-not-free-avpacket-when-decode-h264-stream





av_free_packet will clear your packet data, the same pointer that was allocated inside av_read_frame. But you changed it in packet.data += bytesDecoded; => crash.

Several advices:

  • No need to call av_init_packet if the first use of your packet is av_read_frame (it is done inside this function). But if you keep your code, you need it in order to initialize packet.size to 0 (tested, but not initialized the first time)

  • Call av_free_packet each time you are done with the packet data, only when the decode is successful. In your code, it means you must call it after avcodec_decode_video2, even if the frame is not finished.

  • Once your packed is decoded (i.e. avcodec_decode_video2 is ok, no matter if frameFinished is true or false), you can free it. No need to keep it and change data pointer. The process is "read packet, decode it, free it. read next packet, decode it, free it.". (Note that this does not apply to audio packets).

I suggest simplify your main loop by something like (read first, decode after):

while(true)
{
    // Step 1: Read packet
    while(true)
    {
        av_read_frame(pFormatCtx, &packet);

        // todo: Error handling

        if(packet.stream_index != videoStreamIndex)
        {
            av_free_packet(&packet);
        }
        else
        {
            break;
        }
    } while (packet.stream_index != videoStreamIndex);

    // Step 2/3: Decode and free
    avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
    av_free_packet(&packet);

    // todo: Error handling and checking frameFinished if needed
    // Of course, if you need to use the packet now, move the av_free_packet() after this
}


Posted by 세모아
,



할일: ManagedDecoder.cpp에서, decodingRTSP에서 2종류의 avpacket을 한번만 선언해서 사용하기




pkt을 한번만 선언해서 av_free_packet(.)해도 계속 사용하는 사례

2015년에 작성된 H264 codec test 샘플코드임.


https://ffmpeg.org/doxygen/2.8/api-h264-test_8c_source.html#l00032


106  i = 0;
107  av_init_packet(&pkt);
108  do {
109  if (!end_of_stream)
110  if (av_read_frame(fmt_ctx, &pkt) < 0)
111  end_of_stream = 1;
112  if (end_of_stream) {
113  pkt.data = NULL;
114  pkt.size = 0;
115  }
116  if (pkt.stream_index == video_stream || end_of_stream) {
117  got_frame = 0;
118  if (pkt.pts == AV_NOPTS_VALUE)
119  pkt.pts = pkt.dts = i;
120  result = avcodec_decode_video2(ctx, fr, &got_frame, &pkt);
121  if (result < 0) {
122  av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
123  return result;
124  }
125  if (got_frame) {
126  number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
127  (const uint8_t* const *)fr->data, (const int*) fr->linesize,
128  ctx->pix_fmt, ctx->width, ctx->height, 1);
129  if (number_of_written_bytes < 0) {
130  av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
131  return number_of_written_bytes;
132  }
133  printf("%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, 0x%08lx\n", video_stream,
135  number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes));
136  }
137  av_free_packet(&pkt);
138  av_init_packet(&pkt);
139  }
140  i++;
141  } while (!end_of_stream || got_frame);







https://ffmpeg.org/doxygen/2.8/decoding__encoding_8c_source.html#l00347



Posted by 세모아
,

출처: http://bluexmas.tistory.com/273



ffmpeg - rtmp sample 테스트

Programming/C++ 2012.05.25 17:19 Posted by 파란크리스마스

출처 : RTMPDump
Compiling RTMPdump on Max OS X
Compiling FFmpeg 0.9 with libRTMP and libmp3lame for Intel and ARM CPUs
Compiling FFMPEG 0.6 with RTMP Support for OSX 
ffmpeg ndk 빌드 #2 ffmpeg 옵션별 빌드
best-video-player
Fix rtmp double cflags in pkgconfig module

다운로드

RTMPDump 에서 rtmpdump-2.3.tgz 파일 다운로드

압축풀기

tar xvfz rtmpdump-2.3.gz

ffmpeg 폴더로 이동 시키기

mv rtmpdump-2.3/librtmp ffmpeg-0.10.3

rtmp 라이브러리 컴파일, 설치

cd ffmpeg-0.10.3/librtmp
make SYS=linux install
echo 'export PKG_CONFIG_PATH=/lib/pkgconfig:/usr/local/lib/pkgconfig' | tee -a ~/.bashrc

configure

config.sh 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export TMPDIR=c:/ffmpegtmp
 
./configure --disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-devices \
--disable-filters \
--disable-yasm \
--enable-network \
--enable-protocol=tcp \
--enable-demuxer=rtsp \
--enable-decoder=h264 \
--enable-librtmp

./config.sh

make

testmain.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
  
  
int main(int argc, char** argv) {
  
    AVFormatContext* context = avformat_alloc_context();
    int video_stream_index;
  
    av_register_all();
    avcodec_register_all();
    avformat_network_init();
 
    //if(avformat_open_input(&context, "rtsp://192.168.0.40/vod/mp4:sample.mp4",NULL,NULL) != 0){
    //if(avformat_open_input(&context, "rtmp://192.168.0.40/vod/sample.mp4",NULL,NULL) != 0){      
    //if(avformat_open_input(&context, "d:\\windows\\b.mp4",NULL,NULL) != 0){
  
    //open rtsp
    if(avformat_open_input(&context, "rtmp://192.168.0.40/vod/sample.mp4",NULL,NULL) != 0){        
        return EXIT_FAILURE;
    }
  
    if(avformat_find_stream_info(context,NULL) < 0){
        return EXIT_FAILURE;
    }
  
    //search video stream
    int i;
    for(i =0;i<context->nb_streams;i++){
        if(context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            video_stream_index = i;
    }
  
    AVPacket packet;
    av_init_packet(&packet);
  
    //open output file
    AVOutputFormat* fmt = av_guess_format(NULL,"test2.avi",NULL);
    AVFormatContext* oc = avformat_alloc_context();
    oc->oformat = fmt;
    avio_open2(&oc->pb, "test.avi", AVIO_FLAG_WRITE,NULL,NULL);
  
    AVStream* stream=NULL;
    int cnt = 0;
    //start reading packets from stream and write them to file
  
    av_read_play(context);//play RTSP
    while(av_read_frame(context,&packet)>=0 && cnt <100){//read 100 frames
        if(packet.stream_index == video_stream_index){//packet is video               
            if(stream == NULL){//create stream in file
                stream = avformat_new_stream(oc,context->streams[video_stream_index]->codec->codec);
                avcodec_copy_context(stream->codec,context->streams[video_stream_index]->codec);
                stream->sample_aspect_ratio = context->streams[video_stream_index]->codec->sample_aspect_ratio;
                avformat_write_header(oc,NULL);
            }
            packet.stream_index = stream->id;
  
            av_write_frame(oc,&packet);
            cnt++;
        }
        av_free_packet(&packet);
        av_init_packet(&packet);
    }
    av_read_pause(context);
    av_write_trailer(oc);
    avio_close(oc->pb);
    avformat_free_context(oc);
  
    return (EXIT_SUCCESS);
}

Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
TARGET = hello3
 
FFMPEGDIR = /home/bluesanta/ffmpeg-0.10.3
 
LIBDIR = /lib
 
CC  = gcc
AR  = ar
LD  = ld
NM  = nm
RANLIB  = ranlib
STRIP = strip
 
INCLUDE = -I.. -I.
 
CFLAGS = -Wall
LDFFMPEG = -L$(FFMPEGDIR)/libavformat -L$(FFMPEGDIR)/libavcodec -L$(FFMPEGDIR)/libavutil -L$(FFMPEGDIR)/librtmp
LDFLAGS = -L$(LIBDIR) $(LDFFMPEG) -lavformat -lavcodec -lavutil -lrtmp -lkernel32 -lcygwin -lm -lgcc -lc -lssl -lz -lcrypto
 
# application file
APPSOURCES = testmain.c
APPOBJS = $(APPSOURCES:.c=.o)
 
# define the rule
.SUFFIXES:.c .o
 
.c.o:
    @echo Compiling: $<
    $(CC) -c $(CFLAGS)  $(INCLUDE) -o $@ $<
 
all: app
 
app: $(APPOBJS)
    @echo Linking: $(TARGET)
    $(CC) -o $(TARGET) $(APPOBJS) $(LDFLAGS)
    $(STRIP) -s $(TARGET)
 
clean:
    @rm -vf $(APPOBJS) $(TARGET)

컴파일, 실행

 


'Programming > C++' 카테고리의 다른 글

av_free_packet 예제들  (0) 2016.03.27
[펌] How to: Debug from a DLL Project  (0) 2016.03.26
[펌] pragma에 관한 사용법  (0) 2016.03.07
Posted by 세모아
,