标签:
Step 1
1 @Override 2 protected SetDataSourceTaskResult doInBackground(Object... params) { 3 String url = (String) params[0]; 4 @SuppressWarnings("unchecked") 5 Map<String, String> map = (Map<String, String>) params[1]; 6 Integer videoStream = (Integer) params[2]; 7 Integer audioStream = (Integer) params[3]; 8 Integer subtitleStream = (Integer) params[4]; 9 10 int videoStreamNo = videoStream == null ? -1 : videoStream.intValue(); 11 int audioStreamNo = audioStream == null ? -1 : audioStream.intValue(); 12 int subtitleStreamNo = subtitleStream == null ? -1 : subtitleStream.intValue(); 13 14 int err = player.setDataSourceNative(url, map, videoStreamNo, audioStreamNo, subtitleStreamNo); 15 SetDataSourceTaskResult result = new SetDataSourceTaskResult(); 16 if (err < 0) { 17 result.error = new FFmpegError(err); 18 result.streams = null; 19 } else { 20 result.error = null; 21 result.streams = player.getStreamsInfo(); 22 } 23 return result; 24 }
Step 2
1 int jni_player_set_data_source(JNIEnv *env, jobject thiz, jstring string, 2 jobject dictionary, int video_stream_no, int audio_stream_no, 3 int subtitle_stream_no) { 4 5 AVDictionary *dict = NULL; 6 if (dictionary != NULL) { 7 jni_player_read_dictionary(env, &dict, dictionary); 8 (*env)->DeleteLocalRef(env, dictionary); 9 } 10 11 const char *file_path = (*env)->GetStringUTFChars(env, string, NULL); 12 struct Player * player = player_get_player_field(env, thiz); 13 struct State state = { player: player, env: env}; 14 15 int ret = player_set_data_source(&state, file_path, dict, video_stream_no, 16 audio_stream_no, subtitle_stream_no); 17 18 (*env)->ReleaseStringUTFChars(env, string, file_path); 19 return ret; 20 }
Step 3
1 int player_set_data_source(struct State *state, const char *file_path, 2 AVDictionary *dictionary, int video_stream_no, int audio_stream_no, 3 int subtitle_stream_no) { 4 struct Player *player = state->player; 5 int err = ERROR_NO_ERROR; 6 int i; 7 8 pthread_mutex_lock(&player->mutex_operation); 9 10 player_stop_without_lock(state); 11 12 if (player->playing) 13 goto end; 14 15 #ifdef SUBTITLES 16 char *font_path = NULL; 17 AVDictionaryEntry *entry = av_dict_get(dictionary, "ass_default_font_path", 18 NULL, 0); 19 20 if (entry != NULL) { 21 LOGI(3, "ass font found: %s", entry->value); 22 int length = strlen(entry->value); 23 font_path = malloc(sizeof(char) * (length + 1)); 24 if (font_path == NULL) { 25 err = ERROR_COULD_NOT_ALLOCATE_MEMORY; 26 goto error; 27 } 28 strcpy(font_path, entry->value); 29 font_path[length] = ‘\0‘; 30 } 31 #endif // SUBTITLES 32 LOGI(2,"solo -----> player_set_data_source -----> initial setup"); 33 // initial setup 34 player->pause = TRUE; 35 player->start_time = 0; 36 player->pause_time = 0; 37 38 // trying decode video 39 if ((err = player_create_context(player)) < 0) 40 goto error; 41 42 if ((err = player_create_interrupt_callback(player)) < 0) 43 goto error; 44 45 if ((err = player_open_input(player, file_path, dictionary)) < 0) 46 goto error; 47 48 if ((err = player_find_stream_info(player)) < 0) 49 goto error; 50 51 player_print_video_informations(player, file_path); 52 53 if ((err = player_print_report_video_streams(state->env, player->thiz, 54 player)) < 0) 55 goto error; 56 57 if ((player->video_stream_no = player_find_stream(player, 58 AVMEDIA_TYPE_VIDEO, video_stream_no)) < 0) { 59 err = player->video_stream_no; 60 goto error; 61 } 62 63 if ((player->audio_stream_no = player_find_stream(player, 64 AVMEDIA_TYPE_AUDIO, audio_stream_no)) < 0) { 65 err = player->audio_stream_no; 66 LOGW(3, "player_set_data_source, Can not find audio stream"); 67 player->no_audio = TRUE; 68 } else { 69 player->no_audio = FALSE; 70 } 71 #ifdef SUBTITLES 72 if (subtitle_stream_no == NO_STREAM) { 73 player->subtitle_stream_no = -1; 74 } else { 75 if ((player->subtitle_stream_no = player_find_stream(player, 76 AVMEDIA_TYPE_SUBTITLE, subtitle_stream_no)) < 0) { 77 // if no subtitles - just go without it 78 } 79 } 80 81 if ((player->subtitle_stream_no >= 0)) { 82 err = player_prepare_ass_decoder(player, font_path); 83 if (err < 0) 84 goto error; 85 } 86 #endif // SUBTITLES 87 if ((err = player_alloc_video_frames(player)) < 0) { 88 goto error; 89 } 90 91 if ((err = player_alloc_frames(player)) < 0) 92 goto error; 93 94 if ((err = player_alloc_queues(state)) < 0) 95 goto error; 96 97 struct DecoderState video_decoder_state = { stream_no 98 : player->video_stream_no, player: player, env:state->env}; 99 #ifdef SUBTITLES 100 if (player->subtitle_stream_no >= 0) { 101 struct DecoderState subtitle_decoder_state = { stream_no 102 : player->subtitle_stream_no, player: player, env:state->env}; 103 if ((err = player_prepare_subtitles_queue(&subtitle_decoder_state, 104 state)) < 0) 105 goto error; 106 } 107 #endif // SUBTITLES 108 if (player->no_audio == FALSE) { 109 if ((err = player_create_audio_track(player, state)) < 0) { 110 goto error; 111 } 112 } 113 114 player_get_video_duration(player); 115 player_update_time(state, FALSE); 116 117 player_play_prepare(player); 118 119 if ((err = player_start_decoding_threads(player)) < 0) { 120 goto error; 121 } 122 123 // SUCCESS 124 player->playing = TRUE; 125 LOGI(3, "player_set_data_source success"); 126 goto end; 127 128 error: 129 LOGI(3, "player_set_data_source error"); 130 131 player_play_prepare_free(player); 132 player_start_decoding_threads_free(player); 133 if (player->no_audio == FALSE) { 134 player_create_audio_track_free(player, state); 135 } 136 #ifdef SUBTITLES 137 player_prepare_subtitles_queue_free(state); 138 #endif // SUBTITLES 139 player_alloc_queues_free(state); 140 player_alloc_frames_free(player); 141 player_alloc_video_frames_free(player); 142 #ifdef SUBTITLES 143 player_prepare_ass_decoder_free(player); 144 #endif // SUBTITLES 145 player_print_report_video_streams_free(state->env, player->thiz, player); 146 player_find_streams_free(player); 147 player_find_stream_info_free(player); 148 player_open_input_free(player); 149 player_create_context_free(player); 150 #ifdef SUBTITLES 151 if (font_path != NULL) 152 free(font_path); 153 #endif // SUBTITLES 154 end: 155 LOGI(7, "player_set_data_source end"); 156 pthread_mutex_unlock(&player->mutex_operation); 157 return err; 158 }
标签:
原文地址:http://www.cnblogs.com/solo-heart/p/4607239.html