- _worker_condition.notify_all ();
- }
-}
-
-void
-Encoder::link (string a, string b) const
-{
-#ifdef DVDOMATIC_POSIX
- int const r = symlink (a.c_str(), b.c_str());
- if (r) {
- throw EncodeError (String::compose ("could not create symlink from %1 to %2", a, b));
- }
-#endif
-
-#ifdef DVDOMATIC_WINDOWS
- boost::filesystem::copy_file (a, b);
-#endif
-}
-
-struct WriteQueueSorter
-{
- bool operator() (pair<shared_ptr<EncodedData>, int> const & a, pair<shared_ptr<EncodedData>, int> const & b) {
- return a.second < b.second;
- }
-};
-
-void
-Encoder::writer_thread ()
-{
- while (1)
- {
- boost::mutex::scoped_lock lock (_writer_mutex);
-
- while (1) {
- if (_finish_writer ||
- _write_queue.size() > _maximum_frames_in_memory ||
- (!_write_queue.empty() && _write_queue.front().second == (_last_written_frame + 1))) {
-
- break;
- }
-
- TIMING ("writer sleeps with a queue of %1; %2 pending", _write_queue.size(), _pending.size());
- _writer_condition.wait (lock);
- TIMING ("writer wakes with a queue of %1", _write_queue.size());
-
- _write_queue.sort (WriteQueueSorter ());
- }
-
- if (_finish_writer && _write_queue.empty() && _pending.empty()) {
- return;
- }
-
- /* Write any frames that we can write; i.e. those that are in sequence */
- while (!_write_queue.empty() && _write_queue.front().second == (_last_written_frame + 1)) {
- pair<boost::shared_ptr<EncodedData>, int> encoded = _write_queue.front ();
- _write_queue.pop_front ();
-
- lock.unlock ();
- /* XXX: write to mxf */
- _film->log()->log (String::compose ("Writer writes %1 to MXF", encoded.second));
- if (encoded.first) {
- _picture_asset_writer->write (encoded.first->data(), encoded.first->size());
- _last_written = encoded.first;
- } else {
- _picture_asset_writer->write (_last_written->data(), _last_written->size());
- }
- lock.lock ();
-
- ++_last_written_frame;
- }
-
- while (_write_queue.size() > _maximum_frames_in_memory) {
- /* Too many frames in memory which can't yet be written to the stream.
- Put some to disk.
- */
-
- pair<boost::shared_ptr<EncodedData>, int> encoded = _write_queue.front ();
- _write_queue.pop_back ();
- if (!encoded.first) {
- /* This is a `repeat-last' frame, so no need to write it to disk */
- continue;
- }
-
- lock.unlock ();
- _film->log()->log (String::compose ("Writer full; pushes %1 to disk", encoded.second));
- encoded.first->write (_film, encoded.second);
- lock.lock ();
-
- _pending.push_back (encoded.second);
- }
-
- while (_write_queue.size() < _maximum_frames_in_memory && !_pending.empty()) {
- /* We have some space in memory. Fetch some frames back off disk. */
-
- _pending.sort ();
- int const fetch = _pending.front ();
-
- lock.unlock ();
- _film->log()->log (String::compose ("Writer pulls %1 back from disk", fetch));
- shared_ptr<EncodedData> encoded;
- if (boost::filesystem::exists (_film->frame_out_path (fetch, false))) {
- /* It's an actual frame (not a repeat-last); load it in */
- encoded.reset (new EncodedData (_film->frame_out_path (fetch, false)));
- }
- lock.lock ();
-
- _write_queue.push_back (make_pair (encoded, fetch));
- _pending.remove (fetch);
- }