virtual void SetFormat(const Encoder::Settings &settings) { FinishStream(); ASSERT(!inited && !started); formatInfo = enc.GetTraits().formats[settings.Format]; ASSERT(formatInfo.Sampleformat.IsValid()); ASSERT(formatInfo.Samplerate > 0); ASSERT(formatInfo.Channels > 0); writeTags = settings.Tags; encoder = FLAC__stream_encoder_new(); FLAC__stream_encoder_set_channels(encoder, formatInfo.Channels); FLAC__stream_encoder_set_bits_per_sample(encoder, formatInfo.Sampleformat.GetBitsPerSample()); FLAC__stream_encoder_set_sample_rate(encoder, formatInfo.Samplerate); int compressionLevel = StreamEncoderSettings::Instance().FLACCompressionLevel; FLAC__stream_encoder_set_compression_level(encoder, compressionLevel); inited = true; ASSERT(inited && !started); }
DWORD CThreadInfo::ThreadFunc() { for (;;) { Encoder->CanProcessEvent.Lock(); Encoder->CS.Enter(); if (Encoder->CloseThreads) { Encoder->CS.Leave(); return 0; } if (Encoder->StreamWasFinished) { FinishStream(true); continue; } HRESULT res = S_OK; bool needLeave = true; try { UInt32 blockSize = Encoder->ReadRleBlock(m_Block); m_PackSize = Encoder->m_InStream.GetProcessedSize(); m_BlockIndex = Encoder->NextBlockIndex; if (++Encoder->NextBlockIndex == Encoder->NumThreads) Encoder->NextBlockIndex = 0; if (blockSize == 0) { FinishStream(true); continue; } Encoder->CS.Leave(); needLeave = false; res = EncodeBlock3(blockSize); } catch(const CInBufferException &e) { res = e.ErrorCode; } catch(const COutBufferException &e) { res = e.ErrorCode; } catch(...) { res = E_FAIL; } if (res != S_OK) { Encoder->Result = res; FinishStream(needLeave); continue; } } }
virtual void SetFormat(const Encoder::Settings &settings) { FinishStream(); ASSERT(!inited && !started); formatInfo = enc.GetTraits().formats[settings.Format]; ASSERT(formatInfo.Sampleformat.IsValid()); ASSERT(formatInfo.Samplerate > 0); ASSERT(formatInfo.Channels > 0); writeTags = settings.Tags; fileWAV = new WAVWriter(&f); fileWAV->WriteFormat(formatInfo.Samplerate, formatInfo.Sampleformat.GetBitsPerSample(), (uint16)formatInfo.Channels, formatInfo.Sampleformat.IsFloat() ? WAVFormatChunk::fmtFloat : WAVFormatChunk::fmtPCM); inited = true; ASSERT(inited && !started); }
bool ResourceStreamService::SendNextResourceStreamData(ResourceStreamRecord* rec) { uint8_t* data = new uint8_t[DEFAULT_STREAM_DATA_SIZE]; size_t res = fread(data, 1, DEFAULT_STREAM_DATA_SIZE, rec->src); if (res == 0) { FinishStream(rec); return false; } SmartPacket pkt(SP_RESOURCE_DATA); pkt.WriteUInt8(rec->type); pkt.WriteUInt32(rec->id); pkt.WriteUInt16((uint16_t)res); for (size_t i = 0; i < res; i++) pkt.WriteUInt8(data[i]); sNetwork->SendPacketToSession(pkt, rec->sessionId); delete data; return true; }
void CState::ThreadFunc() { for (;;) { Decoder->CanProcessEvent.Lock(); Decoder->CS.Enter(); if (Decoder->CloseThreads) { Decoder->CS.Leave(); return; } if (Decoder->StreamWasFinished1) { FinishStream(); continue; } HRESULT res = S_OK; UInt32 blockIndex = Decoder->NextBlockIndex; UInt32 nextBlockIndex = blockIndex + 1; if (nextBlockIndex == Decoder->NumThreads) nextBlockIndex = 0; Decoder->NextBlockIndex = nextBlockIndex; UInt32 crc; UInt64 packSize = 0; CBlockProps props; try { res = Decoder->ReadSignature(crc); if (res != S_OK) { Decoder->Result1 = res; FinishStream(); continue; } if (Decoder->BzWasFinished) { Decoder->Result1 = res; FinishStream(); continue; } props.randMode = true; res = Decoder->Base.ReadBlock(Counters, Decoder->BlockSizeMax, &props); if (res != S_OK) { Decoder->Result1 = res; FinishStream(); continue; } packSize = Decoder->Base.BitDecoder.GetProcessedSize(); } catch(const CInBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } catch(...) { res = E_FAIL; } if (res != S_OK) { Decoder->Result1 = res; FinishStream(); continue; } Decoder->CS.Leave(); DecodeBlock1(Counters, props.blockSize); bool needFinish = true; try { Decoder->m_States[blockIndex].CanWriteEvent.Lock(); needFinish = Decoder->StreamWasFinished2; if (!needFinish) { if (DecodeBlock(props, Counters + 256, Decoder->m_OutStream) == crc) res = Decoder->SetRatioProgress(packSize); else res = S_FALSE; } } catch(const COutBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } catch(...) { res = E_FAIL; } if (res != S_OK) { Decoder->Result2 = res; Decoder->StreamWasFinished2 = true; } Decoder->m_States[nextBlockIndex].CanWriteEvent.Set(); if (res != S_OK || needFinish) { StreamWasFinishedEvent.Set(); Decoder->CanStartWaitingEvent.Lock(); WaitingWasStartedEvent.Set(); } } }
void CState::ThreadFunc() { for (;;) { Decoder->CanProcessEvent.Lock(); Decoder->CS.Enter(); if (Decoder->CloseThreads) { Decoder->CS.Leave(); return; } if (Decoder->StreamWasFinished1) { FinishStream(); continue; } HRESULT res = S_OK; UInt32 blockIndex = Decoder->NextBlockIndex; UInt32 nextBlockIndex = blockIndex + 1; if (nextBlockIndex == Decoder->NumThreads) nextBlockIndex = 0; Decoder->NextBlockIndex = nextBlockIndex; UInt32 crc; UInt64 packSize; UInt32 blockSize = 0, origPtr = 0; bool randMode = false; try { bool wasFinished; res = Decoder->ReadSignatures(wasFinished, crc); if (res != S_OK) { Decoder->Result1 = res; FinishStream(); continue; } if (wasFinished) { Decoder->Result1 = res; FinishStream(); continue; } res = ReadBlock(&Decoder->m_InStream, Counters, Decoder->BlockSizeMax, Decoder->m_Selectors, Decoder->m_HuffmanDecoders, &blockSize, &origPtr, &randMode); if (res != S_OK) { Decoder->Result1 = res; FinishStream(); continue; } packSize = Decoder->m_InStream.GetProcessedSize(); } catch(const CInBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } catch(...) { res = E_FAIL; } if (res != S_OK) { Decoder->Result1 = res; FinishStream(); continue; } Decoder->CS.Leave(); DecodeBlock1(Counters, blockSize); bool needFinish = true; try { Decoder->m_States[blockIndex].CanWriteEvent.Lock(); needFinish = Decoder->StreamWasFinished2; if (!needFinish) { if ((randMode ? DecodeBlock2Rand(Counters + 256, blockSize, origPtr, Decoder->m_OutStream) : DecodeBlock2(Counters + 256, blockSize, origPtr, Decoder->m_OutStream)) == crc) { if (Decoder->Progress) { UInt64 unpackSize = Decoder->m_OutStream.GetProcessedSize(); res = Decoder->Progress->SetRatioInfo(&packSize, &unpackSize); } } else res = S_FALSE; } } catch(const COutBufferException &e) { res = e.ErrorCode; if (res != S_OK) res = E_FAIL; } catch(...) { res = E_FAIL; } if (res != S_OK) { Decoder->Result2 = res; Decoder->StreamWasFinished2 = true; } Decoder->m_States[nextBlockIndex].CanWriteEvent.Set(); if (res != S_OK || needFinish) { StreamWasFinishedEvent.Set(); Decoder->CanStartWaitingEvent.Lock(); WaitingWasStartedEvent.Set(); } } }
virtual ~WavStreamWriter() { FinishStream(); ASSERT(!inited && !started); }
virtual void Finalize() { ASSERT(inited); FinishStream(); ASSERT(!inited && !started); }