void Audio_Stream::seekToTime(unsigned newSeekTime) { unsigned duration = durationInSeconds(); if (!(duration > 0)) { return; } close(); setState(SEEKING); HTTP_Stream_Position position; double offset = (double)newSeekTime / (double)duration; position.start = m_dataOffset + offset * (contentLength() - m_dataOffset); position.end = contentLength(); m_seekTime = newSeekTime; if (m_httpStream->open(position)) { AS_TRACE("%s: HTTP stream opened, buffering...\n", __PRETTY_FUNCTION__); m_httpStreamRunning = true; } else { AS_TRACE("%s: failed to open the HTTP stream\n", __PRETTY_FUNCTION__); setState(FAILED); } }
void Audio_Stream::seekToTime(unsigned newSeekTime) { unsigned duration = durationInSeconds(); if (!(duration > 0)) { return; } if (state() == SEEKING) { return; } else { setState(SEEKING); } m_seekTime = newSeekTime; double offset = (double)newSeekTime / (double)duration; UInt64 seekByteOffset = m_dataOffset + offset * (contentLength() - m_dataOffset); HTTP_Stream_Position position; position.start = seekByteOffset; position.end = contentLength(); double packetDuration = m_srcFormat.mFramesPerPacket / m_srcFormat.mSampleRate; if (packetDuration > 0 && bitrate() > 0) { UInt32 ioFlags = 0; SInt64 packetAlignedByteOffset; SInt64 seekPacket = floor((double)newSeekTime / packetDuration); OSStatus err = AudioFileStreamSeek(m_audioFileStream, seekPacket, &packetAlignedByteOffset, &ioFlags); if (!err) { position.start = packetAlignedByteOffset + m_dataOffset; } } close(); AS_TRACE("Seeking position %llu\n", position.start); if (m_httpStream->open(position)) { AS_TRACE("%s: HTTP stream opened, buffering...\n", __PRETTY_FUNCTION__); m_httpStreamRunning = true; } else { AS_TRACE("%s: failed to open the fHTTP stream\n", __PRETTY_FUNCTION__); setState(FAILED); } }