BaseInFileStream::BaseInFileStream(BaseProtocol *pProtocol, StreamsManager *pStreamsManager, uint64_t type, string name) : BaseInStream(pProtocol, pStreamsManager, type, name) { if (!TAG_KIND_OF(type, ST_IN_FILE)) { ASSERT("Incorrect stream type. Wanted a stream type in class %s and got %s", STR(tagToString(ST_IN_FILE)), STR(tagToString(type))); } _pTimer = NULL; _pSeekFile = NULL; _pFile = NULL; //frame info _totalFrames = 0; _currentFrameIndex = 0; memset(&_currentFrame, 0, sizeof (MediaFrame)); //timing info _totalSentTime = 0; _totalSentTimeBase = 0; _startFeedingTime = 0; //buffering info _clientSideBufferLength = 0; //current state info _paused = true; _audioVideoCodecsSent = false; }
bool BaseOutStream::Link(BaseInStream *pInStream, bool reverseLink) { if ((!pInStream->IsCompatibleWithType(GetType())) || (!IsCompatibleWithType(pInStream->GetType()))) { FATAL("stream type %s not compatible with stream type %s", STR(tagToString(GetType())), STR(tagToString(pInStream->GetType()))); return false; } if (_pInStream != NULL) { if (_pInStream->GetUniqueId() == pInStream->GetUniqueId()) { WARN("BaseOutStream::Link: This stream is already linked"); return true; } FATAL("BaseOutStream::Link: This stream is already linked to stream with unique id %u", _pInStream->GetUniqueId()); return false; } _pInStream = pInStream; if (reverseLink) { if (!_pInStream->Link(this, false)) { FATAL("BaseOutStream::Link: Unable to reverse link"); _pInStream = NULL; return false; } } SignalAttachedToInStream(); return true; }
bool BaseInStream::Link(BaseOutStream *pOutStream, bool reverseLink) { if ((!pOutStream->IsCompatibleWithType(GetType())) || (!IsCompatibleWithType(pOutStream->GetType()))) { FATAL("stream type %s not compatible with stream type %s", STR(tagToString(GetType())), STR(tagToString(pOutStream->GetType()))); return false; } if (MAP_HAS1(_linkedStreams, pOutStream->GetUniqueId())) { WARN("BaseInStream::Link: This stream is already linked"); return true; } _pOutStreams = AddLinkedList(_pOutStreams, pOutStream, true); _linkedStreams[pOutStream->GetUniqueId()] = pOutStream; if (reverseLink) { if (!pOutStream->Link(this, false)) { FATAL("BaseInStream::Link: Unable to reverse link"); //TODO: here we must remove the link from _pOutStreams and _linkedStreams NYIA; } } SignalOutStreamAttached(pOutStream); return true; }
BaseInNetStream::BaseInNetStream(BaseProtocol *pProtocol, uint64_t type, string name) : BaseInStream(pProtocol, type, name) { if (!TAG_KIND_OF(type, ST_IN_NET)) { ASSERT("Incorrect stream type. Wanted a stream type in class %s and got %s", STR(tagToString(ST_IN_NET)), STR(tagToString(type))); } }
InFileRTMPStream::InFileRTMPStream(BaseProtocol *pProtocol, StreamsManager *pStreamsManager, uint64_t type, string name) : BaseInFileStream(pProtocol, pStreamsManager, type, name) { if (!TAG_KIND_OF(type, ST_IN_FILE_RTMP)) { ASSERT("Incorrect stream type. Wanted a stream type in class %s and got %s", STR(tagToString(ST_IN_FILE_RTMP)), STR(tagToString(type))); } _chunkSize = 4 * 1024 * 1024; }
BaseInStream::BaseInStream(BaseProtocol *pProtocol, uint64_t type, string name) : BaseStream(pProtocol, type, name) { if (!TAG_KIND_OF(type, ST_IN)) { ASSERT("Incorrect stream type. Wanted a stream type in class %s and got %s", STR(tagToString(ST_IN)), STR(tagToString(type))); } _pOutStreams = NULL; _canCallOutStreamDetached = true; }
BaseOutStream::BaseOutStream(BaseProtocol *pProtocol, StreamsManager *pStreamsManager, uint64_t type, string name) : BaseStream(pProtocol, pStreamsManager, type, name) { if (!TAG_KIND_OF(type, ST_OUT)) { ASSERT("Incorrect stream type. Wanted a stream type in class %s and got %s", STR(tagToString(ST_OUT)), STR(tagToString(type))); } _canCallDetachedFromInStream = true; _pInStream = NULL; }
bool BaseVariantAppProtocolHandler::SignalProtocolCreated(BaseProtocol *pProtocol, Variant ¶meters) { //1. Get the application BaseClientApplication *pApplication = ClientApplicationManager::FindAppByName( parameters["applicationName"]); if (pApplication == NULL) { FATAL("Unable to find application %s", STR(parameters["applicationName"])); return false; } //2. get the protocol handler BaseAppProtocolHandler *pHandler = pApplication->GetProtocolHandler(PT_BIN_VAR); if (pHandler == NULL) { pHandler = pApplication->GetProtocolHandler(PT_XML_VAR); if (pHandler == NULL) { WARN("Unable to get protocol handler for variant protocol"); } } //3. Is the connection up if (pProtocol == NULL) { if (pHandler != NULL) { ((BaseVariantAppProtocolHandler *) pHandler)->ConnectionFailed(parameters); } else { WARN("Connection failed:\n%s", STR(parameters.ToString())); } return false; } //1. Validate the protocol if (pProtocol->GetType() != PT_BIN_VAR && pProtocol->GetType() != PT_XML_VAR) { FATAL("Invalid protocol type. Wanted: %s or %s; Got: %s", STR(tagToString(PT_BIN_VAR)), STR(tagToString(PT_XML_VAR)), STR(tagToString(pProtocol->GetType()))); return false; } //3. Register the protocol to it pProtocol->SetApplication(pApplication); if (pProtocol->GetFarProtocol() == NULL) { FATAL("Invalid far protocol"); return false; } //4. Do the actual request if (pProtocol->GetFarProtocol()->GetType() == PT_TCP) { return ((BaseVariantProtocol *) pProtocol)->Send(parameters["payload"]); } else { return ((BaseVariantProtocol *) pProtocol)->Send(parameters); } }
bool InFileRTMPStream::Initialize(Metadata &metadata, TimerType timerType, uint32_t granularity) { //1. Base init if (!BaseInFileStream::Initialize(metadata, timerType, granularity)) { FATAL("Unable to initialize stream"); return false; } //2. Get stream capabilities StreamCapabilities *pCapabilities = GetCapabilities(); if (pCapabilities == NULL) { FATAL("Invalid stream capabilities"); return false; } pCapabilities->SetRTMPMetadata(_completeMetadata.publicMetadata()); //3. Create the video builder uint64_t videoCodec = pCapabilities->GetVideoCodecType(); if ((videoCodec != 0) && (videoCodec != CODEC_VIDEO_UNKNOWN) && (videoCodec != CODEC_VIDEO_H264) && (videoCodec != CODEC_VIDEO_PASS_THROUGH)) { FATAL("Invalid video stream capabilities: %s", STR(tagToString(videoCodec))); return false; } if (videoCodec == CODEC_VIDEO_H264) { _pVideoBuilder = new AVCBuilder(); } else if (videoCodec == CODEC_VIDEO_PASS_THROUGH) { _pVideoBuilder = new PassThroughBuilder(); } //4. Create the audio builder uint64_t audioCodec = pCapabilities->GetAudioCodecType(); if ((audioCodec != 0) && (audioCodec != CODEC_AUDIO_UNKNOWN) && (audioCodec != CODEC_AUDIO_AAC) && (audioCodec != CODEC_AUDIO_MP3) && (audioCodec != CODEC_AUDIO_PASS_THROUGH)) { FATAL("Invalid audio stream capabilities: %s", STR(tagToString(audioCodec))); return false; } if (audioCodec == CODEC_AUDIO_AAC) { _pAudioBuilder = new AACBuilder(); } else if (audioCodec == CODEC_AUDIO_MP3) { _pAudioBuilder = new MP3Builder(); } else if (audioCodec == CODEC_AUDIO_PASS_THROUGH) { _pAudioBuilder = new PassThroughBuilder(); } return true; }
bool InFileRTMPStream::Initialize(int32_t clientSideBufferLength, bool hasTimer) { //1. Base init INFO("clientSideBufferLength %d", clientSideBufferLength); if (!BaseInFileStream::Initialize(clientSideBufferLength, hasTimer)) { FATAL("Unable to initialize stream"); return false; } //2. Get stream capabilities StreamCapabilities *pCapabilities = GetCapabilities(); if (pCapabilities == NULL) { FATAL("Invalid stream capabilities"); return false; } //3. Create the video builder if ((pCapabilities->videoCodecId != 0) && (pCapabilities->videoCodecId != CODEC_VIDEO_UNKNOWN) && (pCapabilities->videoCodecId != CODEC_VIDEO_AVC) && (pCapabilities->videoCodecId != CODEC_VIDEO_PASS_THROUGH)) { FATAL("Invalid video stream capabilities: %s", STR(tagToString(pCapabilities->videoCodecId))); return false; } if (pCapabilities->videoCodecId == CODEC_VIDEO_AVC) { _pVideoBuilder = new AVCBuilder(); } else if (pCapabilities->videoCodecId == CODEC_VIDEO_PASS_THROUGH) { _pVideoBuilder = new PassThroughBuilder(); } //4. Create the audio builder if ((pCapabilities->audioCodecId != 0) && (pCapabilities->audioCodecId != CODEC_AUDIO_UNKNOWN) && (pCapabilities->audioCodecId != CODEC_AUDIO_AAC) && (pCapabilities->audioCodecId != CODEC_AUDIO_MP3) && (pCapabilities->audioCodecId != CODEC_AUDIO_PASS_THROUGH)) { FATAL("Invalid audio stream capabilities: %s", STR(tagToString(pCapabilities->audioCodecId))); return false; } if (pCapabilities->audioCodecId == CODEC_AUDIO_AAC) { _pAudioBuilder = new AACBuilder(); } else if (pCapabilities->audioCodecId == CODEC_AUDIO_MP3) { _pAudioBuilder = new MP3Builder(); } else if (pCapabilities->audioCodecId == CODEC_AUDIO_PASS_THROUGH) { _pAudioBuilder = new PassThroughBuilder(); } return true; }
UString debugTag(uint32 tag, bool trim) { UString str; if (tagToString(tag, trim, str)) return UString::format("0x%08X ('%s')", FROM_BE_32(tag), str.c_str()); return UString::format("0x%08X", FROM_BE_32(tag)); }
BaseInFileStream::BaseInFileStream(BaseProtocol *pProtocol, StreamsManager *pStreamsManager, uint64_t type, string name) : BaseInStream(pProtocol, pStreamsManager, type, name) { if (!TAG_KIND_OF(type, ST_IN_FILE)) { ASSERT("Incorrect stream type. Wanted a stream type in class %s and got %s", STR(tagToString(ST_IN_FILE)), STR(tagToString(type))); } _pTimer = NULL; _pSeekFile = NULL; _pFile = NULL; //frame info _totalFrames = 0; _currentFrameIndex = 0; memset(&_currentFrame, 0, sizeof (MediaFrame)); //timing info _totalSentTime = 0; _totalSentTimeBase = 0; _startFeedingTime = 0; //buffering info _clientSideBufferLength = 0; //current state info _streamingState = FILE_STREAMING_STATE_PAUSED; _audioVideoCodecsSent = false; _seekBaseOffset = 0; _framesBaseOffset = 0; _timeToIndexOffset = 0; _streamCapabilities.Clear(); _playLimit = -1; }
bool BaseCLIAppProtocolHandler::Send(BaseProtocol *pTo, string status, string description, Variant &data) { if (pTo == NULL) return true; //1. Prepare the final message Variant message; message["status"] = status; message["description"] = description; message["data"] = data; //3. Send it bool result; switch (pTo->GetType()) { case PT_INBOUND_JSONCLI: result = ((InboundBaseCLIProtocol *) pTo)->SendMessage(message); if ((pTo->GetFarProtocol())->GetType() == PT_HTTP_4_CLI) { // If this is an http based CLI, enqueue it for delete pTo->GracefullyEnqueueForDelete(); } return result; default: WARN("Protocol %s not supported yet", STR(tagToString(pTo->GetType()))); return false; } }
BaseProtocol *DefaultProtocolFactory::SpawnProtocol(uint64_t type, Variant ¶meters) { BaseProtocol *pResult = NULL; switch (type) { case PT_TCP: pResult = new TCPProtocol(); break; case PT_UDP: pResult = new UDPProtocol(); break; case PT_INBOUND_SSL: pResult = new InboundSSLProtocol(); break; case PT_OUTBOUND_SSL: pResult = new OutboundSSLProtocol(); break; #ifdef HAS_PROTOCOL_DNS case PT_INBOUND_DNS: pResult = new InboundDNSResolverProtocol(); break; case PT_OUTBOUND_DNS: pResult = new OutboundDNSResolverProtocol(); break; #endif /* HAS_PROTOCOL_DNS */ #ifdef HAS_PROTOCOL_RTMP case PT_INBOUND_RTMP: pResult = new InboundRTMPProtocol(); break; case PT_INBOUND_RTMPS_DISC: pResult = new InboundRTMPSDiscriminatorProtocol(); break; case PT_OUTBOUND_RTMP: pResult = new OutboundRTMPProtocol(); break; #ifdef HAS_PROTOCOL_HTTP case PT_INBOUND_HTTP_FOR_RTMP: pResult = new InboundHTTP4RTMP(); break; #endif /* HAS_PROTOCOL_HTTP */ #endif /* HAS_PROTOCOL_RTMP */ #ifdef HAS_PROTOCOL_TS case PT_INBOUND_TS: pResult = new InboundTSProtocol(); break; #endif /* HAS_PROTOCOL_TS */ #ifdef HAS_PROTOCOL_HTTP case PT_INBOUND_HTTP: pResult = new InboundHTTPProtocol(); break; case PT_OUTBOUND_HTTP: pResult = new OutboundHTTPProtocol(); break; #endif /* HAS_PROTOCOL_HTTP */ #ifdef HAS_PROTOCOL_LIVEFLV case PT_INBOUND_LIVE_FLV: pResult = new InboundLiveFLVProtocol(); break; #endif /* HAS_PROTOCOL_LIVEFLV */ #ifdef HAS_PROTOCOL_VAR case PT_XML_VAR: pResult = new XmlVariantProtocol(); break; case PT_BIN_VAR: pResult = new BinVariantProtocol(); break; case PT_JSON_VAR: pResult = new JsonVariantProtocol(); break; #endif /* HAS_PROTOCOL_VAR */ #ifdef HAS_PROTOCOL_RTP case PT_RTSP: pResult = new RTSPProtocol(); break; case PT_RTCP: pResult = new RTCPProtocol(); break; case PT_INBOUND_RTP: pResult = new InboundRTPProtocol(); break; case PT_RTP_NAT_TRAVERSAL: pResult = new NATTraversalProtocol(); break; #endif /* HAS_PROTOCOL_RTP */ #ifdef HAS_PROTOCOL_CLI case PT_INBOUND_JSONCLI: pResult = new InboundJSONCLIProtocol(); break; case PT_HTTP_4_CLI: pResult = new HTTP4CLIProtocol(); break; #endif /* HAS_PROTOCOL_CLI */ #ifdef HAS_PROTOCOL_MMS case PT_OUTBOUND_MMS: pResult = new MMSProtocol(); break; #endif /* HAS_PROTOCOL_MMS */ #ifdef HAS_PROTOCOL_RAWHTTPSTREAM case PT_INBOUND_RAW_HTTP_STREAM: pResult = new InboundRawHTTPStreamProtocol(); break; #endif /* HAS_PROTOCOL_RAWHTTPSTREAM */ default: FATAL("Spawning protocol %s not yet implemented", STR(tagToString(type))); break; } if (pResult != NULL) { if (!pResult->Initialize(parameters)) { FATAL("Unable to initialize protocol %s", STR(tagToString(type))); delete pResult; pResult = NULL; } } return pResult; }