bool QProcessResponseThread::ParseMultipleResponseData( QByteArray &byResponse ) { bool bRet = false; qint32 nIndex = byResponse.indexOf( Protocol::byToken ); if ( -1 == nIndex ) { byResponse.clear( ); return bRet; // No Head } if ( 0 < nIndex) { // Remove byResponse.remove( 0, nIndex ); } qint32 nLen = Protocol::nTokenLength; if ( nLen == byResponse.length( ) ) { return bRet; } nLen += sizeof ( quint8 ); // MessageType nLen += sizeof ( quint32 ); // TcpStreamLength if ( nLen >= byResponse.length( ) ) { return bRet; } quint32 nTotal = GetStreamLength( byResponse ); if ( nTotal > byResponse.length( ) ) { return bRet; } switch ( GetMessageType( byResponse ) ) { case Protocol::ResponseBallotSenseState : //nLen += 2; break; case Protocol::ResponseGateSenseState : //nLen += 2; break; case Protocol::ResponseInfraredState : //nLen += 2; break; case Protocol::ResponsePlateData : //nLen += 2; break; case Protocol::ResponseActiveSend : //nLen += 2; break; } ParseResponseData( byResponse ); byResponse.remove( 0, nTotal ); // Remove one Request bRet = ( 0 < byResponse.length( ) ); return bRet; }
bool OMXReader::SeekTime(int64_t seek_ms, int seek_flags, double *startpts) { if(seek_ms < 0) seek_ms = 0; if(!m_pFile || !m_pFormatContext) return false; if(!m_pFile->IoControl(IOCTRL_SEEK_POSSIBLE, NULL)) return false; Lock(); //FlushRead(); if(m_ioContext) m_ioContext->buf_ptr = m_ioContext->buf_end; int64_t seek_pts = (int64_t)seek_ms * (AV_TIME_BASE / 1000); if (m_pFormatContext->start_time != (int64_t)AV_NOPTS_VALUE) seek_pts += m_pFormatContext->start_time; /* seek behind eof */ if((seek_pts / AV_TIME_BASE) > (GetStreamLength() / 1000)) { m_eof = true; UnLock(); return true; } int ret = m_dllAvFormat.av_seek_frame(m_pFormatContext, -1, seek_pts, seek_flags ? AVSEEK_FLAG_BACKWARD : 0); if(ret >= 0) { UpdateCurrentPTS(); m_eof = false; } if(m_iCurrentPts == DVD_NOPTS_VALUE) { CLog::Log(LOGDEBUG, "OMXReader::SeekTime - unknown position after seek"); } else { CLog::Log(LOGDEBUG, "OMXReader::SeekTime - seek ended up on time %d",(int)(m_iCurrentPts / DVD_TIME_BASE * 1000)); } if(startpts) *startpts = DVD_MSEC_TO_TIME(seek_ms); UnLock(); return (ret >= 0); }
bool OMXReader::Open(std::string filename, bool dump_format) { if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllAvFormat.Load()) return false; m_iCurrentPts = DVD_NOPTS_VALUE; m_filename = filename; m_speed = DVD_PLAYSPEED_NORMAL; m_program = UINT_MAX; const AVIOInterruptCB int_cb = { interrupt_cb, NULL }; ClearStreams(); m_dllAvFormat.av_register_all(); m_dllAvFormat.avformat_network_init(); m_dllAvUtil.av_log_set_level(AV_LOG_QUIET); int result = -1; AVInputFormat *iformat = NULL; unsigned char *buffer = NULL; unsigned int flags = READ_TRUNCATED | READ_BITRATE | READ_CHUNKED; #ifndef STANDALONE if( CFileItem(m_filename, false).IsInternetStream() ) flags |= READ_CACHED; #endif if(m_filename.substr(0, 8) == "shout://" ) m_filename.replace(0, 8, "http://"); if(m_filename.substr(0,6) == "mms://" || m_filename.substr(0,7) == "http://" || m_filename.substr(0,7) == "rtmp://" || m_filename.substr(0,6) == "udp://") { // ffmpeg dislikes the useragent from AirPlay urls //int idx = m_filename.Find("|User-Agent=AppleCoreMedia"); size_t idx = m_filename.find("|"); if(idx != string::npos) m_filename = m_filename.substr(0, idx); result = m_dllAvFormat.avformat_open_input(&m_pFormatContext, m_filename.c_str(), iformat, NULL); if(result < 0) { CLog::Log(LOGERROR, "COMXPlayer::OpenFile - avformat_open_input %s ", m_filename.c_str()); Close(); return false; } } else { m_pFile = new CFile(); if (!m_pFile->Open(m_filename, flags)) { CLog::Log(LOGERROR, "COMXPlayer::OpenFile - %s ", m_filename.c_str()); Close(); return false; } buffer = (unsigned char*)m_dllAvUtil.av_malloc(FFMPEG_FILE_BUFFER_SIZE); m_ioContext = m_dllAvFormat.avio_alloc_context(buffer, FFMPEG_FILE_BUFFER_SIZE, 0, m_pFile, dvd_file_read, NULL, dvd_file_seek); m_ioContext->max_packet_size = 6144; if(m_ioContext->max_packet_size) m_ioContext->max_packet_size *= FFMPEG_FILE_BUFFER_SIZE / m_ioContext->max_packet_size; if(m_pFile->IoControl(IOCTRL_SEEK_POSSIBLE, NULL) == 0) m_ioContext->seekable = 0; m_dllAvFormat.av_probe_input_buffer(m_ioContext, &iformat, m_filename.c_str(), NULL, 0, 0); if(!iformat) { CLog::Log(LOGERROR, "COMXPlayer::OpenFile - av_probe_input_buffer %s ", m_filename.c_str()); Close(); return false; } m_pFormatContext = m_dllAvFormat.avformat_alloc_context(); m_pFormatContext->pb = m_ioContext; result = m_dllAvFormat.avformat_open_input(&m_pFormatContext, m_filename.c_str(), iformat, NULL); if(result < 0) { Close(); return false; } } // set the interrupt callback, appeared in libavformat 53.15.0 m_pFormatContext->interrupt_callback = int_cb; m_bMatroska = strncmp(m_pFormatContext->iformat->name, "matroska", 8) == 0; // for "matroska.webm" m_bAVI = strcmp(m_pFormatContext->iformat->name, "avi") == 0; m_bMpeg = strcmp(m_pFormatContext->iformat->name, "mpeg") == 0; // if format can be nonblocking, let's use that m_pFormatContext->flags |= AVFMT_FLAG_NONBLOCK; // analyse very short to speed up mjpeg playback start if (iformat && (strcmp(iformat->name, "mjpeg") == 0) && m_ioContext->seekable == 0) m_pFormatContext->max_analyze_duration = 500000; #ifdef STANDALONE if(/*m_bAVI || */m_bMatroska) m_pFormatContext->max_analyze_duration = 0; #endif result = m_dllAvFormat.avformat_find_stream_info(m_pFormatContext, NULL); if(result < 0) { Close(); return false; } if(!GetStreams()) { Close(); return false; } if(m_pFile) { int64_t len = m_pFile->GetLength(); int64_t tim = GetStreamLength(); if(len > 0 && tim > 0) { unsigned rate = len * 1000 / tim; unsigned maxrate = rate + 1024 * 1024 / 8; if(m_pFile->IoControl(IOCTRL_CACHE_SETRATE, &maxrate) >= 0) CLog::Log(LOGDEBUG, "COMXPlayer::OpenFile - set cache throttle rate to %u bytes per second", maxrate); } } printf("file : %s reult %d format %s audio streams %d video streams %d chapters %d subtitles %d\n", m_filename.c_str(), result, m_pFormatContext->iformat->name, m_audio_count, m_video_count, m_chapter_count, m_subtitle_count); m_speed = DVD_PLAYSPEED_NORMAL; if(dump_format) m_dllAvFormat.av_dump_format(m_pFormatContext, 0, m_filename.c_str(), 0); UpdateCurrentPTS(); m_open = true; return true; }
bool OMXReader::Open(std::string filename, bool dump_format, bool live /* =false */, float timeout /* = 0.0f */, std::string cookie /* = "" */, std::string user_agent /* = "" */) { if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllAvFormat.Load()) return false; timeout_default_duration = (int64_t) (timeout * 1e9); m_iCurrentPts = DVD_NOPTS_VALUE; m_filename = filename; m_speed = DVD_PLAYSPEED_NORMAL; m_program = UINT_MAX; const AVIOInterruptCB int_cb = { interrupt_cb, NULL }; RESET_TIMEOUT(3); ClearStreams(); m_dllAvFormat.av_register_all(); m_dllAvFormat.avformat_network_init(); m_dllAvUtil.av_log_set_level(dump_format ? AV_LOG_INFO:AV_LOG_QUIET); int result = -1; AVInputFormat *iformat = NULL; unsigned char *buffer = NULL; unsigned int flags = READ_TRUNCATED | READ_BITRATE | READ_CHUNKED; m_pFormatContext = m_dllAvFormat.avformat_alloc_context(); if (!m_pFormatContext) { log_err("Failed to create format context."); return false; } // set the interrupt callback, appeared in libavformat 53.15.0 m_pFormatContext->interrupt_callback = int_cb; // if format can be nonblocking, let's use that m_pFormatContext->flags |= AVFMT_FLAG_NONBLOCK; if(m_filename.substr(0, 8) == "shout://" ) m_filename.replace(0, 8, "http://"); if(m_filename.substr(0,6) == "mms://" || m_filename.substr(0,7) == "mmsh://" || m_filename.substr(0,7) == "mmst://" || m_filename.substr(0,7) == "mmsu://" || m_filename.substr(0,7) == "http://" || m_filename.substr(0,8) == "https://" || m_filename.substr(0,7) == "rtmp://" || m_filename.substr(0,6) == "udp://" || m_filename.substr(0,7) == "rtsp://" || m_filename.substr(0,6) == "rtp://" || m_filename.substr(0,6) == "ftp://" || m_filename.substr(0,7) == "sftp://" || m_filename.substr(0,6) == "smb://") { // ffmpeg dislikes the useragent from AirPlay urls //int idx = m_filename.Find("|User-Agent=AppleCoreMedia"); size_t idx = m_filename.find("|"); if(idx != string::npos) m_filename = m_filename.substr(0, idx); AVDictionary *d = NULL; // Enable seeking if http, ftp if(!live && (m_filename.substr(0,7) == "http://" || m_filename.substr(0,6) == "ftp://" || m_filename.substr(0,7) == "sftp://" || m_filename.substr(0,6) == "smb://")) { av_dict_set(&d, "seekable", "1", 0); if(!cookie.empty()) { av_dict_set(&d, "cookies", cookie.c_str(), 0); } if(!user_agent.empty()) { av_dict_set(&d, "user_agent", user_agent.c_str(), 0); } } CLog::Log(LOGDEBUG, "COMXPlayer::OpenFile - avformat_open_input %s ", m_filename.c_str()); result = m_dllAvFormat.avformat_open_input(&m_pFormatContext, m_filename.c_str(), iformat, &d); if(av_dict_count(d) == 0) { CLog::Log(LOGDEBUG, "COMXPlayer::OpenFile - avformat_open_input enabled SEEKING "); if(m_filename.substr(0,7) == "http://") m_pFormatContext->pb->seekable = AVIO_SEEKABLE_NORMAL; } av_dict_free(&d); if(result < 0) { CLog::Log(LOGERROR, "COMXPlayer::OpenFile - avformat_open_input %s ", m_filename.c_str()); Close(); return false; } } else { m_pFile = new CFile(); if (!m_pFile->Open(m_filename, flags)) { CLog::Log(LOGERROR, "COMXPlayer::OpenFile - %s ", m_filename.c_str()); Close(); return false; } buffer = (unsigned char*)m_dllAvUtil.av_malloc(FFMPEG_FILE_BUFFER_SIZE); m_ioContext = m_dllAvFormat.avio_alloc_context(buffer, FFMPEG_FILE_BUFFER_SIZE, 0, m_pFile, dvd_file_read, NULL, dvd_file_seek); if (!m_ioContext) { log_err("Failed to create avio context."); return false; } m_ioContext->max_packet_size = 6144; if(m_ioContext->max_packet_size) m_ioContext->max_packet_size *= FFMPEG_FILE_BUFFER_SIZE / m_ioContext->max_packet_size; if(m_pFile->IoControl(IOCTRL_SEEK_POSSIBLE, NULL) == 0) m_ioContext->seekable = 0; m_dllAvFormat.av_probe_input_buffer(m_ioContext, &iformat, m_filename.c_str(), NULL, 0, 0); if(!iformat) { CLog::Log(LOGERROR, "COMXPlayer::OpenFile - av_probe_input_buffer %s ", m_filename.c_str()); Close(); return false; } m_pFormatContext->pb = m_ioContext; if (!m_pFormatContext->internal) { log_err("AVFormat internal null."); abort(); } result = m_dllAvFormat.avformat_open_input(&m_pFormatContext, m_filename.c_str(), iformat, NULL); if(result < 0) { Close(); return false; } } m_bMatroska = strncmp(m_pFormatContext->iformat->name, "matroska", 8) == 0; // for "matroska.webm" m_bAVI = strcmp(m_pFormatContext->iformat->name, "avi") == 0; // analyse very short to speed up mjpeg playback start if (iformat && (strcmp(iformat->name, "mjpeg") == 0) && m_ioContext->seekable == 0) m_pFormatContext->max_analyze_duration = 500000; if(/*m_bAVI || */m_bMatroska) m_pFormatContext->max_analyze_duration = 0; if (live) m_pFormatContext->flags |= AVFMT_FLAG_NOBUFFER; result = m_dllAvFormat.avformat_find_stream_info(m_pFormatContext, NULL); if(result < 0) { Close(); return false; } if(!GetStreams()) { Close(); return false; } if(m_pFile) { int64_t len = m_pFile->GetLength(); int64_t tim = GetStreamLength(); if(len > 0 && tim > 0) { unsigned rate = len * 1000 / tim; unsigned maxrate = rate + 1024 * 1024 / 8; if(m_pFile->IoControl(IOCTRL_CACHE_SETRATE, &maxrate) >= 0) CLog::Log(LOGDEBUG, "COMXPlayer::OpenFile - set cache throttle rate to %u bytes per second", maxrate); } } m_speed = DVD_PLAYSPEED_NORMAL; if(dump_format) m_dllAvFormat.av_dump_format(m_pFormatContext, 0, m_filename.c_str(), 0); UpdateCurrentPTS(); m_open = true; return true; }