Skip to content

Instantly share code, notes, and snippets.

@wseemann
Last active May 22, 2017 20:51
Show Gist options
  • Save wseemann/b1694cbef5689ca2a4ded5064eb91750 to your computer and use it in GitHub Desktop.
Save wseemann/b1694cbef5689ca2a4ded5064eb91750 to your computer and use it in GitHub Desktop.
int set_data_source_fd(State **ps, int fd, int64_t offset, int64_t length) {
char path[256] = "";
State *state = *ps;
ANativeWindow *native_window = NULL;
if (state && state->native_window) {
native_window = state->native_window;
}
init(&state);
state->native_window = native_window;
int myfd = dup(fd);
char str[20];
sprintf(str, "pipe:%d", myfd);
strcat(path, str);
state->fd = myfd;
state->offset = offset;
*ps = state;
return set_data_source_l(ps, path);
}
int set_data_source_l(State **ps, const char* path) {
printf("set_data_source\n");
int audio_index = -1;
int video_index = -1;
int i;
State *state = *ps;
printf("Path: %s\n", path);
AVDictionary *options = NULL;
av_dict_set(&options, "icy", "1", 0);
av_dict_set(&options, "user-agent", "FFmpegMediaMetadataRetriever", 0);
if (state->headers) {
av_dict_set(&options, "headers", state->headers, 0);
}
if (state->offset > 0) {
state->pFormatCtx = avformat_alloc_context();
state->pFormatCtx->skip_initial_bytes = state->offset;
}
if (avformat_open_input(&state->pFormatCtx, path, NULL, &options) != 0) {
printf("Metadata could not be retrieved\n");
*ps = NULL;
return FAILURE;
}
if (avformat_find_stream_info(state->pFormatCtx, NULL) < 0) {
printf("Metadata could not be retrieved\n");
avformat_close_input(&state->pFormatCtx);
*ps = NULL;
return FAILURE;
}
set_duration(state->pFormatCtx);
set_shoutcast_metadata(state->pFormatCtx);
//av_dump_format(state->pFormatCtx, 0, path, 0);
// Find the first audio and video stream
for (i = 0; i < state->pFormatCtx->nb_streams; i++) {
if (state->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO && video_index < 0) {
video_index = i;
}
if (state->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && audio_index < 0) {
audio_index = i;
}
set_codec(state->pFormatCtx, i);
}
if (audio_index >= 0) {
stream_component_open(state, audio_index);
}
if (video_index >= 0) {
stream_component_open(state, video_index);
}
/*if(state->video_stream < 0 || state->audio_stream < 0) {
avformat_close_input(&state->pFormatCtx);
*ps = NULL;
return FAILURE;
}*/
set_rotation(state->pFormatCtx, state->audio_st, state->video_st);
set_framerate(state->pFormatCtx, state->audio_st, state->video_st);
set_filesize(state->pFormatCtx);
set_chapter_count(state->pFormatCtx);
set_video_dimensions(state->pFormatCtx, state->video_st);
/*printf("Found metadata\n");
AVDictionaryEntry *tag = NULL;
while ((tag = av_dict_get(state->pFormatCtx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
printf("Key %s: \n", tag->key);
printf("Value %s: \n", tag->value);
}*/
*ps = state;
return SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment