| 893 | } |
| 894 | |
| 895 | inline std::vector<frame> audio_fifo::push_pull( |
| 896 | frame&& in |
| 897 | ) |
| 898 | { |
| 899 | using std::chrono::system_clock; |
| 900 | DLIB_ASSERT(in.is_audio(), "this isn't an audio frame"); |
| 901 | |
| 902 | std::vector<frame> outs; |
| 903 | |
| 904 | //check that the configuration hasn't suddenly changed this would be exceptional |
| 905 | auto current_params = std::tie(fmt, nchannels); |
| 906 | auto new_params = std::make_tuple(in.samplefmt(), in.nchannels()); |
| 907 | |
| 908 | if (current_params != new_params) |
| 909 | throw std::runtime_error("new audio frame params differ from first "); |
| 910 | |
| 911 | if (frame_size == 0) |
| 912 | { |
| 913 | outs.push_back(std::move(in)); |
| 914 | } |
| 915 | else |
| 916 | { |
| 917 | if (av_audio_fifo_write(fifo.get(), (void**)in.get_frame().data, in.nsamples()) != in.nsamples()) |
| 918 | throw std::runtime_error("av_audio_fifo_write() failed to write all samples"); |
| 919 | |
| 920 | while (av_audio_fifo_size(fifo.get()) >= frame_size) |
| 921 | { |
| 922 | const system_clock::time_point timestamp{system_clock::duration{av_rescale_q( |
| 923 | sample_count, |
| 924 | {1, in.sample_rate()}, |
| 925 | {system_clock::duration::period::num, system_clock::duration::period::den})}}; |
| 926 | |
| 927 | frame out(in.sample_rate(), frame_size, in.layout(), in.samplefmt(), timestamp); |
| 928 | |
| 929 | if (av_audio_fifo_read(fifo.get(), (void**)out.get_frame().data, out.nsamples()) != out.nsamples()) |
| 930 | throw std::runtime_error("av_audio_fifo_read() failed to read all requested samples"); |
| 931 | |
| 932 | sample_count += out.nsamples(); |
| 933 | outs.push_back(std::move(out)); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | return outs; |
| 938 | } |
| 939 | |
| 940 | // --------------------------------------------------------------------------------------------------- |
| 941 | |