void FileUploader::uploadMedia(const FullMsgId &msgId, const ReadyLocalMedia &media) { if (media.type == PreparePhoto) { App::feedPhoto(media.photo, media.photoThumbs); } else if (media.type == PrepareDocument) { DocumentData *document; if (media.photoThumbs.isEmpty()) { document = App::feedDocument(media.document); } else { document = App::feedDocument(media.document, media.photoThumbs.begin().value()); } document->status = FileUploading; if (!media.file.isEmpty()) { document->setLocation(FileLocation(StorageFilePartial, media.file)); } } else if (media.type == PrepareAudio) { AudioData *audio = App::feedAudio(media.audio); audio->status = FileUploading; audio->setData(media.data); } queue.insert(msgId, File(media)); sendNext(); }
void FileUploader::upload(const FullMsgId &msgId, const FileLoadResultPtr &file) { if (file->type == PreparePhoto) { PhotoData *photo = App::feedPhoto(file->photo, file->photoThumbs); photo->uploadingData = new PhotoData::UploadingData(file->partssize); } else if (file->type == PrepareDocument) { DocumentData *document; if (file->thumb.isNull()) { document = App::feedDocument(file->document); } else { document = App::feedDocument(file->document, file->thumb); } document->status = FileUploading; if (!file->filepath.isEmpty()) { document->setLocation(FileLocation(StorageFilePartial, file->filepath)); } } else if (file->type == PrepareAudio) { AudioData *audio = App::feedAudio(file->audio); audio->status = FileUploading; audio->setData(file->content); } queue.insert(msgId, File(file)); sendNext(); }
// This method only supports PCM/uncompressed format, with a single fmt // chunk followed by a single data chunk AudioData* loadWaveFile(int fd) { int srate = 0; int channels = 0; if (fd == -1) return 0; unsigned char hdr[36]; if (!readBytes(fd, hdr, 36)) { close(fd); return 0; } if (hdr[0] != 'R' || hdr[1] != 'I' || hdr[2] != 'F' || hdr[3] != 'F') { close(fd); return 0; } // Note: bytes 4 thru 7 contain the file size - 8 bytes if (hdr[8] != 'W' || hdr[9] != 'A' || hdr[10] != 'V' || hdr[11] != 'E') { close(fd); return 0; } if (hdr[12] != 'f' || hdr[13] != 'm' || hdr[14] != 't' || hdr[15] != ' ') { close(fd); return 0; } long extraBytes = hdr[16] + (hdr[17] << 8) + (hdr[18] << 16) + (hdr[19] << 24) - 16; int compression = hdr[20] + (hdr[21] << 8); // Type 1 is PCM/Uncompressed if (compression != 1) { close(fd); return 0; } channels = hdr[22] + (hdr[23] << 8); // Only mono or stereo PCM is supported in this example if (channels < 1 || channels > 2) { close(fd); return 0; } // Samples per second, independent of number of channels srate = hdr[24] + (hdr[25] << 8) + (hdr[26] << 16) + (hdr[27] << 24); // Bytes 28-31 contain the "average bytes per second", unneeded here // Bytes 32-33 contain the number of bytes per sample (includes channels) // Bytes 34-35 contain the number of bits per single sample int bits = hdr[34] + (hdr[35] << 8); // Supporting othe sample depths will require conversion if (bits != 16) { close(fd); return 0; } // Skip past extra bytes, if any unsigned char extraskip[extraBytes]; if(!readBytes(fd,extraskip,extraBytes)){ return 0; } // Start reading the next frame. Only supported frame is the data block unsigned char b[8]; if (!readBytes(fd, b, 8)) { close(fd); return 0; } // Do we have a fact block? if (b[0] == 'f' && b[1] == 'a' && b[2] == 'c' && b[3] == 't') { // Skip the fact block unsigned char factskip[12]; if(!readBytes(fd,factskip,12)){ return 0; } // Read the next frame if (!readBytes(fd, b, 8)) { close(fd); return 0; } } // Now look for the data block if (b[0] != 'd' || b[1] != 'a' || b[2] != 't' || b[3] != 'a') { close(fd); return 0; } // this will be 0 if ffmpeg is used // since it can't seek the stream to write this value // so we ignore this value and just read to the end if it is 0 long bytes = b[4] + (b[5] << 8) + (b[6] << 16) + (b[7] << 24); // No need to read the whole file, just the first 135 seconds long bytesPerSecond = srate * 2 * channels; if(bytes == 0) //maximum data is 2 gigabyte, getting a puid won't work with bigger files bytes = 2*1000*1000*1000; // Now we read parts of the file until the eof or bytes is reached // bytesPerSecond is used as puffersize int readSize = bytesPerSecond; unsigned char* samples = NULL; int size = 0; while(size<bytes){ samples = (unsigned char*)realloc ( samples, size+readSize ); int n = read(fd,samples+size,readSize); if(n < 0){ delete[] samples; close(fd); return 0; } size += n; if(n == 0) break; } close(fd); long ms = (size/2)/(srate/1000); if ( channels == 2 ) ms /= 2; AudioData *data = new AudioData(); data->setData(samples, OFA_LITTLE_ENDIAN, size/2, srate, channels == 2 ? 1 : 0, ms, "wav"); return data; }