Ejemplo n.º 1
0
bool Caching_Stream::open(const Input_Stream_Position& position)
{
    bool status;
    
    if (CFURLResourceIsReachable(m_metaDataUrl, NULL) &&
        CFURLResourceIsReachable(m_fileUrl, NULL)) {
        m_cacheable = false;
        m_writable  = false;
        m_useCache  = true;
        m_cacheMetaDataWritten = false;
        
        readMetaData();
        
        CS_TRACE("Playing file from cache\n");
        CS_TRACE_CFURL(m_fileUrl);
        
        status = m_fileStream->open(position);
    } else {
        m_cacheable = false;
        m_writable  = false;
        m_useCache  = false;
        m_cacheMetaDataWritten = false;
        
        CS_TRACE("File not cached\n");
        
        status = m_target->open(position);
    }
    
    return status;
}
Ejemplo n.º 2
0
void Caching_Stream::streamEndEncountered()
{
    if (m_fileOutput) {
        delete m_fileOutput, m_fileOutput = 0;
    }
    
    if (m_cacheable) {
        if (m_writable) {
            CS_TRACE("Successfully cached the stream\n");
            CS_TRACE_CFURL(m_fileUrl);
            
            // We only write the meta data if the stream was successfully streamed.
            // In that way we can use the meta data as an indicator that there is a file to stream.
            
            if (!m_cacheMetaDataWritten) {
                CFWriteStreamRef writeStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault, m_metaDataUrl);
                
                if (writeStream) {
                    if (CFWriteStreamOpen(writeStream)) {
                        CFStringRef contentType = m_target->contentType();
                        
                        UInt8 buf[1024];
                        CFIndex usedBytes = 0;
                        
                        if (contentType) {
                            // It is possible that some streams don't provide a content type
                            CFStringGetBytes(contentType,
                                             CFRangeMake(0, CFStringGetLength(contentType)),
                                             kCFStringEncodingUTF8,
                                             '?',
                                             false,
                                             buf,
                                             1024,
                                             &usedBytes);
                        }
                        
                        if (usedBytes > 0) {
                            CS_TRACE("Writing the meta data\n");
                            CS_TRACE_CFSTRING(contentType);
                            
                            CFWriteStreamWrite(writeStream, buf, usedBytes);
                        }
                        
                        CFWriteStreamClose(writeStream);
                    }
                    
                    CFRelease(writeStream);
                }
                
                m_cacheable = false;
                m_writable  = false;
                m_useCache  = true;
                m_cacheMetaDataWritten = true;
            }
        }
    }
    if (m_delegate) {
        m_delegate->streamEndEncountered();
    }
}