FFMPEG 03

MAC 上添加开关控制 打开关闭 录音

#include "test.h"
#include 
#include 

static int record_status = 0;
#define ms_sleep 5
void mssleep(unsigned long ms);

void setRecordStatus(int status) {
    record_status = status;
}

void startRecord(void) {
    printf("开始录制");
    av_log_set_level(AV_LOG_DEBUG);
    initDevice();
}
void initDevice() {
    AVFormatContext *fmt_ctx = NULL;
    
    AVDictionary *options = NULL;
    
    int ret = 0;
    
    size_t errbuf_size = 1024;
    
    char errors[1024] = {0,};
    
    //[[video device]:[audio device]]
    char *deviceName = ":0";
        
    AVPacket pkt;

    /// 注册音视频
    avdevice_register_all();
    
    // getformat
    const AVInputFormat *iformat = av_find_input_format("avfoundation");
    
    // open device
    if ((ret = avformat_open_input(&fmt_ctx, deviceName, iformat, &options)) < 0) {
        
        av_strerror(ret, &errors, errbuf_size);
        
        printf(stderr,"Failed to open audio device,[%d] %s\n",ret,errors);
        
        avformat_close_input(&fmt_ctx);
        
        return;
    }

    /// 打开一个文件
    FILE *outFile = fopen("/Users/king/Desktop/ffmpeg/audio/o1.pcm", "wb+");
    
    if (outFile == NULL) {
        printf("文件创建失败");
        return;
    }
    record_status = 1;
    av_init_packet(&pkt);
    
    //read data from device
    while (((ret = av_read_frame(fmt_ctx, &pkt)) == 0 || ret == -35) && record_status) {
        
        if (ret == -35) {
            
            mssleep(ms_sleep);
            
            printf(stderr,"ret == -35 failed,[%d] %s\n",ret,errors);
            
            continue;
        }
        /// 写文件
        fwrite(pkt.data, pkt.size, 1, outFile);
        fflush(outFile);
        av_log(NULL,AV_LOG_INFO,"pkt size is  %d data is %p record_status = %d\n", pkt.size,pkt.data ,record_status);
        
        av_packet_unref(&pkt);
    }
    
    fclose(outFile);
    
    avformat_close_input(&fmt_ctx);
    
    av_log(NULL,AV_LOG_DEBUG,"record finish");
    //AVInputFormat
    //AVPacket 包
    /// 读取音视频API
    //av_read_frame(<#AVFormatContext *s#>, AVPacket *pkt) 0 成功 非0 失败
    //av_init_packet(<#AVPacket *pkt#>);
//    av_packet_unref(AVPacket *pkt)
//    av_packet_alloc()
//    av_packet_free(<#AVPacket **pkt#>)
}

你可能感兴趣的:(ffmpeg)