2016-07-05 17:44:02 +00:00
/*
This file is part of Telegram Desktop ,
2018-01-03 10:23:14 +00:00
the official desktop application for the Telegram messaging service .
2016-07-05 17:44:02 +00:00
2018-01-03 10:23:14 +00:00
For license and copyright information please follow this link :
https : //github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2016-07-05 17:44:02 +00:00
*/
# include "media/media_audio_loader.h"
2017-05-03 11:36:39 +00:00
AudioPlayerLoader : : AudioPlayerLoader ( const FileLocation & file , const QByteArray & data , base : : byte_vector & & bytes )
: _file ( file )
, _data ( data )
, _bytes ( std : : move ( bytes ) ) {
2016-07-05 17:44:02 +00:00
}
AudioPlayerLoader : : ~ AudioPlayerLoader ( ) {
2017-05-03 11:36:39 +00:00
if ( _access ) {
_file . accessDisable ( ) ;
_access = false ;
2016-07-05 17:44:02 +00:00
}
}
bool AudioPlayerLoader : : check ( const FileLocation & file , const QByteArray & data ) {
2017-05-03 11:36:39 +00:00
return this - > _file = = file & & this - > _data . size ( ) = = data . size ( ) ;
2016-07-05 17:44:02 +00:00
}
void AudioPlayerLoader : : saveDecodedSamples ( QByteArray * samples , int64 * samplesCount ) {
2017-08-17 09:06:26 +00:00
Assert ( _savedSamplesCount = = 0 ) ;
Assert ( _savedSamples . isEmpty ( ) ) ;
Assert ( ! _holdsSavedSamples ) ;
2016-07-05 17:44:02 +00:00
samples - > swap ( _savedSamples ) ;
std : : swap ( * samplesCount , _savedSamplesCount ) ;
_holdsSavedSamples = true ;
}
void AudioPlayerLoader : : takeSavedDecodedSamples ( QByteArray * samples , int64 * samplesCount ) {
2017-08-17 09:06:26 +00:00
Assert ( * samplesCount = = 0 ) ;
Assert ( samples - > isEmpty ( ) ) ;
Assert ( _holdsSavedSamples ) ;
2016-07-05 17:44:02 +00:00
samples - > swap ( _savedSamples ) ;
std : : swap ( * samplesCount , _savedSamplesCount ) ;
_holdsSavedSamples = false ;
}
bool AudioPlayerLoader : : holdsSavedDecodedSamples ( ) const {
return _holdsSavedSamples ;
}
bool AudioPlayerLoader : : openFile ( ) {
2017-05-03 11:36:39 +00:00
if ( _data . isEmpty ( ) & & _bytes . empty ( ) ) {
if ( _f . isOpen ( ) ) _f . close ( ) ;
if ( ! _access ) {
if ( ! _file . accessEnable ( ) ) {
LOG ( ( " Audio Error: could not open file access '%1', data size '%2', error %3, %4 " ) . arg ( _file . name ( ) ) . arg ( _data . size ( ) ) . arg ( _f . error ( ) ) . arg ( _f . errorString ( ) ) ) ;
2016-07-05 17:44:02 +00:00
return false ;
}
2017-05-03 11:36:39 +00:00
_access = true ;
2016-07-05 17:44:02 +00:00
}
2017-05-03 11:36:39 +00:00
_f . setFileName ( _file . name ( ) ) ;
if ( ! _f . open ( QIODevice : : ReadOnly ) ) {
LOG ( ( " Audio Error: could not open file '%1', data size '%2', error %3, %4 " ) . arg ( _file . name ( ) ) . arg ( _data . size ( ) ) . arg ( _f . error ( ) ) . arg ( _f . errorString ( ) ) ) ;
2016-07-05 17:44:02 +00:00
return false ;
}
}
2017-05-03 11:36:39 +00:00
_dataPos = 0 ;
2016-07-05 17:44:02 +00:00
return true ;
}