0

When working with NPAPI, you have the control over two functions: NPP_WriteReady & NPP_Write. This is basically a data push model.

However I need to implement support for a new file format. The library I am using takes any concrete subclass of the following source model (simplified c++ code):

struct compressed_source {
  virtual int read(char *buf, int num_bytes) = 0;
}

This model is trivial to implement when dealing with FILE* (C) or socket (BSD) and other(s), since they comply with a pull data model. However I do not see how to fullfill this pull model from the NPAPI push model.

As far as I understand I cannot explicitely call NPP_Write within my concrete implementation of ::read(char *, size_t).

What is the solution here ?

EDIT:

I did not want to add too much details to avoid confusing answer. Just for reference, I want to be build an OpenJPEG/NPAPI plugin. OpenJPEG is a huge library, and the underlying JPEG 2000 implementation really wants a pull data model to allow fine access on massive image (eg: specific sub-region of an 100000 x 100000 image thanks to low level indexing information). In other word, I really need a pull data model plugin interface.

malat
  • 12,152
  • 13
  • 89
  • 158
  • 1
    For the sake of argument, are you certain that you actually need a plugin for this? For example, if you can live with it only working (well) in Firefox and Chrome, you could use emscripten and do the download with standard AJAX style requests, then decode it using your emscripten binary and display using a canvas. I would really avoid any kind of hackery involving the NPAPI streams stuff – taxilian Jul 13 '14 at 04:21

1 Answers1

1

Preload the file

Well, preloading the whole file is always an option that would work, but often not a good one. From your other questions I gather that files/downloads in question might be rather large, so avoiding network traffic might be a good idea, so preloading the file is not really an option.

Hack the library

If you're using some open source library, you might be able to implement a push API along or instead of the current pull API directly within the library.

Or you could implement things entirely by yourself. IIRC you're trying to decode some image format, and image formats are usually reasonably easy to implement from scratch.

Implement blocking reads by blocking the thread

You could put the image decoding stuff into a new thread, and whenever there is not enough buffered data already to fulfill a read immediately, do a blocking wait for the data receiving thread (main thread in case of NPAPI) until it indicates the buffer is sufficiently filled again. This is essentially the Producer/Consumer problem.

Of course, you'll first need to choose how to use threads and synchronization primitives (a library such as C++11 std::thread, Boost threads, low-level pthreads and/or Windows threads, etc.). There are tons of related SO questions on SO/SE and tons of articles/postings/discussions/tutorials etc. all over the internet.

nmaier
  • 32,336
  • 5
  • 63
  • 78
  • Thanks ! The blocking reads solution feels like a hack. I'll check other NPAPI options first. – malat Jul 12 '14 at 08:55