Add a Frame to the cache
| 87 | |
| 88 | // Add a Frame to the cache |
| 89 | void CacheDisk::Add(std::shared_ptr<Frame> frame) |
| 90 | { |
| 91 | // Create a scoped lock, to protect the cache from multiple threads |
| 92 | const std::lock_guard<std::recursive_mutex> lock(*cacheMutex); |
| 93 | int64_t frame_number = frame->number; |
| 94 | |
| 95 | // Freshen frame if it already exists |
| 96 | if (frames.count(frame_number)) |
| 97 | // Move frame to front of queue |
| 98 | Touch(frame_number); |
| 99 | |
| 100 | else |
| 101 | { |
| 102 | // Add frame to queue and map |
| 103 | frames[frame_number] = frame_number; |
| 104 | frame_numbers.push_front(frame_number); |
| 105 | ordered_frame_numbers.push_back(frame_number); |
| 106 | needs_range_processing = true; |
| 107 | |
| 108 | // Save image to disk (if needed) |
| 109 | QString frame_path(path.path() + "/" + QString("%1.").arg(frame_number) + QString(image_format.c_str()).toLower()); |
| 110 | frame->Save(frame_path.toStdString(), image_scale, image_format, image_quality); |
| 111 | if (frame_size_bytes == 0) { |
| 112 | // Get compressed size of frame image (to correctly apply max size against) |
| 113 | QFile image_file(frame_path); |
| 114 | frame_size_bytes = image_file.size(); |
| 115 | } |
| 116 | |
| 117 | // Save audio data (if needed) |
| 118 | if (frame->has_audio_data) { |
| 119 | QString audio_path(path.path() + "/" + QString("%1").arg(frame_number) + ".audio"); |
| 120 | QFile audio_file(audio_path); |
| 121 | |
| 122 | if (audio_file.open(QIODevice::WriteOnly)) { |
| 123 | QTextStream audio_stream(&audio_file); |
| 124 | audio_stream << frame->SampleRate() << Qt::endl; |
| 125 | audio_stream << frame->GetAudioChannelsCount() << Qt::endl; |
| 126 | audio_stream << frame->GetAudioSamplesCount() << Qt::endl; |
| 127 | audio_stream << frame->ChannelsLayout() << Qt::endl; |
| 128 | |
| 129 | // Loop through all samples |
| 130 | for (int channel = 0; channel < frame->GetAudioChannelsCount(); channel++) |
| 131 | { |
| 132 | // Get audio for this channel |
| 133 | float *samples = frame->GetAudioSamples(channel); |
| 134 | for (int sample = 0; sample < frame->GetAudioSamplesCount(); sample++) |
| 135 | audio_stream << samples[sample] << Qt::endl; |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | // Clean up old frames |
| 143 | CleanUp(); |
| 144 | } |
| 145 | } |
| 146 |
no test coverage detected