/* Set for each worker the buffer slice to handle */ static int buff_init(uint32_t tid) { char *buff; off_t worker_end; if(fa_read_init()) return -1; worker_slice = file_size / nb_thread; worker_current = worker_slice * tid; /* Last thread handle remaining bytes */ if(tid == (nb_thread-1)) worker_slice += file_size % nb_thread; worker_end = worker_current + worker_slice; /* Balance worker_slice to include words at the ends */ /* skip first letters if we are not the first thread */ do { if(tid == 0) break; if(fa_read(tid, &buff, 1, worker_current) != 1) return -1; if(!IS_A_LETTER(*buff)) break; dmsg3("%d skipping letter %c\n", tid, *buff); worker_current++; worker_slice--; } while(*buff); /* add letters of the last word if we are not the last thread */ do { if(tid == (nb_thread-1)) break; if(fa_read(tid, &buff, 1, worker_end) != 1) return -1; if(!IS_A_LETTER(*buff)) break; dmsg3("%d adding letter %c\n", tid, *buff); worker_end++; worker_slice++; } while(*buff); dmsg2("%d: slice start %d, slice size %d, slice end %d\n", tid, worker_current, worker_slice, worker_end); return 0; }
/* Read a buffer from the file. */ static int buff_read(uint32_t tid, char **buff, off_t *size, char *last) { off_t size_read; if(!worker_slice) return 0; dmsg2("Worker %d, worker current %d, slice size %d\n", tid, worker_current, worker_slice); size_read = fa_read(tid, buff, worker_slice, worker_current); if(size_read == -1) return -1; *size = size_read; worker_current += size_read; worker_slice -= size_read; if(!worker_slice) *last = 1; dmsg2("Worker %d, size read %d, slice start %d, slice size %d\n", tid, size_read, worker_current, worker_slice); return 0; }
static int cmp_read(fa_handle_t *handle, void *buf, size_t size) { cmp_t *s = (cmp_t *)handle; int r1 = fa_read(s->s_src, buf, size); void *tmp = malloc(size); off_t pos = lseek(s->s_fd, 0, SEEK_CUR); int r2 = read(s->s_fd, tmp, size); if(r1 != r2) { TRACE(TRACE_ERROR, "FACMP", "read(%d) @ %"PRId64" failed fa:%"PRId64" local:%"PRId64, (int)size, pos, r1, r2); exit(1); } char *m1 = buf; char *m2 = tmp; int i; for(i = 0; i < r1; i++) { if(m1[i] != m2[i]) { TRACE(TRACE_ERROR, "FACMP", "Read(%d) mismatch @ %"PRId64" + %d got %02x expected %02x", (int)size, pos, i, m1[i], m2[i]); exit(1); } } return r1; }
void load_syms(void) { char sympath[256]; char errbuf[256]; snprintf(sympath, sizeof(sympath), "%s/showtime.syms", showtime_dataroot()); my_trace("sympath: %s\n", sympath); fa_handle_t *fh = fa_open(sympath, errbuf, sizeof(errbuf)); if(fh == NULL) { my_trace("Unable to open symbol file %s -- %s", sympath, errbuf); return; } int size = fa_fsize(fh); char *buf = halloc(size + 1); int r = fa_read(fh, buf, size); if(r != size) { my_trace("Unable to read %d bytes", size); hfree(buf, size+1); } else { buf[size] = 0; my_trace("Loaded symbol table %d bytes to %p", size, buf); symbuf = buf; } fa_close(fh); }
/** * fd, buffer, offset, length, position */ static int es_file_read(duk_context *ctx) { es_fd_t *efd = es_fd_get(ctx, 0); duk_size_t bufsize; char *buf = duk_require_buffer_data(ctx, 1, &bufsize); const int offset = duk_to_int(ctx, 2); const int len = duk_to_int(ctx, 3); if(offset + len > bufsize) duk_error(ctx, DUK_ERR_ERROR, "Buffer too small %zd < %d + %d", bufsize, offset + len); if(!duk_is_null(ctx, 4)) { // Seek fa_seek(efd->efd_fh, duk_require_number(ctx, 4), SEEK_SET); } int r = fa_read(efd->efd_fh, buf + offset, len); if(r < 0) duk_error(ctx, DUK_ERR_ERROR, "Read error from '%s'", efd->efd_path); duk_push_int(ctx, r); return 1; }
static int jpeginfo_reader(void *handle, void *buf, off_t offset, size_t size) { if(fa_seek(handle, offset, SEEK_SET) != offset) return -1; return fa_read(handle, buf, size); }
/** * Check if file is an iso image * pb is guaranteed to point at 64k of data */ int fa_probe_iso(metadata_t *md, fa_handle_t *fh) { uint8_t pb[128]; if(fa_seek(fh, 0x8000, SEEK_SET) != 0x8000) return -1; if(fa_read(fh, pb, sizeof(pb)) != sizeof(pb)) return -1; return fa_probe_iso0(md, pb); }
static int sidfile_scandir(fa_dir_t *fd, const char *url, char *errbuf, size_t errlen) { void *fh = NULL; char *p, *fpath = mystrdupa(url); char buf[128]; char name[32]; char turl[URL_MAX]; int tracks, i; fa_dir_entry_t *fde; rstr_t *album, *artist; if((p = strrchr(fpath, '/')) == NULL) { snprintf(errbuf, errlen, "Invalid filename"); return -1; } *p = 0; if((fh = fa_open(fpath, errbuf, errlen)) == NULL) return -1; if(fa_read(fh, buf, 128) != 128) { snprintf(errbuf, errlen, "Unable to read file"); fa_close(fh); return -1; } album = rstr_alloc(utf8_from_bytes((char *)buf + 0x16, 32, NULL)); artist = rstr_alloc(utf8_from_bytes((char *)buf + 0x36, 32, NULL)); tracks = buf[0xf]; for(i = 0; i < tracks; i++) { snprintf(name, sizeof(name), "Track %02d", i + 1); snprintf(turl, sizeof(turl), "sidplayer:%s/%d", fpath, i + 1); fde = fa_dir_add(fd, turl, name, CONTENT_AUDIO); fde->fde_probestatus = FDE_PROBE_DEEP; fde->fde_metadata = prop_create_root("metadata"); prop_set_string(prop_create(fde->fde_metadata, "title"), name); prop_set_rstring(prop_create(fde->fde_metadata, "album"), album); prop_set_rstring(prop_create(fde->fde_metadata, "artist"), artist); } rstr_release(album); rstr_release(artist); fa_close(fh); return 0; }
static int bwlimit_read(fa_handle_t *handle, void *buf, size_t size) { bwlimit_t *s = (bwlimit_t *)handle; int64_t ts = showtime_get_ts(); int r = fa_read(s->s_src, buf, size); ts = showtime_get_ts() - ts; int64_t delay = s->s_spill + r * 1000000LL / s->s_bps - ts; if(delay > 0) { usleep(delay); s->s_spill = 0; } else { s->s_spill = ts; } return r; }
static int fab_read(fa_handle_t *handle, void *buf, size_t size) { buffered_file_t *bf = (buffered_file_t *)handle; int64_t pre = bf->bf_fpos; printf("fab_read(%ld + %zd) <eof = %ld> = ", bf->bf_fpos, size, bf->bf_size); int r = fab_read0(handle, buf, size); printf("%d <now at %ld d=%ld>\n", r, bf->bf_fpos, bf->bf_fpos - pre); unsigned char *m1 = malloc(size); unsigned char *m2 = buf; if(fa_seek(bf->bf_chk, pre, SEEK_SET) != pre) { printf("Seek to %ld failed\n", pre); abort(); } int r2 = fa_read(bf->bf_chk, m1, size); printf(" r=%d r2=%d\n", r, r2); assert(r == r2); int i; for(i = 0; i < r; i++) { if(m1[i] != m2[i]) { printf("Mismatch at byte %d %02x != %02x\n", i, m1[i], m2[i]); abort(); } } free(m1); return r; }
static event_t * openspc_play(media_pipe_t *mp, AVIOContext *avio, char *errbuf, size_t errlen) { media_queue_t *mq = &mp->mp_audio; #error fa_fsize can return -1 .. deal with it size_t r, siz = fa_fsize(fh); uint8_t *buf = malloc(siz); media_buf_t *mb = NULL; event_t *e; int hold = 0, lost_focus = 0; int sample = 0; unsigned int duration = INT32_MAX; mp_set_playstatus_by_hold(mp, hold, NULL); mp->mp_audio.mq_stream = 0; fa_seek(fh, 0, SEEK_SET); r = fa_read(fh, buf, siz); fa_close(fh); if(r != siz) { free(buf); snprintf(errbuf, errlen, "openspc: Unable to read file"); return NULL; } if(OSPC_Init(buf, siz)) { free(buf); snprintf(errbuf, errlen, "openspc: Unable to initialize file"); return NULL; } if(!memcmp("v0.30", buf + 0x1c, 4) && buf[0x23] == 0x1a) { char str[4]; memcpy(str, buf + 0xa9, 3); str[3] = 0; duration = atoi(str) * 32000; } mp_set_play_caps(mp, MP_PLAY_CAPS_PAUSE); mp_become_primary(mp); while(1) { if(mb == NULL) { if(sample > duration) { while((e = mp_wait_for_empty_queues(mp, 0)) != NULL) { if(event_is_type(e, EVENT_PLAYQUEUE_JUMP) || event_is_action(e, ACTION_PREV_TRACK) || event_is_action(e, ACTION_NEXT_TRACK) || event_is_action(e, ACTION_STOP)) { mp_flush(mp, 0); break; } event_release(e); } if(e == NULL) e = event_create_type(EVENT_EOF); break; } mb = media_buf_alloc(); mb->mb_data_type = MB_AUDIO; mb->mb_size = sizeof(int16_t) * 2048 * 2; mb->mb_data = malloc(mb->mb_size); mb->mb_size = OSPC_Run(-1, mb->mb_data, mb->mb_size); mb->mb_channels = 2; mb->mb_rate = 32000; mb->mb_time = sample * 1000000LL / mb->mb_rate; sample += 2048; } if((e = mb_enqueue_with_events(mp, mq, mb)) == NULL) { mb = NULL; /* Enqueue succeeded */ continue; } if(event_is_type(e, EVENT_PLAYQUEUE_JUMP)) { mp_flush(mp, 0); break; } else if(event_is_action(e, ACTION_PLAYPAUSE) || event_is_action(e, ACTION_PLAY) || event_is_action(e, ACTION_PAUSE)) { hold = action_update_hold_by_event(hold, e); mp_send_cmd_head(mp, mq, hold ? MB_CTRL_PAUSE : MB_CTRL_PLAY); lost_focus = 0; mp_set_playstatus_by_hold(mp, hold, NULL); } else if(event_is_type(e, EVENT_MP_NO_LONGER_PRIMARY)) { hold = 1; lost_focus = 1; mp_send_cmd_head(mp, mq, MB_CTRL_PAUSE); mp_set_playstatus_by_hold(mp, hold, e->e_payload); } else if(event_is_type(e, EVENT_MP_IS_PRIMARY)) { if(lost_focus) { hold = 0; lost_focus = 0; mp_send_cmd_head(mp, mq, MB_CTRL_PLAY); mp_set_playstatus_by_hold(mp, hold, NULL); } } else if(event_is_type(e, EVENT_INTERNAL_PAUSE)) { hold = 1; lost_focus = 0; mp_send_cmd_head(mp, mq, MB_CTRL_PAUSE); mp_set_playstatus_by_hold(mp, hold, e->e_payload); } else if(event_is_action(e, ACTION_PREV_TRACK) || event_is_action(e, ACTION_NEXT_TRACK) || event_is_action(e, ACTION_STOP)) { mp_flush(mp, 0); break; } event_release(e); } free(buf); return e; }
event_t * be_file_playaudio(const char *url, media_pipe_t *mp, char *errbuf, size_t errlen, int hold, const char *mimetype) { AVFormatContext *fctx; AVCodecContext *ctx; AVPacket pkt; media_format_t *fw; int i, r, si; media_buf_t *mb = NULL; media_queue_t *mq; event_ts_t *ets; int64_t ts, seekbase = 0; media_codec_t *cw; event_t *e; int lost_focus = 0; int registered_play = 0; mp_set_playstatus_by_hold(mp, hold, NULL); fa_handle_t *fh = fa_open_ex(url, errbuf, errlen, FA_BUFFERED_SMALL, NULL); if(fh == NULL) return NULL; // First we need to check for a few other formats #if ENABLE_LIBOPENSPC || ENABLE_LIBGME uint8_t pb[128]; size_t psiz; psiz = fa_read(fh, pb, sizeof(pb)); if(psiz < sizeof(pb)) { fa_close(fh); snprintf(errbuf, errlen, "Fill too small"); return NULL; } #if ENABLE_LIBGME if(*gme_identify_header(pb)) return fa_gme_playfile(mp, fh, errbuf, errlen, hold, url); #endif #if ENABLE_LIBOPENSPC if(!memcmp(pb, "SNES-SPC700 Sound File Data", 27)) return openspc_play(mp, fh, errbuf, errlen); #endif #endif AVIOContext *avio = fa_libav_reopen(fh); if(avio == NULL) { fa_close(fh); return NULL; } if((fctx = fa_libav_open_format(avio, url, errbuf, errlen, mimetype)) == NULL) { fa_libav_close(avio); return NULL; } TRACE(TRACE_DEBUG, "Audio", "Starting playback of %s", url); mp_configure(mp, MP_PLAY_CAPS_SEEK | MP_PLAY_CAPS_PAUSE, MP_BUFFER_SHALLOW); mp->mp_audio.mq_stream = -1; mp->mp_video.mq_stream = -1; fw = media_format_create(fctx); cw = NULL; for(i = 0; i < fctx->nb_streams; i++) { ctx = fctx->streams[i]->codec; if(ctx->codec_type != AVMEDIA_TYPE_AUDIO) continue; cw = media_codec_create(ctx->codec_id, 0, fw, ctx, NULL, mp); mp->mp_audio.mq_stream = i; break; } if(cw == NULL) { media_format_deref(fw); snprintf(errbuf, errlen, "Unable to open codec"); return NULL; } mp_become_primary(mp); mq = &mp->mp_audio; while(1) { /** * Need to fetch a new packet ? */ if(mb == NULL) { mp->mp_eof = 0; r = av_read_frame(fctx, &pkt); if(r == AVERROR(EAGAIN)) continue; if(r == AVERROR_EOF || r == AVERROR(EIO)) { mb = MB_SPECIAL_EOF; mp->mp_eof = 1; continue; } if(r != 0) { char msg[100]; fa_ffmpeg_error_to_txt(r, msg, sizeof(msg)); TRACE(TRACE_ERROR, "Audio", "Playback error: %s", msg); while((e = mp_wait_for_empty_queues(mp)) != NULL) { if(event_is_type(e, EVENT_PLAYQUEUE_JUMP) || event_is_action(e, ACTION_PREV_TRACK) || event_is_action(e, ACTION_NEXT_TRACK) || event_is_action(e, ACTION_STOP)) { mp_flush(mp, 0); break; } event_release(e); } if(e == NULL) e = event_create_type(EVENT_EOF); break; } si = pkt.stream_index; if(si != mp->mp_audio.mq_stream) { av_free_packet(&pkt); continue; } mb = media_buf_alloc_unlocked(mp, pkt.size); mb->mb_data_type = MB_AUDIO; mb->mb_pts = rescale(fctx, pkt.pts, si); mb->mb_dts = rescale(fctx, pkt.dts, si); mb->mb_duration = rescale(fctx, pkt.duration, si); mb->mb_cw = media_codec_ref(cw); /* Move the data pointers from ffmpeg's packet */ mb->mb_stream = pkt.stream_index; memcpy(mb->mb_data, pkt.data, pkt.size); if(mb->mb_pts != AV_NOPTS_VALUE) { if(fctx->start_time == AV_NOPTS_VALUE) mb->mb_time = mb->mb_pts; else mb->mb_time = mb->mb_pts - fctx->start_time; } else mb->mb_time = AV_NOPTS_VALUE; mb->mb_send_pts = 1; av_free_packet(&pkt); } /* * Try to send the buffer. If mb_enqueue() returns something we * catched an event instead of enqueueing the buffer. In this case * 'mb' will be left untouched. */ if(mb == MB_SPECIAL_EOF) { // We have reached EOF, drain queues e = mp_wait_for_empty_queues(mp); if(e == NULL) { e = event_create_type(EVENT_EOF); break; } } else if((e = mb_enqueue_with_events(mp, mq, mb)) == NULL) { mb = NULL; /* Enqueue succeeded */ continue; } if(event_is_type(e, EVENT_PLAYQUEUE_JUMP)) { mp_flush(mp, 0); break; } else if(event_is_type(e, EVENT_CURRENT_PTS)) { ets = (event_ts_t *)e; seekbase = ets->ts; if(registered_play == 0) { if(ets->ts - fctx->start_time > METADB_AUDIO_PLAY_THRESHOLD) { registered_play = 1; metadb_register_play(url, 1, CONTENT_AUDIO); } } } else if(event_is_type(e, EVENT_SEEK)) { ets = (event_ts_t *)e; ts = ets->ts + fctx->start_time; if(ts < fctx->start_time) ts = fctx->start_time; av_seek_frame(fctx, -1, ts, AVSEEK_FLAG_BACKWARD); seekflush(mp, &mb); } else if(event_is_action(e, ACTION_SEEK_FAST_BACKWARD)) { av_seek_frame(fctx, -1, seekbase - 60000000, AVSEEK_FLAG_BACKWARD); seekflush(mp, &mb); } else if(event_is_action(e, ACTION_SEEK_BACKWARD)) { av_seek_frame(fctx, -1, seekbase - 15000000, AVSEEK_FLAG_BACKWARD); seekflush(mp, &mb); } else if(event_is_action(e, ACTION_SEEK_FAST_FORWARD)) { av_seek_frame(fctx, -1, seekbase + 60000000, 0); seekflush(mp, &mb); } else if(event_is_action(e, ACTION_SEEK_FORWARD)) { av_seek_frame(fctx, -1, seekbase + 15000000, 0); seekflush(mp, &mb); #if 0 } else if(event_is_action(e, ACTION_RESTART_TRACK)) { av_seek_frame(fctx, -1, 0, AVSEEK_FLAG_BACKWARD); seekflush(mp, &mb); #endif } else if(event_is_action(e, ACTION_PLAYPAUSE) || event_is_action(e, ACTION_PLAY) || event_is_action(e, ACTION_PAUSE)) { hold = action_update_hold_by_event(hold, e); mp_send_cmd_head(mp, mq, hold ? MB_CTRL_PAUSE : MB_CTRL_PLAY); lost_focus = 0; mp_set_playstatus_by_hold(mp, hold, NULL); } else if(event_is_type(e, EVENT_MP_NO_LONGER_PRIMARY)) { hold = 1; lost_focus = 1; mp_send_cmd_head(mp, mq, MB_CTRL_PAUSE); mp_set_playstatus_by_hold(mp, hold, e->e_payload); } else if(event_is_type(e, EVENT_MP_IS_PRIMARY)) { if(lost_focus) { hold = 0; lost_focus = 0; mp_send_cmd_head(mp, mq, MB_CTRL_PLAY); mp_set_playstatus_by_hold(mp, hold, NULL); } } else if(event_is_type(e, EVENT_INTERNAL_PAUSE)) { hold = 1; lost_focus = 0; mp_send_cmd_head(mp, mq, MB_CTRL_PAUSE); mp_set_playstatus_by_hold(mp, hold, e->e_payload); } else if(event_is_action(e, ACTION_PREV_TRACK) || event_is_action(e, ACTION_NEXT_TRACK) || event_is_action(e, ACTION_STOP)) { mp_flush(mp, 0); break; } event_release(e); } if(mb != NULL && mb != MB_SPECIAL_EOF) media_buf_free_unlocked(mp, mb); media_codec_deref(cw); media_format_deref(fw); if(hold) { // If we were paused, release playback again. mp_send_cmd(mp, mq, MB_CTRL_PLAY); mp_set_playstatus_by_hold(mp, 0, NULL); } return e; }
pixmap_t * fa_imageloader(const char *url, const struct image_meta *im, const char **vpaths, char *errbuf, size_t errlen, int *cache_control, cancellable_t *c) { uint8_t p[16]; int r; int width = -1, height = -1, orientation = 0; fa_handle_t *fh; pixmap_t *pm; pixmap_type_t fmt; #if ENABLE_LIBAV if(strchr(url, '#')) return fa_image_from_video(url, im, errbuf, errlen, cache_control, c); #endif if(!im->im_want_thumb) return fa_imageloader2(url, vpaths, errbuf, errlen, cache_control, c); fa_open_extra_t foe = { .foe_c = c }; if((fh = fa_open_vpaths(url, vpaths, errbuf, errlen, FA_BUFFERED_SMALL, &foe)) == NULL) return NULL; if(ONLY_CACHED(cache_control)) { snprintf(errbuf, errlen, "Not cached"); return NULL; } if(fa_read(fh, p, sizeof(p)) != sizeof(p)) { snprintf(errbuf, errlen, "File too short"); fa_close(fh); return NULL; } /* Probe format */ if((p[6] == 'J' && p[7] == 'F' && p[8] == 'I' && p[9] == 'F') || (p[6] == 'E' && p[7] == 'x' && p[8] == 'i' && p[9] == 'f')) { jpeginfo_t ji; if(jpeg_info(&ji, jpeginfo_reader, fh, JPEG_INFO_DIMENSIONS | JPEG_INFO_ORIENTATION | (im->im_want_thumb ? JPEG_INFO_THUMBNAIL : 0), p, sizeof(p), errbuf, errlen)) { fa_close(fh); return NULL; } if(im->im_want_thumb && ji.ji_thumbnail) { pixmap_t *pm = pixmap_dup(ji.ji_thumbnail); fa_close(fh); jpeg_info_clear(&ji); return pm; } fmt = PIXMAP_JPEG; width = ji.ji_width; height = ji.ji_height; orientation = ji.ji_orientation; jpeg_info_clear(&ji); } else if(!memcmp(pngsig, p, 8)) { fmt = PIXMAP_PNG; } else if(!memcmp(gif87sig, p, sizeof(gif87sig)) || !memcmp(gif89sig, p, sizeof(gif89sig))) { fmt = PIXMAP_GIF; } else if(!memcmp(svgsig1, p, sizeof(svgsig1)) || !memcmp(svgsig2, p, sizeof(svgsig2))) { fmt = PIXMAP_SVG; } else { snprintf(errbuf, errlen, "Unknown format"); fa_close(fh); return NULL; } int64_t s = fa_fsize(fh); if(s < 0) { snprintf(errbuf, errlen, "Can't read from non-seekable file"); fa_close(fh); return NULL; } pm = pixmap_alloc_coded(NULL, s, fmt); if(pm == NULL) { snprintf(errbuf, errlen, "Out of memory"); fa_close(fh); return NULL; } pm->pm_width = width; pm->pm_height = height; pm->pm_orientation = orientation; fa_seek(fh, SEEK_SET, 0); r = fa_read(fh, pm->pm_data, pm->pm_size); fa_close(fh); if(r != pm->pm_size) { pixmap_release(pm); snprintf(errbuf, errlen, "Read error"); return NULL; } return pm; }
static int gme_probe(metadata_t *md, const char *url, fa_handle_t *fh) { uint8_t b4[4], *buf; gme_err_t err; Music_Emu *emu; gme_info_t *info; int tracks; size_t size; const char *type; if(fa_read(fh, b4, 4) != 4) return 0; type = gme_identify_header(b4); if(*type == 0) return 0; size = fa_fsize(fh); if(size == -1) return -1; buf = malloc(size); fa_seek(fh, 0, SEEK_SET); if(fa_read(fh, buf, size) != size) { free(buf); return 0; } err = gme_open_data(buf, size, &emu, gme_info_only); free(buf); if(err != NULL) return 0; err = gme_track_info(emu, &info, 0); if(err != NULL) { gme_delete(emu); return 0; } tracks = gme_track_count(emu); #if 0 printf("tracks : %d\n", tracks); printf("system : %s\n", info->system); printf("game : %s\n", info->game); printf("song : %s\n", info->song); printf("author : %s\n", info->author); printf("copyright: %s\n", info->copyright); printf("comment : %s\n", info->comment); printf("dumper : %s\n", info->dumper); #endif if(tracks == 1) { md->md_title = info->song[0] ? rstr_alloc(info->song) : NULL; md->md_album = info->game[0] ? rstr_alloc(info->game) : NULL; md->md_artist = info->author[0] ? rstr_alloc(info->author) : NULL; md->md_duration = info->play_length / 1000.0; md->md_contenttype = CONTENT_AUDIO; } else { md->md_title = info->game[0] ? rstr_alloc(info->game) : NULL; md->md_artist = info->author[0] ? rstr_alloc(info->author) : NULL; md->md_contenttype = CONTENT_ALBUM; metdata_set_redirect(md, "gmefile://%s/", url); } gme_free_info(info); gme_delete(emu); return 1; }
/** * Probe file by checking its header * * pb is guaranteed to point to at least 256 bytes of valid data */ static int fa_probe_header(metadata_t *md, const char *url, fa_handle_t *fh, const char *filename) { uint16_t flags; uint8_t buf[256]; if(fa_read(fh, buf, sizeof(buf)) != sizeof(buf)) return 0; if(!memcmp(buf, "SNES-SPC700 Sound File Data", 27)) { fa_probe_spc(md, buf, filename); md->md_contenttype = CONTENT_AUDIO; return 1; } if(!memcmp(buf, "PSID", 4) || !memcmp(buf, "RSID", 4)) { fa_probe_psid(md, buf); md->md_contenttype = CONTENT_ALBUM; metdata_set_redirect(md, "sidfile://%s/", url); return 1; } if(buf[0] == 'R' && buf[1] == 'a' && buf[2] == 'r' && buf[3] == '!' && buf[4] == 0x1a && buf[5] == 0x07 && buf[6] == 0x0 && buf[9] == 0x73) { flags = buf[10] | buf[11] << 8; if((flags & 0x101) == 1) { /* Don't include slave volumes */ md->md_contenttype = CONTENT_UNKNOWN; return 1; } metdata_set_redirect(md, "rar://%s", url); md->md_contenttype = CONTENT_ARCHIVE; return 1; } if(buf[0] == 0x50 && buf[1] == 0x4b && buf[2] == 0x03 && buf[3] == 0x04) { char path[256]; char *buf; snprintf(path, sizeof(path), "zip://%s/plugin.json", url); buf = fa_load(path, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL); if(buf != NULL) { htsmsg_t *json = htsmsg_json_deserialize(buf); free(buf); if(json != NULL) { const char *title = htsmsg_get_str(json, "title"); if(title != NULL && htsmsg_get_str(json, "id") != NULL && htsmsg_get_str(json, "type") != NULL) { md->md_title = rstr_alloc(title); md->md_contenttype = CONTENT_PLUGIN; htsmsg_destroy(json); return 1; } htsmsg_destroy(json); } } metdata_set_redirect(md, "zip://%s", url); md->md_contenttype = CONTENT_ARCHIVE; return 1; } #if 0 if(!strncasecmp((char *)buf, "[playlist]", 10)) { /* Playlist */ fa_probe_playlist(md, url, buf, sizeof(buf)); md->md_contenttype = CONTENT_PLAYLIST; return 1; } #endif if((buf[6] == 'J' && buf[7] == 'F' && buf[8] == 'I' && buf[9] == 'F') || (buf[6] == 'E' && buf[7] == 'x' && buf[8] == 'i' && buf[9] == 'f')) { /* JPEG image */ md->md_contenttype = CONTENT_IMAGE; fa_probe_exif(md, url, buf, fh); // Try to get more info return 1; } if(!memcmp(buf, "<showtimeplaylist", strlen("<showtimeplaylist"))) { /* Ugly playlist thing (see fa_video.c) */ md->md_contenttype = CONTENT_VIDEO; return 1; } if(!memcmp(buf, pngsig, 8)) { /* PNG */ md->md_contenttype = CONTENT_IMAGE; return 1; } if(!memcmp(buf, gifsig, sizeof(gifsig))) { /* GIF */ md->md_contenttype = CONTENT_IMAGE; return 1; } if(buf[0] == '%' && buf[1] == 'P' && buf[2] == 'D' && buf[3] == 'F') { md->md_contenttype = CONTENT_UNKNOWN; return 1; } if(!memcmp(buf, ttfsig, sizeof(ttfsig)) || !memcmp(buf, otfsig, sizeof(otfsig))) { /* TTF or OTF */ md->md_contenttype = CONTENT_FONT; return 1; } return 0; }
static ssize_t cookie_read(void *fh, char *buf, size_t size) { return fa_read(fh, buf, size); }
static int fa_libav_read(void *opaque, uint8_t *buf, int size) { fa_handle_t *fh = opaque; return fa_read(fh, buf, size); }
static int gmefile_scandir(fa_dir_t *fd, const char *url, char *errbuf, size_t errlen) { void *fh = NULL; char *p, *fpath = mystrdupa(url); char name[32]; char turl[URL_MAX]; int tracks, i, size; fa_dir_entry_t *fde; const char *title; char *buf; Music_Emu *emu; gme_info_t *info; gme_err_t err; size_t r; if((p = strrchr(fpath, '/')) == NULL) { snprintf(errbuf, errlen, "Invalid filename"); return -1; } *p = 0; if((fh = fa_open(fpath, errbuf, errlen)) == NULL) return -1; size = fa_fsize(fh); buf = malloc(size); r = fa_read(fh, buf, size); fa_close(fh); if(r != size) { snprintf(errbuf, errlen, "Unable to read file"); free(buf); return -1; } err = gme_open_data(buf, size, &emu, gme_info_only); free(buf); if(err != NULL) return 0; tracks = gme_track_count(emu); for(i = 0; i < tracks; i++) { snprintf(turl, sizeof(turl), "gmeplayer:%s/%d", fpath, i + 1); err = gme_track_info(emu, &info, i); if(err == NULL && info->song[0]) { title = info->song; } else { snprintf(name, sizeof(name), "Track %02d", i + 1); title = name; } fde = fa_dir_add(fd, turl, title, CONTENT_AUDIO); fde->fde_probestatus = FDE_PROBE_DEEP; fde->fde_metadata = prop_create_root("metadata"); prop_set_string(prop_create(fde->fde_metadata, "title"), title); if(err == NULL) { if(info->game[0]) prop_set_string(prop_create(fde->fde_metadata, "album"), info->game); if(info->author[0]) prop_set_string(prop_create(fde->fde_metadata, "artist"), info->author); prop_set_float(prop_create(fde->fde_metadata, "duration"), info->play_length / 1000.0); gme_free_info(info); } } gme_delete(emu); return 0; }
static event_t * fa_gme_playfile_internal(media_pipe_t *mp, void *fh, char *errbuf, size_t errlen, int hold, int track) { media_queue_t *mq = &mp->mp_audio; Music_Emu *emu; gme_err_t err; char *buf; int lost_focus = 0; size_t size, r; int sample_rate = 48000; media_buf_t *mb = NULL; event_t *e; size = fa_fsize(fh); buf = malloc(size); r = fa_read(fh, buf, size); if(r != size) { snprintf(errbuf, errlen, "Unable to read file"); free(buf); return NULL; } err = gme_open_data(buf, size, &emu, sample_rate); free(buf); if(err != NULL) { snprintf(errbuf, errlen, "Unable to load file -- %s", err); return NULL; } gme_start_track(emu, track); mp_set_playstatus_by_hold(mp, hold, NULL); mp->mp_audio.mq_stream = 0; mp_set_play_caps(mp, MP_PLAY_CAPS_PAUSE | MP_PLAY_CAPS_SEEK); mp_become_primary(mp); while(1) { if(gme_track_ended(emu)) { e = event_create_type(EVENT_EOF); break; } if(mb == NULL) { mb = media_buf_alloc(); mb->mb_data_type = MB_AUDIO; mb->mb_channels = 2; mb->mb_size = sizeof(int16_t) * CHUNK_SIZE * mb->mb_channels; mb->mb_data = malloc(mb->mb_size); mb->mb_rate = sample_rate; mb->mb_time = gme_tell(emu) * 1000; gme_play(emu, CHUNK_SIZE * mb->mb_channels, mb->mb_data); } if((e = mb_enqueue_with_events(mp, mq, mb)) == NULL) { mb = NULL; /* Enqueue succeeded */ continue; } if(event_is_type(e, EVENT_PLAYQUEUE_JUMP)) { mp_flush(mp, 0); break; } else if(event_is_type(e, EVENT_SEEK)) { event_ts_t *ets = (event_ts_t *)e; gme_seek(emu, ets->pts / 1000); seekflush(mp, &mb); } else if(event_is_action(e, ACTION_SEEK_FAST_BACKWARD)) { deltaseek(mp, &mb, emu, -60000); } else if(event_is_action(e, ACTION_SEEK_BACKWARD)) { deltaseek(mp, &mb, emu, -15000); } else if(event_is_action(e, ACTION_SEEK_FAST_FORWARD)) { deltaseek(mp, &mb, emu, 60000); } else if(event_is_action(e, ACTION_SEEK_FORWARD)) { deltaseek(mp, &mb, emu, 15000); } else if(event_is_action(e, ACTION_PLAYPAUSE) || event_is_action(e, ACTION_PLAY) || event_is_action(e, ACTION_PAUSE)) { hold = action_update_hold_by_event(hold, e); mp_send_cmd_head(mp, mq, hold ? MB_CTRL_PAUSE : MB_CTRL_PLAY); lost_focus = 0; mp_set_playstatus_by_hold(mp, hold, NULL); } else if(event_is_type(e, EVENT_MP_NO_LONGER_PRIMARY)) { hold = 1; lost_focus = 1; mp_send_cmd_head(mp, mq, MB_CTRL_PAUSE); mp_set_playstatus_by_hold(mp, hold, e->e_payload); } else if(event_is_type(e, EVENT_MP_IS_PRIMARY)) { if(lost_focus) { hold = 0; lost_focus = 0; mp_send_cmd_head(mp, mq, MB_CTRL_PLAY); mp_set_playstatus_by_hold(mp, hold, NULL); } } else if(event_is_type(e, EVENT_INTERNAL_PAUSE)) { hold = 1; lost_focus = 0; mp_send_cmd_head(mp, mq, MB_CTRL_PAUSE); mp_set_playstatus_by_hold(mp, hold, e->e_payload); } else if(event_is_action(e, ACTION_PREV_TRACK) || event_is_action(e, ACTION_NEXT_TRACK) || event_is_action(e, ACTION_STOP)) { mp_flush(mp, 0); break; } event_release(e); } gme_delete(emu); if(mb != NULL) media_buf_free(mb); if(hold) { // If we were paused, release playback again. mp_send_cmd(mp, mq, MB_CTRL_PLAY); mp_set_playstatus_by_hold(mp, 0, NULL); } return e; }
image_t * fa_imageloader(const char *url, const struct image_meta *im, const char **vpaths, char *errbuf, size_t errlen, int *cache_control, cancellable_t *c) { uint8_t p[16]; int r; int width = -1, height = -1, orientation = 0; fa_handle_t *fh; image_t *img; image_coded_type_t fmt; #if ENABLE_LIBAV if(strchr(url, '#')) return fa_image_from_video(url, im, errbuf, errlen, cache_control, c); #endif if(!im->im_want_thumb) return fa_imageloader2(url, vpaths, errbuf, errlen, cache_control, c); fa_open_extra_t foe = { .foe_cancellable = c }; if((fh = fa_open_vpaths(url, vpaths, errbuf, errlen, FA_BUFFERED_SMALL, &foe)) == NULL) return NULL; if(ONLY_CACHED(cache_control)) { snprintf(errbuf, errlen, "Not cached"); return NULL; } if(fa_read(fh, p, sizeof(p)) != sizeof(p)) { snprintf(errbuf, errlen, "File too short"); fa_close(fh); return NULL; } /* Probe format */ if(p[0] == 0xff && p[1] == 0xd8 && p[2] == 0xff) { jpeginfo_t ji; if(jpeg_info(&ji, jpeginfo_reader, fh, JPEG_INFO_DIMENSIONS | JPEG_INFO_ORIENTATION | (im->im_want_thumb ? JPEG_INFO_THUMBNAIL : 0), p, sizeof(p), errbuf, errlen)) { fa_close(fh); return NULL; } if(im->im_want_thumb && ji.ji_thumbnail) { image_t *im = image_retain(ji.ji_thumbnail); fa_close(fh); jpeg_info_clear(&ji); im->im_flags |= IMAGE_ADAPTED; return im; } fmt = IMAGE_JPEG; width = ji.ji_width; height = ji.ji_height; orientation = ji.ji_orientation; jpeg_info_clear(&ji); } else if(!memcmp(pngsig, p, 8)) { fmt = IMAGE_PNG; } else if(!memcmp(gif87sig, p, sizeof(gif87sig)) || !memcmp(gif89sig, p, sizeof(gif89sig))) { fmt = IMAGE_GIF; } else if(p[0] == 'B' && p[1] == 'M') { fmt = IMAGE_BMP; } else if(!memcmp(svgsig1, p, sizeof(svgsig1)) || !memcmp(svgsig2, p, sizeof(svgsig2))) { fmt = IMAGE_SVG; } else { snprintf(errbuf, errlen, "Unknown format"); fa_close(fh); return NULL; } int64_t s = fa_fsize(fh); if(s < 0) { snprintf(errbuf, errlen, "Can't read from non-seekable file"); fa_close(fh); return NULL; } void *ptr; img = image_coded_alloc(&ptr, s, fmt); if(img == NULL) { snprintf(errbuf, errlen, "Out of memory"); fa_close(fh); return NULL; } img->im_width = width; img->im_height = height; img->im_orientation = orientation; fa_seek(fh, SEEK_SET, 0); r = fa_read(fh, ptr, s); fa_close(fh); if(r != s) { image_release(img); snprintf(errbuf, errlen, "Read error"); return NULL; } return img; }