Created
May 31, 2019 00:27
-
-
Save meme/f42ad864229e917553331c50950b824b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
==23692== Bad permissions for mapped region at address 0x8615FF8 | |
==23692== at 0x483FAC8: memmove (vg_replace_strmem.c:1270) | |
==23692== by 0x2619A1: av_packet_copy_props (avpacket.c:593) | |
==23692== by 0x261FD2: av_packet_ref (avpacket.c:612) | |
==23692== by 0x290DF3: avcodec_send_packet (decode.c:693) | |
*/ | |
AVFormatContext* input_format_context; | |
AVCodecContext* avc_codec_context; | |
AVCodec* av_input_codec; | |
FILE* f = fopen("output.pcm", "wb"); | |
input_format_context = avformat_alloc_context(); | |
AVDictionary* hls_option_dict = nullptr; | |
av_dict_set_int(&hls_option_dict, "http_persistent", 0, 0); | |
if (avformat_open_input(&input_format_context, | |
"http://example.com/example.m3u8", | |
nullptr, &hls_option_dict) != 0) { | |
fprintf(stderr, "Failed to open stream\n"); | |
abort(); | |
} | |
if (avformat_find_stream_info(input_format_context, nullptr) < 0) { | |
fprintf(stderr, "Failed to find stream information\n"); | |
abort(); | |
} | |
av_input_codec = avcodec_find_decoder( | |
input_format_context->streams[0]->codecpar->codec_id); | |
assert(av_input_codec); | |
avc_codec_context = avcodec_alloc_context3(av_input_codec); | |
assert(avc_codec_context); | |
assert(!avcodec_parameters_to_context( | |
avc_codec_context, input_format_context->streams[0]->codecpar)); | |
assert(!avcodec_open2(avc_codec_context, av_input_codec, nullptr)); | |
const uint64_t k_out_channel_layout = AV_CH_LAYOUT_STEREO; | |
const AVSampleFormat k_out_sample_format = AV_SAMPLE_FMT_S16; | |
const int k_out_sample_rate = avc_codec_context->sample_rate; | |
const int k_out_channels = | |
av_get_channel_layout_nb_channels(k_out_channel_layout); | |
auto out_buffer_sz = (size_t) av_samples_get_buffer_size(nullptr, | |
k_out_channels, avc_codec_context->frame_size, k_out_sample_format, 1); | |
auto* out_buffer = (uint8_t*) av_malloc(MAX_AUDIO_FRAME_SIZE * 2); | |
// Resampler is not initialized with all information [?] | |
struct SwrContext* resampler_context; | |
resampler_context = swr_alloc(); | |
resampler_context = swr_alloc_set_opts(resampler_context, | |
k_out_channel_layout, k_out_sample_format, k_out_sample_rate, | |
av_get_default_channel_layout(avc_codec_context->channels), | |
avc_codec_context->sample_fmt, avc_codec_context->sample_rate, 0, | |
nullptr); | |
swr_init(resampler_context); | |
// Begin processing and conversion | |
int sample = 0; | |
AVPacket packet; | |
av_init_packet(&packet); | |
while (av_read_frame(input_format_context, &packet) >= 0) { | |
// Slow it down a bit | |
if (sample % 1000 == 0) { | |
sleep(3); | |
} | |
// Ensure the packet has encryption info attached to it | |
assert(packet.side_data_elems && | |
packet.side_data->type == AV_PKT_DATA_ENCRYPTION_INFO); | |
AVEncryptionInfo* av_encryption_info = av_encryption_info_get_side_data( | |
packet.side_data->data, static_cast<size_t>(packet.side_data->size)); | |
// <snipped> decrypting | |
// Assume decrypted_block_out has been initialized properly | |
auto decrypted_data_sz = decrypted_block_out.DecryptedBuffer()->Size(); | |
auto decrypted_data_buf = new uint8_t[decrypted_data_sz]; | |
memcpy(decrypted_data_buf, decrypted_block_out.DecryptedBuffer()->Data(), decrypted_data_sz); | |
AVPacket decrypted_packet; | |
av_packet_from_data(&decrypted_packet, | |
decrypted_data_buf, | |
decrypted_data_sz); | |
avcodec_send_packet(avc_codec_context, &decrypted_packet); | |
AVFrame* frame = av_frame_alloc(); | |
while (!avcodec_receive_frame(avc_codec_context, frame)) { | |
swr_convert(resampler_context, &out_buffer, MAX_AUDIO_FRAME_SIZE, | |
(const uint8_t**) &frame->data, frame->nb_samples); | |
printf("AVCODEC RECEIVE FRAME -- sample=%d,size=%d\n", sample, | |
packet.size); | |
fwrite(out_buffer, 1, out_buffer_sz, f); | |
sample++; | |
} | |
} | |
av_packet_unref(&packet); | |
swr_free(&resampler_context); | |
fclose(f); | |
av_free(out_buffer); | |
avcodec_close(avc_codec_context); | |
avformat_close_input(&input_format_context); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment